PageSelectedRect.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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.MiddlBottom:
  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.MiddlBottom:
  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. drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  814. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  815. return true;
  816. }
  817. /// <summary>
  818. /// Proportional scaling offset calibration.
  819. /// </summary>
  820. /// <param name="movePoint">
  821. /// Current mouse position
  822. /// </param>
  823. /// <returns>
  824. /// Return the calibrated offset value
  825. /// </returns>
  826. protected Point ProportionalScalingOffsetPos(Point movePoint)
  827. {
  828. if (isProportionalScaling)
  829. {
  830. Point offsetPos = movePoint;
  831. double ratioX = cacheRect.Width > 0 ? cacheRect.Height / cacheRect.Width : 1;
  832. double ratioY = cacheRect.Height > 0 ? cacheRect.Width / cacheRect.Height : 1;
  833. switch (hitControlType)
  834. {
  835. case PointControlType.LeftTop:
  836. case PointControlType.RightBottom:
  837. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  838. break;
  839. case PointControlType.LeftBottom:
  840. case PointControlType.RightTop:
  841. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  842. break;
  843. case PointControlType.LeftMiddle:
  844. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  845. break;
  846. case PointControlType.RightMiddle:
  847. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  848. break;
  849. case PointControlType.MiddlBottom:
  850. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? 1 : -1), movePoint.Y);
  851. break;
  852. case PointControlType.MiddleTop:
  853. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? -1 : 1), movePoint.Y);
  854. break;
  855. default:
  856. break;
  857. }
  858. return offsetPos;
  859. }
  860. else
  861. {
  862. return movePoint;
  863. }
  864. }
  865. /// <summary>
  866. /// Calc the offset of the current rectangle in the maximum rectangle range
  867. /// </summary>
  868. /// <param name="currentRect">
  869. /// The rectangle cached when pressed.
  870. /// </param>
  871. /// <param name="offsetPoint">
  872. /// Equivalent to the offset value when pressed.
  873. /// </param>
  874. /// <param name="maxRect">
  875. /// The maximum rectangle range.
  876. /// </param>
  877. /// <returns>
  878. /// Return the offset value after the calculation.
  879. /// </returns>
  880. protected Point CalcMoveBound(Rect currentRect, Point offsetPoint, Rect maxRect)
  881. {
  882. double cLeft = currentRect.Left;
  883. double cRight = currentRect.Right;
  884. double cUp = currentRect.Top;
  885. double cDown = currentRect.Bottom;
  886. double TmpLeft = cLeft + offsetPoint.X;
  887. double TmpRight = cRight + offsetPoint.X;
  888. double TmpUp = cUp + offsetPoint.Y;
  889. double TmpDown = cDown + offsetPoint.Y;
  890. if (TmpLeft < maxRect.Left)
  891. {
  892. TmpRight = (cRight - cLeft) + maxRect.Left;
  893. TmpLeft = maxRect.Left;
  894. }
  895. if (TmpUp < maxRect.Top)
  896. {
  897. TmpDown = (cDown - cUp) + maxRect.Top;
  898. TmpUp = maxRect.Top;
  899. }
  900. if (TmpRight > maxRect.Right)
  901. {
  902. TmpLeft = maxRect.Right - (cRight - cLeft);
  903. TmpRight = maxRect.Right;
  904. }
  905. if (TmpDown > maxRect.Bottom)
  906. {
  907. TmpUp = maxRect.Bottom - (cDown - cUp);
  908. TmpDown = maxRect.Bottom;
  909. }
  910. offsetPoint = new Point(TmpLeft - cLeft, TmpUp - cUp);
  911. return offsetPoint;
  912. }
  913. /// <summary>
  914. /// Get which control point the coordinate is on
  915. /// </summary>
  916. /// <param name="clickPoint">
  917. /// The coordinate of the click
  918. /// </param>
  919. /// <returns>
  920. /// Return the control point type
  921. /// </returns>
  922. public PointControlType GetHitControlIndex(Point point)
  923. {
  924. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  925. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  926. {
  927. List<PointControlType> ignoreList = GetIgnorePoints();
  928. List<Point> IgnorePointsList = new List<Point>();
  929. foreach (PointControlType type in ignoreList)
  930. {
  931. if ((int)type < controlPoints.Count)
  932. {
  933. IgnorePointsList.Add(controlPoints[(int)type]);
  934. }
  935. }
  936. for (int i = 0; i < controlPoints.Count; i++)
  937. {
  938. Point checkPoint = controlPoints[i];
  939. if (IgnorePointsList.Contains(checkPoint))
  940. {
  941. continue;
  942. }
  943. switch (currentDrawPointType)
  944. {
  945. case DrawPointType.Circle:
  946. Vector checkVector = checkPoint - point;
  947. if (checkVector.Length < pointSize)
  948. {
  949. return (PointControlType)i;
  950. }
  951. break;
  952. case DrawPointType.Square:
  953. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize,0), pointSize * 2, pointSize * 2);
  954. if (checkRect.Contains(point))
  955. {
  956. return (PointControlType)i;
  957. }
  958. break;
  959. case DrawPointType.Crop:
  960. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  961. if (cropRect.Contains(point))
  962. {
  963. return (PointControlType)i;
  964. }
  965. break;
  966. default:
  967. break;
  968. }
  969. }
  970. if (drawRect.Contains(point))
  971. {
  972. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), drawRect.Width - 2 * rectPadding, drawRect.Height - 2 * rectPadding);
  973. if (rect.Contains(point))
  974. {
  975. if (!ignoreList.Contains(PointControlType.Body))
  976. {
  977. return PointControlType.Body;
  978. }
  979. }
  980. if (!ignoreList.Contains(PointControlType.Body))
  981. {
  982. return PointControlType.Line;
  983. }
  984. }
  985. }
  986. return PointControlType.None;
  987. }
  988. /// <summary>
  989. /// Get the current set of ignored points
  990. /// </summary>
  991. /// The dataset of ignored points
  992. /// </returns>
  993. private List<PointControlType> GetIgnorePoints()
  994. {
  995. List<PointControlType> IgnorePointsList = new List<PointControlType>();
  996. foreach (PointControlType type in ignorePoints)
  997. {
  998. IgnorePointsList.Add(type);
  999. }
  1000. return IgnorePointsList;
  1001. }
  1002. /// <summary>
  1003. /// Notify events during/after drawing data
  1004. /// </summary>
  1005. /// <param name="isFinish">
  1006. /// Identifies whether the data has changed
  1007. /// </param>
  1008. protected void InvokeDataChangEvent(bool isFinish)
  1009. {
  1010. if (isFinish)
  1011. {
  1012. DataChanged?.Invoke(this, drawRect);
  1013. }
  1014. else
  1015. {
  1016. DataChanging?.Invoke(this, drawRect);
  1017. }
  1018. }
  1019. }
  1020. }