CPDFFreehandUI.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Compdfkit_Tools.Data;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer;
  4. using System;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. namespace Compdfkit_Tools.PDFControlUI
  9. {
  10. public partial class CPDFFreehandUI : UserControl
  11. {
  12. public event EventHandler<CPDFAnnotationData> PropertyChanged;
  13. public event EventHandler<bool> EraseClickHandler;
  14. public event EventHandler<double> EraseChangeHandler;
  15. private AnnotAttribEvent annotAttribEvent;
  16. public CPDFFreehandUI()
  17. {
  18. InitializeComponent();
  19. ColorPickerControl.ColorChanged += ColorPickerControl_ColorChanged;
  20. CPDFOpacityControl.OpacityChanged += CPDFOpacityControl_OpacityChanged;
  21. CPDFThicknessControl.ThicknessChanged += CPDFThicknessControl_ThicknessChanged;
  22. EraseThickness.ThicknessChanged += EraseThickness_ThicknessChanged;
  23. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  24. ColorPickerControl.SetCheckedForColor(GetFreehandData().BorderColor);
  25. }
  26. private void EraseThickness_ThicknessChanged(object sender, EventArgs e)
  27. {
  28. EraseChangeHandler?.Invoke(this, EraseThickness.Thickness);
  29. EraseCircle.Width = EraseThickness.Thickness * 6;
  30. EraseCircle.Height = EraseThickness.Thickness * 6;
  31. }
  32. private void CPDFOpacityControl_OpacityChanged(object sender, EventArgs e)
  33. {
  34. if (annotAttribEvent == null)
  35. {
  36. PropertyChanged?.Invoke(this, GetFreehandData());
  37. }
  38. else
  39. {
  40. annotAttribEvent.UpdateAttrib(AnnotAttrib.Transparency, CPDFOpacityControl.OpacityValue / 100.0);
  41. annotAttribEvent.UpdateAnnot();
  42. }
  43. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  44. }
  45. private void CPDFThicknessControl_ThicknessChanged(object sender, EventArgs e)
  46. {
  47. if (annotAttribEvent == null)
  48. {
  49. PropertyChanged?.Invoke(this, GetFreehandData());
  50. }
  51. else
  52. {
  53. annotAttribEvent.UpdateAttrib(AnnotAttrib.Thickness, CPDFThicknessControl.Thickness);
  54. annotAttribEvent.UpdateAnnot();
  55. }
  56. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  57. }
  58. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  59. {
  60. if (annotAttribEvent == null)
  61. {
  62. PropertyChanged?.Invoke(this, GetFreehandData());
  63. }
  64. else
  65. {
  66. annotAttribEvent.UpdateAttrib(AnnotAttrib.Color, ((SolidColorBrush)ColorPickerControl.Brush).Color);
  67. annotAttribEvent.UpdateAnnot();
  68. }
  69. CPDFAnnotationPreviewerControl.DrawFreehandPreview(GetFreehandData());
  70. }
  71. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  72. {
  73. if (annotAttribEvent == null)
  74. {
  75. PropertyChanged?.Invoke(this, GetFreehandData());
  76. }
  77. else
  78. {
  79. annotAttribEvent.UpdateAttrib(AnnotAttrib.NoteText, NoteTextBox.Text);
  80. annotAttribEvent.UpdateAnnot();
  81. }
  82. }
  83. public void SetPresentAnnotAttrib(AnnotAttribEvent annotAttribEvent)
  84. {
  85. this.annotAttribEvent = null;
  86. ColorPickerControl.Brush = new SolidColorBrush((Color)annotAttribEvent.Attribs[AnnotAttrib.Color]);
  87. CPDFOpacityControl.OpacityValue = (int)((double)annotAttribEvent.Attribs[AnnotAttrib.Transparency] * 100);
  88. CPDFThicknessControl.Thickness =Convert.ToInt16(annotAttribEvent.Attribs[AnnotAttrib.Thickness]);
  89. if(annotAttribEvent.Attribs.ContainsKey(AnnotAttrib.Color))
  90. {
  91. ColorPickerControl.Loaded += ColorPickerControl_Loaded;
  92. }
  93. NoteTextBox.Text = (string)annotAttribEvent.Attribs[AnnotAttrib.NoteText];
  94. this.annotAttribEvent = annotAttribEvent;
  95. }
  96. private void ColorPickerControl_Loaded(object sender, RoutedEventArgs e)
  97. {
  98. ColorPickerControl.SetCheckedForColor((Color)annotAttribEvent.Attribs[AnnotAttrib.Color]);
  99. ColorPickerControl.Loaded -= ColorPickerControl_Loaded;
  100. }
  101. public CPDFFreehandData GetFreehandData()
  102. {
  103. CPDFFreehandData pdfFreehandData = new CPDFFreehandData();
  104. pdfFreehandData.AnnotationType = CPDFAnnotationType.Freehand;
  105. pdfFreehandData.BorderColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  106. pdfFreehandData.Opacity = CPDFOpacityControl.OpacityValue / 100.0;
  107. pdfFreehandData.Thickness = CPDFThicknessControl.Thickness;
  108. pdfFreehandData.Note = NoteTextBox.Text;
  109. return pdfFreehandData;
  110. }
  111. public void SetEraseCheck(bool isCheck)
  112. {
  113. if(isCheck)
  114. {
  115. FreehandBtn.IsChecked = false;
  116. EraseBtn.IsChecked = true;
  117. FreehandPanel.Visibility = Visibility.Collapsed;
  118. ErasePanel.Visibility = Visibility.Visible;
  119. CPDFAnnotationPreviewerControl.Visibility = Visibility.Collapsed;
  120. EraseCirclePanel.Visibility = Visibility.Visible;
  121. }
  122. else
  123. {
  124. FreehandBtn.IsChecked = true;
  125. EraseBtn.IsChecked = false;
  126. FreehandPanel.Visibility = Visibility.Visible;
  127. ErasePanel.Visibility = Visibility.Collapsed;
  128. CPDFAnnotationPreviewerControl.Visibility = Visibility.Visible;
  129. EraseCirclePanel.Visibility=Visibility.Collapsed;
  130. }
  131. }
  132. internal void ClearAnnotAttribEvent()
  133. {
  134. annotAttribEvent = null;
  135. }
  136. internal int GetEraseThickness()
  137. {
  138. return EraseThickness.Thickness;
  139. }
  140. private void FreehandBtn_Click(object sender, RoutedEventArgs e)
  141. {
  142. SetEraseCheck(false);
  143. EraseClickHandler?.Invoke(this, false);
  144. }
  145. private void EraseBtn_Click(object sender, RoutedEventArgs e)
  146. {
  147. SetEraseCheck(true);
  148. EraseClickHandler?.Invoke(this, true);
  149. }
  150. }
  151. }