SelectedRect.cs 35 KB

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