CPDFViewerTool.SelectedRect.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using ComPDFKit.Tool.DrawTool;
  2. using ComPDFKitViewer.Layer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Input;
  9. using System.Windows;
  10. using ComPDFKit.Viewer.Layer;
  11. using System.Windows.Media;
  12. using ComPDFKit.Tool.Help;
  13. using ComPDFKitViewer;
  14. using ComPDFKit.PDFAnnotation;
  15. namespace ComPDFKit.Tool
  16. {
  17. public partial class CPDFViewerTool
  18. {
  19. bool isDrawSelectRect = false;
  20. int selectedRectViewTag = -1;
  21. public event EventHandler<SelectedAnnotData> SelectedDataChanging;
  22. public event EventHandler<SelectedAnnotData> SelectedDataChanged;
  23. private void InsertSelectedRectView()
  24. {
  25. int selectedRectViewIndex = PDFViewer.GetMaxViewIndex();
  26. CustomizeLayer customizeLayer = new CustomizeLayer();
  27. SelectedRect selectedRect = new SelectedRect(GetDefaultDrawParam(), SelectedType.Annot);
  28. selectedRect.SetDrawMoveType(DrawMoveType.kDefault);
  29. customizeLayer.Children.Add(selectedRect);
  30. selectedRect.DataChanged += SelectedRect_DataChanged;
  31. selectedRect.DataChanging += SelectedRect_DataChanging;
  32. PDFViewer.InsertView(selectedRectViewIndex, customizeLayer);
  33. selectedRectViewTag= customizeLayer.GetResTag();
  34. }
  35. private void SelectedRect_DataChanging(object sender, SelectedAnnotData e)
  36. {
  37. SelectedDataChanging?.Invoke(this, e);
  38. }
  39. private void SelectedRect_DataChanged(object sender, SelectedAnnotData e)
  40. {
  41. SelectedDataChanged?.Invoke(this, e);
  42. }
  43. /// <summary>
  44. /// Start to draw the rectangle
  45. /// </summary>
  46. public void DrawStartSelectedRect()
  47. {
  48. Point point = Mouse.GetPosition(this);
  49. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  50. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  51. if (selectedRect != null)
  52. {
  53. selectedRect.Draw();
  54. selectedRect.OnMouseLeftButtonDown(point);
  55. isDrawSelectRect = true;
  56. }
  57. }
  58. public Cursor GetMoveSelectedRectCursor()
  59. {
  60. Point point = Mouse.GetPosition(this);
  61. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  62. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  63. if (selectedRect != null)
  64. {
  65. return selectedRect.GetCursor(point, this.Cursor);
  66. }
  67. return this.Cursor;
  68. }
  69. /// <summary>
  70. /// Draw the rectangle when dragging
  71. /// </summary>
  72. public bool DrawMoveSelectedRect()
  73. {
  74. bool DrawTag = false;
  75. Point point = Mouse.GetPosition(this);
  76. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  77. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  78. if (selectedRect != null)
  79. {
  80. selectedRect.OnMouseMove(point, out DrawTag,PDFViewer.ActualWidth,PDFViewer.ActualHeight);
  81. selectedRect.Draw();
  82. }
  83. return DrawTag;
  84. }
  85. /// <summary>
  86. /// End of drawing the rectangle
  87. /// </summary>
  88. public void DrawEndSelectedRect()
  89. {
  90. Point point = Mouse.GetPosition(this);
  91. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  92. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  93. if (selectedRect != null)
  94. {
  95. selectedRect.OnMouseLeftButtonUp(point);
  96. selectedRect.Draw();
  97. }
  98. }
  99. /// <summary>
  100. /// Clear the rectangle drawing
  101. /// </summary>
  102. public void CleanSelectedRect()
  103. {
  104. Point point = Mouse.GetPosition(this);
  105. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  106. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  107. if (selectedRect != null)
  108. {
  109. selectedRect.ClearDraw();
  110. isDrawSelectRect = false;
  111. }
  112. }
  113. private void SelectedAnnot()
  114. {
  115. if (!isHitTestLink&& cacheHitTestAnnot?.CurrentType== C_ANNOTATION_TYPE.C_ANNOTATION_LINK)
  116. {
  117. return ;
  118. }
  119. if (isHitTestRedact && cacheHitTestAnnot?.CurrentType!= C_ANNOTATION_TYPE.C_ANNOTATION_REDACT)
  120. {
  121. return ;
  122. }
  123. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  124. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  125. if (selectedRect != null)
  126. {
  127. selectedRect.SetAnnotData(cacheHitTestAnnot.GetAnnotData());
  128. }
  129. }
  130. private void SelectedAnnot(AnnotData annotData)
  131. {
  132. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  133. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  134. if (selectedRect != null)
  135. {
  136. if (annotData==null)
  137. {
  138. selectedRect.ClearDraw();
  139. }
  140. else
  141. {
  142. selectedRect.SetAnnotData(annotData);
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// Refresh the drawing
  148. /// </summary>
  149. private void DrawSelectedLayer()
  150. {
  151. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  152. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  153. if (selectedRect != null)
  154. {
  155. selectedRect.Draw();
  156. }
  157. }
  158. /// <summary>
  159. /// Identify whether the mouse is on the rectangle
  160. /// </summary>
  161. /// <returns></returns>
  162. private bool DrawSelectRectDownEvent()
  163. {
  164. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  165. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  166. if (selectedRect != null)
  167. {
  168. if (selectedRect.GetHitControlIndex(Mouse.GetPosition(this)) != PointControlType.None)
  169. {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. }
  176. }