SelectedRect.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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 = editHoverPen;//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,Pen editHoverPen=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. if (editHoverPen == null)
  415. {
  416. this.editHoverPen = DrawParam.PDFEditRectLineHoverPen;
  417. }
  418. else
  419. {
  420. this.editHoverPen = editHoverPen;
  421. }
  422. }
  423. public virtual void ClearDraw()
  424. {
  425. SetDrawRect = drawRect = new Rect();
  426. drawDC = RenderOpen();
  427. drawDC?.Close();
  428. drawDC = null;
  429. }
  430. /// <summary>
  431. /// Hide the drawing
  432. /// </summary>
  433. public virtual void HideDraw()
  434. {
  435. drawDC = RenderOpen();
  436. drawDC?.Close();
  437. }
  438. public void SetRect(Rect newRect, double zoom)
  439. {
  440. if (newRect == Rect.Empty || newRect == null)
  441. {
  442. return;
  443. }
  444. 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));
  445. currentZoom = zoom;
  446. SetDrawRect = drawRect = newRect;
  447. drawCenterPoint = new Point(drawRect.Left + drawRect.Width / 2, drawRect.Top + drawRect.Height / 2);
  448. }
  449. /// <summary>
  450. /// Get the original set Rect, not the calculated fill
  451. /// </summary>
  452. /// <param name="newRect">
  453. /// The new rect to set
  454. /// </param>
  455. public Rect GetRect()
  456. {
  457. 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));
  458. return rect;
  459. }
  460. public void SetRectPadding(double rectPadding)
  461. {
  462. this.rectPadding = rectPadding;
  463. }
  464. public double GetRectPadding()
  465. {
  466. return rectPadding;
  467. }
  468. public Rect GetDrawRect()
  469. {
  470. return drawRect;
  471. }
  472. /// <summary>
  473. /// Get ClipRect
  474. /// </summary>
  475. /// <returns></returns>
  476. public Rect GetClipRect()
  477. {
  478. return clipRect;
  479. }
  480. /// <summary>
  481. /// Set ClipRect And DrawRect
  482. /// </summary>
  483. /// <returns></returns>
  484. public void SetClipRect(Rect rect)
  485. {
  486. drawRect = rect;
  487. clipRect = rect;
  488. }
  489. public void SetMaxRect(Rect rect)
  490. {
  491. maxRect = rect;
  492. }
  493. public Rect GetMaxRect()
  494. {
  495. return maxRect;
  496. }
  497. public void SetAnnotData(AnnotData annotData)
  498. {
  499. SetIgnorePoints(new List<PointControlType>());
  500. SetIsProportionalScaling(false);
  501. isProportionalScaling = false;
  502. switch (annotData.AnnotType)
  503. {
  504. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  505. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  506. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  507. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  508. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  509. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  510. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  511. DisableAll();
  512. break;
  513. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  514. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  515. SetIgnorePointsAll();
  516. break;
  517. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  518. SetIsProportionalScaling(true);
  519. break;
  520. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  521. SetIgnorePointsAll();
  522. break;
  523. default:
  524. break;
  525. }
  526. SetMaxRect(annotData.PaintOffset);
  527. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  528. selectedRectData = new SelectedAnnotData();
  529. selectedRectData.annotData = annotData;
  530. }
  531. public void SetIsProportionalScaling(bool isProportionalScaling)
  532. {
  533. this.isProportionalScaling = isProportionalScaling;
  534. ignorePoints.Clear();
  535. if (isProportionalScaling)
  536. {
  537. ignorePoints.Add(PointControlType.LeftMiddle);
  538. ignorePoints.Add(PointControlType.MiddlBottom);
  539. ignorePoints.Add(PointControlType.RightMiddle);
  540. ignorePoints.Add(PointControlType.MiddleTop);
  541. ignorePoints.Add(PointControlType.Rotate);
  542. }
  543. }
  544. public void SetDrawType(DrawPointType drawType)
  545. {
  546. currentDrawPointType = drawType;
  547. }
  548. public void SetDrawMoveType(DrawMoveType drawType)
  549. {
  550. currentDrawMoveType = drawType;
  551. }
  552. /// <summary>
  553. /// Set the types that need to be ignored
  554. /// </summary>
  555. /// <param name="types">
  556. /// The collection of point types that need to be ignored
  557. /// </param>
  558. public void SetIgnorePoints(List<PointControlType> types)
  559. {
  560. ignorePoints.Clear();
  561. foreach (PointControlType type in types)
  562. {
  563. ignorePoints.Add(type);
  564. }
  565. }
  566. /// <summary>
  567. /// Set Edit that need to be ignored
  568. /// </summary>
  569. /// <param name="types">
  570. /// The collection of point types that need to be ignored
  571. /// </param>
  572. public void SetEditIgnorePoints(List<PointControlType> ignoreTextPoints, List<PointControlType> ignoreImagePoints, DrawPointType drawEditPointType, bool IsText = true)
  573. {
  574. SetCurrentDrawPointType(drawEditPointType);
  575. if (IsText)
  576. {
  577. ignorePoints.Clear();
  578. foreach (PointControlType type in ignoreTextPoints)
  579. {
  580. ignorePoints.Add(type);
  581. }
  582. }
  583. else
  584. {
  585. ignorePoints.Clear();
  586. foreach (PointControlType type in ignoreImagePoints)
  587. {
  588. ignorePoints.Add(type);
  589. }
  590. }
  591. }
  592. /// <summary>
  593. /// Ignore all points
  594. /// </summary>
  595. public void SetIgnorePointsAll()
  596. {
  597. ignorePoints.Clear();
  598. ignorePoints.Add(PointControlType.LeftTop);
  599. ignorePoints.Add(PointControlType.LeftMiddle);
  600. ignorePoints.Add(PointControlType.LeftBottom);
  601. ignorePoints.Add(PointControlType.MiddlBottom);
  602. ignorePoints.Add(PointControlType.RightBottom);
  603. ignorePoints.Add(PointControlType.RightMiddle);
  604. ignorePoints.Add(PointControlType.RightTop);
  605. ignorePoints.Add(PointControlType.MiddleTop);
  606. }
  607. /// <summary>
  608. /// Disable all functions
  609. /// </summary>
  610. public void DisableAll()
  611. {
  612. ignorePoints.Clear();
  613. ignorePoints.Add(PointControlType.LeftTop);
  614. ignorePoints.Add(PointControlType.LeftMiddle);
  615. ignorePoints.Add(PointControlType.LeftBottom);
  616. ignorePoints.Add(PointControlType.MiddlBottom);
  617. ignorePoints.Add(PointControlType.RightBottom);
  618. ignorePoints.Add(PointControlType.RightMiddle);
  619. ignorePoints.Add(PointControlType.RightTop);
  620. ignorePoints.Add(PointControlType.MiddleTop);
  621. ignorePoints.Add(PointControlType.Rotate);
  622. ignorePoints.Add(PointControlType.Body);
  623. ignorePoints.Add(PointControlType.Line);
  624. }
  625. /// <summary>
  626. /// Set the left alignment in the set maximum rectangle
  627. /// </summary>
  628. public virtual void SetAlignLeftForMaxRect()
  629. {
  630. DrawAlignRect(AlignmentsHelp.SetAlignLeft(drawRect, maxRect));
  631. }
  632. /// <summary>
  633. /// Set horizontal center alignment in the set maximum rectangle
  634. /// </summary>
  635. public virtual void SetAlignHorizonCenterForMaxRect()
  636. {
  637. DrawAlignRect(AlignmentsHelp.SetAlignHorizonCenter(drawRect, maxRect));
  638. }
  639. /// <summary>
  640. /// Set horizontal right alignment in the set maximum rectangle
  641. /// </summary>
  642. public virtual void SetAlignRightForMaxRect()
  643. {
  644. DrawAlignRect(AlignmentsHelp.SetAlignRight(drawRect, maxRect));
  645. }
  646. /// <summary>
  647. /// Set the top alignment in the set maximum rectangle
  648. /// </summary>
  649. public virtual void SetAlignTopForMaxRect()
  650. {
  651. DrawAlignRect(AlignmentsHelp.SetAlignTop(drawRect, maxRect));
  652. }
  653. /// <summary>
  654. /// Set vertical center alignment in the set maximum rectangle
  655. /// </summary>
  656. public virtual void SetAlignVerticalCenterForMaxRect()
  657. {
  658. DrawAlignRect(AlignmentsHelp.SetAlignVerticalCenter(drawRect, maxRect));
  659. }
  660. /// <summary>
  661. /// Set vertical center alignment in the set maximum rectangle
  662. /// </summary>
  663. public virtual void SetAlignHorizonVerticalCenterForMaxRect()
  664. {
  665. DrawAlignRect(AlignmentsHelp.SetAlignHorizonVerticalCenter(drawRect, maxRect));
  666. }
  667. /// <summary>
  668. /// Set the bottom alignment in the set maximum rectangle
  669. /// </summary>
  670. public virtual void SetAlignBottomForMaxRect()
  671. {
  672. DrawAlignRect(AlignmentsHelp.SetAlignBottom(drawRect, maxRect));
  673. }
  674. /// <summary>
  675. /// Get which control point the coordinate is on
  676. /// </summary>
  677. /// <param name="clickPoint">
  678. /// The point to check
  679. /// </param>
  680. /// <returns>
  681. /// The control point type
  682. /// </returns>
  683. public PointControlType GetHitControlIndex(Point point, bool isIgnore = true)
  684. {
  685. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  686. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  687. {
  688. List<PointControlType> ignoreList = GetIgnorePoints();
  689. List<Point> IgnorePointsList = new List<Point>();
  690. foreach (PointControlType type in ignoreList)
  691. {
  692. if ((int)type < controlPoints.Count)
  693. {
  694. IgnorePointsList.Add(controlPoints[(int)type]);
  695. }
  696. }
  697. for (int i = 0; i < controlPoints.Count; i++)
  698. {
  699. Point checkPoint = controlPoints[i];
  700. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  701. {
  702. continue;
  703. }
  704. switch (currentDrawPointType)
  705. {
  706. case DrawPointType.Circle:
  707. Vector checkVector = checkPoint - point;
  708. if (checkVector.Length < pointSize)
  709. {
  710. return (PointControlType)i;
  711. }
  712. break;
  713. case DrawPointType.Square:
  714. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  715. if (checkRect.Contains(point))
  716. {
  717. return (PointControlType)i;
  718. }
  719. break;
  720. case DrawPointType.Crop:
  721. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  722. if (cropRect.Contains(point))
  723. {
  724. return (PointControlType)i;
  725. }
  726. break;
  727. default:
  728. break;
  729. }
  730. }
  731. if (drawRect.Contains(point))
  732. {
  733. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  734. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  735. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  736. if (rect.Contains(point))
  737. {
  738. if (!ignoreList.Contains(PointControlType.Body))
  739. {
  740. return PointControlType.Body;
  741. }
  742. }
  743. if (!ignoreList.Contains(PointControlType.Body))
  744. {
  745. return PointControlType.Line;
  746. }
  747. }
  748. }
  749. return PointControlType.None;
  750. }
  751. /// <summary>
  752. /// The position of the points in the cropping box
  753. /// </summary>
  754. /// <param name="point"></param>
  755. /// <param name="isIgnore"></param>
  756. /// <returns></returns>
  757. public PointControlType GetHitCropControlIndex(Point point, bool isIgnore = true)
  758. {
  759. List<Point> controlCurrentPoints = GetControlPoint(drawRect);
  760. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  761. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  762. {
  763. List<PointControlType> ignoreList = GetIgnorePoints();
  764. List<Point> IgnorePointsList = new List<Point>();
  765. foreach (PointControlType type in ignoreList)
  766. {
  767. if ((int)type < controlCurrentPoints.Count)
  768. {
  769. IgnorePointsList.Add(controlCurrentPoints[(int)type]);
  770. }
  771. }
  772. for (int i = 0; i < controlCurrentPoints.Count; i++)
  773. {
  774. Point checkPoint = controlCurrentPoints[i];
  775. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  776. {
  777. continue;
  778. }
  779. switch (currentDrawPointType)
  780. {
  781. case DrawPointType.Circle:
  782. Vector checkVector = checkPoint - point;
  783. if (checkVector.Length < pointSize)
  784. {
  785. return (PointControlType)i;
  786. }
  787. break;
  788. case DrawPointType.Square:
  789. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  790. if (checkRect.Contains(point))
  791. {
  792. return (PointControlType)i;
  793. }
  794. break;
  795. case DrawPointType.Crop:
  796. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  797. if (cropRect.Contains(point))
  798. {
  799. return (PointControlType)i;
  800. }
  801. break;
  802. default:
  803. break;
  804. }
  805. }
  806. if (drawRect.Contains(point))
  807. {
  808. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  809. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  810. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  811. if (rect.Contains(point))
  812. {
  813. if (!ignoreList.Contains(PointControlType.Body))
  814. {
  815. return PointControlType.Body;
  816. }
  817. }
  818. if (!ignoreList.Contains(PointControlType.Body))
  819. {
  820. return PointControlType.Line;
  821. }
  822. }
  823. }
  824. return PointControlType.None;
  825. }
  826. }
  827. }