SelectedRect.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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, PointBrush);
  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 UpdateAnnotData(AnnotData annotData)
  524. {
  525. if(selectedRectData!=null)
  526. {
  527. selectedRectData.annotData = annotData;
  528. }
  529. }
  530. public void SetAnnotData(AnnotData annotData)
  531. {
  532. SetIgnorePoints(new List<PointControlType>());
  533. SetIsProportionalScaling(false);
  534. isProportionalScaling = false;
  535. switch (annotData.AnnotType)
  536. {
  537. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  538. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  539. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  540. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  541. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  542. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  543. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  544. DisableAll();
  545. break;
  546. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  547. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  548. SetIgnorePointsAll();
  549. break;
  550. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  551. SetIsProportionalScaling(true);
  552. break;
  553. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  554. SetIgnorePointsAll();
  555. break;
  556. default:
  557. break;
  558. }
  559. SetMaxRect(annotData.PaintOffset);
  560. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  561. selectedRectData = new SelectedAnnotData();
  562. selectedRectData.annotData = annotData;
  563. }
  564. public void SetIsProportionalScaling(bool isProportionalScaling)
  565. {
  566. this.isProportionalScaling = isProportionalScaling;
  567. ignorePoints.Clear();
  568. if (isProportionalScaling)
  569. {
  570. ignorePoints.Add(PointControlType.LeftMiddle);
  571. ignorePoints.Add(PointControlType.MiddlBottom);
  572. ignorePoints.Add(PointControlType.RightMiddle);
  573. ignorePoints.Add(PointControlType.MiddleTop);
  574. ignorePoints.Add(PointControlType.Rotate);
  575. }
  576. }
  577. public void SetDrawType(DrawPointType drawType)
  578. {
  579. currentDrawPointType = drawType;
  580. }
  581. public void SetDrawMoveType(DrawMoveType drawType)
  582. {
  583. currentDrawMoveType = drawType;
  584. }
  585. /// <summary>
  586. /// Set the types that need to be ignored
  587. /// </summary>
  588. /// <param name="types">
  589. /// The collection of point types that need to be ignored
  590. /// </param>
  591. public void SetIgnorePoints(List<PointControlType> types)
  592. {
  593. ignorePoints.Clear();
  594. foreach (PointControlType type in types)
  595. {
  596. ignorePoints.Add(type);
  597. }
  598. }
  599. /// <summary>
  600. /// Set Edit that need to be ignored
  601. /// </summary>
  602. /// <param name="types">
  603. /// The collection of point types that need to be ignored
  604. /// </param>
  605. public void SetEditIgnorePoints(List<PointControlType> ignoreTextPoints, List<PointControlType> ignoreImagePoints, DrawPointType drawEditPointType, bool IsText = true)
  606. {
  607. SetCurrentDrawPointType(drawEditPointType);
  608. if (IsText)
  609. {
  610. ignorePoints.Clear();
  611. foreach (PointControlType type in ignoreTextPoints)
  612. {
  613. ignorePoints.Add(type);
  614. }
  615. }
  616. else
  617. {
  618. ignorePoints.Clear();
  619. foreach (PointControlType type in ignoreImagePoints)
  620. {
  621. ignorePoints.Add(type);
  622. }
  623. }
  624. }
  625. /// <summary>
  626. /// Ignore all points
  627. /// </summary>
  628. public void SetIgnorePointsAll()
  629. {
  630. ignorePoints.Clear();
  631. ignorePoints.Add(PointControlType.LeftTop);
  632. ignorePoints.Add(PointControlType.LeftMiddle);
  633. ignorePoints.Add(PointControlType.LeftBottom);
  634. ignorePoints.Add(PointControlType.MiddlBottom);
  635. ignorePoints.Add(PointControlType.RightBottom);
  636. ignorePoints.Add(PointControlType.RightMiddle);
  637. ignorePoints.Add(PointControlType.RightTop);
  638. ignorePoints.Add(PointControlType.MiddleTop);
  639. }
  640. /// <summary>
  641. /// Disable all functions
  642. /// </summary>
  643. public void DisableAll()
  644. {
  645. ignorePoints.Clear();
  646. ignorePoints.Add(PointControlType.LeftTop);
  647. ignorePoints.Add(PointControlType.LeftMiddle);
  648. ignorePoints.Add(PointControlType.LeftBottom);
  649. ignorePoints.Add(PointControlType.MiddlBottom);
  650. ignorePoints.Add(PointControlType.RightBottom);
  651. ignorePoints.Add(PointControlType.RightMiddle);
  652. ignorePoints.Add(PointControlType.RightTop);
  653. ignorePoints.Add(PointControlType.MiddleTop);
  654. ignorePoints.Add(PointControlType.Rotate);
  655. ignorePoints.Add(PointControlType.Body);
  656. ignorePoints.Add(PointControlType.Line);
  657. }
  658. /// <summary>
  659. /// Set the left alignment in the set maximum rectangle
  660. /// </summary>
  661. public virtual void SetAlignLeftForMaxRect()
  662. {
  663. DrawAlignRect(AlignmentsHelp.SetAlignLeft(drawRect, maxRect));
  664. }
  665. /// <summary>
  666. /// Set horizontal center alignment in the set maximum rectangle
  667. /// </summary>
  668. public virtual void SetAlignHorizonCenterForMaxRect()
  669. {
  670. DrawAlignRect(AlignmentsHelp.SetAlignHorizonCenter(drawRect, maxRect));
  671. }
  672. /// <summary>
  673. /// Set horizontal right alignment in the set maximum rectangle
  674. /// </summary>
  675. public virtual void SetAlignRightForMaxRect()
  676. {
  677. DrawAlignRect(AlignmentsHelp.SetAlignRight(drawRect, maxRect));
  678. }
  679. /// <summary>
  680. /// Set the top alignment in the set maximum rectangle
  681. /// </summary>
  682. public virtual void SetAlignTopForMaxRect()
  683. {
  684. DrawAlignRect(AlignmentsHelp.SetAlignTop(drawRect, maxRect));
  685. }
  686. /// <summary>
  687. /// Set vertical center alignment in the set maximum rectangle
  688. /// </summary>
  689. public virtual void SetAlignVerticalCenterForMaxRect()
  690. {
  691. DrawAlignRect(AlignmentsHelp.SetAlignVerticalCenter(drawRect, maxRect));
  692. }
  693. /// <summary>
  694. /// Set vertical center alignment in the set maximum rectangle
  695. /// </summary>
  696. public virtual void SetAlignHorizonVerticalCenterForMaxRect()
  697. {
  698. DrawAlignRect(AlignmentsHelp.SetAlignHorizonVerticalCenter(drawRect, maxRect));
  699. }
  700. /// <summary>
  701. /// Set the bottom alignment in the set maximum rectangle
  702. /// </summary>
  703. public virtual void SetAlignBottomForMaxRect()
  704. {
  705. DrawAlignRect(AlignmentsHelp.SetAlignBottom(drawRect, maxRect));
  706. }
  707. /// <summary>
  708. /// Get which control point the coordinate is on
  709. /// </summary>
  710. /// <param name="clickPoint">
  711. /// The point to check
  712. /// </param>
  713. /// <returns>
  714. /// The control point type
  715. /// </returns>
  716. public PointControlType GetHitControlIndex(Point point, bool isIgnore = true)
  717. {
  718. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  719. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  720. {
  721. List<PointControlType> ignoreList = GetIgnorePoints();
  722. List<Point> IgnorePointsList = new List<Point>();
  723. foreach (PointControlType type in ignoreList)
  724. {
  725. if ((int)type < controlPoints.Count)
  726. {
  727. IgnorePointsList.Add(controlPoints[(int)type]);
  728. }
  729. }
  730. for (int i = 0; i < controlPoints.Count; i++)
  731. {
  732. Point checkPoint = controlPoints[i];
  733. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  734. {
  735. continue;
  736. }
  737. switch (currentDrawPointType)
  738. {
  739. case DrawPointType.Circle:
  740. if (IgnorePointsList.Contains(checkPoint))
  741. {
  742. continue;
  743. }
  744. Vector checkVector = checkPoint - point;
  745. double wlen = drawRect.Width;
  746. if (wlen > 50)
  747. {
  748. wlen = 20;
  749. }
  750. else
  751. {
  752. wlen = wlen / 3;
  753. }
  754. double hlen = drawRect.Height;
  755. if (hlen > 50)
  756. {
  757. hlen = 20;
  758. }
  759. else
  760. {
  761. hlen = wlen / 3;
  762. }
  763. if ((PointControlType)i == PointControlType.RightMiddle)
  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.LeftMiddle)
  771. {
  772. if (Math.Abs(point.X - checkPoint.X) < wlen && checkVector.Length < drawRect.Height/3)
  773. {
  774. return (PointControlType)i;
  775. }
  776. }
  777. if ((PointControlType)i == PointControlType.MiddleTop)
  778. {
  779. if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width/3)
  780. {
  781. return (PointControlType)i;
  782. }
  783. }
  784. if ((PointControlType)i == PointControlType.MiddlBottom)
  785. {
  786. if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width/3)
  787. {
  788. return (PointControlType)i;
  789. }
  790. }
  791. if (checkVector.Length < pointSize)
  792. {
  793. return (PointControlType)i;
  794. }
  795. break;
  796. case DrawPointType.Square:
  797. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  798. if (checkRect.Contains(point))
  799. {
  800. return (PointControlType)i;
  801. }
  802. break;
  803. case DrawPointType.Crop:
  804. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  805. if (cropRect.Contains(point))
  806. {
  807. return (PointControlType)i;
  808. }
  809. break;
  810. default:
  811. break;
  812. }
  813. }
  814. if (drawRect.Contains(point))
  815. {
  816. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  817. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  818. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  819. if (rect.Contains(point))
  820. {
  821. if (!ignoreList.Contains(PointControlType.Body))
  822. {
  823. return PointControlType.Body;
  824. }
  825. }
  826. if (!ignoreList.Contains(PointControlType.Body))
  827. {
  828. return PointControlType.Line;
  829. }
  830. }
  831. }
  832. return PointControlType.None;
  833. }
  834. /// <summary>
  835. /// The position of the points in the cropping box
  836. /// </summary>
  837. /// <param name="point"></param>
  838. /// <param name="isIgnore"></param>
  839. /// <returns></returns>
  840. public PointControlType GetHitCropControlIndex(Point point, bool isIgnore = true)
  841. {
  842. List<Point> controlCurrentPoints = GetControlPoint(drawRect);
  843. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  844. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  845. {
  846. List<PointControlType> ignoreList = GetIgnorePoints();
  847. List<Point> IgnorePointsList = new List<Point>();
  848. foreach (PointControlType type in ignoreList)
  849. {
  850. if ((int)type < controlCurrentPoints.Count)
  851. {
  852. IgnorePointsList.Add(controlCurrentPoints[(int)type]);
  853. }
  854. }
  855. for (int i = 0; i < controlCurrentPoints.Count; i++)
  856. {
  857. Point checkPoint = controlCurrentPoints[i];
  858. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  859. {
  860. continue;
  861. }
  862. switch (currentDrawPointType)
  863. {
  864. case DrawPointType.Circle:
  865. Vector checkVector = checkPoint - point;
  866. if (checkVector.Length < pointSize)
  867. {
  868. return (PointControlType)i;
  869. }
  870. break;
  871. case DrawPointType.Square:
  872. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  873. if (checkRect.Contains(point))
  874. {
  875. return (PointControlType)i;
  876. }
  877. break;
  878. case DrawPointType.Crop:
  879. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  880. if (cropRect.Contains(point))
  881. {
  882. return (PointControlType)i;
  883. }
  884. break;
  885. default:
  886. break;
  887. }
  888. }
  889. if (drawRect.Contains(point))
  890. {
  891. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  892. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  893. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  894. if (rect.Contains(point))
  895. {
  896. if (!ignoreList.Contains(PointControlType.Body))
  897. {
  898. return PointControlType.Body;
  899. }
  900. }
  901. if (!ignoreList.Contains(PointControlType.Body))
  902. {
  903. return PointControlType.Line;
  904. }
  905. }
  906. }
  907. return PointControlType.None;
  908. }
  909. }
  910. }