SelectedRect.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. if (selectedType == SelectedType.PDFEdit)
  251. {
  252. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect, DrawParam.PDFEditMoveRectPen);
  253. }
  254. else
  255. {
  256. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect);
  257. }
  258. }
  259. break;
  260. default:
  261. break;
  262. }
  263. SolidColorBrush solidColorBrush = DrawParam.AnnotRectFillBrush;
  264. Pen pen = DrawParam.AnnotRectLinePen;
  265. GetBrushAndPen(ref solidColorBrush, ref pen);
  266. drawDC?.DrawRectangle(solidColorBrush, pen, currentRect);
  267. SolidColorBrush PointBrush = DrawParam.AnnotPointBorderBrush;
  268. Pen PointPen = DrawParam.AnnotPointPen;
  269. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  270. switch (currentDrawPointType)
  271. {
  272. case DrawPointType.Circle:
  273. if (selectedType == SelectedType.PDFEdit)
  274. {
  275. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, new SolidColorBrush(Color.FromRgb(71, 126, 222)));
  276. //DrawEditSelectionBox(drawDC, PointPen);
  277. }
  278. else
  279. {
  280. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  281. }
  282. break;
  283. case DrawPointType.Square:
  284. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  285. break;
  286. case DrawPointType.Crop:
  287. DrawCropPoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  288. break;
  289. }
  290. drawDC?.Close();
  291. drawDC = null;
  292. });
  293. }
  294. private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  295. {
  296. switch (selectedType)
  297. {
  298. case SelectedType.None:
  299. break;
  300. case SelectedType.Annot:
  301. colorBrush = DrawParam.AnnotMoveBrush;
  302. pen = DrawParam.AnnotMovePen;
  303. break;
  304. case SelectedType.PDFEdit:
  305. colorBrush = DrawParam.PDFEditMoveBrush;
  306. pen = DrawParam.PDFEditMovePen;
  307. break;
  308. default:
  309. break;
  310. }
  311. }
  312. private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  313. {
  314. switch (selectedType)
  315. {
  316. case SelectedType.None:
  317. break;
  318. case SelectedType.Annot:
  319. colorBrush = DrawParam.AnnotPointBorderBrush;
  320. pen = DrawParam.AnnotPointPen;
  321. break;
  322. case SelectedType.PDFEdit:
  323. if (isHover)
  324. {
  325. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  326. pen = DrawParam.PDFEditPointHoverPen;
  327. }
  328. else if (currentDrawPointType == DrawPointType.Crop)
  329. {
  330. colorBrush = DrawParam.SPDFEditCropBorderBrush;//new SolidColorBrush((DrawParam.SPDFEditPointPen.Brush as SolidColorBrush).Color);
  331. pen = DrawParam.SPDFEditPointPen.Clone();
  332. pen.DashStyle = DashStyles.Solid;
  333. }
  334. else
  335. {
  336. if (isSelected)
  337. {
  338. colorBrush = DrawParam.SPDFEditPointBorderBrush;
  339. pen = DrawParam.SPDFEditPointPen;
  340. }
  341. else
  342. {
  343. colorBrush = DrawParam.PDFEditPointBorderBrush;
  344. pen = DrawParam.PDFEditPointPen;
  345. }
  346. }
  347. break;
  348. default:
  349. break;
  350. }
  351. }
  352. private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  353. {
  354. switch (selectedType)
  355. {
  356. case SelectedType.None:
  357. break;
  358. case SelectedType.Annot:
  359. if (isHover)
  360. {
  361. colorBrush = DrawParam.AnnotRectFillBrush;
  362. pen = DrawParam.AnnotRectHoverPen;
  363. }
  364. else
  365. {
  366. colorBrush = DrawParam.AnnotRectFillBrush;
  367. pen = DrawParam.AnnotRectLinePen;
  368. }
  369. break;
  370. case SelectedType.PDFEdit:
  371. if (isHover)
  372. {
  373. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  374. pen = editHoverPen;//DrawParam.PDFEditRectLineHoverPen;
  375. }
  376. else
  377. {
  378. if (isSelected)
  379. {
  380. colorBrush = DrawParam.SPDFEditRectFillBrush;
  381. pen = DrawParam.SPDFEditRectLinePen;
  382. }
  383. else
  384. {
  385. colorBrush = DrawParam.PDFEditRectFillBrush;
  386. //init Color
  387. pen = editPen; //editPen;//// DrawParam.PDFEditRectLinePen;
  388. }
  389. }
  390. break;
  391. default:
  392. break;
  393. }
  394. }
  395. public void SetEditPen(Pen editPen = null,Pen editHoverPen=null)
  396. {
  397. if (editPen == null)
  398. {
  399. this.editPen = DrawParam.PDFEditRectLinePen;
  400. }
  401. else
  402. {
  403. this.editPen=new Pen(editPen.Brush,editPen.Thickness);
  404. }
  405. if (editHoverPen == null)
  406. {
  407. this.editHoverPen = DrawParam.PDFEditRectLineHoverPen;
  408. }
  409. else
  410. {
  411. this.editHoverPen = editHoverPen;
  412. }
  413. }
  414. public virtual void ClearDraw()
  415. {
  416. SetDrawRect = drawRect = new Rect();
  417. drawDC = RenderOpen();
  418. drawDC?.Close();
  419. drawDC = null;
  420. }
  421. /// <summary>
  422. /// Hide the drawing
  423. /// </summary>
  424. public virtual void HideDraw()
  425. {
  426. drawDC = RenderOpen();
  427. drawDC?.Close();
  428. }
  429. public void SetRect(Rect newRect, double zoom)
  430. {
  431. if (newRect == Rect.Empty || newRect == null)
  432. {
  433. return;
  434. }
  435. 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));
  436. currentZoom = zoom;
  437. SetDrawRect = drawRect = newRect;
  438. drawCenterPoint = new Point(drawRect.Left + drawRect.Width / 2, drawRect.Top + drawRect.Height / 2);
  439. }
  440. /// <summary>
  441. /// Get the original set Rect, not the calculated fill
  442. /// </summary>
  443. /// <param name="newRect">
  444. /// The new rect to set
  445. /// </param>
  446. public Rect GetRect()
  447. {
  448. 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));
  449. return rect;
  450. }
  451. public void SetRectPadding(double rectPadding)
  452. {
  453. this.rectPadding = rectPadding;
  454. }
  455. public double GetRectPadding()
  456. {
  457. return rectPadding;
  458. }
  459. public Rect GetDrawRect()
  460. {
  461. return drawRect;
  462. }
  463. /// <summary>
  464. /// Get ClipRect
  465. /// </summary>
  466. /// <returns></returns>
  467. public Rect GetClipRect()
  468. {
  469. return clipRect;
  470. }
  471. /// <summary>
  472. /// Set ClipRect And DrawRect
  473. /// </summary>
  474. /// <returns></returns>
  475. public void SetClipRect(Rect rect)
  476. {
  477. drawRect = rect;
  478. clipRect = rect;
  479. }
  480. public void SetMaxRect(Rect rect)
  481. {
  482. maxRect = rect;
  483. }
  484. public Rect GetMaxRect()
  485. {
  486. return maxRect;
  487. }
  488. public void SetAnnotData(AnnotData annotData)
  489. {
  490. SetIgnorePoints(new List<PointControlType>());
  491. SetIsProportionalScaling(false);
  492. isProportionalScaling = false;
  493. switch (annotData.AnnotType)
  494. {
  495. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  496. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  497. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  498. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  499. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  500. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  501. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  502. DisableAll();
  503. break;
  504. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  505. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  506. SetIgnorePointsAll();
  507. break;
  508. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  509. SetIsProportionalScaling(true);
  510. break;
  511. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  512. SetIgnorePointsAll();
  513. break;
  514. default:
  515. break;
  516. }
  517. SetMaxRect(annotData.PaintOffset);
  518. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  519. selectedRectData = new SelectedAnnotData();
  520. selectedRectData.annotData = annotData;
  521. }
  522. public void SetIsProportionalScaling(bool isProportionalScaling)
  523. {
  524. this.isProportionalScaling = isProportionalScaling;
  525. ignorePoints.Clear();
  526. if (isProportionalScaling)
  527. {
  528. ignorePoints.Add(PointControlType.LeftMiddle);
  529. ignorePoints.Add(PointControlType.MiddlBottom);
  530. ignorePoints.Add(PointControlType.RightMiddle);
  531. ignorePoints.Add(PointControlType.MiddleTop);
  532. ignorePoints.Add(PointControlType.Rotate);
  533. }
  534. }
  535. public void SetDrawType(DrawPointType drawType)
  536. {
  537. currentDrawPointType = drawType;
  538. }
  539. public void SetDrawMoveType(DrawMoveType drawType)
  540. {
  541. currentDrawMoveType = drawType;
  542. }
  543. /// <summary>
  544. /// Set the types that need to be ignored
  545. /// </summary>
  546. /// <param name="types">
  547. /// The collection of point types that need to be ignored
  548. /// </param>
  549. public void SetIgnorePoints(List<PointControlType> types)
  550. {
  551. ignorePoints.Clear();
  552. foreach (PointControlType type in types)
  553. {
  554. ignorePoints.Add(type);
  555. }
  556. }
  557. /// <summary>
  558. /// Set Edit that need to be ignored
  559. /// </summary>
  560. /// <param name="types">
  561. /// The collection of point types that need to be ignored
  562. /// </param>
  563. public void SetEditIgnorePoints(List<PointControlType> ignoreTextPoints, List<PointControlType> ignoreImagePoints, DrawPointType drawEditPointType, bool IsText = true)
  564. {
  565. SetCurrentDrawPointType(drawEditPointType);
  566. if (IsText)
  567. {
  568. ignorePoints.Clear();
  569. foreach (PointControlType type in ignoreTextPoints)
  570. {
  571. ignorePoints.Add(type);
  572. }
  573. }
  574. else
  575. {
  576. ignorePoints.Clear();
  577. foreach (PointControlType type in ignoreImagePoints)
  578. {
  579. ignorePoints.Add(type);
  580. }
  581. }
  582. }
  583. /// <summary>
  584. /// Ignore all points
  585. /// </summary>
  586. public void SetIgnorePointsAll()
  587. {
  588. ignorePoints.Clear();
  589. ignorePoints.Add(PointControlType.LeftTop);
  590. ignorePoints.Add(PointControlType.LeftMiddle);
  591. ignorePoints.Add(PointControlType.LeftBottom);
  592. ignorePoints.Add(PointControlType.MiddlBottom);
  593. ignorePoints.Add(PointControlType.RightBottom);
  594. ignorePoints.Add(PointControlType.RightMiddle);
  595. ignorePoints.Add(PointControlType.RightTop);
  596. ignorePoints.Add(PointControlType.MiddleTop);
  597. }
  598. /// <summary>
  599. /// Disable all functions
  600. /// </summary>
  601. public void DisableAll()
  602. {
  603. ignorePoints.Clear();
  604. ignorePoints.Add(PointControlType.LeftTop);
  605. ignorePoints.Add(PointControlType.LeftMiddle);
  606. ignorePoints.Add(PointControlType.LeftBottom);
  607. ignorePoints.Add(PointControlType.MiddlBottom);
  608. ignorePoints.Add(PointControlType.RightBottom);
  609. ignorePoints.Add(PointControlType.RightMiddle);
  610. ignorePoints.Add(PointControlType.RightTop);
  611. ignorePoints.Add(PointControlType.MiddleTop);
  612. ignorePoints.Add(PointControlType.Rotate);
  613. ignorePoints.Add(PointControlType.Body);
  614. ignorePoints.Add(PointControlType.Line);
  615. }
  616. /// <summary>
  617. /// Set the left alignment in the set maximum rectangle
  618. /// </summary>
  619. public virtual void SetAlignLeftForMaxRect()
  620. {
  621. DrawAlignRect(AlignmentsHelp.SetAlignLeft(drawRect, maxRect));
  622. }
  623. /// <summary>
  624. /// Set horizontal center alignment in the set maximum rectangle
  625. /// </summary>
  626. public virtual void SetAlignHorizonCenterForMaxRect()
  627. {
  628. DrawAlignRect(AlignmentsHelp.SetAlignHorizonCenter(drawRect, maxRect));
  629. }
  630. /// <summary>
  631. /// Set horizontal right alignment in the set maximum rectangle
  632. /// </summary>
  633. public virtual void SetAlignRightForMaxRect()
  634. {
  635. DrawAlignRect(AlignmentsHelp.SetAlignRight(drawRect, maxRect));
  636. }
  637. /// <summary>
  638. /// Set the top alignment in the set maximum rectangle
  639. /// </summary>
  640. public virtual void SetAlignTopForMaxRect()
  641. {
  642. DrawAlignRect(AlignmentsHelp.SetAlignTop(drawRect, maxRect));
  643. }
  644. /// <summary>
  645. /// Set vertical center alignment in the set maximum rectangle
  646. /// </summary>
  647. public virtual void SetAlignVerticalCenterForMaxRect()
  648. {
  649. DrawAlignRect(AlignmentsHelp.SetAlignVerticalCenter(drawRect, maxRect));
  650. }
  651. /// <summary>
  652. /// Set vertical center alignment in the set maximum rectangle
  653. /// </summary>
  654. public virtual void SetAlignHorizonVerticalCenterForMaxRect()
  655. {
  656. DrawAlignRect(AlignmentsHelp.SetAlignHorizonVerticalCenter(drawRect, maxRect));
  657. }
  658. /// <summary>
  659. /// Set the bottom alignment in the set maximum rectangle
  660. /// </summary>
  661. public virtual void SetAlignBottomForMaxRect()
  662. {
  663. DrawAlignRect(AlignmentsHelp.SetAlignBottom(drawRect, maxRect));
  664. }
  665. /// <summary>
  666. /// Get which control point the coordinate is on
  667. /// </summary>
  668. /// <param name="clickPoint">
  669. /// The point to check
  670. /// </param>
  671. /// <returns>
  672. /// The control point type
  673. /// </returns>
  674. public PointControlType GetHitControlIndex(Point point, bool isIgnore = true)
  675. {
  676. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  677. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  678. {
  679. List<PointControlType> ignoreList = GetIgnorePoints();
  680. List<Point> IgnorePointsList = new List<Point>();
  681. foreach (PointControlType type in ignoreList)
  682. {
  683. if ((int)type < controlPoints.Count)
  684. {
  685. IgnorePointsList.Add(controlPoints[(int)type]);
  686. }
  687. }
  688. for (int i = 0; i < controlPoints.Count; i++)
  689. {
  690. Point checkPoint = controlPoints[i];
  691. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  692. {
  693. continue;
  694. }
  695. switch (currentDrawPointType)
  696. {
  697. case DrawPointType.Circle:
  698. Vector checkVector = checkPoint - point;
  699. if (checkVector.Length < pointSize)
  700. {
  701. return (PointControlType)i;
  702. }
  703. break;
  704. case DrawPointType.Square:
  705. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  706. if (checkRect.Contains(point))
  707. {
  708. return (PointControlType)i;
  709. }
  710. break;
  711. case DrawPointType.Crop:
  712. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  713. if (cropRect.Contains(point))
  714. {
  715. return (PointControlType)i;
  716. }
  717. break;
  718. default:
  719. break;
  720. }
  721. }
  722. if (drawRect.Contains(point))
  723. {
  724. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  725. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  726. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  727. if (rect.Contains(point))
  728. {
  729. if (!ignoreList.Contains(PointControlType.Body))
  730. {
  731. return PointControlType.Body;
  732. }
  733. }
  734. if (!ignoreList.Contains(PointControlType.Body))
  735. {
  736. return PointControlType.Line;
  737. }
  738. }
  739. }
  740. return PointControlType.None;
  741. }
  742. /// <summary>
  743. /// The position of the points in the cropping box
  744. /// </summary>
  745. /// <param name="point"></param>
  746. /// <param name="isIgnore"></param>
  747. /// <returns></returns>
  748. public PointControlType GetHitCropControlIndex(Point point, bool isIgnore = true)
  749. {
  750. List<Point> controlCurrentPoints = GetControlPoint(drawRect);
  751. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  752. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  753. {
  754. List<PointControlType> ignoreList = GetIgnorePoints();
  755. List<Point> IgnorePointsList = new List<Point>();
  756. foreach (PointControlType type in ignoreList)
  757. {
  758. if ((int)type < controlCurrentPoints.Count)
  759. {
  760. IgnorePointsList.Add(controlCurrentPoints[(int)type]);
  761. }
  762. }
  763. for (int i = 0; i < controlCurrentPoints.Count; i++)
  764. {
  765. Point checkPoint = controlCurrentPoints[i];
  766. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  767. {
  768. continue;
  769. }
  770. switch (currentDrawPointType)
  771. {
  772. case DrawPointType.Circle:
  773. Vector checkVector = checkPoint - point;
  774. if (checkVector.Length < pointSize)
  775. {
  776. return (PointControlType)i;
  777. }
  778. break;
  779. case DrawPointType.Square:
  780. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  781. if (checkRect.Contains(point))
  782. {
  783. return (PointControlType)i;
  784. }
  785. break;
  786. case DrawPointType.Crop:
  787. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  788. if (cropRect.Contains(point))
  789. {
  790. return (PointControlType)i;
  791. }
  792. break;
  793. default:
  794. break;
  795. }
  796. }
  797. if (drawRect.Contains(point))
  798. {
  799. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  800. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  801. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  802. if (rect.Contains(point))
  803. {
  804. if (!ignoreList.Contains(PointControlType.Body))
  805. {
  806. return PointControlType.Body;
  807. }
  808. }
  809. if (!ignoreList.Contains(PointControlType.Body))
  810. {
  811. return PointControlType.Line;
  812. }
  813. }
  814. }
  815. return PointControlType.None;
  816. }
  817. }
  818. }