PageSelectedRect.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. using ComPDFKit.Tool.SettingParam;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. namespace ComPDFKit.Tool.DrawTool
  8. {
  9. public class PageSelectedRect : DrawingVisual
  10. {
  11. /// <summary>
  12. /// A method that repositions child elements
  13. /// </summary>
  14. public void Arrange()
  15. {
  16. foreach (Visual child in Children)
  17. {
  18. if (!(child is UIElement))
  19. {
  20. continue;
  21. }
  22. UIElement checkChild = child as UIElement;
  23. try
  24. {
  25. double left = Canvas.GetLeft(checkChild);
  26. double top = Canvas.GetTop(checkChild);
  27. double width = (double)checkChild.GetValue(FrameworkElement.WidthProperty);
  28. double height = (double)checkChild.GetValue(FrameworkElement.HeightProperty);
  29. checkChild.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  30. checkChild.Arrange(new Rect(
  31. double.IsNaN(left) ? 0 : left,
  32. double.IsNaN(top) ? 0 : top,
  33. double.IsNaN(width) ? checkChild.DesiredSize.Width : width,
  34. double.IsNaN(height) ? checkChild.DesiredSize.Height : height));
  35. }
  36. catch (Exception ex)
  37. {
  38. }
  39. }
  40. }
  41. protected DefaultDrawParam DrawParam = new DefaultDrawParam();
  42. protected DrawingContext drawDC { get; set; }
  43. /// <summary>
  44. /// Data changing event
  45. /// </summary>
  46. public event EventHandler<Rect> DataChanging;
  47. /// <summary>
  48. /// Data changed event
  49. /// </summary>
  50. public event EventHandler<Rect> DataChanged;
  51. /// <summary>
  52. /// Minimum width of the rectangle
  53. /// </summary>
  54. protected int rectMinWidth { get; set; } = 10;
  55. /// <summary>
  56. /// Minimum height of the rectangle
  57. /// </summary>
  58. protected int rectMinHeight { get; set; } = 10;
  59. /// <summary>
  60. /// Identifies whether the mouse is pressed
  61. /// </summary>
  62. protected bool isMouseDown { get; set; }
  63. /// <summary>
  64. /// Current click hit control point
  65. /// </summary>
  66. protected PointControlType hitControlType { get; set; }
  67. /// <summary>
  68. /// Location information recorded when the mouse is pressed
  69. /// </summary>
  70. protected Point mouseDownPoint { get; set; }
  71. /// <summary>
  72. /// Current set ignore point
  73. /// </summary>
  74. protected List<PointControlType> ignorePoints { get; set; } = new List<PointControlType>();
  75. /// <summary>
  76. /// Current control point coordinates
  77. /// </summary>
  78. protected List<Point> controlPoints { get; set; } = new List<Point>();
  79. /// <summary>
  80. /// Move offset during movement
  81. /// </summary>
  82. protected Point moveOffset { get; set; } = new Point(0, 0);
  83. /// <summary>
  84. /// Current PDFVIewer actual display width
  85. /// </summary>
  86. protected double PDFViewerActualWidth { get; set; } = 0;
  87. /// <summary>
  88. /// Current PDFVIewer actual display height
  89. /// </summary>
  90. protected double PDFViewerActualHeight { get; set; } = 0;
  91. /// <summary>
  92. /// Current control point drawing style
  93. /// </summary>
  94. protected DrawPointType currentDrawPointType { get; set; }
  95. /// <summary>
  96. /// Current drag drawing style
  97. /// </summary>
  98. protected DrawMoveType currentDrawMoveType { get; set; }
  99. /// <summary>
  100. /// Control point size
  101. /// </summary>
  102. protected int pointSize { get; set; } = 4;
  103. /// <summary>
  104. /// Current drawing rectangle (calculated during operation)
  105. /// </summary>
  106. protected Rect drawRect { get; set; } = new Rect(0, 0, 0, 0);
  107. /// <summary>
  108. /// Draw border and content internal padding
  109. /// </summary>
  110. protected double rectPadding = 5;
  111. /// <summary>
  112. /// Cache the rectangle when the mouse is pressed
  113. /// </summary>
  114. protected Rect cacheRect { get; set; } = new Rect(0, 0, 0, 0);
  115. /// <summary>
  116. /// Current set drawing rectangle (original data)
  117. /// </summary>
  118. protected Rect setDrawRect { get; set; } = new Rect(0, 0, 0, 0);
  119. /// <summary>
  120. /// Maximum drawable range
  121. /// </summary>
  122. protected Rect maxRect { get; set; } = new Rect(0, 0, 0, 0);
  123. /// <summary>
  124. /// Mouse start drawing point
  125. /// </summary>
  126. protected Point mouseStartPoint { get; set; }
  127. /// <summary>
  128. /// Mouse end drawing point
  129. /// </summary>
  130. protected Point mouseEndPoint { get; set; }
  131. /// <summary>
  132. /// Identifies whether proportional scaling is required
  133. /// </summary>
  134. protected bool isProportionalScaling { get; set; } = false;
  135. /// <summary>
  136. /// Identifies whether to draw a hover state
  137. /// </summary>
  138. protected bool isHover = false;
  139. /// <summary>
  140. /// Identifies whether to draw a selected state
  141. /// </summary>
  142. protected bool isSelected = false;
  143. protected bool isDrawCreateSelected = false;
  144. public PageSelectedRect(DefaultDrawParam defaultDrawParam)
  145. {
  146. DrawParam = defaultDrawParam;
  147. currentDrawPointType = DrawPointType.Square;
  148. }
  149. public void SetIsHover(bool hover)
  150. {
  151. isHover = hover;
  152. }
  153. public bool GetIsHover()
  154. {
  155. return isHover;
  156. }
  157. public void SetIsSelected(bool selected)
  158. {
  159. isSelected = selected;
  160. }
  161. public bool GetIsSelected()
  162. {
  163. return isSelected;
  164. }
  165. /// <summary>
  166. /// Get the original set Rect, not the calculated filled
  167. /// </summary>
  168. /// <param name="newRect">
  169. /// The new rectangle to be set
  170. /// </param>
  171. public Rect GetRect()
  172. {
  173. Rect rect = new Rect(drawRect.X + rectPadding, drawRect.Y + rectPadding, Math.Max(0, drawRect.Width - 2 * rectPadding), Math.Max(0, drawRect.Height - 2 * rectPadding));
  174. return rect;
  175. }
  176. public void SetRect(Rect newRect)
  177. {
  178. newRect = new Rect(newRect.X - rectPadding, newRect.Y - rectPadding, newRect.Width + 2 * rectPadding, newRect.Height + 2 * rectPadding);
  179. setDrawRect = drawRect = newRect;
  180. }
  181. public void SetMaxRect(Rect rect)
  182. {
  183. maxRect = rect;
  184. }
  185. public Rect GetMaxRect()
  186. {
  187. return maxRect;
  188. }
  189. public void SetDrawMoveType(DrawMoveType drawType)
  190. {
  191. currentDrawMoveType = drawType;
  192. }
  193. public DrawMoveType GetDrawMoveType()
  194. {
  195. return currentDrawMoveType;
  196. }
  197. public void SetPDFViewerActualSize(double width, double height)
  198. {
  199. PDFViewerActualWidth = width;
  200. PDFViewerActualHeight = height;
  201. }
  202. public virtual void OnMouseLeftButtonDown(Point downPoint)
  203. {
  204. isMouseDown = true;
  205. hitControlType = PointControlType.None;
  206. mouseDownPoint = downPoint;
  207. moveOffset = new Point(0, 0);
  208. HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
  209. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  210. {
  211. hitControlType = GetHitControlIndex(downPoint);
  212. if (hitControlType != PointControlType.None)
  213. {
  214. cacheRect = drawRect;
  215. }
  216. }
  217. }
  218. public virtual void OnMouseLeftButtonUp(Point upPoint)
  219. {
  220. if (isDrawCreateSelected)
  221. {
  222. Draw();
  223. if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
  224. {
  225. InvokeDataChangEvent(true);
  226. }
  227. }
  228. else
  229. {
  230. if (isMouseDown && hitControlType != PointControlType.None)
  231. {
  232. isMouseDown = false;
  233. cacheRect = setDrawRect = drawRect;
  234. Draw();
  235. if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
  236. {
  237. InvokeDataChangEvent(true);
  238. }
  239. }
  240. moveOffset = new Point(0, 0);
  241. }
  242. isDrawCreateSelected = false;
  243. }
  244. public virtual void OnMouseMove(Point mousePoint, out bool Tag, double width, double height)
  245. {
  246. Tag = false;
  247. Tag = isMouseDown;
  248. SetPDFViewerActualSize(width, height);
  249. if (isDrawCreateSelected)
  250. {
  251. mouseEndPoint = mousePoint;
  252. Draw();
  253. }
  254. else
  255. {
  256. if (isMouseDown && hitControlType != PointControlType.None)
  257. {
  258. if (CalcHitPointMove(mousePoint))
  259. {
  260. Draw();
  261. if ((int)mousePoint.X != (int)mouseDownPoint.X || (int)mousePoint.Y != (int)mouseDownPoint.Y)
  262. {
  263. InvokeDataChangEvent(false);
  264. }
  265. }
  266. }
  267. }
  268. }
  269. public void CreateRect(Point downPoint, Point cropPoint, Rect maxRect, double width, double height)
  270. {
  271. mouseEndPoint = mouseStartPoint = downPoint;
  272. this.maxRect = maxRect;
  273. PDFViewerActualWidth = width;
  274. PDFViewerActualHeight = height;
  275. isDrawCreateSelected = true;
  276. }
  277. public void Draw()
  278. {
  279. if (isDrawCreateSelected)
  280. {
  281. CreateRect();
  282. }
  283. else
  284. {
  285. EditRect();
  286. }
  287. }
  288. private void CreateRect()
  289. {
  290. Dispatcher.Invoke(() =>
  291. {
  292. drawDC = RenderOpen();
  293. SolidColorBrush bgsolidColorBrush = DrawParam.PageSelectedBgBrush;
  294. Pen bgpen = DrawParam.PageSelectedRectLinePen;
  295. CombinedGeometry clipGeometry = new CombinedGeometry();
  296. clipGeometry.Geometry1 = new RectangleGeometry() { Rect = maxRect };
  297. clipGeometry.Geometry2 = new RectangleGeometry() { Rect = drawRect };
  298. clipGeometry.GeometryCombineMode = GeometryCombineMode.Exclude;
  299. drawDC?.DrawGeometry(bgsolidColorBrush, bgpen, clipGeometry);
  300. SolidColorBrush solidColorBrush = DrawParam.PageSelectedRectFillBrush;
  301. Pen pen = DrawParam.PageSelectedRectLinePen;
  302. if (isProportionalScaling)
  303. {
  304. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  305. if (mouseOffset.X < 0)
  306. {
  307. if (mouseOffset.Y > 0)
  308. {
  309. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  310. }
  311. else
  312. {
  313. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  314. }
  315. }
  316. else
  317. {
  318. if (mouseOffset.Y > 0)
  319. {
  320. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  321. }
  322. else
  323. {
  324. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  325. }
  326. }
  327. }
  328. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  329. double mLeft = rect.Left;
  330. double mRight = rect.Right;
  331. double mUp = rect.Top;
  332. double mDown = rect.Bottom;
  333. if (rect.Left < maxRect.Left)
  334. {
  335. mLeft = maxRect.Left;
  336. }
  337. if (rect.Right > maxRect.Right)
  338. {
  339. mRight = maxRect.Right;
  340. }
  341. if (rect.Top < maxRect.Top)
  342. {
  343. mUp = maxRect.Top;
  344. }
  345. if (rect.Bottom > maxRect.Bottom)
  346. {
  347. mDown = maxRect.Bottom;
  348. }
  349. Rect DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  350. int halfPenWidth = (int)Math.Ceiling(pen.Thickness / 2);
  351. double drawWidth = DPIRect.Width - halfPenWidth * 2;
  352. double drawHeight = DPIRect.Height - halfPenWidth * 2;
  353. if (drawWidth > 0 && drawHeight > 0)
  354. {
  355. drawRect = new Rect(
  356. (int)DPIRect.Left + halfPenWidth,
  357. (int)DPIRect.Top + halfPenWidth,
  358. (int)DPIRect.Width - halfPenWidth * 2,
  359. (int)DPIRect.Height - halfPenWidth * 2);
  360. drawDC?.DrawRectangle(solidColorBrush, pen, drawRect);
  361. CalcControlPoint(drawRect);
  362. SolidColorBrush PointBrush = DrawParam.PageSelectedPointBorderBrush;
  363. Pen PointPen = DrawParam.PageSelectedPointPen;
  364. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  365. switch (currentDrawPointType)
  366. {
  367. case DrawPointType.Circle:
  368. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  369. break;
  370. case DrawPointType.Square:
  371. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  372. break;
  373. }
  374. }
  375. drawDC?.Close();
  376. drawDC = null;
  377. });
  378. }
  379. private void EditRect()
  380. {
  381. Dispatcher.Invoke(() =>
  382. {
  383. drawDC = RenderOpen();
  384. SolidColorBrush bgsolidColorBrush = DrawParam.PageSelectedBgBrush;
  385. Pen bgpen = DrawParam.PageSelectedRectLinePen;
  386. CombinedGeometry clipGeometry = new CombinedGeometry();
  387. clipGeometry.Geometry1 = new RectangleGeometry() { Rect = maxRect };
  388. clipGeometry.Geometry2 = new RectangleGeometry() { Rect = drawRect };
  389. clipGeometry.GeometryCombineMode = GeometryCombineMode.Exclude;
  390. drawDC?.DrawGeometry(bgsolidColorBrush, bgpen, clipGeometry);
  391. CalcControlPoint(drawRect);
  392. SolidColorBrush solidColorBrush = DrawParam.PageSelectedRectFillBrush;
  393. Pen pen = DrawParam.PageSelectedRectLinePen;
  394. SolidColorBrush PointBrush = DrawParam.PageSelectedPointBorderBrush;
  395. Pen PointPen = DrawParam.PageSelectedPointPen;
  396. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  397. switch (currentDrawMoveType)
  398. {
  399. case DrawMoveType.kDefault:
  400. break;
  401. case DrawMoveType.kReferenceLine:
  402. if (isMouseDown == true)
  403. {
  404. SolidColorBrush moveBrush = DrawParam.PDFEditMultiMoveBrush;
  405. Pen movepen = DrawParam.PDFEditMultiMovePen;
  406. GetMoveBrushAndPen(ref moveBrush, ref movepen);
  407. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect);
  408. }
  409. drawDC?.DrawRectangle(solidColorBrush, pen, drawRect);
  410. break;
  411. default:
  412. break;
  413. }
  414. switch (currentDrawPointType)
  415. {
  416. case DrawPointType.Circle:
  417. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  418. break;
  419. case DrawPointType.Square:
  420. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  421. break;
  422. }
  423. drawDC?.Close();
  424. drawDC = null;
  425. });
  426. }
  427. public virtual void ClearDraw()
  428. {
  429. setDrawRect = drawRect = new Rect();
  430. drawDC = RenderOpen();
  431. drawDC?.Close();
  432. drawDC = null;
  433. }
  434. private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  435. {
  436. colorBrush = DrawParam.PageSelectedMoveBrush;
  437. pen = DrawParam.PageSelectedMovePen;
  438. }
  439. private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  440. {
  441. if (isHover)
  442. {
  443. colorBrush = DrawParam.PageSelectedRectFillHoverBrush;
  444. pen = DrawParam.PageSelectedRectLineHoverPen;
  445. }
  446. else
  447. {
  448. if (isSelected)
  449. {
  450. colorBrush = DrawParam.SPageSelectedRectFillBrush;
  451. pen = DrawParam.SPageSelectedRectLinePen;
  452. }
  453. else
  454. {
  455. colorBrush = DrawParam.PageSelectedRectFillBrush;
  456. pen = DrawParam.PageSelectedRectLinePen;
  457. }
  458. }
  459. }
  460. private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  461. {
  462. if (isHover)
  463. {
  464. colorBrush = DrawParam.PageSelectedRectFillHoverBrush;
  465. pen = DrawParam.PageSelectedRectLineHoverPen;
  466. }
  467. else
  468. {
  469. if (isSelected)
  470. {
  471. colorBrush = DrawParam.SPageSelectedPointBorderBrush;
  472. pen = DrawParam.SPageSelectedPointPen;
  473. }
  474. else
  475. {
  476. colorBrush = DrawParam.PageSelectedPointBorderBrush;
  477. pen = DrawParam.PageSelectedPointHoverPen;
  478. }
  479. }
  480. }
  481. /// <summary>
  482. /// Inner drawing circle point
  483. /// </summary>
  484. /// <param name="drawingContext">
  485. /// Drawing context
  486. /// </param>
  487. /// <param name="ControlPoints">
  488. /// Dataset of ignored points. Collection of positions where points need to be drawn.
  489. /// </param>
  490. /// <param name="PointSize">
  491. /// Size of the point
  492. /// </param>
  493. /// <param name="PointPen">
  494. /// Point brush
  495. /// </param>
  496. /// <param name="BorderBrush">
  497. /// Border brush for drawing points
  498. /// </param>
  499. protected void DrawCirclePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  500. {
  501. GeometryGroup controlGroup = new GeometryGroup();
  502. controlGroup.FillRule = FillRule.Nonzero;
  503. List<Point> ignorePointsList = new List<Point>();
  504. // Get specific points
  505. foreach (PointControlType type in ignoreList)
  506. {
  507. if ((int)type < controlPoints.Count)
  508. {
  509. ignorePointsList.Add(controlPoints[(int)type]);
  510. }
  511. }
  512. for (int i = 0; i < controlPoints.Count; i++)
  513. {
  514. Point controlPoint = controlPoints[i];
  515. if (ignorePointsList.Contains(controlPoint))
  516. {
  517. continue;
  518. }
  519. EllipseGeometry circlPoint = new EllipseGeometry(controlPoint, PointSize, PointSize);
  520. controlGroup.Children.Add(circlPoint);
  521. }
  522. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  523. }
  524. /// <summary>
  525. /// Inner drawing square
  526. /// </summary>
  527. /// <param name="drawingContext">
  528. /// Drawing context
  529. /// </param>
  530. /// <param name="ControlPoints">
  531. /// Collection of positions where points need to be drawn
  532. /// </param>
  533. /// <param name="PointSize">
  534. /// Collection of positions where points need to be drawn
  535. /// </param>
  536. /// <param name="PointPen">
  537. /// The brush for drawing points
  538. /// </param>
  539. /// <param name="BorderBrush">
  540. /// The brush for drawing the border of points
  541. /// </param>
  542. protected void DrawSquarePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  543. {
  544. GeometryGroup controlGroup = new GeometryGroup();
  545. controlGroup.FillRule = FillRule.Nonzero;
  546. List<Point> ignorePointsList = new List<Point>();
  547. // Get specific points
  548. foreach (PointControlType type in ignoreList)
  549. {
  550. if ((int)type < controlPoints.Count)
  551. {
  552. ignorePointsList.Add(controlPoints[(int)type]);
  553. }
  554. }
  555. for (int i = 0; i < controlPoints.Count; i++)
  556. {
  557. Point controlPoint = controlPoints[i];
  558. if (ignorePointsList.Contains(controlPoint))
  559. {
  560. continue;
  561. }
  562. RectangleGeometry rectPoint = new RectangleGeometry(new Rect(controlPoint.X - PointSize, controlPoint.Y - PointSize,
  563. PointSize * 2, PointSize * 2), 1, 1);
  564. controlGroup.Children.Add(rectPoint);
  565. }
  566. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  567. }
  568. /// <summary>
  569. /// Draw the reference line in the moving state
  570. /// </summary>
  571. /// <param name="drawDc">
  572. /// Drawing context handle
  573. /// </param>
  574. /// <param name="controltype">
  575. /// Current selected control point type
  576. /// </param>
  577. /// <param name="activePen">
  578. /// Line brush
  579. /// </param>
  580. /// <param name="moveBrush">
  581. /// Rectangle brush
  582. /// </param>
  583. /// <param name="moveRect">
  584. /// Current rectangle to be drawn
  585. /// </param>
  586. protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect)
  587. {
  588. switch (controltype)
  589. {
  590. case PointControlType.LeftTop:
  591. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  592. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  593. break;
  594. case PointControlType.LeftMiddle:
  595. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  596. break;
  597. case PointControlType.LeftBottom:
  598. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  599. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  600. break;
  601. case PointControlType.MiddleBottom:
  602. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  603. break;
  604. case PointControlType.RightBottom:
  605. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  606. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  607. break;
  608. case PointControlType.RightMiddle:
  609. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  610. break;
  611. case PointControlType.RightTop:
  612. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  613. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  614. break;
  615. case PointControlType.MiddleTop:
  616. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  617. break;
  618. case PointControlType.Rotate:
  619. break;
  620. case PointControlType.Body:
  621. case PointControlType.Line:
  622. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  623. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  624. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  625. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  626. break;
  627. default:
  628. break;
  629. }
  630. drawDc?.DrawRectangle(moveBrush, null, moveRect);
  631. }
  632. /// <summary>
  633. /// Calculate the current control point
  634. /// </summary>
  635. /// <param name="currentRect">
  636. /// Control point target rectangle
  637. /// </param>
  638. protected void CalcControlPoint(Rect currentRect)
  639. {
  640. controlPoints.Clear();
  641. int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
  642. int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
  643. controlPoints.Add(new Point(currentRect.Left, currentRect.Top));
  644. controlPoints.Add(new Point(currentRect.Left, centerY));
  645. controlPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  646. controlPoints.Add(new Point(centerX, currentRect.Bottom));
  647. controlPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  648. controlPoints.Add(new Point(currentRect.Right, centerY));
  649. controlPoints.Add(new Point(currentRect.Right, currentRect.Top));
  650. controlPoints.Add(new Point(centerX, currentRect.Top));
  651. }
  652. /// <summary>
  653. /// Calculate the movement of the hit point
  654. /// </summary>
  655. /// <param name="mousePoint">
  656. /// Current mouse position
  657. /// </param>
  658. /// <returns>
  659. /// Return true if the calculation is successful
  660. /// </returns>
  661. protected bool CalcHitPointMove(Point mousePoint)
  662. {
  663. if (isMouseDown == false || hitControlType == PointControlType.None)
  664. {
  665. return false;
  666. }
  667. return NormalScaling(mousePoint);
  668. }
  669. /// <summary>
  670. /// Draw the algorithm of the normal scaling form (drag a point, only scale in one direction)
  671. /// </summary>
  672. /// <param name="mousePoint">
  673. /// Current mouse position
  674. /// </param>
  675. /// <returns>
  676. /// Return true if the calculation is successful
  677. /// </returns>
  678. protected bool NormalScaling(Point mousePoint)
  679. {
  680. double mLeft = cacheRect.Left;
  681. double mRight = cacheRect.Right;
  682. double mUp = cacheRect.Top;
  683. double mDown = cacheRect.Bottom;
  684. double TmpLeft = mLeft, TmpRight = mRight, TmpUp = mUp, TmpDown = mDown;
  685. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  686. Point moveVector = (Point)(mousePoint - centerPoint);
  687. moveVector = ProportionalScalingOffsetPos(moveVector);
  688. switch (hitControlType)
  689. {
  690. case PointControlType.LeftTop:
  691. TmpLeft = centerPoint.X + moveVector.X;
  692. TmpRight = cacheRect.Right;
  693. if (TmpLeft + rectMinWidth > TmpRight)
  694. {
  695. TmpLeft = TmpRight - rectMinWidth;
  696. }
  697. TmpUp = centerPoint.Y + moveVector.Y;
  698. TmpDown = cacheRect.Bottom;
  699. if (TmpUp + rectMinHeight > TmpDown)
  700. {
  701. TmpUp = TmpDown - rectMinHeight;
  702. }
  703. break;
  704. case PointControlType.LeftMiddle:
  705. TmpLeft = centerPoint.X + moveVector.X;
  706. TmpRight = cacheRect.Right;
  707. if (TmpLeft + rectMinWidth > TmpRight)
  708. {
  709. TmpLeft = TmpRight - rectMinWidth;
  710. }
  711. TmpUp = cacheRect.Top;
  712. TmpDown = cacheRect.Bottom;
  713. break;
  714. case PointControlType.LeftBottom:
  715. TmpLeft = centerPoint.X + moveVector.X;
  716. TmpRight = cacheRect.Right;
  717. if (TmpLeft + rectMinWidth > TmpRight)
  718. {
  719. TmpLeft = TmpRight - rectMinWidth;
  720. }
  721. TmpUp = cacheRect.Top;
  722. TmpDown = centerPoint.Y + moveVector.Y;
  723. if (TmpUp + rectMinHeight > TmpDown)
  724. {
  725. TmpDown = TmpUp + rectMinHeight;
  726. }
  727. break;
  728. case PointControlType.MiddleBottom:
  729. TmpLeft = cacheRect.Left;
  730. TmpRight = cacheRect.Right;
  731. TmpUp = cacheRect.Top;
  732. TmpDown = centerPoint.Y + moveVector.Y;
  733. if (TmpUp + rectMinHeight > TmpDown)
  734. {
  735. TmpDown = TmpUp + rectMinHeight;
  736. }
  737. break;
  738. case PointControlType.RightBottom:
  739. TmpLeft = cacheRect.Left;
  740. TmpRight = centerPoint.X + moveVector.X;
  741. if (TmpLeft + rectMinWidth > TmpRight)
  742. {
  743. TmpRight = TmpLeft + rectMinWidth;
  744. }
  745. TmpUp = cacheRect.Top;
  746. TmpDown = centerPoint.Y + moveVector.Y;
  747. if (TmpUp + rectMinHeight > TmpDown)
  748. {
  749. TmpDown = TmpUp + rectMinHeight;
  750. }
  751. break;
  752. case PointControlType.RightMiddle:
  753. TmpLeft = cacheRect.Left;
  754. TmpRight = centerPoint.X + moveVector.X;
  755. if (TmpLeft + rectMinWidth > TmpRight)
  756. {
  757. TmpRight = TmpLeft + rectMinWidth;
  758. }
  759. TmpUp = cacheRect.Top;
  760. TmpDown = cacheRect.Bottom;
  761. break;
  762. case PointControlType.RightTop:
  763. TmpLeft = cacheRect.Left;
  764. TmpRight = centerPoint.X + moveVector.X;
  765. if (TmpLeft + rectMinWidth > TmpRight)
  766. {
  767. TmpRight = TmpLeft + rectMinWidth;
  768. }
  769. TmpUp = centerPoint.Y + moveVector.Y;
  770. TmpDown = cacheRect.Bottom;
  771. if (TmpUp + rectMinHeight > TmpDown)
  772. {
  773. TmpUp = TmpDown - rectMinHeight;
  774. }
  775. break;
  776. case PointControlType.MiddleTop:
  777. TmpLeft = cacheRect.Left;
  778. TmpRight = cacheRect.Right;
  779. TmpUp = centerPoint.Y + moveVector.Y;
  780. TmpDown = cacheRect.Bottom;
  781. if (TmpUp + rectMinHeight > TmpDown)
  782. {
  783. TmpUp = TmpDown - rectMinHeight;
  784. }
  785. break;
  786. case PointControlType.Body:
  787. case PointControlType.Line:
  788. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), maxRect);
  789. TmpLeft = cacheRect.Left + OffsetPos.X;
  790. TmpRight = cacheRect.Right + OffsetPos.X;
  791. TmpUp = cacheRect.Top + OffsetPos.Y;
  792. TmpDown = cacheRect.Bottom + OffsetPos.Y;
  793. break;
  794. default:
  795. break;
  796. }
  797. if (TmpLeft < maxRect.Left)
  798. {
  799. TmpLeft = maxRect.Left;
  800. }
  801. if (TmpUp < maxRect.Top)
  802. {
  803. TmpUp = maxRect.Top;
  804. }
  805. if (TmpRight > maxRect.Right)
  806. {
  807. TmpRight = maxRect.Right;
  808. }
  809. if (TmpDown > maxRect.Bottom)
  810. {
  811. TmpDown = maxRect.Bottom;
  812. }
  813. if (TmpRight - TmpLeft < 0.0 || TmpDown - TmpUp < 0.0)
  814. {
  815. return false;
  816. }
  817. drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  818. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  819. return true;
  820. }
  821. /// <summary>
  822. /// Proportional scaling offset calibration.
  823. /// </summary>
  824. /// <param name="movePoint">
  825. /// Current mouse position
  826. /// </param>
  827. /// <returns>
  828. /// Return the calibrated offset value
  829. /// </returns>
  830. protected Point ProportionalScalingOffsetPos(Point movePoint)
  831. {
  832. if (isProportionalScaling)
  833. {
  834. Point offsetPos = movePoint;
  835. double ratioX = cacheRect.Width > 0 ? cacheRect.Height / cacheRect.Width : 1;
  836. double ratioY = cacheRect.Height > 0 ? cacheRect.Width / cacheRect.Height : 1;
  837. switch (hitControlType)
  838. {
  839. case PointControlType.LeftTop:
  840. case PointControlType.RightBottom:
  841. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  842. break;
  843. case PointControlType.LeftBottom:
  844. case PointControlType.RightTop:
  845. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  846. break;
  847. case PointControlType.LeftMiddle:
  848. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  849. break;
  850. case PointControlType.RightMiddle:
  851. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  852. break;
  853. case PointControlType.MiddleBottom:
  854. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? 1 : -1), movePoint.Y);
  855. break;
  856. case PointControlType.MiddleTop:
  857. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? -1 : 1), movePoint.Y);
  858. break;
  859. default:
  860. break;
  861. }
  862. return offsetPos;
  863. }
  864. else
  865. {
  866. return movePoint;
  867. }
  868. }
  869. /// <summary>
  870. /// Calc the offset of the current rectangle in the maximum rectangle range
  871. /// </summary>
  872. /// <param name="currentRect">
  873. /// The rectangle cached when pressed.
  874. /// </param>
  875. /// <param name="offsetPoint">
  876. /// Equivalent to the offset value when pressed.
  877. /// </param>
  878. /// <param name="maxRect">
  879. /// The maximum rectangle range.
  880. /// </param>
  881. /// <returns>
  882. /// Return the offset value after the calculation.
  883. /// </returns>
  884. protected Point CalcMoveBound(Rect currentRect, Point offsetPoint, Rect maxRect)
  885. {
  886. double cLeft = currentRect.Left;
  887. double cRight = currentRect.Right;
  888. double cUp = currentRect.Top;
  889. double cDown = currentRect.Bottom;
  890. double TmpLeft = cLeft + offsetPoint.X;
  891. double TmpRight = cRight + offsetPoint.X;
  892. double TmpUp = cUp + offsetPoint.Y;
  893. double TmpDown = cDown + offsetPoint.Y;
  894. if (TmpLeft < maxRect.Left)
  895. {
  896. TmpRight = (cRight - cLeft) + maxRect.Left;
  897. TmpLeft = maxRect.Left;
  898. }
  899. if (TmpUp < maxRect.Top)
  900. {
  901. TmpDown = (cDown - cUp) + maxRect.Top;
  902. TmpUp = maxRect.Top;
  903. }
  904. if (TmpRight > maxRect.Right)
  905. {
  906. TmpLeft = maxRect.Right - (cRight - cLeft);
  907. TmpRight = maxRect.Right;
  908. }
  909. if (TmpDown > maxRect.Bottom)
  910. {
  911. TmpUp = maxRect.Bottom - (cDown - cUp);
  912. TmpDown = maxRect.Bottom;
  913. }
  914. offsetPoint = new Point(TmpLeft - cLeft, TmpUp - cUp);
  915. return offsetPoint;
  916. }
  917. /// <summary>
  918. /// Get which control point the coordinate is on
  919. /// </summary>
  920. /// <param name="clickPoint">
  921. /// The coordinate of the click
  922. /// </param>
  923. /// <returns>
  924. /// Return the control point type
  925. /// </returns>
  926. public PointControlType GetHitControlIndex(Point point)
  927. {
  928. try
  929. {
  930. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  931. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  932. {
  933. List<PointControlType> ignoreList = GetIgnorePoints();
  934. List<Point> IgnorePointsList = new List<Point>();
  935. foreach (PointControlType type in ignoreList)
  936. {
  937. if ((int)type < controlPoints.Count)
  938. {
  939. IgnorePointsList.Add(controlPoints[(int)type]);
  940. }
  941. }
  942. for (int i = 0; i < controlPoints.Count; i++)
  943. {
  944. Point checkPoint = controlPoints[i];
  945. if (IgnorePointsList.Contains(checkPoint))
  946. {
  947. continue;
  948. }
  949. switch (currentDrawPointType)
  950. {
  951. case DrawPointType.Circle:
  952. Vector checkVector = checkPoint - point;
  953. if (checkVector.Length < pointSize)
  954. {
  955. return (PointControlType)i;
  956. }
  957. break;
  958. case DrawPointType.Square:
  959. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  960. if (checkRect.Contains(point))
  961. {
  962. return (PointControlType)i;
  963. }
  964. break;
  965. case DrawPointType.Crop:
  966. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  967. if (cropRect.Contains(point))
  968. {
  969. return (PointControlType)i;
  970. }
  971. break;
  972. default:
  973. break;
  974. }
  975. }
  976. if (drawRect.Contains(point))
  977. {
  978. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), drawRect.Width - 2 * rectPadding, drawRect.Height - 2 * rectPadding);
  979. if (rect.Contains(point))
  980. {
  981. if (!ignoreList.Contains(PointControlType.Body))
  982. {
  983. return PointControlType.Body;
  984. }
  985. }
  986. if (!ignoreList.Contains(PointControlType.Body))
  987. {
  988. return PointControlType.Line;
  989. }
  990. }
  991. }
  992. }
  993. catch (Exception ex)
  994. {
  995. }
  996. return PointControlType.None;
  997. }
  998. /// <summary>
  999. /// Get the current set of ignored points
  1000. /// </summary>
  1001. /// The dataset of ignored points
  1002. /// </returns>
  1003. private List<PointControlType> GetIgnorePoints()
  1004. {
  1005. List<PointControlType> IgnorePointsList = new List<PointControlType>();
  1006. foreach (PointControlType type in ignorePoints)
  1007. {
  1008. IgnorePointsList.Add(type);
  1009. }
  1010. return IgnorePointsList;
  1011. }
  1012. /// <summary>
  1013. /// Notify events during/after drawing data
  1014. /// </summary>
  1015. /// <param name="isFinish">
  1016. /// Identifies whether the data has changed
  1017. /// </param>
  1018. protected void InvokeDataChangEvent(bool isFinish)
  1019. {
  1020. if (isFinish)
  1021. {
  1022. DataChanged?.Invoke(this, drawRect);
  1023. }
  1024. else
  1025. {
  1026. DataChanging?.Invoke(this, drawRect);
  1027. }
  1028. }
  1029. }
  1030. }