SelectedRect.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.Tool.Help;
  4. using ComPDFKit.Tool.SettingParam;
  5. using ComPDFKit.Viewer.Helper;
  6. using ComPDFKitViewer;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. namespace ComPDFKit.Tool.DrawTool
  15. {
  16. public enum PointControlType
  17. {
  18. None = -1,
  19. LeftTop,
  20. LeftMiddle,
  21. LeftBottom,
  22. MiddleBottom,
  23. RightBottom,
  24. RightMiddle,
  25. RightTop,
  26. MiddleTop,
  27. Body,
  28. Line,
  29. Rotate
  30. }
  31. public enum SelectedType
  32. {
  33. None = -1,
  34. Annot,
  35. PDFEdit
  36. }
  37. public enum DrawPointType
  38. {
  39. Circle,
  40. Square,
  41. Crop,
  42. }
  43. public enum DrawMoveType
  44. {
  45. kDefault,
  46. kReferenceLine,
  47. kRotatable
  48. }
  49. public class SelectedAnnotData
  50. {
  51. /// <summary>
  52. /// Current size of the rectangle
  53. /// </summary>
  54. public Rect Square { get; set; }
  55. /// <summary>
  56. /// Current points of the rectangle
  57. /// </summary>
  58. public PointCollection Points { get; set; }
  59. public AnnotData annotData { get; set; }
  60. public int rotationAngle { get; set; }
  61. }
  62. public partial class SelectedRect : DrawingVisual
  63. {
  64. /// <summary>
  65. /// Re-layout child elements
  66. /// </summary>
  67. public void Arrange()
  68. {
  69. foreach (Visual child in Children)
  70. {
  71. if (!(child is UIElement))
  72. {
  73. continue;
  74. }
  75. UIElement checkChild = child as UIElement;
  76. try
  77. {
  78. double left = Canvas.GetLeft(checkChild);
  79. double top = Canvas.GetTop(checkChild);
  80. double width = (double)checkChild.GetValue(FrameworkElement.WidthProperty);
  81. double height = (double)checkChild.GetValue(FrameworkElement.HeightProperty);
  82. checkChild.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  83. checkChild.Arrange(new Rect(
  84. double.IsNaN(left) ? 0 : left,
  85. double.IsNaN(top) ? 0 : top,
  86. double.IsNaN(width) ? checkChild.DesiredSize.Width : width,
  87. double.IsNaN(height) ? checkChild.DesiredSize.Height : height));
  88. }
  89. catch (Exception ex)
  90. {
  91. }
  92. }
  93. }
  94. protected DefaultDrawParam DrawParam = new DefaultDrawParam();
  95. protected DrawingContext drawDC { get; set; }
  96. /// <summary>
  97. /// Data changing event
  98. /// </summary>
  99. public event EventHandler<SelectedAnnotData> DataChanging;
  100. /// <summary>
  101. /// Data changed event
  102. /// </summary>
  103. public event EventHandler<SelectedAnnotData> DataChanged;
  104. protected bool isHover = false;
  105. protected bool isSelected = false;
  106. protected bool canRotate = false;
  107. protected SelectedType selectedType = SelectedType.None;
  108. public SelectedType GetSelectedType()
  109. {
  110. return selectedType;
  111. }
  112. public void SetIsHover(bool hover)
  113. {
  114. isHover = hover;
  115. }
  116. public bool GetIsHover()
  117. {
  118. return isHover;
  119. }
  120. public void SetIsSelected(bool selected)
  121. {
  122. isSelected = selected;
  123. }
  124. public bool GetIsSelected()
  125. {
  126. return isSelected;
  127. }
  128. public void SetCurrentDrawPointType(DrawPointType type)
  129. {
  130. currentDrawPointType = type;
  131. }
  132. public DrawPointType GetCurrentDrawPointType()
  133. {
  134. return currentDrawPointType;
  135. }
  136. public virtual void OnMouseLeftButtonDown(Point downPoint)
  137. {
  138. isMouseDown = true;
  139. hitControlType = PointControlType.None;
  140. mouseDownPoint = downPoint;
  141. moveOffset = new Point(0, 0);
  142. HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
  143. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  144. {
  145. //Crop judgment point
  146. if (currentDrawPointType == DrawPointType.Crop)
  147. {
  148. hitControlType = GetHitCropControlIndex(downPoint);
  149. }
  150. else
  151. {
  152. hitControlType = GetHitControlIndex(downPoint);
  153. }
  154. if (hitControlType != PointControlType.None)
  155. {
  156. cacheRect = drawRect;
  157. rotateRect = drawRect;
  158. rotateControlPoints = controlPoints.ToList();
  159. if (hitControlType != PointControlType.Rotate)
  160. {
  161. isInScaling = true;
  162. }
  163. else
  164. {
  165. isInRotate = true;
  166. }
  167. }
  168. }
  169. }
  170. public virtual void OnMouseLeftButtonUp(Point upPoint)
  171. {
  172. if (isMouseDown && hitControlType != PointControlType.None)
  173. {
  174. isMouseDown = false;
  175. isInScaling = false;
  176. isInRotate = false;
  177. cacheRect = SetDrawRect = drawRect;
  178. Draw();
  179. if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
  180. {
  181. InvokeDataChangEvent(true);
  182. }
  183. }
  184. moveOffset = new Point(0, 0);
  185. }
  186. public virtual void OnMouseMove(Point mousePoint, out bool Tag, double width, double height)
  187. {
  188. PDFViewerActualWidth = width;
  189. PDFViewerActualHeight = height;
  190. Tag = false;
  191. if (isMouseDown && hitControlType != PointControlType.None)
  192. {
  193. Tag = isMouseDown;
  194. if (CalcHitPointMove(mousePoint))
  195. {
  196. Draw();
  197. if ((int)mousePoint.X != (int)mouseDownPoint.X || (int)mousePoint.Y != (int)mouseDownPoint.Y)
  198. {
  199. InvokeDataChangEvent(false);
  200. }
  201. }
  202. }
  203. }
  204. public Cursor GetCursor(Point downPoint, Cursor cursor)
  205. {
  206. if (isMouseDown)
  207. {
  208. return cursor;
  209. }
  210. hitControlType = GetHitControlIndex(downPoint);
  211. switch (hitControlType)
  212. {
  213. case PointControlType.LeftTop:
  214. case PointControlType.RightBottom:
  215. return Cursors.SizeNWSE;
  216. case PointControlType.LeftMiddle:
  217. case PointControlType.RightMiddle:
  218. return Cursors.SizeWE;
  219. case PointControlType.LeftBottom:
  220. case PointControlType.RightTop:
  221. return Cursors.SizeNESW;
  222. case PointControlType.MiddleBottom:
  223. case PointControlType.MiddleTop:
  224. return Cursors.SizeNS;
  225. case PointControlType.Body:
  226. return Cursors.Arrow;
  227. case PointControlType.Line:
  228. return Cursors.SizeAll;
  229. case PointControlType.Rotate:
  230. return CommonHelper.RotationCursor;
  231. default:
  232. return Cursors.Arrow;
  233. }
  234. }
  235. public SelectedRect(DefaultDrawParam defaultDrawParam, SelectedType type) : base()
  236. {
  237. DrawParam = defaultDrawParam;
  238. currentDrawPointType = DrawPointType.Square;
  239. selectedType = type;
  240. }
  241. public void Draw()
  242. {
  243. Dispatcher.Invoke(() =>
  244. {
  245. Rect currentRect = SetDrawRect;
  246. drawDC = RenderOpen();
  247. switch (currentDrawMoveType)
  248. {
  249. case DrawMoveType.kDefault:
  250. currentRect = drawRect;
  251. CalcControlPoint(currentRect);
  252. break;
  253. case DrawMoveType.kReferenceLine:
  254. CalcControlPoint(currentRect);
  255. if (isMouseDown == true)
  256. {
  257. SolidColorBrush moveBrush = DrawParam.AnnotMoveBrush;
  258. Pen movepen = DrawParam.AnnotMovePen;
  259. GetMoveBrushAndPen(ref moveBrush, ref movepen);
  260. if (selectedType == SelectedType.PDFEdit)
  261. {
  262. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect, DrawParam.PDFEditMoveRectPen);
  263. }
  264. else
  265. {
  266. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect);
  267. }
  268. }
  269. break;
  270. default:
  271. break;
  272. }
  273. SolidColorBrush solidColorBrush = DrawParam.AnnotRectFillBrush;
  274. Pen pen = DrawParam.AnnotRectLinePen;
  275. GetBrushAndPen(ref solidColorBrush, ref pen);
  276. RotateTransform rotateTransform = new RotateTransform(rotateAngle, centerPoint.X, centerPoint.Y);
  277. drawDC.PushTransform(rotateTransform);
  278. drawDC?.DrawRectangle(solidColorBrush, pen, currentRect);
  279. drawDC.Pop();
  280. SolidColorBrush PointBrush = DrawParam.AnnotPointBorderBrush;
  281. Pen PointPen = DrawParam.AnnotPointPen;
  282. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  283. switch (currentDrawPointType)
  284. {
  285. case DrawPointType.Circle:
  286. if (selectedType == SelectedType.PDFEdit)
  287. {
  288. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  289. }
  290. else
  291. {
  292. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  293. }
  294. break;
  295. case DrawPointType.Square:
  296. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  297. break;
  298. case DrawPointType.Crop:
  299. DrawCropPoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  300. break;
  301. }
  302. drawDC?.Close();
  303. drawDC = null;
  304. });
  305. }
  306. private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  307. {
  308. switch (selectedType)
  309. {
  310. case SelectedType.None:
  311. break;
  312. case SelectedType.Annot:
  313. colorBrush = DrawParam.AnnotMoveBrush;
  314. pen = DrawParam.AnnotMovePen;
  315. break;
  316. case SelectedType.PDFEdit:
  317. colorBrush = DrawParam.PDFEditMoveBrush;
  318. pen = DrawParam.PDFEditMovePen;
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  325. {
  326. switch (selectedType)
  327. {
  328. case SelectedType.None:
  329. break;
  330. case SelectedType.Annot:
  331. colorBrush = DrawParam.AnnotPointBorderBrush;
  332. pen = DrawParam.AnnotPointPen;
  333. break;
  334. case SelectedType.PDFEdit:
  335. if (isHover)
  336. {
  337. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  338. pen = DrawParam.PDFEditPointHoverPen;
  339. }
  340. else if (currentDrawPointType == DrawPointType.Crop)
  341. {
  342. colorBrush = DrawParam.SPDFEditCropBorderBrush;
  343. //new SolidColorBrush((DrawParam.SPDFEditPointPen.Brush as SolidColorBrush).Color);
  344. pen = DrawParam.SPDFEditPointPen.Clone();
  345. pen.DashStyle = DashStyles.Solid;
  346. }
  347. else
  348. {
  349. if (isSelected)
  350. {
  351. colorBrush = DrawParam.SPDFEditPointBorderBrush;
  352. pen = DrawParam.SPDFEditPointPen;
  353. }
  354. else
  355. {
  356. colorBrush = DrawParam.PDFEditPointBorderBrush;
  357. pen = DrawParam.PDFEditPointPen;
  358. }
  359. }
  360. break;
  361. default:
  362. break;
  363. }
  364. }
  365. private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  366. {
  367. switch (selectedType)
  368. {
  369. case SelectedType.None:
  370. break;
  371. case SelectedType.Annot:
  372. if (isHover)
  373. {
  374. colorBrush = DrawParam.AnnotRectFillBrush;
  375. pen = DrawParam.AnnotRectHoverPen;
  376. }
  377. else
  378. {
  379. colorBrush = DrawParam.AnnotRectFillBrush;
  380. pen = DrawParam.AnnotRectLinePen;
  381. }
  382. break;
  383. case SelectedType.PDFEdit:
  384. if (isHover)
  385. {
  386. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  387. pen = editHoverPen;//DrawParam.PDFEditRectLineHoverPen;
  388. }
  389. else
  390. {
  391. if (isSelected)
  392. {
  393. colorBrush = DrawParam.SPDFEditRectFillBrush;
  394. pen = DrawParam.SPDFEditRectLinePen;
  395. }
  396. else
  397. {
  398. colorBrush = DrawParam.PDFEditRectFillBrush;
  399. //init Color
  400. if (showCreatTextRect)
  401. {
  402. pen = DrawParam.PDFEditRectLinePen;
  403. }
  404. else
  405. {
  406. pen = editPen;
  407. }
  408. // editPen; //editPen;//// DrawParam.PDFEditRectLinePen;
  409. }
  410. }
  411. break;
  412. default:
  413. break;
  414. }
  415. }
  416. public void SetShowCreatTextRect(bool ShowCreatTextRect)
  417. {
  418. showCreatTextRect = ShowCreatTextRect;
  419. }
  420. public void SetEditPen(Pen editPen = null, Pen editHoverPen = null)
  421. {
  422. if (editPen == null)
  423. {
  424. this.editPen = DrawParam.PDFEditRectLinePen;
  425. }
  426. else
  427. {
  428. this.editPen = new Pen(editPen.Brush, editPen.Thickness);
  429. }
  430. if (editHoverPen == null)
  431. {
  432. this.editHoverPen = DrawParam.PDFEditRectLineHoverPen;
  433. }
  434. else
  435. {
  436. this.editHoverPen = editHoverPen;
  437. }
  438. }
  439. public virtual void ClearDraw()
  440. {
  441. SetDrawRect = drawRect = new Rect();
  442. rotateAngle = 0;
  443. drawDC = RenderOpen();
  444. drawDC?.Close();
  445. drawDC = null;
  446. }
  447. /// <summary>
  448. /// Hide the drawing
  449. /// </summary>
  450. public virtual void HideDraw()
  451. {
  452. drawDC = RenderOpen();
  453. drawDC?.Close();
  454. }
  455. public void SetRect(Rect newRect, double zoom)
  456. {
  457. if (newRect == Rect.Empty || newRect == null)
  458. {
  459. return;
  460. }
  461. newRect = new Rect((newRect.X - rectPadding * zoom), (newRect.Y - rectPadding * zoom), (newRect.Width + 2 * rectPadding * zoom), (newRect.Height + 2 * rectPadding * zoom));
  462. currentZoom = zoom;
  463. SetDrawRect = drawRect = newRect;
  464. drawCenterPoint = new Point(drawRect.Left + drawRect.Width / 2, drawRect.Top + drawRect.Height / 2);
  465. }
  466. /// <summary>
  467. /// Get the original set Rect, not the calculated fill
  468. /// </summary>
  469. /// <param name="newRect">
  470. /// The new rect to set
  471. /// </param>
  472. public Rect GetRect()
  473. {
  474. Rect rect = new Rect(drawRect.X + rectPadding * currentZoom, drawRect.Y + rectPadding * currentZoom, Math.Max(RectMinWidth, drawRect.Width - 2 * rectPadding * currentZoom), Math.Max(RectMinHeight, drawRect.Height - 2 * rectPadding * currentZoom));
  475. return rect;
  476. }
  477. public int GetRotation()
  478. {
  479. return rotateAngle;
  480. }
  481. public void SetRotation(int rotationAngle)
  482. {
  483. this.rotateAngle = rotationAngle;
  484. }
  485. public void SetRectPadding(double rectPadding)
  486. {
  487. this.rectPadding = rectPadding;
  488. }
  489. public double GetRectPadding()
  490. {
  491. return rectPadding;
  492. }
  493. public Rect GetDrawRect()
  494. {
  495. return drawRect;
  496. }
  497. /// <summary>
  498. /// Obtain cropped and actual region margin
  499. /// </summary>
  500. /// <returns></returns>
  501. public Thickness GetClipThickness()
  502. {
  503. return clipThickness;
  504. }
  505. /// <summary>
  506. /// Get ClipRect
  507. /// </summary>
  508. /// <returns></returns>
  509. public Rect GetClipRect()
  510. {
  511. Rect drawrect = new Rect(0, 0, 0, 0);
  512. drawrect.X = SetDrawRect.X - clipThickness.Left * currentZoom;
  513. drawrect.Y = SetDrawRect.Y - clipThickness.Top * currentZoom;
  514. drawrect.Width = SetDrawRect.Width - clipThickness.Right * currentZoom + clipThickness.Left * currentZoom;
  515. drawrect.Height = SetDrawRect.Height - clipThickness.Bottom * currentZoom + clipThickness.Top * currentZoom;
  516. return drawrect;
  517. }
  518. /// <summary>
  519. /// Set cropping and actual area margins
  520. /// </summary>
  521. /// <returns></returns>
  522. public void SetClipThickness(Thickness rect)
  523. {
  524. try
  525. {
  526. Rect drawrect = new Rect(0, 0, 0, 0);
  527. drawrect.X = SetDrawRect.X - rect.Left * currentZoom;
  528. drawrect.Y = SetDrawRect.Y - rect.Top * currentZoom;
  529. drawrect.Width = SetDrawRect.Width - rect.Right * currentZoom + rect.Left * currentZoom;
  530. drawrect.Height = SetDrawRect.Height - rect.Bottom * currentZoom + rect.Top * currentZoom;
  531. drawRect = drawrect;
  532. clipThickness = rect;
  533. }
  534. catch { }
  535. }
  536. public void SetMaxRect(Rect rect)
  537. {
  538. maxRect = rect;
  539. }
  540. public Rect GetMaxRect()
  541. {
  542. return maxRect;
  543. }
  544. public void SetAnnotData(AnnotData annotData, CPDFViewer viewer)
  545. {
  546. SetIgnorePoints(new List<PointControlType>());
  547. SetIsProportionalScaling(false);
  548. SetRoationHandle(false);
  549. isProportionalScaling = false;
  550. switch (annotData.AnnotType)
  551. {
  552. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  553. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  554. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  555. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  556. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  557. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  558. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  559. DisableAll();
  560. break;
  561. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  562. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  563. SetIgnorePointsAll();
  564. break;
  565. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  566. SetIsProportionalScaling(true);
  567. SetRoationHandle(true);
  568. break;
  569. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  570. SetIgnorePointsAll();
  571. break;
  572. default:
  573. break;
  574. }
  575. SetMaxRect(annotData.PaintOffset);
  576. if(annotData.AnnotType == C_ANNOTATION_TYPE.C_ANNOTATION_STAMP)
  577. {
  578. CRect sourceRect = new CRect();
  579. annotData.Annot.GetSourceRect(ref sourceRect);
  580. if (!sourceRect.IsEmpty)
  581. {
  582. RenderData renderData = viewer.GetCurrentRenderPageForIndex(annotData.PageIndex);
  583. Rect zoomRect = new Rect(sourceRect.left / 72 * 96 * annotData.CurrentZoom, sourceRect.top / 72 * 96 * annotData.CurrentZoom, sourceRect.width() / 72 * 96 * annotData.CurrentZoom, sourceRect.height() / 72 * 96 * annotData.CurrentZoom);
  584. Rect rotateRect = zoomRect;
  585. rotateRect.X += renderData.PageBound.X - renderData.CropLeft * annotData.CurrentZoom;
  586. rotateRect.Y += renderData.PageBound.Y - renderData.CropTop * annotData.CurrentZoom;
  587. SetRect(rotateRect, annotData.CurrentZoom);
  588. rotateAngle = -(annotData.Annot as CPDFStampAnnotation).AnnotationRotator.GetRotation();
  589. }
  590. else
  591. {
  592. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  593. }
  594. }
  595. else
  596. {
  597. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  598. }
  599. //SetRotation(annotData.Rotation);
  600. selectedRectData = new SelectedAnnotData();
  601. selectedRectData.annotData = annotData;
  602. }
  603. private void SetRoationHandle(bool canRotate)
  604. {
  605. this.canRotate = canRotate;
  606. }
  607. public void SetIsProportionalScaling(bool isProportionalScaling)
  608. {
  609. this.isProportionalScaling = isProportionalScaling;
  610. ignorePoints.Clear();
  611. if (isProportionalScaling)
  612. {
  613. ignorePoints.Add(PointControlType.LeftMiddle);
  614. ignorePoints.Add(PointControlType.MiddleBottom);
  615. ignorePoints.Add(PointControlType.RightMiddle);
  616. ignorePoints.Add(PointControlType.MiddleTop);
  617. ignorePoints.Add(PointControlType.Rotate);
  618. }
  619. }
  620. public void SetDrawType(DrawPointType drawType)
  621. {
  622. currentDrawPointType = drawType;
  623. }
  624. public void SetDrawMoveType(DrawMoveType drawType)
  625. {
  626. currentDrawMoveType = drawType;
  627. }
  628. /// <summary>
  629. /// Set the types that need to be ignored
  630. /// </summary>
  631. /// <param name="types">
  632. /// The collection of point types that need to be ignored
  633. /// </param>
  634. public void SetIgnorePoints(List<PointControlType> types)
  635. {
  636. ignorePoints.Clear();
  637. foreach (PointControlType type in types)
  638. {
  639. ignorePoints.Add(type);
  640. }
  641. }
  642. /// <summary>
  643. /// Set Edit that need to be ignored
  644. /// </summary>
  645. /// <param name="types">
  646. /// The collection of point types that need to be ignored
  647. /// </param>
  648. public void SetEditIgnorePoints(List<PointControlType> ignoreTextPoints, List<PointControlType> ignoreImagePoints, DrawPointType drawEditPointType, bool IsText = true)
  649. {
  650. SetCurrentDrawPointType(drawEditPointType);
  651. if (IsText)
  652. {
  653. ignorePoints.Clear();
  654. foreach (PointControlType type in ignoreTextPoints)
  655. {
  656. ignorePoints.Add(type);
  657. }
  658. }
  659. else
  660. {
  661. ignorePoints.Clear();
  662. foreach (PointControlType type in ignoreImagePoints)
  663. {
  664. ignorePoints.Add(type);
  665. }
  666. }
  667. }
  668. /// <summary>
  669. /// Ignore all points
  670. /// </summary>
  671. public void SetIgnorePointsAll()
  672. {
  673. ignorePoints.Clear();
  674. ignorePoints.Add(PointControlType.LeftTop);
  675. ignorePoints.Add(PointControlType.LeftMiddle);
  676. ignorePoints.Add(PointControlType.LeftBottom);
  677. ignorePoints.Add(PointControlType.MiddleBottom);
  678. ignorePoints.Add(PointControlType.RightBottom);
  679. ignorePoints.Add(PointControlType.RightMiddle);
  680. ignorePoints.Add(PointControlType.RightTop);
  681. ignorePoints.Add(PointControlType.MiddleTop);
  682. }
  683. /// <summary>
  684. /// Disable all functions
  685. /// </summary>
  686. public void DisableAll()
  687. {
  688. ignorePoints.Clear();
  689. ignorePoints.Add(PointControlType.LeftTop);
  690. ignorePoints.Add(PointControlType.LeftMiddle);
  691. ignorePoints.Add(PointControlType.LeftBottom);
  692. ignorePoints.Add(PointControlType.MiddleBottom);
  693. ignorePoints.Add(PointControlType.RightBottom);
  694. ignorePoints.Add(PointControlType.RightMiddle);
  695. ignorePoints.Add(PointControlType.RightTop);
  696. ignorePoints.Add(PointControlType.MiddleTop);
  697. ignorePoints.Add(PointControlType.Rotate);
  698. ignorePoints.Add(PointControlType.Body);
  699. ignorePoints.Add(PointControlType.Line);
  700. }
  701. /// <summary>
  702. /// Set the left alignment in the set maximum rectangle
  703. /// </summary>
  704. public virtual void SetAlignLeftForMaxRect()
  705. {
  706. DrawAlignRect(AlignmentsHelp.SetAlignLeft(drawRect, maxRect));
  707. }
  708. /// <summary>
  709. /// Set horizontal center alignment in the set maximum rectangle
  710. /// </summary>
  711. public virtual void SetAlignHorizonCenterForMaxRect()
  712. {
  713. DrawAlignRect(AlignmentsHelp.SetAlignHorizonCenter(drawRect, maxRect));
  714. }
  715. /// <summary>
  716. /// Set horizontal right alignment in the set maximum rectangle
  717. /// </summary>
  718. public virtual void SetAlignRightForMaxRect()
  719. {
  720. DrawAlignRect(AlignmentsHelp.SetAlignRight(drawRect, maxRect));
  721. }
  722. /// <summary>
  723. /// Set the top alignment in the set maximum rectangle
  724. /// </summary>
  725. public virtual void SetAlignTopForMaxRect()
  726. {
  727. DrawAlignRect(AlignmentsHelp.SetAlignTop(drawRect, maxRect));
  728. }
  729. /// <summary>
  730. /// Set vertical center alignment in the set maximum rectangle
  731. /// </summary>
  732. public virtual void SetAlignVerticalCenterForMaxRect()
  733. {
  734. DrawAlignRect(AlignmentsHelp.SetAlignVerticalCenter(drawRect, maxRect));
  735. }
  736. /// <summary>
  737. /// Set vertical center alignment in the set maximum rectangle
  738. /// </summary>
  739. public virtual void SetAlignHorizonVerticalCenterForMaxRect()
  740. {
  741. DrawAlignRect(AlignmentsHelp.SetAlignHorizonVerticalCenter(drawRect, maxRect));
  742. }
  743. /// <summary>
  744. /// Set the bottom alignment in the set maximum rectangle
  745. /// </summary>
  746. public virtual void SetAlignBottomForMaxRect()
  747. {
  748. DrawAlignRect(AlignmentsHelp.SetAlignBottom(drawRect, maxRect));
  749. }
  750. /// <summary>
  751. /// Get which control point the coordinate is on
  752. /// </summary>
  753. /// <param name="clickPoint">
  754. /// The point to check
  755. /// </param>
  756. /// <returns>
  757. /// The control point type
  758. /// </returns>
  759. public PointControlType GetHitControlIndex(Point point, bool isIgnore = true)
  760. {
  761. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  762. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  763. {
  764. List<PointControlType> ignoreList = GetIgnorePoints();
  765. List<Point> IgnorePointsList = new List<Point>();
  766. foreach (PointControlType type in ignoreList)
  767. {
  768. if ((int)type < controlPoints.Count)
  769. {
  770. IgnorePointsList.Add(controlPoints[(int)type]);
  771. }
  772. }
  773. for (int i = 0; i < controlPoints.Count; i++)
  774. {
  775. Point checkPoint = controlPoints[i];
  776. if (canRotate)
  777. {
  778. // Convert the rotation angle from degrees to radians
  779. double angleRad = rotateAngle * Math.PI / 180.0;
  780. // Calculate the sine and cosine of the angle
  781. double cosAngle = Math.Cos(angleRad);
  782. double sinAngle = Math.Sin(angleRad);
  783. // Translate checkPoint to the origin (centerPoint becomes the origin)
  784. double translatedX = checkPoint.X - centerPoint.X;
  785. double translatedY = checkPoint.Y - centerPoint.Y;
  786. // Apply the rotation matrix
  787. double rotatedX = translatedX * cosAngle - translatedY * sinAngle;
  788. double rotatedY = translatedX * sinAngle + translatedY * cosAngle;
  789. // Translate the point back to its original position
  790. checkPoint.X = rotatedX + centerPoint.X;
  791. checkPoint.Y = rotatedY + centerPoint.Y;
  792. }
  793. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  794. {
  795. continue;
  796. }
  797. switch (currentDrawPointType)
  798. {
  799. case DrawPointType.Circle:
  800. if (IgnorePointsList.Contains(checkPoint))
  801. {
  802. continue;
  803. }
  804. Vector checkVector = checkPoint - point;
  805. double wlen = drawRect.Width;
  806. if (wlen > 50)
  807. {
  808. wlen = 20;
  809. }
  810. else
  811. {
  812. wlen = wlen / 3;
  813. }
  814. double hlen = drawRect.Height;
  815. if (hlen > 50)
  816. {
  817. hlen = 20;
  818. }
  819. else
  820. {
  821. hlen = wlen / 3;
  822. }
  823. if ((PointControlType)i == PointControlType.RightMiddle)
  824. {
  825. if (Math.Abs(point.X - checkPoint.X) < wlen && checkVector.Length < drawRect.Height/3)
  826. {
  827. return (PointControlType)i;
  828. }
  829. }
  830. if ((PointControlType)i == PointControlType.LeftMiddle)
  831. {
  832. if (Math.Abs(point.X - checkPoint.X) < wlen && checkVector.Length < drawRect.Height/3)
  833. {
  834. return (PointControlType)i;
  835. }
  836. }
  837. if ((PointControlType)i == PointControlType.MiddleTop)
  838. {
  839. if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width/3)
  840. {
  841. return (PointControlType)i;
  842. }
  843. }
  844. if ((PointControlType)i == PointControlType.MiddleBottom)
  845. {
  846. if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width/3)
  847. {
  848. return (PointControlType)i;
  849. }
  850. }
  851. if (checkVector.Length < pointSize)
  852. {
  853. return (PointControlType)i;
  854. }
  855. break;
  856. case DrawPointType.Square:
  857. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  858. if (checkRect.Contains(point))
  859. {
  860. return (PointControlType)i;
  861. }
  862. if (canRotate)
  863. {
  864. Point hitRotationPoint = new Point(centerPoint.X + (rotationPoint.X - centerPoint.X) * Math.Cos(rotateAngle * Math.PI / 180) - (rotationPoint.Y - centerPoint.Y) * Math.Sin(rotateAngle * Math.PI / 180),
  865. centerPoint.Y + (rotationPoint.X - centerPoint.X) * Math.Sin(rotateAngle * Math.PI / 180) + (rotationPoint.Y - centerPoint.Y) * Math.Cos(rotateAngle * Math.PI / 180));
  866. Vector checkVector1 = point - hitRotationPoint;
  867. if (checkVector1.Length < pointSize)
  868. {
  869. return PointControlType.Rotate;
  870. }
  871. }
  872. break;
  873. case DrawPointType.Crop:
  874. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  875. if (cropRect.Contains(point))
  876. {
  877. return (PointControlType)i;
  878. }
  879. break;
  880. default:
  881. break;
  882. }
  883. }
  884. if (canRotate)
  885. {
  886. bool isIn = DataConversionForWPF.IsPointInRotatedRectangle(point, drawRect, rotateAngle);
  887. if(isIn)
  888. {
  889. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  890. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  891. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  892. isIn = DataConversionForWPF.IsPointInRotatedRectangle(point, rect, rotateAngle);
  893. if (isIn)
  894. {
  895. if (!ignoreList.Contains(PointControlType.Body))
  896. {
  897. return PointControlType.Body;
  898. }
  899. }
  900. if (!ignoreList.Contains(PointControlType.Body))
  901. {
  902. return PointControlType.Line;
  903. }
  904. }
  905. }
  906. else
  907. {
  908. if (drawRect.Contains(point))
  909. {
  910. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  911. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  912. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  913. if (rect.Contains(point))
  914. {
  915. if (!ignoreList.Contains(PointControlType.Body))
  916. {
  917. return PointControlType.Body;
  918. }
  919. }
  920. if (!ignoreList.Contains(PointControlType.Body))
  921. {
  922. return PointControlType.Line;
  923. }
  924. }
  925. }
  926. }
  927. return PointControlType.None;
  928. }
  929. /// <summary>
  930. /// The position of the points in the cropping box
  931. /// </summary>
  932. /// <param name="point"></param>
  933. /// <param name="isIgnore"></param>
  934. /// <returns></returns>
  935. public PointControlType GetHitCropControlIndex(Point point, bool isIgnore = true)
  936. {
  937. List<Point> controlCurrentPoints = GetControlPoint(drawRect);
  938. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  939. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  940. {
  941. List<PointControlType> ignoreList = GetIgnorePoints();
  942. List<Point> IgnorePointsList = new List<Point>();
  943. foreach (PointControlType type in ignoreList)
  944. {
  945. if ((int)type < controlCurrentPoints.Count)
  946. {
  947. IgnorePointsList.Add(controlCurrentPoints[(int)type]);
  948. }
  949. }
  950. for (int i = 0; i < controlCurrentPoints.Count; i++)
  951. {
  952. Point checkPoint = controlCurrentPoints[i];
  953. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  954. {
  955. continue;
  956. }
  957. switch (currentDrawPointType)
  958. {
  959. case DrawPointType.Circle:
  960. Vector checkVector = checkPoint - point;
  961. if (checkVector.Length < pointSize)
  962. {
  963. return (PointControlType)i;
  964. }
  965. break;
  966. case DrawPointType.Square:
  967. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  968. if (checkRect.Contains(point))
  969. {
  970. return (PointControlType)i;
  971. }
  972. break;
  973. case DrawPointType.Crop:
  974. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  975. if (cropRect.Contains(point))
  976. {
  977. return (PointControlType)i;
  978. }
  979. break;
  980. default:
  981. break;
  982. }
  983. }
  984. if (drawRect.Contains(point))
  985. {
  986. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  987. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  988. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  989. if (rect.Contains(point))
  990. {
  991. if (!ignoreList.Contains(PointControlType.Body))
  992. {
  993. return PointControlType.Body;
  994. }
  995. }
  996. if (!ignoreList.Contains(PointControlType.Body))
  997. {
  998. return PointControlType.Line;
  999. }
  1000. }
  1001. }
  1002. return PointControlType.None;
  1003. }
  1004. }
  1005. }