SelectedRect.cs 35 KB

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