FillManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool.DrawTool;
  3. using ComPDFKit.Tool.UndoManger;
  4. using ComPDFKitViewer;
  5. using ComPDFKitViewer.BaseObject;
  6. using ComPDFKitViewer.Widget;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls.Primitives;
  15. using System.Windows.Input;
  16. namespace ComPDFKit.Tool.FillTool
  17. {
  18. public enum FillType
  19. {
  20. Unknow,
  21. Circle,
  22. Square,
  23. Text,
  24. Line,
  25. Check,
  26. Cross,
  27. Image
  28. }
  29. public class FillItem
  30. {
  31. public FillType Type { get; set; }
  32. public int PageIndex { get; set; }
  33. public int AnnotIndex { get; set; }
  34. public AnnotParam Param { get; set; }
  35. public int Width { get; set; }
  36. public int Height { get; set; }
  37. public bool KeepRate { get; set; }
  38. internal AnnotHistory HistoryItem { get; set; }
  39. public FillItem Clone()
  40. {
  41. FillItem subItem = new FillItem();
  42. subItem.Type = Type;
  43. subItem.PageIndex = PageIndex;
  44. subItem.AnnotIndex = AnnotIndex;
  45. if (Param != null)
  46. {
  47. switch (this.Type)
  48. {
  49. case FillType.Square:
  50. SquareParam squareParam = new SquareParam();
  51. Param.CopyTo(squareParam);
  52. subItem.Param = squareParam;
  53. break;
  54. case FillType.Circle:
  55. CircleParam circleParam = new CircleParam();
  56. Param.CopyTo(circleParam);
  57. subItem.Param = circleParam;
  58. break;
  59. case FillType.Line:
  60. LineParam lineParam = new LineParam();
  61. Param.CopyTo(lineParam);
  62. subItem.Param = lineParam;
  63. break;
  64. case FillType.Check:
  65. InkParam checkParam = new InkParam();
  66. Param.CopyTo(checkParam);
  67. subItem.Param = checkParam;
  68. break;
  69. case FillType.Cross:
  70. InkParam crossParam = new InkParam();
  71. Param.CopyTo(crossParam);
  72. subItem.Param = crossParam;
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. subItem.Width = Width;
  79. subItem.Height = Height;
  80. subItem.KeepRate = KeepRate;
  81. return subItem;
  82. }
  83. }
  84. public class FillManager
  85. {
  86. private Dictionary<int, List<FillItem>> Items { get; set; } = new Dictionary<int, List<FillItem>>();
  87. private CPDFViewerTool ViewTool { get; set; }
  88. public CPDFToolManager ToolManager { get; set; }
  89. private FillCreateTool FillCreator { get; set; }
  90. private int ViewIndex { get; set; } = -1;
  91. private FillItem FillParam { get; set; }
  92. public bool IsFillMode { get; private set; }
  93. public bool AllowFillHit { get; set; }
  94. public FillManager(CPDFViewerTool tool)
  95. {
  96. ViewTool = tool;
  97. ViewTool.MouseLeave -= ViewTool_MouseLeave;
  98. ViewTool.MouseLeave += ViewTool_MouseLeave;
  99. ViewTool.DrawChanged -= ViewTool_DrawChanged;
  100. ViewTool.DrawChanged += ViewTool_DrawChanged;
  101. }
  102. private void ViewTool_DrawChanged(object sender, EventArgs e)
  103. {
  104. FillCreator?.Draw();
  105. }
  106. private void ViewTool_MouseLeave(object sender, MouseEventArgs e)
  107. {
  108. FillCreator?.CleanDraw();
  109. }
  110. public FillItem GetFillItem(int pageIndex, int annotIndex)
  111. {
  112. if (pageIndex < 0 || annotIndex < 0)
  113. {
  114. return null;
  115. }
  116. if (Items.ContainsKey(pageIndex))
  117. {
  118. foreach (FillItem item in Items[pageIndex])
  119. {
  120. if (item.HistoryItem == null)
  121. {
  122. continue;
  123. }
  124. if (item.HistoryItem.GetAnnotIndex() == annotIndex)
  125. {
  126. return item;
  127. }
  128. }
  129. }
  130. return null;
  131. }
  132. internal bool InsertFillLayer()
  133. {
  134. if (ViewTool == null && ViewTool.PDFViewer == null)
  135. {
  136. return false;
  137. }
  138. if (FillCreator != null)
  139. {
  140. return true;
  141. }
  142. CPDFViewer pdfViewer = ViewTool.PDFViewer;
  143. if (ViewIndex != -1)
  144. {
  145. FillCreator = pdfViewer.GetViewForTag(ViewIndex) as FillCreateTool;
  146. if (FillCreator != null)
  147. {
  148. return true;
  149. }
  150. }
  151. FillCreator = new FillCreateTool();
  152. int annotViewindex = pdfViewer.GetMaxViewIndex();
  153. pdfViewer.InsertView(annotViewindex, FillCreator);
  154. ViewIndex = FillCreator.GetResTag();
  155. FillCreator.PDFViewer = pdfViewer;
  156. return true;
  157. }
  158. public bool EnableFillMode(bool isFill)
  159. {
  160. IsFillMode = false;
  161. if (ViewTool == null && ViewTool.PDFViewer == null)
  162. {
  163. return false;
  164. }
  165. if (isFill)
  166. {
  167. IsFillMode = InsertFillLayer();
  168. if (IsFillMode)
  169. {
  170. HideCursor();
  171. }
  172. FillCreator?.Draw();
  173. return IsFillMode;
  174. }
  175. ShowCursor();
  176. if (FillCreator == null)
  177. {
  178. return true;
  179. }
  180. CPDFViewer pdfViewer = ViewTool.PDFViewer;
  181. pdfViewer.RemoveView(FillCreator);
  182. FillCreator = null;
  183. ViewIndex = -1;
  184. FillParam = null;
  185. return true;
  186. }
  187. public void ClearFillMode()
  188. {
  189. EnableFillMode(false);
  190. ClearFillItem();
  191. }
  192. public void ClearFillItem()
  193. {
  194. Items?.Clear();
  195. }
  196. public void SetFillParam(FillItem param)
  197. {
  198. FillParam = param;
  199. FillCreator?.SetFillParam(FillParam);
  200. }
  201. private bool IsNeedProcess()
  202. {
  203. if (IsFillMode == false || FillParam == null || FillCreator == null)
  204. {
  205. return false;
  206. }
  207. List<FillType> allowType = new List<FillType>()
  208. {
  209. FillType.Circle,
  210. FillType.Square,
  211. FillType.Line,
  212. FillType.Check,
  213. FillType.Cross
  214. };
  215. if (allowType.Contains(FillParam.Type) == false)
  216. {
  217. return false;
  218. }
  219. return true;
  220. }
  221. private bool IsMouseOnHitAnnot()
  222. {
  223. if (ViewTool != null && ViewTool.cacheHitTestAnnot != null)
  224. {
  225. BaseAnnot cacheAnnot = ViewTool.cacheHitTestAnnot;
  226. BaseAnnot baseAnnot = ViewTool.PDFViewer.AnnotHitTest(false);
  227. if (baseAnnot != null && baseAnnot.CurrentType != C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  228. {
  229. AnnotData cacheData = cacheAnnot.GetAnnotData();
  230. AnnotData data = baseAnnot.GetAnnotData();
  231. if (data.PageIndex == cacheData.PageIndex && data.AnnotIndex == cacheData.AnnotIndex)
  232. {
  233. return true;
  234. }
  235. }
  236. Point point = Mouse.GetPosition(ViewTool);
  237. // PointControlType pointControlType = ViewTool.GetLastSelectedRect().GetHitControlIndex(point);
  238. //if (pointControlType != PointControlType.None)
  239. //{
  240. // return true;
  241. //}
  242. }
  243. return false;
  244. }
  245. private bool IsFillHitProcess()
  246. {
  247. if (IsFillMode)
  248. {
  249. BaseAnnot oldAnnot = ViewTool.cacheHitTestAnnot;
  250. ViewTool.cacheHitTestAnnot = null;
  251. ViewTool.isDrawSelectRect = false;
  252. BaseAnnot baseAnnot = ViewTool.PDFViewer.AnnotHitTest(true);
  253. if (baseAnnot != null && baseAnnot.CurrentType != C_ANNOTATION_TYPE.C_ANNOTATION_WIDGET)
  254. {
  255. ViewTool.cacheHitTestAnnot = baseAnnot;
  256. ViewTool.isDrawSelectRect = true;
  257. List<C_ANNOTATION_TYPE> list = new List<C_ANNOTATION_TYPE>()
  258. {
  259. C_ANNOTATION_TYPE.C_ANNOTATION_LINE,
  260. C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE,
  261. C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON,
  262. };
  263. MouseEventObject eventObj = new MouseEventObject();
  264. if (list.Contains(baseAnnot.CurrentType))
  265. {
  266. ViewTool.SetEditAnnotObject();
  267. ViewTool.DrawEditAnnotLayer();
  268. eventObj.IsMersured = baseAnnot.GetAnnotData().Annot.IsMeasured();
  269. }
  270. else
  271. {
  272. ViewTool.SelectedAnnot();
  273. }
  274. eventObj.annotType = baseAnnot.CurrentType;
  275. eventObj.hitTestType = MouseHitTestType.Annot;
  276. ViewTool.DrawSelectedLayer();
  277. ViewTool.InvokeMouseLeftDown(eventObj);
  278. return true;
  279. }
  280. if (oldAnnot != null)
  281. {
  282. ViewTool.CleanSelectedRect();
  283. MouseEventObject eventObj = new MouseEventObject();
  284. eventObj.annotType = C_ANNOTATION_TYPE.C_ANNOTATION_NONE;
  285. eventObj.hitTestType = MouseHitTestType.Unknown;
  286. ViewTool.InvokeMouseLeftDown(eventObj);
  287. HideCursor();
  288. }
  289. }
  290. return false;
  291. }
  292. private void HideCursor()
  293. {
  294. if (ViewTool != null && ViewTool.PDFViewer != null)
  295. {
  296. CPDFViewer pdfViewer = ViewTool.PDFViewer;
  297. if (pdfViewer.Cursor != Cursors.None)
  298. {
  299. pdfViewer.Cursor = Cursors.None;
  300. ViewTool.Cursor = Cursors.None;
  301. }
  302. }
  303. }
  304. private void ShowCursor()
  305. {
  306. if (ViewTool != null && ViewTool.PDFViewer != null)
  307. {
  308. CPDFViewer pdfViewer = ViewTool.PDFViewer;
  309. if (pdfViewer.Cursor == Cursors.None)
  310. {
  311. pdfViewer.Cursor = Cursors.Arrow;
  312. ViewTool.Cursor = Cursors.Arrow;
  313. }
  314. }
  315. }
  316. internal bool MouseDownHandle(MouseButtonEventArgs e)
  317. {
  318. if (ViewTool != null && ViewTool.cacheHitTestAnnot != null && ViewTool.DrawSelectRectDownEvent())
  319. {
  320. return false;
  321. }
  322. if (IsFillHitProcess())
  323. {
  324. FillCreator.CleanDraw();
  325. return false;
  326. }
  327. if (IsNeedProcess())
  328. {
  329. ViewTool.PDFViewer.EnableZoom(false);
  330. ViewTool.PDFViewer.CanHorizontallyScroll = false;
  331. ViewTool.PDFViewer.CanVerticallyScroll = false;
  332. ViewTool.PDFViewer.CaptureMouse();
  333. FillCreator.MouseDownPos = e.GetPosition(ViewTool.PDFViewer);
  334. FillCreator.CurrentMousePos = FillCreator.MouseDownPos;
  335. ViewTool.PDFViewer.GetPointPageInfo(FillCreator.MouseDownPos, out int pageIndex, out Rect paintRect, out Rect pageBound);
  336. FillCreator.PageIndex = pageIndex;
  337. FillCreator.PageBound = pageBound;
  338. FillCreator.IsMouseMoved = false;
  339. return true;
  340. }
  341. return false;
  342. }
  343. internal bool MouseUpHandle(MouseButtonEventArgs e)
  344. {
  345. if (ViewTool != null && ViewTool.cacheHitTestAnnot != null)
  346. {
  347. return false;
  348. }
  349. if (IsNeedProcess())
  350. {
  351. FillItem annotItem = FillCreator.SaveFillItem();
  352. ViewTool.PDFViewer.EnableZoom(true);
  353. ViewTool.PDFViewer.CanHorizontallyScroll = true;
  354. ViewTool.PDFViewer.CanVerticallyScroll = true;
  355. ViewTool.PDFViewer.ReleaseMouseCapture();
  356. if (annotItem != null)
  357. {
  358. if (Items.ContainsKey(annotItem.PageIndex) == false)
  359. {
  360. Items[annotItem.PageIndex] = new List<FillItem> { annotItem };
  361. }
  362. Items[annotItem.PageIndex].Add(annotItem);
  363. ViewTool.PDFViewer.UpdateAnnotFrame();
  364. FillCreator.MouseDownPos = new Point(-1, -1);
  365. FillCreator.CurrentMousePos = FillCreator.MouseDownPos;
  366. FillCreator.PageIndex = -1;
  367. FillCreator.PageBound = Rect.Empty;
  368. FillCreator.IsMouseMoved = false;
  369. FillCreator.CleanDraw();
  370. }
  371. return true;
  372. }
  373. return false;
  374. }
  375. internal bool MouseMoveHandle(MouseEventArgs e)
  376. {
  377. if (ViewTool != null && ViewTool.cacheHitTestAnnot != null)
  378. {
  379. if (e.LeftButton == MouseButtonState.Pressed || ViewTool.DrawSelectRectDownEvent())
  380. {
  381. FillCreator?.CleanDraw();
  382. return false;
  383. }
  384. }
  385. if (IsNeedProcess())
  386. {
  387. HideCursor();
  388. FillCreator.IsMouseMoved = (e.LeftButton == MouseButtonState.Pressed);
  389. FillCreator.CurrentMousePos = e.GetPosition(ViewTool.PDFViewer);
  390. FillCreator.Draw();
  391. return true;
  392. }
  393. return false;
  394. }
  395. }
  396. }