SelectedRect.cs 32 KB

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