CPDFViewerTool.SelectedRect.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. private bool isOutSideScaling = false;
  22. public event EventHandler<SelectedAnnotData> SelectedDataChanging;
  23. public event EventHandler<SelectedAnnotData> SelectedDataChanged;
  24. private void InsertSelectedRectView()
  25. {
  26. int selectedRectViewIndex = PDFViewer.GetMaxViewIndex();
  27. CustomizeLayer customizeLayer = new CustomizeLayer();
  28. SelectedRect selectedRect = new SelectedRect(GetDefaultDrawParam(), SelectedType.Annot);
  29. selectedRect.SetDrawMoveType(DrawMoveType.kDefault);
  30. customizeLayer.Children.Add(selectedRect);
  31. selectedRect.DataChanged += SelectedRect_DataChanged;
  32. selectedRect.DataChanging += SelectedRect_DataChanging;
  33. PDFViewer.InsertView(selectedRectViewIndex, customizeLayer);
  34. selectedRectViewTag= customizeLayer.GetResTag();
  35. }
  36. /// <summary>
  37. /// Set whether the border can be moved outside the border
  38. /// </summary>
  39. /// <param name="IsOutSideScaling"></param>
  40. public void SetOutSideScaling(bool IsOutSideScaling)
  41. {
  42. isOutSideScaling = IsOutSideScaling;
  43. }
  44. private void SelectedRect_DataChanging(object sender, SelectedAnnotData e)
  45. {
  46. SelectedDataChanging?.Invoke(this, e);
  47. }
  48. private void SelectedRect_DataChanged(object sender, SelectedAnnotData e)
  49. {
  50. SelectedDataChanged?.Invoke(this, e);
  51. }
  52. /// <summary>
  53. /// Start to draw the rectangle
  54. /// </summary>
  55. public void DrawStartSelectedRect()
  56. {
  57. Point point = Mouse.GetPosition(this);
  58. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  59. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  60. if (selectedRect != null)
  61. {
  62. selectedRect.Draw();
  63. selectedRect.OnMouseLeftButtonDown(point);
  64. isDrawSelectRect = true;
  65. }
  66. }
  67. public Cursor GetMoveSelectedRectCursor()
  68. {
  69. Point point = Mouse.GetPosition(this);
  70. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  71. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  72. if (selectedRect != null)
  73. {
  74. return selectedRect.GetCursor(point, this.Cursor);
  75. }
  76. return this.Cursor;
  77. }
  78. /// <summary>
  79. /// Draw the rectangle when dragging
  80. /// </summary>
  81. public bool DrawMoveSelectedRect()
  82. {
  83. bool DrawTag = false;
  84. Point point = Mouse.GetPosition(this);
  85. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  86. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  87. if (selectedRect != null)
  88. {
  89. selectedRect.SetOutSideScaling(isOutSideScaling);
  90. selectedRect.OnMouseMove(point, out DrawTag,PDFViewer.ActualWidth,PDFViewer.ActualHeight);
  91. selectedRect.Draw();
  92. }
  93. return DrawTag;
  94. }
  95. /// <summary>
  96. /// End of drawing the rectangle
  97. /// </summary>
  98. public void DrawEndSelectedRect()
  99. {
  100. Point point = Mouse.GetPosition(this);
  101. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  102. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  103. if (selectedRect != null)
  104. {
  105. selectedRect.OnMouseLeftButtonUp(point);
  106. selectedRect.Draw();
  107. }
  108. }
  109. /// <summary>
  110. /// Clear the rectangle drawing
  111. /// </summary>
  112. public void CleanSelectedRect()
  113. {
  114. Point point = Mouse.GetPosition(this);
  115. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  116. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  117. if (selectedRect != null)
  118. {
  119. selectedRect.ClearDraw();
  120. isDrawSelectRect = false;
  121. }
  122. }
  123. private void SelectedAnnot()
  124. {
  125. if (!isHitTestLink&& cacheHitTestAnnot?.CurrentType== C_ANNOTATION_TYPE.C_ANNOTATION_LINK)
  126. {
  127. return ;
  128. }
  129. if (isHitTestRedact && cacheHitTestAnnot?.CurrentType!= C_ANNOTATION_TYPE.C_ANNOTATION_REDACT)
  130. {
  131. return ;
  132. }
  133. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  134. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  135. if (selectedRect != null)
  136. {
  137. selectedRect.SetAnnotData(cacheHitTestAnnot.GetAnnotData(), PDFViewer);
  138. }
  139. }
  140. private void SelectedAnnot(AnnotData annotData)
  141. {
  142. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  143. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  144. if (selectedRect != null)
  145. {
  146. if (annotData==null)
  147. {
  148. selectedRect.ClearDraw();
  149. }
  150. else
  151. {
  152. selectedRect.SetAnnotData(annotData, PDFViewer);
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// Refresh the drawing
  158. /// </summary>
  159. private void DrawSelectedLayer()
  160. {
  161. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  162. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  163. if (selectedRect != null)
  164. {
  165. selectedRect.Draw();
  166. }
  167. }
  168. /// <summary>
  169. /// Identify whether the mouse is on the rectangle
  170. /// </summary>
  171. /// <returns></returns>
  172. private bool DrawSelectRectDownEvent()
  173. {
  174. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  175. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  176. if (selectedRect != null)
  177. {
  178. if (selectedRect.GetHitControlIndex(Mouse.GetPosition(this)) != PointControlType.None)
  179. {
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. }
  186. }