CPDFViewerTool.SelectedRect.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. internal 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. internal 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. SelectAnnot(cacheHitTestAnnot.GetAnnotData());
  134. }
  135. internal void SelectAnnot(AnnotData selectData)
  136. {
  137. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  138. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  139. if (selectedRect != null)
  140. {
  141. selectedRect.SetAnnotData(selectData,PDFViewer);
  142. }
  143. }
  144. private void SelectedAnnot(AnnotData annotData)
  145. {
  146. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  147. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  148. if (selectedRect != null)
  149. {
  150. if (annotData==null)
  151. {
  152. selectedRect.ClearDraw();
  153. }
  154. else
  155. {
  156. selectedRect.SetAnnotData(annotData, PDFViewer);
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// Refresh the drawing
  162. /// </summary>
  163. internal void DrawSelectedLayer()
  164. {
  165. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  166. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  167. if (selectedRect != null)
  168. {
  169. selectedRect.Draw();
  170. }
  171. }
  172. /// <summary>
  173. /// Identify whether the mouse is on the rectangle
  174. /// </summary>
  175. /// <returns></returns>
  176. internal bool DrawSelectRectDownEvent()
  177. {
  178. BaseLayer baseLayer = PDFViewer.GetViewForTag(selectedRectViewTag);
  179. SelectedRect selectedRect = CommonHelper.FindVisualChild<SelectedRect>(baseLayer as CustomizeLayer);
  180. if (selectedRect != null)
  181. {
  182. if (selectedRect.GetHitControlIndex(Mouse.GetPosition(this)) != PointControlType.None)
  183. {
  184. return true;
  185. }
  186. }
  187. return false;
  188. }
  189. }
  190. }