MultiSelectedRect.cs 45 KB

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