SelectedRect.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.Tool.SettingParam;
  3. using ComPDFKit.Viewer.Layer;
  4. using ComPDFKitViewer;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Diagnostics;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Controls.Primitives;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Media3D;
  19. using System.Xml.Linq;
  20. using static System.Net.Mime.MediaTypeNames;
  21. namespace ComPDFKit.Tool.DrawTool
  22. {
  23. public enum PointControlType
  24. {
  25. None = -1,
  26. LeftTop,
  27. LeftMiddle,
  28. LeftBottom,
  29. MiddlBottom,
  30. RightBottom,
  31. RightMiddle,
  32. RightTop,
  33. MiddleTop,
  34. Rotate,
  35. Body,
  36. Line
  37. }
  38. public enum SelectedType
  39. {
  40. None = -1,
  41. Annot,
  42. PDFEdit
  43. }
  44. public enum DrawPointType
  45. {
  46. Circle,
  47. Square,
  48. Crop
  49. }
  50. public enum DrawMoveType
  51. {
  52. kDefault,
  53. kReferenceLine,
  54. }
  55. public class SelectedAnnotData
  56. {
  57. /// <summary>
  58. /// Current size of the rectangle
  59. /// </summary>
  60. public Rect Square { get; set; }
  61. /// <summary>
  62. /// Current points of the rectangle
  63. /// </summary>
  64. public PointCollection Points { get; set; }
  65. public AnnotData annotData { get; set; }
  66. }
  67. public partial class SelectedRect : DrawingVisual
  68. {
  69. /// <summary>
  70. /// Re-layout child elements
  71. /// </summary>
  72. public void Arrange()
  73. {
  74. foreach (Visual child in Children)
  75. {
  76. if (!(child is UIElement))
  77. {
  78. continue;
  79. }
  80. UIElement checkChild = child as UIElement;
  81. try
  82. {
  83. double left = Canvas.GetLeft(checkChild);
  84. double top = Canvas.GetTop(checkChild);
  85. double width = (double)checkChild.GetValue(FrameworkElement.WidthProperty);
  86. double height = (double)checkChild.GetValue(FrameworkElement.HeightProperty);
  87. checkChild.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  88. checkChild.Arrange(new Rect(
  89. double.IsNaN(left) ? 0 : left,
  90. double.IsNaN(top) ? 0 : top,
  91. double.IsNaN(width) ? checkChild.DesiredSize.Width : width,
  92. double.IsNaN(height) ? checkChild.DesiredSize.Height : height));
  93. }
  94. catch (Exception ex)
  95. {
  96. }
  97. }
  98. }
  99. protected DefaultDrawParam DrawParam = new DefaultDrawParam();
  100. protected DrawingContext drawDC { get; set; }
  101. /// <summary>
  102. /// Data changing event
  103. /// </summary>
  104. public event EventHandler<SelectedAnnotData> DataChanging;
  105. /// <summary>
  106. /// Data changed event
  107. /// </summary>
  108. public event EventHandler<SelectedAnnotData> DataChanged;
  109. protected bool isHover = false;
  110. protected bool isSelected = false;
  111. protected SelectedType selectedType = SelectedType.None;
  112. public SelectedType GetSelectedType()
  113. {
  114. return selectedType;
  115. }
  116. public void SetIsHover(bool hover)
  117. {
  118. isHover = hover;
  119. }
  120. public bool GetIsHover()
  121. {
  122. return isHover;
  123. }
  124. public void SetIsSelected(bool selected)
  125. {
  126. isSelected = selected;
  127. }
  128. public bool GetIsSelected()
  129. {
  130. return isSelected;
  131. }
  132. public void SetCurrentDrawPointType(DrawPointType type)
  133. {
  134. currentDrawPointType = type;
  135. }
  136. public DrawPointType GetCurrentDrawPointType()
  137. {
  138. return currentDrawPointType;
  139. }
  140. public virtual void OnMouseLeftButtonDown(Point downPoint)
  141. {
  142. isMouseDown = true;
  143. hitControlType = PointControlType.None;
  144. mouseDownPoint = downPoint;
  145. moveOffset = new Point(0, 0);
  146. HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
  147. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  148. {
  149. //Crop judgment point
  150. if (currentDrawPointType == DrawPointType.Crop)
  151. {
  152. hitControlType = GetHitCropControlIndex(downPoint);
  153. }
  154. else
  155. {
  156. hitControlType = GetHitControlIndex(downPoint);
  157. }
  158. if (hitControlType != PointControlType.None)
  159. {
  160. cacheRect = drawRect;
  161. }
  162. }
  163. }
  164. public virtual void OnMouseLeftButtonUp(Point upPoint)
  165. {
  166. if (isMouseDown && hitControlType != PointControlType.None)
  167. {
  168. isMouseDown = false;
  169. cacheRect = SetDrawRect = drawRect;
  170. Draw();
  171. if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
  172. {
  173. InvokeDataChangEvent(true);
  174. }
  175. }
  176. moveOffset = new Point(0, 0);
  177. }
  178. public virtual void OnMouseMove(Point mousePoint, out bool Tag, double width, double height)
  179. {
  180. PDFViewerActualWidth = width;
  181. PDFViewerActualHeight = height;
  182. Tag = false;
  183. if (isMouseDown && hitControlType != PointControlType.None)
  184. {
  185. Tag = isMouseDown;
  186. if (CalcHitPointMove(mousePoint))
  187. {
  188. Draw();
  189. if ((int)mousePoint.X != (int)mouseDownPoint.X || (int)mousePoint.Y != (int)mouseDownPoint.Y)
  190. {
  191. InvokeDataChangEvent(false);
  192. }
  193. }
  194. }
  195. }
  196. public Cursor GetCursor(Point downPoint, Cursor cursor)
  197. {
  198. if (isMouseDown)
  199. {
  200. return cursor;
  201. }
  202. hitControlType = GetHitControlIndex(downPoint);
  203. switch (hitControlType)
  204. {
  205. case PointControlType.LeftTop:
  206. case PointControlType.RightBottom:
  207. return Cursors.SizeNWSE;
  208. case PointControlType.LeftMiddle:
  209. case PointControlType.RightMiddle:
  210. return Cursors.SizeWE;
  211. case PointControlType.LeftBottom:
  212. case PointControlType.RightTop:
  213. return Cursors.SizeNESW;
  214. case PointControlType.MiddlBottom:
  215. case PointControlType.MiddleTop:
  216. return Cursors.SizeNS;
  217. case PointControlType.Body:
  218. return Cursors.Arrow;
  219. case PointControlType.Line:
  220. return Cursors.SizeAll;
  221. default:
  222. return Cursors.Arrow;
  223. }
  224. }
  225. public SelectedRect(DefaultDrawParam defaultDrawParam, SelectedType type) : base()
  226. {
  227. DrawParam = defaultDrawParam;
  228. currentDrawPointType = DrawPointType.Square;
  229. selectedType = type;
  230. }
  231. public void Draw()
  232. {
  233. Dispatcher.Invoke(() =>
  234. {
  235. Rect currentRect = SetDrawRect;
  236. drawDC = RenderOpen();
  237. switch (currentDrawMoveType)
  238. {
  239. case DrawMoveType.kDefault:
  240. currentRect = drawRect;
  241. CalcControlPoint(currentRect);
  242. break;
  243. case DrawMoveType.kReferenceLine:
  244. CalcControlPoint(currentRect);
  245. if (isMouseDown == true)
  246. {
  247. SolidColorBrush moveBrush = DrawParam.AnnotMoveBrush;
  248. Pen movepen = DrawParam.AnnotMovePen;
  249. GetMoveBrushAndPen(ref moveBrush, ref movepen);
  250. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect);
  251. }
  252. break;
  253. default:
  254. break;
  255. }
  256. SolidColorBrush solidColorBrush = DrawParam.AnnotRectFillBrush;
  257. Pen pen = DrawParam.AnnotRectLinePen;
  258. GetBrushAndPen(ref solidColorBrush, ref pen);
  259. drawDC?.DrawRectangle(solidColorBrush, pen, currentRect);
  260. SolidColorBrush PointBrush = DrawParam.AnnotPointBorderBrush;
  261. Pen PointPen = DrawParam.AnnotPointPen;
  262. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  263. switch (currentDrawPointType)
  264. {
  265. case DrawPointType.Circle:
  266. if (selectedType == SelectedType.PDFEdit)
  267. {
  268. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, new SolidColorBrush(Color.FromRgb(71, 126, 222)));
  269. //DrawEditSelectionBox(drawDC, PointPen);
  270. }
  271. else
  272. {
  273. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  274. }
  275. break;
  276. case DrawPointType.Square:
  277. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  278. break;
  279. case DrawPointType.Crop:
  280. DrawCropPoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  281. break;
  282. }
  283. drawDC?.Close();
  284. drawDC = null;
  285. });
  286. }
  287. /// <summary>
  288. /// Edit Selection Box,The timing logic needs to obtain the current page size in the future
  289. /// </summary>
  290. /// <param name="drawingContext"></param>
  291. /// <param name="PointPen"></param>
  292. private void DrawEditSelectionBox(DrawingContext drawingContext, Pen PointPen)
  293. {
  294. drawingContext?.DrawLine(PointPen, new Point(0, SetDrawRect.Top), new Point(SetDrawRect.Left, SetDrawRect.Top));
  295. drawingContext?.DrawLine(PointPen, new Point(0, SetDrawRect.Bottom), new Point(SetDrawRect.Left, SetDrawRect.Bottom));
  296. drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Left, 0), new Point(SetDrawRect.Left, SetDrawRect.Top));
  297. drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Right, 0), new Point(SetDrawRect.Right, SetDrawRect.Top));
  298. drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Left, SetDrawRect.Bottom), new Point(SetDrawRect.Left, SetDrawRect.Bottom * 3));
  299. drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Right, SetDrawRect.Bottom), new Point(SetDrawRect.Right, SetDrawRect.Bottom * 3));
  300. drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Right, SetDrawRect.Bottom), new Point(SetDrawRect.Right * 3, SetDrawRect.Bottom));
  301. drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Right, SetDrawRect.Top), new Point(SetDrawRect.Right * 3, SetDrawRect.Top));
  302. }
  303. private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  304. {
  305. switch (selectedType)
  306. {
  307. case SelectedType.None:
  308. break;
  309. case SelectedType.Annot:
  310. colorBrush = DrawParam.AnnotMoveBrush;
  311. pen = DrawParam.AnnotMovePen;
  312. break;
  313. case SelectedType.PDFEdit:
  314. colorBrush = DrawParam.PDFEditMoveBrush;
  315. pen = DrawParam.PDFEditMovePen;
  316. break;
  317. default:
  318. break;
  319. }
  320. }
  321. private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  322. {
  323. switch (selectedType)
  324. {
  325. case SelectedType.None:
  326. break;
  327. case SelectedType.Annot:
  328. colorBrush = DrawParam.AnnotPointBorderBrush;
  329. pen = DrawParam.AnnotPointPen;
  330. break;
  331. case SelectedType.PDFEdit:
  332. if (isHover)
  333. {
  334. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  335. pen = DrawParam.PDFEditPointHoverPen;
  336. }
  337. else if (currentDrawPointType == DrawPointType.Crop)
  338. {
  339. colorBrush = DrawParam.SPDFEditCropBorderBrush;//new SolidColorBrush((DrawParam.SPDFEditPointPen.Brush as SolidColorBrush).Color);
  340. pen = DrawParam.SPDFEditPointPen.Clone();
  341. pen.DashStyle = DashStyles.Solid;
  342. }
  343. else
  344. {
  345. if (isSelected)
  346. {
  347. colorBrush = DrawParam.SPDFEditPointBorderBrush;
  348. pen = DrawParam.SPDFEditPointPen;
  349. }
  350. else
  351. {
  352. colorBrush = DrawParam.PDFEditPointBorderBrush;
  353. pen = DrawParam.PDFEditPointPen;
  354. }
  355. }
  356. break;
  357. default:
  358. break;
  359. }
  360. }
  361. private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  362. {
  363. switch (selectedType)
  364. {
  365. case SelectedType.None:
  366. break;
  367. case SelectedType.Annot:
  368. if (isHover)
  369. {
  370. colorBrush = DrawParam.AnnotRectFillBrush;
  371. pen = DrawParam.AnnotRectHoverPen;
  372. }
  373. else
  374. {
  375. colorBrush = DrawParam.AnnotRectFillBrush;
  376. pen = DrawParam.AnnotRectLinePen;
  377. }
  378. break;
  379. case SelectedType.PDFEdit:
  380. if (isHover)
  381. {
  382. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  383. pen = DrawParam.PDFEditRectLineHoverPen;
  384. }
  385. else
  386. {
  387. if (isSelected)
  388. {
  389. colorBrush = DrawParam.SPDFEditRectFillBrush;
  390. pen = DrawParam.SPDFEditRectLinePen;
  391. }
  392. else
  393. {
  394. colorBrush = DrawParam.PDFEditRectFillBrush;
  395. //init Color
  396. pen = editPen; //editPen;//// DrawParam.PDFEditRectLinePen;
  397. }
  398. }
  399. break;
  400. default:
  401. break;
  402. }
  403. }
  404. public void SetEditPen(Pen editPen = null)
  405. {
  406. if (editPen == null)
  407. {
  408. this.editPen = DrawParam.PDFEditRectLinePen;
  409. }
  410. else
  411. {
  412. this.editPen=new Pen(editPen.Brush,editPen.Thickness);
  413. }
  414. }
  415. public virtual void ClearDraw()
  416. {
  417. SetDrawRect = drawRect = new Rect();
  418. drawDC = RenderOpen();
  419. drawDC?.Close();
  420. drawDC = null;
  421. }
  422. /// <summary>
  423. /// Hide the drawing
  424. /// </summary>
  425. public virtual void HideDraw()
  426. {
  427. drawDC = RenderOpen();
  428. drawDC?.Close();
  429. }
  430. public void SetRect(Rect newRect, double zoom)
  431. {
  432. if (newRect == Rect.Empty || newRect == null)
  433. {
  434. return;
  435. }
  436. newRect = new Rect((int)(newRect.X - rectPadding * zoom), (int)(newRect.Y - rectPadding * zoom), (int)(newRect.Width + 2 * rectPadding * zoom), (int)(newRect.Height + 2 * rectPadding * zoom));
  437. currentZoom = zoom;
  438. SetDrawRect = drawRect = newRect;
  439. drawCenterPoint = new Point(drawRect.Left + drawRect.Width / 2, drawRect.Top + drawRect.Height / 2);
  440. }
  441. /// <summary>
  442. /// Get the original set Rect, not the calculated fill
  443. /// </summary>
  444. /// <param name="newRect">
  445. /// The new rect to set
  446. /// </param>
  447. public Rect GetRect()
  448. {
  449. Rect rect = new Rect(drawRect.X + rectPadding * currentZoom, drawRect.Y + rectPadding * currentZoom, Math.Max(rectMinWidth, drawRect.Width - 2 * rectPadding * currentZoom), Math.Max(RectMinHeight, drawRect.Height - 2 * rectPadding * currentZoom));
  450. return rect;
  451. }
  452. public void SetRectPadding(double rectPadding)
  453. {
  454. this.rectPadding = rectPadding;
  455. }
  456. public double GetRectPadding()
  457. {
  458. return rectPadding;
  459. }
  460. public Rect GetDrawRect()
  461. {
  462. return drawRect;
  463. }
  464. /// <summary>
  465. /// Get ClipRect
  466. /// </summary>
  467. /// <returns></returns>
  468. public Rect GetClipRect()
  469. {
  470. return clipRect;
  471. }
  472. /// <summary>
  473. /// Set ClipRect And DrawRect
  474. /// </summary>
  475. /// <returns></returns>
  476. public void SetClipRect(Rect rect)
  477. {
  478. drawRect = rect;
  479. clipRect = rect;
  480. }
  481. public void SetMaxRect(Rect rect)
  482. {
  483. maxRect = rect;
  484. }
  485. public Rect GetMaxRect()
  486. {
  487. return maxRect;
  488. }
  489. public void SetAnnotData(AnnotData annotData)
  490. {
  491. SetIgnorePoints(new List<PointControlType>());
  492. SetIsProportionalScaling(false);
  493. isProportionalScaling = false;
  494. switch (annotData.AnnotType)
  495. {
  496. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  497. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  498. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  499. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  500. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  501. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  502. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  503. DisableAll();
  504. break;
  505. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  506. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  507. SetIgnorePointsAll();
  508. break;
  509. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  510. SetIsProportionalScaling(true);
  511. break;
  512. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  513. SetIgnorePointsAll();
  514. break;
  515. default:
  516. break;
  517. }
  518. SetMaxRect(annotData.PaintOffset);
  519. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  520. selectedRectData = new SelectedAnnotData();
  521. selectedRectData.annotData = annotData;
  522. }
  523. public void SetIsProportionalScaling(bool isProportionalScaling)
  524. {
  525. this.isProportionalScaling = isProportionalScaling;
  526. ignorePoints.Clear();
  527. if (isProportionalScaling)
  528. {
  529. ignorePoints.Add(PointControlType.LeftMiddle);
  530. ignorePoints.Add(PointControlType.MiddlBottom);
  531. ignorePoints.Add(PointControlType.RightMiddle);
  532. ignorePoints.Add(PointControlType.MiddleTop);
  533. ignorePoints.Add(PointControlType.Rotate);
  534. }
  535. }
  536. public void SetDrawType(DrawPointType drawType)
  537. {
  538. currentDrawPointType = drawType;
  539. }
  540. public void SetDrawMoveType(DrawMoveType drawType)
  541. {
  542. currentDrawMoveType = drawType;
  543. }
  544. /// <summary>
  545. /// Set the types that need to be ignored
  546. /// </summary>
  547. /// <param name="types">
  548. /// The collection of point types that need to be ignored
  549. /// </param>
  550. public void SetIgnorePoints(List<PointControlType> types)
  551. {
  552. ignorePoints.Clear();
  553. foreach (PointControlType type in types)
  554. {
  555. ignorePoints.Add(type);
  556. }
  557. }
  558. /// <summary>
  559. /// Set Edit that need to be ignored
  560. /// </summary>
  561. /// <param name="types">
  562. /// The collection of point types that need to be ignored
  563. /// </param>
  564. public void SetEditIgnorePoints(List<PointControlType> ignoreTextPoints, List<PointControlType> ignoreImagePoints, DrawPointType drawEditPointType, bool IsText = true)
  565. {
  566. SetCurrentDrawPointType(drawEditPointType);
  567. if (IsText)
  568. {
  569. ignorePoints.Clear();
  570. foreach (PointControlType type in ignoreTextPoints)
  571. {
  572. ignorePoints.Add(type);
  573. }
  574. }
  575. else
  576. {
  577. ignorePoints.Clear();
  578. foreach (PointControlType type in ignoreImagePoints)
  579. {
  580. ignorePoints.Add(type);
  581. }
  582. }
  583. }
  584. /// <summary>
  585. /// Ignore all points
  586. /// </summary>
  587. public void SetIgnorePointsAll()
  588. {
  589. ignorePoints.Clear();
  590. ignorePoints.Add(PointControlType.LeftTop);
  591. ignorePoints.Add(PointControlType.LeftMiddle);
  592. ignorePoints.Add(PointControlType.LeftBottom);
  593. ignorePoints.Add(PointControlType.MiddlBottom);
  594. ignorePoints.Add(PointControlType.RightBottom);
  595. ignorePoints.Add(PointControlType.RightMiddle);
  596. ignorePoints.Add(PointControlType.RightTop);
  597. ignorePoints.Add(PointControlType.MiddleTop);
  598. }
  599. /// <summary>
  600. /// Disable all functions
  601. /// </summary>
  602. public void DisableAll()
  603. {
  604. ignorePoints.Clear();
  605. ignorePoints.Add(PointControlType.LeftTop);
  606. ignorePoints.Add(PointControlType.LeftMiddle);
  607. ignorePoints.Add(PointControlType.LeftBottom);
  608. ignorePoints.Add(PointControlType.MiddlBottom);
  609. ignorePoints.Add(PointControlType.RightBottom);
  610. ignorePoints.Add(PointControlType.RightMiddle);
  611. ignorePoints.Add(PointControlType.RightTop);
  612. ignorePoints.Add(PointControlType.MiddleTop);
  613. ignorePoints.Add(PointControlType.Rotate);
  614. ignorePoints.Add(PointControlType.Body);
  615. ignorePoints.Add(PointControlType.Line);
  616. }
  617. /// <summary>
  618. /// Set the left alignment in the set maximum rectangle
  619. /// </summary>
  620. public virtual void SetAlignLeftForMaxRect()
  621. {
  622. DrawAlignRect(AlignmentsHelp.SetAlignLeft(drawRect, maxRect));
  623. }
  624. /// <summary>
  625. /// Set horizontal center alignment in the set maximum rectangle
  626. /// </summary>
  627. public virtual void SetAlignHorizonCenterForMaxRect()
  628. {
  629. DrawAlignRect(AlignmentsHelp.SetAlignHorizonCenter(drawRect, maxRect));
  630. }
  631. /// <summary>
  632. /// Set horizontal right alignment in the set maximum rectangle
  633. /// </summary>
  634. public virtual void SetAlignRightForMaxRect()
  635. {
  636. DrawAlignRect(AlignmentsHelp.SetAlignRight(drawRect, maxRect));
  637. }
  638. /// <summary>
  639. /// Set the top alignment in the set maximum rectangle
  640. /// </summary>
  641. public virtual void SetAlignTopForMaxRect()
  642. {
  643. DrawAlignRect(AlignmentsHelp.SetAlignTop(drawRect, maxRect));
  644. }
  645. /// <summary>
  646. /// Set vertical center alignment in the set maximum rectangle
  647. /// </summary>
  648. public virtual void SetAlignVerticalCenterForMaxRect()
  649. {
  650. DrawAlignRect(AlignmentsHelp.SetAlignVerticalCenter(drawRect, maxRect));
  651. }
  652. /// <summary>
  653. /// Set vertical center alignment in the set maximum rectangle
  654. /// </summary>
  655. public virtual void SetAlignHorizonVerticalCenterForMaxRect()
  656. {
  657. DrawAlignRect(AlignmentsHelp.SetAlignHorizonVerticalCenter(drawRect, maxRect));
  658. }
  659. /// <summary>
  660. /// Set the bottom alignment in the set maximum rectangle
  661. /// </summary>
  662. public virtual void SetAlignBottomForMaxRect()
  663. {
  664. DrawAlignRect(AlignmentsHelp.SetAlignBottom(drawRect, maxRect));
  665. }
  666. /// <summary>
  667. /// Get which control point the coordinate is on
  668. /// </summary>
  669. /// <param name="clickPoint">
  670. /// The point to check
  671. /// </param>
  672. /// <returns>
  673. /// The control point type
  674. /// </returns>
  675. public PointControlType GetHitControlIndex(Point point, bool isIgnore = true)
  676. {
  677. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  678. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  679. {
  680. List<PointControlType> ignoreList = GetIgnorePoints();
  681. List<Point> IgnorePointsList = new List<Point>();
  682. foreach (PointControlType type in ignoreList)
  683. {
  684. if ((int)type < controlPoints.Count)
  685. {
  686. IgnorePointsList.Add(controlPoints[(int)type]);
  687. }
  688. }
  689. for (int i = 0; i < controlPoints.Count; i++)
  690. {
  691. Point checkPoint = controlPoints[i];
  692. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  693. {
  694. continue;
  695. }
  696. switch (currentDrawPointType)
  697. {
  698. case DrawPointType.Circle:
  699. Vector checkVector = checkPoint - point;
  700. if (checkVector.Length < pointSize)
  701. {
  702. return (PointControlType)i;
  703. }
  704. break;
  705. case DrawPointType.Square:
  706. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  707. if (checkRect.Contains(point))
  708. {
  709. return (PointControlType)i;
  710. }
  711. break;
  712. case DrawPointType.Crop:
  713. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  714. if (cropRect.Contains(point))
  715. {
  716. return (PointControlType)i;
  717. }
  718. break;
  719. default:
  720. break;
  721. }
  722. }
  723. if (drawRect.Contains(point))
  724. {
  725. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  726. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  727. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  728. if (rect.Contains(point))
  729. {
  730. if (!ignoreList.Contains(PointControlType.Body))
  731. {
  732. return PointControlType.Body;
  733. }
  734. }
  735. if (!ignoreList.Contains(PointControlType.Body))
  736. {
  737. return PointControlType.Line;
  738. }
  739. }
  740. }
  741. return PointControlType.None;
  742. }
  743. /// <summary>
  744. /// The position of the points in the cropping box
  745. /// </summary>
  746. /// <param name="point"></param>
  747. /// <param name="isIgnore"></param>
  748. /// <returns></returns>
  749. public PointControlType GetHitCropControlIndex(Point point, bool isIgnore = true)
  750. {
  751. List<Point> controlCurrentPoints = GetControlPoint(drawRect);
  752. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  753. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  754. {
  755. List<PointControlType> ignoreList = GetIgnorePoints();
  756. List<Point> IgnorePointsList = new List<Point>();
  757. foreach (PointControlType type in ignoreList)
  758. {
  759. if ((int)type < controlCurrentPoints.Count)
  760. {
  761. IgnorePointsList.Add(controlCurrentPoints[(int)type]);
  762. }
  763. }
  764. for (int i = 0; i < controlCurrentPoints.Count; i++)
  765. {
  766. Point checkPoint = controlCurrentPoints[i];
  767. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  768. {
  769. continue;
  770. }
  771. switch (currentDrawPointType)
  772. {
  773. case DrawPointType.Circle:
  774. Vector checkVector = checkPoint - point;
  775. if (checkVector.Length < pointSize)
  776. {
  777. return (PointControlType)i;
  778. }
  779. break;
  780. case DrawPointType.Square:
  781. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  782. if (checkRect.Contains(point))
  783. {
  784. return (PointControlType)i;
  785. }
  786. break;
  787. case DrawPointType.Crop:
  788. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  789. if (cropRect.Contains(point))
  790. {
  791. return (PointControlType)i;
  792. }
  793. break;
  794. default:
  795. break;
  796. }
  797. }
  798. if (drawRect.Contains(point))
  799. {
  800. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  801. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  802. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  803. if (rect.Contains(point))
  804. {
  805. if (!ignoreList.Contains(PointControlType.Body))
  806. {
  807. return PointControlType.Body;
  808. }
  809. }
  810. if (!ignoreList.Contains(PointControlType.Body))
  811. {
  812. return PointControlType.Line;
  813. }
  814. }
  815. }
  816. return PointControlType.None;
  817. }
  818. }
  819. }