SelectedRect.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.Tool.SettingParam;
  4. using ComPDFKit.Viewer.Helper;
  5. using ComPDFKitViewer;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. namespace ComPDFKit.Tool.DrawTool
  14. {
  15. public enum PointControlType
  16. {
  17. None = -1,
  18. LeftTop,
  19. LeftMiddle,
  20. LeftBottom,
  21. MiddleBottom,
  22. RightBottom,
  23. RightMiddle,
  24. RightTop,
  25. MiddleTop,
  26. Body,
  27. Line,
  28. Rotate
  29. }
  30. public enum SelectedType
  31. {
  32. None = -1,
  33. Annot,
  34. PDFEdit
  35. }
  36. public enum DrawPointType
  37. {
  38. Circle,
  39. Square,
  40. Crop,
  41. }
  42. public enum DrawMoveType
  43. {
  44. kDefault,
  45. kReferenceLine,
  46. kRotatable
  47. }
  48. public class SelectedAnnotData
  49. {
  50. /// <summary>
  51. /// Current size of the rectangle
  52. /// </summary>
  53. public Rect Square { get; set; }
  54. /// <summary>
  55. /// Current points of the rectangle
  56. /// </summary>
  57. public PointCollection Points { get; set; }
  58. public AnnotData annotData { get; set; }
  59. public int rotationAngle { get; set; }
  60. }
  61. public partial class SelectedRect : DrawingVisual
  62. {
  63. /// <summary>
  64. /// Re-layout child elements
  65. /// </summary>
  66. public void Arrange()
  67. {
  68. foreach (Visual child in Children)
  69. {
  70. if (!(child is UIElement))
  71. {
  72. continue;
  73. }
  74. UIElement checkChild = child as UIElement;
  75. try
  76. {
  77. double left = Canvas.GetLeft(checkChild);
  78. double top = Canvas.GetTop(checkChild);
  79. double width = (double)checkChild.GetValue(FrameworkElement.WidthProperty);
  80. double height = (double)checkChild.GetValue(FrameworkElement.HeightProperty);
  81. checkChild.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  82. checkChild.Arrange(new Rect(
  83. double.IsNaN(left) ? 0 : left,
  84. double.IsNaN(top) ? 0 : top,
  85. double.IsNaN(width) ? checkChild.DesiredSize.Width : width,
  86. double.IsNaN(height) ? checkChild.DesiredSize.Height : height));
  87. }
  88. catch (Exception ex)
  89. {
  90. }
  91. }
  92. }
  93. protected DefaultDrawParam DrawParam = new DefaultDrawParam();
  94. protected DrawingContext drawDC { get; set; }
  95. /// <summary>
  96. /// Data changing event
  97. /// </summary>
  98. public event EventHandler<SelectedAnnotData> DataChanging;
  99. /// <summary>
  100. /// Data changed event
  101. /// </summary>
  102. public event EventHandler<SelectedAnnotData> DataChanged;
  103. protected bool isHover = false;
  104. protected bool isSelected = false;
  105. protected bool canRotate = false;
  106. protected SelectedType selectedType = SelectedType.None;
  107. public SelectedType GetSelectedType()
  108. {
  109. return selectedType;
  110. }
  111. public void SetIsHover(bool hover)
  112. {
  113. isHover = hover;
  114. }
  115. public bool GetIsHover()
  116. {
  117. return isHover;
  118. }
  119. public void SetIsSelected(bool selected)
  120. {
  121. isSelected = selected;
  122. }
  123. public bool GetIsSelected()
  124. {
  125. return isSelected;
  126. }
  127. public void SetCurrentDrawPointType(DrawPointType type)
  128. {
  129. currentDrawPointType = type;
  130. }
  131. public DrawPointType GetCurrentDrawPointType()
  132. {
  133. return currentDrawPointType;
  134. }
  135. public virtual void OnMouseLeftButtonDown(Point downPoint)
  136. {
  137. isMouseDown = true;
  138. hitControlType = PointControlType.None;
  139. mouseDownPoint = downPoint;
  140. moveOffset = new Point(0, 0);
  141. HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
  142. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  143. {
  144. //Crop judgment point
  145. if (currentDrawPointType == DrawPointType.Crop)
  146. {
  147. hitControlType = GetHitCropControlIndex(downPoint);
  148. }
  149. else
  150. {
  151. hitControlType = GetHitControlIndex(downPoint);
  152. }
  153. if (hitControlType != PointControlType.None)
  154. {
  155. cacheRect = drawRect;
  156. rotateRect = drawRect;
  157. rotateControlPoints = controlPoints.ToList();
  158. if (hitControlType != PointControlType.Rotate)
  159. {
  160. isInScaling = true;
  161. }
  162. else
  163. {
  164. isInRotate = true;
  165. }
  166. }
  167. }
  168. }
  169. public virtual void OnMouseLeftButtonUp(Point upPoint)
  170. {
  171. if (isMouseDown && hitControlType != PointControlType.None)
  172. {
  173. isMouseDown = false;
  174. isInScaling = false;
  175. isInRotate = false;
  176. cacheRect = SetDrawRect = drawRect;
  177. Draw();
  178. if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
  179. {
  180. InvokeDataChangEvent(true);
  181. }
  182. }
  183. moveOffset = new Point(0, 0);
  184. }
  185. public virtual void OnMouseMove(Point mousePoint, out bool Tag, double width, double height)
  186. {
  187. PDFViewerActualWidth = width;
  188. PDFViewerActualHeight = height;
  189. Tag = false;
  190. if (isMouseDown && hitControlType != PointControlType.None)
  191. {
  192. Tag = isMouseDown;
  193. if (CalcHitPointMove(mousePoint))
  194. {
  195. Draw();
  196. if ((int)mousePoint.X != (int)mouseDownPoint.X || (int)mousePoint.Y != (int)mouseDownPoint.Y)
  197. {
  198. InvokeDataChangEvent(false);
  199. }
  200. }
  201. }
  202. }
  203. public Cursor GetCursor(Point downPoint, Cursor cursor)
  204. {
  205. if (isMouseDown)
  206. {
  207. return cursor;
  208. }
  209. hitControlType = GetHitControlIndex(downPoint);
  210. switch (hitControlType)
  211. {
  212. case PointControlType.LeftTop:
  213. case PointControlType.RightBottom:
  214. return Cursors.SizeNWSE;
  215. case PointControlType.LeftMiddle:
  216. case PointControlType.RightMiddle:
  217. return Cursors.SizeWE;
  218. case PointControlType.LeftBottom:
  219. case PointControlType.RightTop:
  220. return Cursors.SizeNESW;
  221. case PointControlType.MiddleBottom:
  222. case PointControlType.MiddleTop:
  223. return Cursors.SizeNS;
  224. case PointControlType.Body:
  225. return Cursors.Arrow;
  226. case PointControlType.Line:
  227. return Cursors.SizeAll;
  228. case PointControlType.Rotate:
  229. return Cursors.SizeAll;
  230. default:
  231. return Cursors.Arrow;
  232. }
  233. }
  234. public SelectedRect(DefaultDrawParam defaultDrawParam, SelectedType type) : base()
  235. {
  236. DrawParam = defaultDrawParam;
  237. currentDrawPointType = DrawPointType.Square;
  238. selectedType = type;
  239. }
  240. public void Draw()
  241. {
  242. Dispatcher.Invoke(() =>
  243. {
  244. Rect currentRect = SetDrawRect;
  245. drawDC = RenderOpen();
  246. switch (currentDrawMoveType)
  247. {
  248. case DrawMoveType.kDefault:
  249. currentRect = drawRect;
  250. CalcControlPoint(currentRect);
  251. break;
  252. case DrawMoveType.kReferenceLine:
  253. CalcControlPoint(currentRect);
  254. if (isMouseDown == true)
  255. {
  256. SolidColorBrush moveBrush = DrawParam.AnnotMoveBrush;
  257. Pen movepen = DrawParam.AnnotMovePen;
  258. GetMoveBrushAndPen(ref moveBrush, ref movepen);
  259. if (selectedType == SelectedType.PDFEdit)
  260. {
  261. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect, DrawParam.PDFEditMoveRectPen);
  262. }
  263. else
  264. {
  265. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect);
  266. }
  267. }
  268. break;
  269. default:
  270. break;
  271. }
  272. SolidColorBrush solidColorBrush = DrawParam.AnnotRectFillBrush;
  273. Pen pen = DrawParam.AnnotRectLinePen;
  274. GetBrushAndPen(ref solidColorBrush, ref pen);
  275. RotateTransform rotateTransform = new RotateTransform(rotateAngle, centerPoint.X, centerPoint.Y);
  276. drawDC.PushTransform(rotateTransform);
  277. drawDC?.DrawRectangle(solidColorBrush, pen, currentRect);
  278. drawDC.Pop();
  279. SolidColorBrush PointBrush = DrawParam.AnnotPointBorderBrush;
  280. Pen PointPen = DrawParam.AnnotPointPen;
  281. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  282. switch (currentDrawPointType)
  283. {
  284. case DrawPointType.Circle:
  285. if (selectedType == SelectedType.PDFEdit)
  286. {
  287. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  288. }
  289. else
  290. {
  291. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  292. }
  293. break;
  294. case DrawPointType.Square:
  295. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  296. break;
  297. case DrawPointType.Crop:
  298. DrawCropPoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  299. break;
  300. }
  301. drawDC?.Close();
  302. drawDC = null;
  303. });
  304. }
  305. private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  306. {
  307. switch (selectedType)
  308. {
  309. case SelectedType.None:
  310. break;
  311. case SelectedType.Annot:
  312. colorBrush = DrawParam.AnnotMoveBrush;
  313. pen = DrawParam.AnnotMovePen;
  314. break;
  315. case SelectedType.PDFEdit:
  316. colorBrush = DrawParam.PDFEditMoveBrush;
  317. pen = DrawParam.PDFEditMovePen;
  318. break;
  319. default:
  320. break;
  321. }
  322. }
  323. private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  324. {
  325. switch (selectedType)
  326. {
  327. case SelectedType.None:
  328. break;
  329. case SelectedType.Annot:
  330. colorBrush = DrawParam.AnnotPointBorderBrush;
  331. pen = DrawParam.AnnotPointPen;
  332. break;
  333. case SelectedType.PDFEdit:
  334. if (isHover)
  335. {
  336. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  337. pen = DrawParam.PDFEditPointHoverPen;
  338. }
  339. else if (currentDrawPointType == DrawPointType.Crop)
  340. {
  341. colorBrush = DrawParam.SPDFEditCropBorderBrush;
  342. //new SolidColorBrush((DrawParam.SPDFEditPointPen.Brush as SolidColorBrush).Color);
  343. pen = DrawParam.SPDFEditPointPen.Clone();
  344. pen.DashStyle = DashStyles.Solid;
  345. }
  346. else
  347. {
  348. if (isSelected)
  349. {
  350. colorBrush = DrawParam.SPDFEditPointBorderBrush;
  351. pen = DrawParam.SPDFEditPointPen;
  352. }
  353. else
  354. {
  355. colorBrush = DrawParam.PDFEditPointBorderBrush;
  356. pen = DrawParam.PDFEditPointPen;
  357. }
  358. }
  359. break;
  360. default:
  361. break;
  362. }
  363. }
  364. private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  365. {
  366. switch (selectedType)
  367. {
  368. case SelectedType.None:
  369. break;
  370. case SelectedType.Annot:
  371. if (isHover)
  372. {
  373. colorBrush = DrawParam.AnnotRectFillBrush;
  374. pen = DrawParam.AnnotRectHoverPen;
  375. }
  376. else
  377. {
  378. colorBrush = DrawParam.AnnotRectFillBrush;
  379. pen = DrawParam.AnnotRectLinePen;
  380. }
  381. break;
  382. case SelectedType.PDFEdit:
  383. if (isHover)
  384. {
  385. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  386. pen = editHoverPen;//DrawParam.PDFEditRectLineHoverPen;
  387. }
  388. else
  389. {
  390. if (isSelected)
  391. {
  392. colorBrush = DrawParam.SPDFEditRectFillBrush;
  393. pen = DrawParam.SPDFEditRectLinePen;
  394. }
  395. else
  396. {
  397. colorBrush = DrawParam.PDFEditRectFillBrush;
  398. //init Color
  399. if (showCreatTextRect)
  400. {
  401. pen = DrawParam.PDFEditRectLinePen;
  402. }
  403. else
  404. {
  405. pen = editPen;
  406. }
  407. // editPen; //editPen;//// DrawParam.PDFEditRectLinePen;
  408. }
  409. }
  410. break;
  411. default:
  412. break;
  413. }
  414. }
  415. public void SetShowCreatTextRect(bool ShowCreatTextRect)
  416. {
  417. showCreatTextRect = ShowCreatTextRect;
  418. }
  419. public void SetEditPen(Pen editPen = null, Pen editHoverPen = null)
  420. {
  421. if (editPen == null)
  422. {
  423. this.editPen = DrawParam.PDFEditRectLinePen;
  424. }
  425. else
  426. {
  427. this.editPen = new Pen(editPen.Brush, editPen.Thickness);
  428. }
  429. if (editHoverPen == null)
  430. {
  431. this.editHoverPen = DrawParam.PDFEditRectLineHoverPen;
  432. }
  433. else
  434. {
  435. this.editHoverPen = editHoverPen;
  436. }
  437. }
  438. public virtual void ClearDraw()
  439. {
  440. SetDrawRect = drawRect = new Rect();
  441. rotateAngle = 0;
  442. drawDC = RenderOpen();
  443. drawDC?.Close();
  444. drawDC = null;
  445. }
  446. /// <summary>
  447. /// Hide the drawing
  448. /// </summary>
  449. public virtual void HideDraw()
  450. {
  451. drawDC = RenderOpen();
  452. drawDC?.Close();
  453. }
  454. public void SetRect(Rect newRect, double zoom)
  455. {
  456. if (newRect == Rect.Empty || newRect == null)
  457. {
  458. return;
  459. }
  460. newRect = new Rect((newRect.X - rectPadding * zoom), (newRect.Y - rectPadding * zoom), (newRect.Width + 2 * rectPadding * zoom), (newRect.Height + 2 * rectPadding * zoom));
  461. currentZoom = zoom;
  462. SetDrawRect = drawRect = newRect;
  463. drawCenterPoint = new Point(drawRect.Left + drawRect.Width / 2, drawRect.Top + drawRect.Height / 2);
  464. }
  465. /// <summary>
  466. /// Get the original set Rect, not the calculated fill
  467. /// </summary>
  468. /// <param name="newRect">
  469. /// The new rect to set
  470. /// </param>
  471. public Rect GetRect()
  472. {
  473. 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));
  474. return rect;
  475. }
  476. public int GetRotation()
  477. {
  478. return rotateAngle;
  479. }
  480. public void SetRotation(int rotationAngle)
  481. {
  482. this.rotateAngle = rotationAngle;
  483. }
  484. public void SetRectPadding(double rectPadding)
  485. {
  486. this.rectPadding = rectPadding;
  487. }
  488. public double GetRectPadding()
  489. {
  490. return rectPadding;
  491. }
  492. public Rect GetDrawRect()
  493. {
  494. return drawRect;
  495. }
  496. /// <summary>
  497. /// Obtain cropped and actual region margin
  498. /// </summary>
  499. /// <returns></returns>
  500. public Thickness GetClipThickness()
  501. {
  502. return clipThickness;
  503. }
  504. /// <summary>
  505. /// Get ClipRect
  506. /// </summary>
  507. /// <returns></returns>
  508. public Rect GetClipRect()
  509. {
  510. Rect drawrect = new Rect(0, 0, 0, 0);
  511. drawrect.X = SetDrawRect.X - clipThickness.Left * currentZoom;
  512. drawrect.Y = SetDrawRect.Y - clipThickness.Top * currentZoom;
  513. drawrect.Width = SetDrawRect.Width - clipThickness.Right * currentZoom + clipThickness.Left * currentZoom;
  514. drawrect.Height = SetDrawRect.Height - clipThickness.Bottom * currentZoom + clipThickness.Top * currentZoom;
  515. return drawrect;
  516. }
  517. /// <summary>
  518. /// Set cropping and actual area margins
  519. /// </summary>
  520. /// <returns></returns>
  521. public void SetClipThickness(Thickness rect)
  522. {
  523. try
  524. {
  525. Rect drawrect = new Rect(0, 0, 0, 0);
  526. drawrect.X = SetDrawRect.X - rect.Left * currentZoom;
  527. drawrect.Y = SetDrawRect.Y - rect.Top * currentZoom;
  528. drawrect.Width = SetDrawRect.Width - rect.Right * currentZoom + rect.Left * currentZoom;
  529. drawrect.Height = SetDrawRect.Height - rect.Bottom * currentZoom + rect.Top * currentZoom;
  530. drawRect = drawrect;
  531. clipThickness = rect;
  532. }
  533. catch { }
  534. }
  535. public void SetMaxRect(Rect rect)
  536. {
  537. maxRect = rect;
  538. }
  539. public Rect GetMaxRect()
  540. {
  541. return maxRect;
  542. }
  543. public void SetAnnotData(AnnotData annotData, CPDFViewer viewer)
  544. {
  545. SetIgnorePoints(new List<PointControlType>());
  546. SetIsProportionalScaling(false);
  547. SetRoationHandle(false);
  548. isProportionalScaling = false;
  549. switch (annotData.AnnotType)
  550. {
  551. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  552. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  553. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  554. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  555. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  556. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  557. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  558. DisableAll();
  559. break;
  560. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  561. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  562. SetIgnorePointsAll();
  563. break;
  564. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  565. SetIsProportionalScaling(true);
  566. SetRoationHandle(true);
  567. break;
  568. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  569. SetIgnorePointsAll();
  570. break;
  571. default:
  572. break;
  573. }
  574. SetMaxRect(annotData.PaintOffset);
  575. if(annotData.AnnotType == C_ANNOTATION_TYPE.C_ANNOTATION_STAMP)
  576. {
  577. CRect sourceRect = new CRect();
  578. annotData.Annot.GetSourceRect(ref sourceRect);
  579. if (!sourceRect.IsEmpty)
  580. {
  581. RenderData renderData = viewer.GetCurrentRenderPageForIndex(annotData.PageIndex);
  582. Rect zoomRect = new Rect(sourceRect.left / 72 * 96 * annotData.CurrentZoom, sourceRect.top / 72 * 96 * annotData.CurrentZoom, sourceRect.width() / 72 * 96 * annotData.CurrentZoom, sourceRect.height() / 72 * 96 * annotData.CurrentZoom);
  583. Rect rotateRect = zoomRect;
  584. rotateRect.X += renderData.PageBound.X - renderData.CropLeft * annotData.CurrentZoom;
  585. rotateRect.Y += renderData.PageBound.Y - renderData.CropTop * annotData.CurrentZoom;
  586. SetRect(rotateRect, annotData.CurrentZoom);
  587. rotateAngle = -(annotData.Annot as CPDFStampAnnotation).AnnotationRotator.GetRotation();
  588. }
  589. else
  590. {
  591. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  592. }
  593. }
  594. else
  595. {
  596. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  597. }
  598. //SetRotation(annotData.Rotation);
  599. selectedRectData = new SelectedAnnotData();
  600. selectedRectData.annotData = annotData;
  601. }
  602. private void SetRoationHandle(bool canRotate)
  603. {
  604. this.canRotate = canRotate;
  605. }
  606. public void SetIsProportionalScaling(bool isProportionalScaling)
  607. {
  608. this.isProportionalScaling = isProportionalScaling;
  609. ignorePoints.Clear();
  610. if (isProportionalScaling)
  611. {
  612. ignorePoints.Add(PointControlType.LeftMiddle);
  613. ignorePoints.Add(PointControlType.MiddleBottom);
  614. ignorePoints.Add(PointControlType.RightMiddle);
  615. ignorePoints.Add(PointControlType.MiddleTop);
  616. ignorePoints.Add(PointControlType.Rotate);
  617. }
  618. }
  619. public void SetDrawType(DrawPointType drawType)
  620. {
  621. currentDrawPointType = drawType;
  622. }
  623. public void SetDrawMoveType(DrawMoveType drawType)
  624. {
  625. currentDrawMoveType = drawType;
  626. }
  627. /// <summary>
  628. /// Set the types that need to be ignored
  629. /// </summary>
  630. /// <param name="types">
  631. /// The collection of point types that need to be ignored
  632. /// </param>
  633. public void SetIgnorePoints(List<PointControlType> types)
  634. {
  635. ignorePoints.Clear();
  636. foreach (PointControlType type in types)
  637. {
  638. ignorePoints.Add(type);
  639. }
  640. }
  641. /// <summary>
  642. /// Set Edit that need to be ignored
  643. /// </summary>
  644. /// <param name="types">
  645. /// The collection of point types that need to be ignored
  646. /// </param>
  647. public void SetEditIgnorePoints(List<PointControlType> ignoreTextPoints, List<PointControlType> ignoreImagePoints, DrawPointType drawEditPointType, bool IsText = true)
  648. {
  649. SetCurrentDrawPointType(drawEditPointType);
  650. if (IsText)
  651. {
  652. ignorePoints.Clear();
  653. foreach (PointControlType type in ignoreTextPoints)
  654. {
  655. ignorePoints.Add(type);
  656. }
  657. }
  658. else
  659. {
  660. ignorePoints.Clear();
  661. foreach (PointControlType type in ignoreImagePoints)
  662. {
  663. ignorePoints.Add(type);
  664. }
  665. }
  666. }
  667. /// <summary>
  668. /// Ignore all points
  669. /// </summary>
  670. public void SetIgnorePointsAll()
  671. {
  672. ignorePoints.Clear();
  673. ignorePoints.Add(PointControlType.LeftTop);
  674. ignorePoints.Add(PointControlType.LeftMiddle);
  675. ignorePoints.Add(PointControlType.LeftBottom);
  676. ignorePoints.Add(PointControlType.MiddleBottom);
  677. ignorePoints.Add(PointControlType.RightBottom);
  678. ignorePoints.Add(PointControlType.RightMiddle);
  679. ignorePoints.Add(PointControlType.RightTop);
  680. ignorePoints.Add(PointControlType.MiddleTop);
  681. }
  682. /// <summary>
  683. /// Disable all functions
  684. /// </summary>
  685. public void DisableAll()
  686. {
  687. ignorePoints.Clear();
  688. ignorePoints.Add(PointControlType.LeftTop);
  689. ignorePoints.Add(PointControlType.LeftMiddle);
  690. ignorePoints.Add(PointControlType.LeftBottom);
  691. ignorePoints.Add(PointControlType.MiddleBottom);
  692. ignorePoints.Add(PointControlType.RightBottom);
  693. ignorePoints.Add(PointControlType.RightMiddle);
  694. ignorePoints.Add(PointControlType.RightTop);
  695. ignorePoints.Add(PointControlType.MiddleTop);
  696. ignorePoints.Add(PointControlType.Rotate);
  697. ignorePoints.Add(PointControlType.Body);
  698. ignorePoints.Add(PointControlType.Line);
  699. }
  700. /// <summary>
  701. /// Set the left alignment in the set maximum rectangle
  702. /// </summary>
  703. public virtual void SetAlignLeftForMaxRect()
  704. {
  705. DrawAlignRect(AlignmentsHelp.SetAlignLeft(drawRect, maxRect));
  706. }
  707. /// <summary>
  708. /// Set horizontal center alignment in the set maximum rectangle
  709. /// </summary>
  710. public virtual void SetAlignHorizonCenterForMaxRect()
  711. {
  712. DrawAlignRect(AlignmentsHelp.SetAlignHorizonCenter(drawRect, maxRect));
  713. }
  714. /// <summary>
  715. /// Set horizontal right alignment in the set maximum rectangle
  716. /// </summary>
  717. public virtual void SetAlignRightForMaxRect()
  718. {
  719. DrawAlignRect(AlignmentsHelp.SetAlignRight(drawRect, maxRect));
  720. }
  721. /// <summary>
  722. /// Set the top alignment in the set maximum rectangle
  723. /// </summary>
  724. public virtual void SetAlignTopForMaxRect()
  725. {
  726. DrawAlignRect(AlignmentsHelp.SetAlignTop(drawRect, maxRect));
  727. }
  728. /// <summary>
  729. /// Set vertical center alignment in the set maximum rectangle
  730. /// </summary>
  731. public virtual void SetAlignVerticalCenterForMaxRect()
  732. {
  733. DrawAlignRect(AlignmentsHelp.SetAlignVerticalCenter(drawRect, maxRect));
  734. }
  735. /// <summary>
  736. /// Set vertical center alignment in the set maximum rectangle
  737. /// </summary>
  738. public virtual void SetAlignHorizonVerticalCenterForMaxRect()
  739. {
  740. DrawAlignRect(AlignmentsHelp.SetAlignHorizonVerticalCenter(drawRect, maxRect));
  741. }
  742. /// <summary>
  743. /// Set the bottom alignment in the set maximum rectangle
  744. /// </summary>
  745. public virtual void SetAlignBottomForMaxRect()
  746. {
  747. DrawAlignRect(AlignmentsHelp.SetAlignBottom(drawRect, maxRect));
  748. }
  749. /// <summary>
  750. /// Get which control point the coordinate is on
  751. /// </summary>
  752. /// <param name="clickPoint">
  753. /// The point to check
  754. /// </param>
  755. /// <returns>
  756. /// The control point type
  757. /// </returns>
  758. public PointControlType GetHitControlIndex(Point point, bool isIgnore = true)
  759. {
  760. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  761. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  762. {
  763. List<PointControlType> ignoreList = GetIgnorePoints();
  764. List<Point> IgnorePointsList = new List<Point>();
  765. foreach (PointControlType type in ignoreList)
  766. {
  767. if ((int)type < controlPoints.Count)
  768. {
  769. IgnorePointsList.Add(controlPoints[(int)type]);
  770. }
  771. }
  772. for (int i = 0; i < controlPoints.Count; i++)
  773. {
  774. Point checkPoint = controlPoints[i];
  775. if (canRotate)
  776. {
  777. // Convert the rotation angle from degrees to radians
  778. double angleRad = rotateAngle * Math.PI / 180.0;
  779. // Calculate the sine and cosine of the angle
  780. double cosAngle = Math.Cos(angleRad);
  781. double sinAngle = Math.Sin(angleRad);
  782. // Translate checkPoint to the origin (centerPoint becomes the origin)
  783. double translatedX = checkPoint.X - centerPoint.X;
  784. double translatedY = checkPoint.Y - centerPoint.Y;
  785. // Apply the rotation matrix
  786. double rotatedX = translatedX * cosAngle - translatedY * sinAngle;
  787. double rotatedY = translatedX * sinAngle + translatedY * cosAngle;
  788. // Translate the point back to its original position
  789. checkPoint.X = rotatedX + centerPoint.X;
  790. checkPoint.Y = rotatedY + centerPoint.Y;
  791. }
  792. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  793. {
  794. continue;
  795. }
  796. switch (currentDrawPointType)
  797. {
  798. case DrawPointType.Circle:
  799. if (IgnorePointsList.Contains(checkPoint))
  800. {
  801. continue;
  802. }
  803. Vector checkVector = checkPoint - point;
  804. double wlen = drawRect.Width;
  805. if (wlen > 50)
  806. {
  807. wlen = 20;
  808. }
  809. else
  810. {
  811. wlen = wlen / 3;
  812. }
  813. double hlen = drawRect.Height;
  814. if (hlen > 50)
  815. {
  816. hlen = 20;
  817. }
  818. else
  819. {
  820. hlen = wlen / 3;
  821. }
  822. if ((PointControlType)i == PointControlType.RightMiddle)
  823. {
  824. if (Math.Abs(point.X - checkPoint.X) < wlen && checkVector.Length < drawRect.Height/3)
  825. {
  826. return (PointControlType)i;
  827. }
  828. }
  829. if ((PointControlType)i == PointControlType.LeftMiddle)
  830. {
  831. if (Math.Abs(point.X - checkPoint.X) < wlen && checkVector.Length < drawRect.Height/3)
  832. {
  833. return (PointControlType)i;
  834. }
  835. }
  836. if ((PointControlType)i == PointControlType.MiddleTop)
  837. {
  838. if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width/3)
  839. {
  840. return (PointControlType)i;
  841. }
  842. }
  843. if ((PointControlType)i == PointControlType.MiddleBottom)
  844. {
  845. if (Math.Abs(point.Y - checkPoint.Y) < hlen && checkVector.Length < drawRect.Width/3)
  846. {
  847. return (PointControlType)i;
  848. }
  849. }
  850. if (checkVector.Length < pointSize)
  851. {
  852. return (PointControlType)i;
  853. }
  854. break;
  855. case DrawPointType.Square:
  856. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  857. if (checkRect.Contains(point))
  858. {
  859. return (PointControlType)i;
  860. }
  861. if (canRotate)
  862. {
  863. Point hitRotationPoint = new Point(centerPoint.X + (rotationPoint.X - centerPoint.X) * Math.Cos(rotateAngle * Math.PI / 180) - (rotationPoint.Y - centerPoint.Y) * Math.Sin(rotateAngle * Math.PI / 180),
  864. centerPoint.Y + (rotationPoint.X - centerPoint.X) * Math.Sin(rotateAngle * Math.PI / 180) + (rotationPoint.Y - centerPoint.Y) * Math.Cos(rotateAngle * Math.PI / 180));
  865. Vector checkVector1 = point - hitRotationPoint;
  866. if (checkVector1.Length < pointSize)
  867. {
  868. return PointControlType.Rotate;
  869. }
  870. }
  871. break;
  872. case DrawPointType.Crop:
  873. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  874. if (cropRect.Contains(point))
  875. {
  876. return (PointControlType)i;
  877. }
  878. break;
  879. default:
  880. break;
  881. }
  882. }
  883. if (canRotate)
  884. {
  885. bool isIn = DataConversionForWPF.IsPointInRotatedRectangle(point, drawRect, rotateAngle);
  886. if(isIn)
  887. {
  888. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  889. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  890. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  891. isIn = DataConversionForWPF.IsPointInRotatedRectangle(point, rect, rotateAngle);
  892. if (isIn)
  893. {
  894. if (!ignoreList.Contains(PointControlType.Body))
  895. {
  896. return PointControlType.Body;
  897. }
  898. }
  899. if (!ignoreList.Contains(PointControlType.Body))
  900. {
  901. return PointControlType.Line;
  902. }
  903. }
  904. }
  905. else
  906. {
  907. if (drawRect.Contains(point))
  908. {
  909. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  910. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  911. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  912. if (rect.Contains(point))
  913. {
  914. if (!ignoreList.Contains(PointControlType.Body))
  915. {
  916. return PointControlType.Body;
  917. }
  918. }
  919. if (!ignoreList.Contains(PointControlType.Body))
  920. {
  921. return PointControlType.Line;
  922. }
  923. }
  924. }
  925. }
  926. return PointControlType.None;
  927. }
  928. /// <summary>
  929. /// The position of the points in the cropping box
  930. /// </summary>
  931. /// <param name="point"></param>
  932. /// <param name="isIgnore"></param>
  933. /// <returns></returns>
  934. public PointControlType GetHitCropControlIndex(Point point, bool isIgnore = true)
  935. {
  936. List<Point> controlCurrentPoints = GetControlPoint(drawRect);
  937. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  938. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  939. {
  940. List<PointControlType> ignoreList = GetIgnorePoints();
  941. List<Point> IgnorePointsList = new List<Point>();
  942. foreach (PointControlType type in ignoreList)
  943. {
  944. if ((int)type < controlCurrentPoints.Count)
  945. {
  946. IgnorePointsList.Add(controlCurrentPoints[(int)type]);
  947. }
  948. }
  949. for (int i = 0; i < controlCurrentPoints.Count; i++)
  950. {
  951. Point checkPoint = controlCurrentPoints[i];
  952. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  953. {
  954. continue;
  955. }
  956. switch (currentDrawPointType)
  957. {
  958. case DrawPointType.Circle:
  959. Vector checkVector = checkPoint - point;
  960. if (checkVector.Length < pointSize)
  961. {
  962. return (PointControlType)i;
  963. }
  964. break;
  965. case DrawPointType.Square:
  966. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  967. if (checkRect.Contains(point))
  968. {
  969. return (PointControlType)i;
  970. }
  971. break;
  972. case DrawPointType.Crop:
  973. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  974. if (cropRect.Contains(point))
  975. {
  976. return (PointControlType)i;
  977. }
  978. break;
  979. default:
  980. break;
  981. }
  982. }
  983. if (drawRect.Contains(point))
  984. {
  985. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  986. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  987. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  988. if (rect.Contains(point))
  989. {
  990. if (!ignoreList.Contains(PointControlType.Body))
  991. {
  992. return PointControlType.Body;
  993. }
  994. }
  995. if (!ignoreList.Contains(PointControlType.Body))
  996. {
  997. return PointControlType.Line;
  998. }
  999. }
  1000. }
  1001. return PointControlType.None;
  1002. }
  1003. }
  1004. }