MultiSelectedRect.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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.Circle;
  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. if (selectedType == SelectedType.PDFEdit)
  301. {
  302. //Edit Settings Frame
  303. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, new SolidColorBrush(Color.FromRgb(71, 126, 222)));
  304. }
  305. else
  306. {
  307. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  308. }
  309. break;
  310. case DrawPointType.Square:
  311. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  312. break;
  313. }
  314. drawDC?.Close();
  315. drawDC = null;
  316. }
  317. });
  318. break;
  319. case MulitiDrawMoveType.Alone:
  320. CalcControlPoint(drawDefaultRect);
  321. foreach (SelectedRect selectRect in selectedRects)
  322. {
  323. selectRect.Draw();
  324. }
  325. break;
  326. default:
  327. break;
  328. }
  329. }
  330. private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  331. {
  332. switch (selectedType)
  333. {
  334. case SelectedType.None:
  335. break;
  336. case SelectedType.Annot:
  337. //colorBrush = DrawParam.AnnotMoveBrush;
  338. //pen = DrawParam.AnnotMovePen;
  339. break;
  340. case SelectedType.PDFEdit:
  341. colorBrush = drawParam.PDFEditMultiMoveBrush;
  342. pen = drawParam.PDFEditMultiMovePen;
  343. break;
  344. default:
  345. break;
  346. }
  347. }
  348. private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  349. {
  350. switch (selectedType)
  351. {
  352. case SelectedType.None:
  353. break;
  354. case SelectedType.Annot:
  355. //if (isHover)
  356. //{
  357. // colorBrush = DrawParam.AnnotRectFillBrush;
  358. // pen = DrawParam.AnnotRectHoverPen;
  359. //}
  360. //else
  361. //{
  362. // colorBrush = DrawParam.AnnotRectFillBrush;
  363. // pen = DrawParam.AnnotRectLinePen;
  364. //}
  365. break;
  366. case SelectedType.PDFEdit:
  367. if (isHover)
  368. {
  369. colorBrush = drawParam.PDFEditMultiRectFillHoverBrush;
  370. pen = drawParam.PDFEditMultiRectLineHoverPen;
  371. }
  372. else
  373. {
  374. if (isSelected)
  375. {
  376. colorBrush = drawParam.SPDFEditMultiRectFillBrush;
  377. pen = drawParam.SPDFEditMultiRectLinePen;
  378. }
  379. else
  380. {
  381. colorBrush = drawParam.PDFEditMultiRectFillBrush;
  382. pen = drawParam.PDFEditMultiRectLinePen;
  383. }
  384. }
  385. break;
  386. default:
  387. break;
  388. }
  389. }
  390. private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  391. {
  392. switch (selectedType)
  393. {
  394. case SelectedType.None:
  395. break;
  396. case SelectedType.Annot:
  397. //colorBrush = DrawParam.AnnotPointBorderBrush;
  398. //pen = DrawParam.AnnotPointPen;
  399. break;
  400. case SelectedType.PDFEdit:
  401. if (isHover)
  402. {
  403. colorBrush = drawParam.PDFEditMultiRectFillHoverBrush;
  404. pen = drawParam.PDFEditMultiPointHoverPen;
  405. }
  406. else
  407. {
  408. if (isSelected)
  409. {
  410. colorBrush = drawParam.SPDFEditMultiPointBorderBrush;
  411. pen = drawParam.SPDFEditMultiPointPen;
  412. }
  413. else
  414. {
  415. colorBrush = drawParam.PDFEditMultiPointBorderBrush;
  416. pen = drawParam.PDFEditMultiPointPen;
  417. }
  418. }
  419. break;
  420. default:
  421. break;
  422. }
  423. }
  424. /// <summary>
  425. /// Internal drawing circle point
  426. /// </summary>
  427. /// <param name="drawingContext">
  428. /// Drawing context
  429. /// </param>
  430. /// <param name="ignoreList">
  431. ///Collection of positions where points need to be drawn
  432. /// </param>
  433. /// <param name="PointSize">
  434. /// Point size
  435. /// </param>
  436. /// <param name="PointPen">
  437. /// Drawing point brush
  438. /// </param>
  439. /// <param name="BorderBrush">
  440. /// Brush for drawing point border
  441. /// </param>
  442. protected void DrawCirclePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  443. {
  444. GeometryGroup controlGroup = new GeometryGroup();
  445. controlGroup.FillRule = FillRule.Nonzero;
  446. List<Point> ignorePointsList = new List<Point>();
  447. // Get specific points
  448. foreach (PointControlType type in ignoreList)
  449. {
  450. if ((int)type < controlPoints.Count)
  451. {
  452. ignorePointsList.Add(controlPoints[(int)type]);
  453. }
  454. }
  455. for (int i = 0; i < controlPoints.Count; i++)
  456. {
  457. Point controlPoint = controlPoints[i];
  458. if (ignorePointsList.Contains(controlPoint))
  459. {
  460. continue;
  461. }
  462. EllipseGeometry circlPoint = new EllipseGeometry(controlPoint, PointSize, PointSize);
  463. controlGroup.Children.Add(circlPoint);
  464. }
  465. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  466. }
  467. /// <summary>
  468. /// Internal drawing square
  469. /// </summary>
  470. /// <param name="drawingContext">
  471. /// Drawing context
  472. /// </param>
  473. /// <param name="ControlPoints">
  474. /// Collection of positions where points need to be drawn
  475. /// </param>
  476. /// <param name="PointSize">
  477. /// Size of the point
  478. /// </param>
  479. /// <param name="PointPen">
  480. /// Brush for drawing point
  481. /// </param>
  482. /// <param name="BorderBrush">
  483. /// Border brush for drawing points
  484. /// </param>
  485. protected void DrawSquarePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  486. {
  487. GeometryGroup controlGroup = new GeometryGroup();
  488. controlGroup.FillRule = FillRule.Nonzero;
  489. List<Point> ignorePointsList = new List<Point>();
  490. // Get specific points
  491. foreach (PointControlType type in ignoreList)
  492. {
  493. if ((int)type < controlPoints.Count)
  494. {
  495. ignorePointsList.Add(controlPoints[(int)type]);
  496. }
  497. }
  498. for (int i = 0; i < controlPoints.Count; i++)
  499. {
  500. Point controlPoint = controlPoints[i];
  501. if (ignorePointsList.Contains(controlPoint))
  502. {
  503. continue;
  504. }
  505. RectangleGeometry rectPoint = new RectangleGeometry(new Rect(controlPoint.X - PointSize, controlPoint.Y - PointSize,
  506. PointSize * 2, PointSize * 2), 1, 1);
  507. controlGroup.Children.Add(rectPoint);
  508. }
  509. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  510. }
  511. /// <summary>
  512. /// Draw the reference line in the moving state
  513. /// </summary>
  514. /// <param name="drawDc">
  515. /// Drawing context
  516. /// </param>
  517. /// <param name="controltype">
  518. /// Drawing context
  519. /// </param>
  520. /// <param name="activePen">
  521. /// Drawing context
  522. /// </param>
  523. /// <param name="moveBrush">
  524. /// Move brush
  525. /// </param>
  526. /// <param name="moveRect">
  527. /// Move rectangle
  528. /// </param>
  529. protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect)
  530. {
  531. switch (controltype)
  532. {
  533. case PointControlType.LeftTop:
  534. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  535. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  536. break;
  537. case PointControlType.LeftMiddle:
  538. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  539. break;
  540. case PointControlType.LeftBottom:
  541. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  542. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  543. break;
  544. case PointControlType.MiddlBottom:
  545. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  546. break;
  547. case PointControlType.RightBottom:
  548. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  549. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  550. break;
  551. case PointControlType.RightMiddle:
  552. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  553. break;
  554. case PointControlType.RightTop:
  555. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  556. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  557. break;
  558. case PointControlType.MiddleTop:
  559. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  560. break;
  561. case PointControlType.Rotate:
  562. break;
  563. case PointControlType.Body:
  564. case PointControlType.Line:
  565. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  566. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  567. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  568. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  569. break;
  570. default:
  571. break;
  572. }
  573. drawDc?.DrawRectangle(moveBrush, null, moveRect);
  574. }
  575. public virtual void ClearDraw()
  576. {
  577. drawDefaultRect = setDrawRect = drawRect = new Rect();
  578. drawDC = RenderOpen();
  579. drawDC?.Close();
  580. drawDC = null;
  581. }
  582. /// <summary>
  583. /// Calculate the current control point
  584. /// </summary>
  585. /// <param name="currentRect">
  586. /// Target rectangle where the control point is located
  587. /// </param>
  588. protected void CalcControlPoint(Rect currentRect)
  589. {
  590. controlPoints.Clear();
  591. int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
  592. int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
  593. controlPoints.Add(new Point(currentRect.Left, currentRect.Top));
  594. controlPoints.Add(new Point(currentRect.Left, centerY));
  595. controlPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  596. controlPoints.Add(new Point(centerX, currentRect.Bottom));
  597. controlPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  598. controlPoints.Add(new Point(currentRect.Right, centerY));
  599. controlPoints.Add(new Point(currentRect.Right, currentRect.Top));
  600. controlPoints.Add(new Point(centerX, currentRect.Top));
  601. }
  602. /// <summary>
  603. /// Get the original set Rect, not the one that has been calculated for padding
  604. /// </summary>
  605. public Rect GetRect()
  606. {
  607. Rect rect = new Rect(drawRect.X + rectPadding, drawRect.Y + rectPadding, Math.Max(0, drawRect.Width - 2 * rectPadding), Math.Max(0, drawRect.Height - 2 * rectPadding));
  608. return rect;
  609. }
  610. public void SetRect(Rect newRect)
  611. {
  612. newRect = new Rect(newRect.X - rectPadding, newRect.Y - rectPadding, newRect.Width + 2 * rectPadding, newRect.Height + 2 * rectPadding);
  613. if (drawDefaultRect != new Rect())
  614. {
  615. newRect.Union(drawDefaultRect);
  616. newRect.Intersect(maxRect);
  617. }
  618. drawDefaultRect = newRect;
  619. setDrawRect = drawRect = newRect;
  620. }
  621. public void SetRectPadding(double rectPadding)
  622. {
  623. this.rectPadding = rectPadding;
  624. }
  625. public double GetRectPadding()
  626. {
  627. return rectPadding;
  628. }
  629. public Rect GetDrawRect()
  630. {
  631. return drawRect;
  632. }
  633. public void SetMaxRect(Rect rect)
  634. {
  635. maxRect = rect;
  636. }
  637. public Rect GetMaxRect()
  638. {
  639. return maxRect;
  640. }
  641. public void SetIsProportionalScaling(bool isProportionalScaling)
  642. {
  643. this.isProportionalScaling = isProportionalScaling;
  644. }
  645. public void SetDrawType(DrawPointType drawType)
  646. {
  647. currentDrawPointType = drawType;
  648. }
  649. public void SetDrawMoveType(DrawMoveType drawType)
  650. {
  651. currentDrawMoveType = drawType;
  652. }
  653. /// <summary>
  654. /// Set the type that needs to be ignored
  655. /// </summary>
  656. /// <param name="types">
  657. /// Collection of point types that need to be shielded
  658. /// </param>
  659. public void SetIgnorePoints(List<PointControlType> types)
  660. {
  661. ignorePoints.Clear();
  662. foreach (PointControlType type in types)
  663. {
  664. ignorePoints.Add(type);
  665. }
  666. }
  667. /// <summary>
  668. /// Set all points to be ignored
  669. /// </summary>
  670. public void SetIgnorePointsAll()
  671. {
  672. ignorePoints.Clear();
  673. ignorePoints.Add(PointControlType.LeftTop);
  674. ignorePoints.Add(PointControlType.LeftMiddle);
  675. ignorePoints.Add(PointControlType.LeftBottom);
  676. ignorePoints.Add(PointControlType.MiddlBottom);
  677. ignorePoints.Add(PointControlType.RightBottom);
  678. ignorePoints.Add(PointControlType.RightMiddle);
  679. ignorePoints.Add(PointControlType.RightTop);
  680. ignorePoints.Add(PointControlType.MiddleTop);
  681. }
  682. /// <summary>
  683. /// Disable all functions
  684. /// </summary>
  685. public void DisableAll()
  686. {
  687. ignorePoints.Clear();
  688. ignorePoints.Add(PointControlType.LeftTop);
  689. ignorePoints.Add(PointControlType.LeftMiddle);
  690. ignorePoints.Add(PointControlType.LeftBottom);
  691. ignorePoints.Add(PointControlType.MiddlBottom);
  692. ignorePoints.Add(PointControlType.RightBottom);
  693. ignorePoints.Add(PointControlType.RightMiddle);
  694. ignorePoints.Add(PointControlType.RightTop);
  695. ignorePoints.Add(PointControlType.MiddleTop);
  696. ignorePoints.Add(PointControlType.Rotate);
  697. ignorePoints.Add(PointControlType.Body);
  698. ignorePoints.Add(PointControlType.Line);
  699. }
  700. /// <summary>
  701. /// Calculate the movement of the hit point
  702. /// </summary>
  703. /// <param name="mousePoint">
  704. /// Current mouse position
  705. /// </param>
  706. /// <returns></returns>
  707. protected bool CalcHitPointMove(Point mousePoint)
  708. {
  709. if (isMouseDown == false || hitControlType == PointControlType.None)
  710. {
  711. return false;
  712. }
  713. return NormalScaling(mousePoint);
  714. }
  715. /// <summary>
  716. /// Draw the algorithm of the normal scaling form (drag a point, only scale in one direction)
  717. /// </summary>
  718. /// <param name="mousePoint">
  719. /// Current mouse position
  720. /// </param>
  721. /// <returns></returns>
  722. protected bool NormalScaling(Point mousePoint)
  723. {
  724. double mLeft = cacheRect.Left;
  725. double mRight = cacheRect.Right;
  726. double mUp = cacheRect.Top;
  727. double mDown = cacheRect.Bottom;
  728. double TmpLeft = mLeft, TmpRight = mRight, TmpUp = mUp, TmpDown = mDown;
  729. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  730. Point moveVector = (Point)(mousePoint - centerPoint);
  731. moveVector = ProportionalScalingOffsetPos(moveVector);
  732. switch (hitControlType)
  733. {
  734. case PointControlType.LeftTop:
  735. TmpLeft = centerPoint.X + moveVector.X;
  736. TmpRight = cacheRect.Right;
  737. if (TmpLeft + rectMinWidth > TmpRight)
  738. {
  739. TmpLeft = TmpRight - rectMinWidth;
  740. }
  741. TmpUp = centerPoint.Y + moveVector.Y;
  742. TmpDown = cacheRect.Bottom;
  743. if (TmpUp + rectMinHeight > TmpDown)
  744. {
  745. TmpUp = TmpDown - rectMinHeight;
  746. }
  747. break;
  748. case PointControlType.LeftMiddle:
  749. TmpLeft = centerPoint.X + moveVector.X;
  750. TmpRight = cacheRect.Right;
  751. if (TmpLeft + rectMinWidth > TmpRight)
  752. {
  753. TmpLeft = TmpRight - rectMinWidth;
  754. }
  755. TmpUp = cacheRect.Top;
  756. TmpDown = cacheRect.Bottom;
  757. break;
  758. case PointControlType.LeftBottom:
  759. TmpLeft = centerPoint.X + moveVector.X;
  760. TmpRight = cacheRect.Right;
  761. if (TmpLeft + rectMinWidth > TmpRight)
  762. {
  763. TmpLeft = TmpRight - rectMinWidth;
  764. }
  765. TmpUp = cacheRect.Top;
  766. TmpDown = centerPoint.Y + moveVector.Y;
  767. if (TmpUp + rectMinHeight > TmpDown)
  768. {
  769. TmpDown = TmpUp + rectMinHeight;
  770. }
  771. break;
  772. case PointControlType.MiddlBottom:
  773. TmpLeft = cacheRect.Left;
  774. TmpRight = cacheRect.Right;
  775. TmpUp = cacheRect.Top;
  776. TmpDown = centerPoint.Y + moveVector.Y;
  777. if (TmpUp + rectMinHeight > TmpDown)
  778. {
  779. TmpDown = TmpUp + rectMinHeight;
  780. }
  781. break;
  782. case PointControlType.RightBottom:
  783. TmpLeft = cacheRect.Left;
  784. TmpRight = centerPoint.X + moveVector.X;
  785. if (TmpLeft + rectMinWidth > TmpRight)
  786. {
  787. TmpRight = TmpLeft + rectMinWidth;
  788. }
  789. TmpUp = cacheRect.Top;
  790. TmpDown = centerPoint.Y + moveVector.Y;
  791. if (TmpUp + rectMinHeight > TmpDown)
  792. {
  793. TmpDown = TmpUp + rectMinHeight;
  794. }
  795. break;
  796. case PointControlType.RightMiddle:
  797. TmpLeft = cacheRect.Left;
  798. TmpRight = centerPoint.X + moveVector.X;
  799. if (TmpLeft + rectMinWidth > TmpRight)
  800. {
  801. TmpRight = TmpLeft + rectMinWidth;
  802. }
  803. TmpUp = cacheRect.Top;
  804. TmpDown = cacheRect.Bottom;
  805. break;
  806. case PointControlType.RightTop:
  807. TmpLeft = cacheRect.Left;
  808. TmpRight = centerPoint.X + moveVector.X;
  809. if (TmpLeft + rectMinWidth > TmpRight)
  810. {
  811. TmpRight = TmpLeft + rectMinWidth;
  812. }
  813. TmpUp = centerPoint.Y + moveVector.Y;
  814. TmpDown = cacheRect.Bottom;
  815. if (TmpUp + rectMinHeight > TmpDown)
  816. {
  817. TmpUp = TmpDown - rectMinHeight;
  818. }
  819. break;
  820. case PointControlType.MiddleTop:
  821. TmpLeft = cacheRect.Left;
  822. TmpRight = cacheRect.Right;
  823. TmpUp = centerPoint.Y + moveVector.Y;
  824. TmpDown = cacheRect.Bottom;
  825. if (TmpUp + rectMinHeight > TmpDown)
  826. {
  827. TmpUp = TmpDown - rectMinHeight;
  828. }
  829. break;
  830. case PointControlType.Body:
  831. case PointControlType.Line:
  832. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), maxRect);
  833. TmpLeft = cacheRect.Left + OffsetPos.X;
  834. TmpRight = cacheRect.Right + OffsetPos.X;
  835. TmpUp = cacheRect.Top + OffsetPos.Y;
  836. TmpDown = cacheRect.Bottom + OffsetPos.Y;
  837. break;
  838. default:
  839. break;
  840. }
  841. if (TmpLeft < maxRect.Left)
  842. {
  843. TmpLeft = maxRect.Left;
  844. }
  845. if (TmpUp < maxRect.Top)
  846. {
  847. TmpUp = maxRect.Top;
  848. }
  849. if (TmpRight > maxRect.Right)
  850. {
  851. TmpRight = maxRect.Right;
  852. }
  853. if (TmpDown > maxRect.Bottom)
  854. {
  855. TmpDown = maxRect.Bottom;
  856. }
  857. if (TmpRight - TmpLeft < 0.0 || TmpDown - TmpUp < 0.0)
  858. {
  859. return false;
  860. }
  861. drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  862. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  863. return true;
  864. }
  865. /// <summary>
  866. /// Proportional scaling offset calibration
  867. /// </summary>
  868. /// <param name="movePoint">
  869. /// Offset value
  870. /// </param>
  871. /// <returns>
  872. /// Offset value after calibration
  873. /// </returns>
  874. protected Point ProportionalScalingOffsetPos(Point movePoint)
  875. {
  876. if (isProportionalScaling)
  877. {
  878. Point offsetPos = movePoint;
  879. double ratioX = cacheRect.Width > 0 ? cacheRect.Height / cacheRect.Width : 1;
  880. double ratioY = cacheRect.Height > 0 ? cacheRect.Width / cacheRect.Height : 1;
  881. switch (hitControlType)
  882. {
  883. case PointControlType.LeftTop:
  884. case PointControlType.RightBottom:
  885. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  886. break;
  887. case PointControlType.LeftBottom:
  888. case PointControlType.RightTop:
  889. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  890. break;
  891. case PointControlType.LeftMiddle:
  892. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  893. break;
  894. case PointControlType.RightMiddle:
  895. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  896. break;
  897. case PointControlType.MiddlBottom:
  898. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? 1 : -1), movePoint.Y);
  899. break;
  900. case PointControlType.MiddleTop:
  901. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? -1 : 1), movePoint.Y);
  902. break;
  903. default:
  904. break;
  905. }
  906. return offsetPos;
  907. }
  908. else
  909. {
  910. return movePoint;
  911. }
  912. }
  913. /// <summary>
  914. /// Set left alignment within the set maximum rectangle
  915. /// </summary>
  916. public virtual void SetAlignLeftForMaxRect()
  917. {
  918. DrawAlignRect(AlignmentsHelp.SetAlignLeft(drawRect, maxRect));
  919. }
  920. /// <summary>
  921. /// Set horizontal center alignment within the set maximum rectangle
  922. /// </summary>
  923. public virtual void SetAlignHorizonCenterForMaxRect()
  924. {
  925. DrawAlignRect(AlignmentsHelp.SetAlignHorizonCenter(drawRect, maxRect));
  926. }
  927. /// <summary>
  928. /// Set right alignment within the set maximum rectangle
  929. /// </summary>
  930. public virtual void SetAlignRightForMaxRect()
  931. {
  932. DrawAlignRect(AlignmentsHelp.SetAlignRight(drawRect, maxRect));
  933. }
  934. /// <summary>
  935. /// Set top alignment within the set maximum rectangle
  936. /// </summary>
  937. public virtual void SetAlignTopForMaxRect()
  938. {
  939. DrawAlignRect(AlignmentsHelp.SetAlignTop(drawRect, maxRect));
  940. }
  941. /// <summary>
  942. /// Set vertical center alignment within the set maximum rectangle
  943. /// </summary>
  944. public virtual void SetAlignVerticalCenterForMaxRect()
  945. {
  946. DrawAlignRect(AlignmentsHelp.SetAlignVerticalCenter(drawRect, maxRect));
  947. }
  948. /// <summary>
  949. /// Set Align center within the set maximum rectangle
  950. /// </summary>
  951. public virtual void SetAlignHorizonVerticalCenterForMaxRect()
  952. {
  953. DrawAlignRect(AlignmentsHelp.SetAlignHorizonVerticalCenter(drawRect, maxRect));
  954. }
  955. /// <summary>
  956. /// Set bottom alignment within the set maximum rectangle
  957. /// </summary>
  958. public virtual void SetAlignBottomForMaxRect()
  959. {
  960. DrawAlignRect(AlignmentsHelp.SetAlignBottom(drawRect, maxRect));
  961. }
  962. /// <summary>
  963. /// Draw the rectangle of the alignment function
  964. /// </summary>
  965. /// <param name="RectMovePoint">
  966. /// Move distance required for the rectangle obtained by the alignment algorithm
  967. /// </param>
  968. private void DrawAlignRect(Point RectMovePoint)
  969. {
  970. double TmpLeft, TmpRight, TmpUp, TmpDown;
  971. Point OffsetPos = CalcMoveBound(drawRect, RectMovePoint, maxRect);
  972. TmpLeft = drawRect.Left + OffsetPos.X;
  973. TmpRight = drawRect.Right + OffsetPos.X;
  974. TmpUp = drawRect.Top + OffsetPos.Y;
  975. TmpDown = drawRect.Bottom + OffsetPos.Y;
  976. setDrawRect = drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  977. Draw();
  978. }
  979. /// <summary>
  980. /// Calculate the offset of the current rectangle within the maximum rectangle range
  981. /// </summary>
  982. /// <param name="currentRect">
  983. /// The rectangle cached when pressed
  984. /// </param>
  985. /// <param name="offsetPoint">
  986. /// The offset value equivalent to when pressed
  987. /// </param>
  988. /// <param name="maxRect">
  989. /// The maximum rectangle range
  990. /// </param>
  991. /// <returns>
  992. /// Offset value after calculation
  993. /// </returns>
  994. protected Point CalcMoveBound(Rect currentRect, Point offsetPoint, Rect maxRect)
  995. {
  996. double cLeft = currentRect.Left;
  997. double cRight = currentRect.Right;
  998. double cUp = currentRect.Top;
  999. double cDown = currentRect.Bottom;
  1000. double TmpLeft = cLeft + offsetPoint.X;
  1001. double TmpRight = cRight + offsetPoint.X;
  1002. double TmpUp = cUp + offsetPoint.Y;
  1003. double TmpDown = cDown + offsetPoint.Y;
  1004. if (TmpLeft < maxRect.Left)
  1005. {
  1006. TmpRight = (cRight - cLeft) + maxRect.Left;
  1007. TmpLeft = maxRect.Left;
  1008. }
  1009. if (TmpUp < maxRect.Top)
  1010. {
  1011. TmpDown = (cDown - cUp) + maxRect.Top;
  1012. TmpUp = maxRect.Top;
  1013. }
  1014. if (TmpRight > maxRect.Right)
  1015. {
  1016. TmpLeft = maxRect.Right - (cRight - cLeft);
  1017. TmpRight = maxRect.Right;
  1018. }
  1019. if (TmpDown > maxRect.Bottom)
  1020. {
  1021. TmpUp = maxRect.Bottom - (cDown - cUp);
  1022. TmpDown = maxRect.Bottom;
  1023. }
  1024. offsetPoint = new Point(TmpLeft - cLeft, TmpUp - cUp);
  1025. return offsetPoint;
  1026. }
  1027. /// <summary>
  1028. /// Used for notification events during the drawing data process/completion.
  1029. /// </summary>
  1030. /// <param name="isFinish">
  1031. /// Identifies whether the data has been changed
  1032. /// </param>
  1033. protected void InvokeDataChangEvent(bool isFinish)
  1034. {
  1035. if (isFinish)
  1036. {
  1037. DataChanged?.Invoke(this, moveOffset);
  1038. }
  1039. else
  1040. {
  1041. DataChanging?.Invoke(this, moveOffset);
  1042. }
  1043. }
  1044. /// <summary>
  1045. /// Get the current set of ignored points
  1046. /// </summary>
  1047. /// <returns>
  1048. /// Dataset of ignored points
  1049. /// </returns>
  1050. private List<PointControlType> GetIgnorePoints()
  1051. {
  1052. List<PointControlType> IgnorePointsList = new List<PointControlType>();
  1053. foreach (PointControlType type in ignorePoints)
  1054. {
  1055. IgnorePointsList.Add(type);
  1056. }
  1057. return IgnorePointsList;
  1058. }
  1059. /// <summary>
  1060. /// Get which control point the coordinate is on
  1061. /// </summary>
  1062. /// <param name="clickPoint">
  1063. /// Coordinate point
  1064. /// </param>
  1065. /// <returns>
  1066. /// Control point type
  1067. /// </returns>
  1068. public PointControlType GetHitControlIndex(Point point)
  1069. {
  1070. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  1071. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  1072. {
  1073. List<PointControlType> ignoreList = GetIgnorePoints();
  1074. List<Point> IgnorePointsList = new List<Point>();
  1075. foreach (PointControlType type in ignoreList)
  1076. {
  1077. if ((int)type < controlPoints.Count)
  1078. {
  1079. IgnorePointsList.Add(controlPoints[(int)type]);
  1080. }
  1081. }
  1082. for (int i = 0; i < controlPoints.Count; i++)
  1083. {
  1084. Point checkPoint = controlPoints[i];
  1085. if (IgnorePointsList.Contains(checkPoint))
  1086. {
  1087. continue;
  1088. }
  1089. switch (currentDrawPointType)
  1090. {
  1091. case DrawPointType.Circle:
  1092. Vector checkVector = checkPoint - point;
  1093. if (checkVector.Length < pointSize)
  1094. {
  1095. return (PointControlType)i;
  1096. }
  1097. break;
  1098. case DrawPointType.Square:
  1099. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  1100. if (checkRect.Contains(point))
  1101. {
  1102. return (PointControlType)i;
  1103. }
  1104. break;
  1105. default:
  1106. break;
  1107. }
  1108. }
  1109. Rect defrect = drawDefaultRect;
  1110. defrect.X -= rectPadding;
  1111. defrect.Y -= rectPadding;
  1112. defrect.Width += rectPadding;
  1113. defrect.Height += rectPadding;
  1114. if (drawDefaultRect.Contains(point))
  1115. {
  1116. Rect rect = new Rect(
  1117. Math.Max(drawDefaultRect.X + rectPadding, 0),
  1118. Math.Max(drawDefaultRect.Y + rectPadding, 0),
  1119. drawDefaultRect.Width - 2 * rectPadding,
  1120. drawDefaultRect.Height - 2 * rectPadding);
  1121. if (rect.Contains(point))
  1122. {
  1123. if (!ignoreList.Contains(PointControlType.Body))
  1124. {
  1125. return PointControlType.Body;
  1126. }
  1127. }
  1128. if (!ignoreList.Contains(PointControlType.Body))
  1129. {
  1130. return PointControlType.Line;
  1131. }
  1132. }
  1133. }
  1134. return PointControlType.None;
  1135. }
  1136. }
  1137. }