SelectedRect.cs 32 KB

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