SelectedRect.cs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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.ComponentModel;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Media3D;
  16. using System.Xml.Linq;
  17. using static ComPDFKit.Tool.Help.ImportWin32;
  18. using static System.Net.Mime.MediaTypeNames;
  19. namespace ComPDFKit.Tool.DrawTool
  20. {
  21. public enum PointControlType
  22. {
  23. None = -1,
  24. LeftTop,
  25. LeftMiddle,
  26. LeftBottom,
  27. MiddlBottom,
  28. RightBottom,
  29. RightMiddle,
  30. RightTop,
  31. MiddleTop,
  32. Rotate,
  33. Body,
  34. Line
  35. }
  36. public enum SelectedType
  37. {
  38. None = -1,
  39. Annot,
  40. PDFEdit
  41. }
  42. public enum DrawPointType
  43. {
  44. Circle,
  45. Square,
  46. Crop
  47. }
  48. public enum DrawMoveType
  49. {
  50. kDefault,
  51. kReferenceLine,
  52. }
  53. public class SelectedAnnotData
  54. {
  55. /// <summary>
  56. /// Current size of the rectangle
  57. /// </summary>
  58. public Rect Square { get; set; }
  59. /// <summary>
  60. /// Current points of the rectangle
  61. /// </summary>
  62. public PointCollection Points { get; set; }
  63. public AnnotData annotData { get; set; }
  64. public int rotationAngle { get; set; }
  65. }
  66. public partial class SelectedRect : DrawingVisual
  67. {
  68. /// <summary>
  69. /// Re-layout child elements
  70. /// </summary>
  71. public void Arrange()
  72. {
  73. foreach (Visual child in Children)
  74. {
  75. if (!(child is UIElement))
  76. {
  77. continue;
  78. }
  79. UIElement checkChild = child as UIElement;
  80. try
  81. {
  82. double left = Canvas.GetLeft(checkChild);
  83. double top = Canvas.GetTop(checkChild);
  84. double width = (double)checkChild.GetValue(FrameworkElement.WidthProperty);
  85. double height = (double)checkChild.GetValue(FrameworkElement.HeightProperty);
  86. checkChild.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  87. checkChild.Arrange(new Rect(
  88. double.IsNaN(left) ? 0 : left,
  89. double.IsNaN(top) ? 0 : top,
  90. double.IsNaN(width) ? checkChild.DesiredSize.Width : width,
  91. double.IsNaN(height) ? checkChild.DesiredSize.Height : height));
  92. }
  93. catch (Exception ex)
  94. {
  95. }
  96. }
  97. }
  98. protected DefaultDrawParam DrawParam = new DefaultDrawParam();
  99. protected DrawingContext drawDC { get; set; }
  100. /// <summary>
  101. /// Data changing event
  102. /// </summary>
  103. public event EventHandler<SelectedAnnotData> DataChanging;
  104. /// <summary>
  105. /// Data changed event
  106. /// </summary>
  107. public event EventHandler<SelectedAnnotData> DataChanged;
  108. protected bool isHover = false;
  109. protected bool isSelected = false;
  110. protected bool canRotate = false;
  111. protected SelectedType selectedType = SelectedType.None;
  112. public SelectedType GetSelectedType()
  113. {
  114. return selectedType;
  115. }
  116. public void SetIsHover(bool hover)
  117. {
  118. isHover = hover;
  119. }
  120. public bool GetIsHover()
  121. {
  122. return isHover;
  123. }
  124. public void SetIsSelected(bool selected)
  125. {
  126. isSelected = selected;
  127. }
  128. public bool GetIsSelected()
  129. {
  130. return isSelected;
  131. }
  132. public void SetCurrentDrawPointType(DrawPointType type)
  133. {
  134. currentDrawPointType = type;
  135. }
  136. public DrawPointType GetCurrentDrawPointType()
  137. {
  138. return currentDrawPointType;
  139. }
  140. public virtual void OnMouseLeftButtonDown(Point downPoint)
  141. {
  142. isMouseDown = true;
  143. hitControlType = PointControlType.None;
  144. mouseDownPoint = downPoint;
  145. moveOffset = new Point(0, 0);
  146. HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
  147. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  148. {
  149. //Crop judgment point
  150. if (currentDrawPointType == DrawPointType.Crop)
  151. {
  152. hitControlType = GetHitCropControlIndex(downPoint);
  153. }
  154. else
  155. {
  156. hitControlType = GetHitControlIndex(downPoint);
  157. }
  158. if (hitControlType != PointControlType.None)
  159. {
  160. cacheRect = drawRect;
  161. rotateRect = drawRect;
  162. rotateControlPoints = controlPoints.ToList();
  163. if (hitControlType != PointControlType.Rotate)
  164. {
  165. isInScaling = true;
  166. }
  167. else
  168. {
  169. isInRotate = true;
  170. }
  171. }
  172. }
  173. }
  174. public virtual void OnMouseLeftButtonUp(Point upPoint)
  175. {
  176. if (isMouseDown && hitControlType != PointControlType.None)
  177. {
  178. isMouseDown = false;
  179. cacheRect = SetDrawRect = drawRect;
  180. Draw();
  181. if ((int)upPoint.X != (int)mouseDownPoint.X || (int)upPoint.Y != (int)mouseDownPoint.Y)
  182. {
  183. InvokeDataChangEvent(true);
  184. }
  185. }
  186. moveOffset = new Point(0, 0);
  187. }
  188. public virtual void OnMouseMove(Point mousePoint, out bool Tag, double width, double height)
  189. {
  190. PDFViewerActualWidth = width;
  191. PDFViewerActualHeight = height;
  192. Tag = false;
  193. if (isMouseDown && hitControlType != PointControlType.None)
  194. {
  195. Tag = isMouseDown;
  196. if (CalcHitPointMove(mousePoint))
  197. {
  198. Draw();
  199. if ((int)mousePoint.X != (int)mouseDownPoint.X || (int)mousePoint.Y != (int)mouseDownPoint.Y)
  200. {
  201. InvokeDataChangEvent(false);
  202. }
  203. }
  204. }
  205. }
  206. public Cursor GetCursor(Point downPoint, Cursor cursor)
  207. {
  208. if (isMouseDown)
  209. {
  210. return cursor;
  211. }
  212. hitControlType = GetHitControlIndex(downPoint);
  213. switch (hitControlType)
  214. {
  215. case PointControlType.LeftTop:
  216. case PointControlType.RightBottom:
  217. return Cursors.SizeNWSE;
  218. case PointControlType.LeftMiddle:
  219. case PointControlType.RightMiddle:
  220. return Cursors.SizeWE;
  221. case PointControlType.LeftBottom:
  222. case PointControlType.RightTop:
  223. return Cursors.SizeNESW;
  224. case PointControlType.MiddlBottom:
  225. case PointControlType.MiddleTop:
  226. return Cursors.SizeNS;
  227. case PointControlType.Body:
  228. return Cursors.Arrow;
  229. case PointControlType.Line:
  230. return Cursors.SizeAll;
  231. default:
  232. return Cursors.Arrow;
  233. }
  234. }
  235. public SelectedRect(DefaultDrawParam defaultDrawParam, SelectedType type) : base()
  236. {
  237. DrawParam = defaultDrawParam;
  238. currentDrawPointType = DrawPointType.Square;
  239. selectedType = type;
  240. }
  241. public void Draw()
  242. {
  243. Dispatcher.Invoke(() =>
  244. {
  245. Rect currentRect = SetDrawRect;
  246. drawDC = RenderOpen();
  247. switch (currentDrawMoveType)
  248. {
  249. case DrawMoveType.kDefault:
  250. currentRect = drawRect;
  251. CalcControlPoint(currentRect);
  252. break;
  253. case DrawMoveType.kReferenceLine:
  254. CalcControlPoint(currentRect);
  255. if (isMouseDown == true)
  256. {
  257. SolidColorBrush moveBrush = DrawParam.AnnotMoveBrush;
  258. Pen movepen = DrawParam.AnnotMovePen;
  259. GetMoveBrushAndPen(ref moveBrush, ref movepen);
  260. if (selectedType == SelectedType.PDFEdit)
  261. {
  262. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect, DrawParam.PDFEditMoveRectPen);
  263. }
  264. else
  265. {
  266. DrawMoveBounds(drawDC, hitControlType, movepen, moveBrush, drawRect);
  267. }
  268. }
  269. break;
  270. default:
  271. break;
  272. }
  273. SolidColorBrush solidColorBrush = DrawParam.AnnotRectFillBrush;
  274. Pen pen = DrawParam.AnnotRectLinePen;
  275. GetBrushAndPen(ref solidColorBrush, ref pen);
  276. drawDC?.DrawRectangle(solidColorBrush, pen, currentRect);
  277. SolidColorBrush PointBrush = DrawParam.AnnotPointBorderBrush;
  278. Pen PointPen = DrawParam.AnnotPointPen;
  279. GetPointBrushAndPen(ref PointBrush, ref PointPen);
  280. switch (currentDrawPointType)
  281. {
  282. case DrawPointType.Circle:
  283. if (selectedType == SelectedType.PDFEdit)
  284. {
  285. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  286. //DrawEditSelectionBox(drawDC, PointPen);
  287. }
  288. else
  289. {
  290. DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  291. }
  292. break;
  293. case DrawPointType.Square:
  294. DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  295. break;
  296. case DrawPointType.Crop:
  297. DrawCropPoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
  298. break;
  299. }
  300. drawDC?.Close();
  301. drawDC = null;
  302. });
  303. }
  304. private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  305. {
  306. switch (selectedType)
  307. {
  308. case SelectedType.None:
  309. break;
  310. case SelectedType.Annot:
  311. colorBrush = DrawParam.AnnotMoveBrush;
  312. pen = DrawParam.AnnotMovePen;
  313. break;
  314. case SelectedType.PDFEdit:
  315. colorBrush = DrawParam.PDFEditMoveBrush;
  316. pen = DrawParam.PDFEditMovePen;
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. private void GetPointBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  323. {
  324. switch (selectedType)
  325. {
  326. case SelectedType.None:
  327. break;
  328. case SelectedType.Annot:
  329. colorBrush = DrawParam.AnnotPointBorderBrush;
  330. pen = DrawParam.AnnotPointPen;
  331. break;
  332. case SelectedType.PDFEdit:
  333. if (isHover)
  334. {
  335. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  336. pen = DrawParam.PDFEditPointHoverPen;
  337. }
  338. else if (currentDrawPointType == DrawPointType.Crop)
  339. {
  340. colorBrush = DrawParam.SPDFEditCropBorderBrush;//new SolidColorBrush((DrawParam.SPDFEditPointPen.Brush as SolidColorBrush).Color);
  341. pen = DrawParam.SPDFEditPointPen.Clone();
  342. pen.DashStyle = DashStyles.Solid;
  343. }
  344. else
  345. {
  346. if (isSelected)
  347. {
  348. colorBrush = DrawParam.SPDFEditPointBorderBrush;
  349. pen = DrawParam.SPDFEditPointPen;
  350. }
  351. else
  352. {
  353. colorBrush = DrawParam.PDFEditPointBorderBrush;
  354. pen = DrawParam.PDFEditPointPen;
  355. }
  356. }
  357. break;
  358. default:
  359. break;
  360. }
  361. }
  362. private void GetBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
  363. {
  364. switch (selectedType)
  365. {
  366. case SelectedType.None:
  367. break;
  368. case SelectedType.Annot:
  369. if (isHover)
  370. {
  371. colorBrush = DrawParam.AnnotRectFillBrush;
  372. pen = DrawParam.AnnotRectHoverPen;
  373. }
  374. else
  375. {
  376. colorBrush = DrawParam.AnnotRectFillBrush;
  377. pen = DrawParam.AnnotRectLinePen;
  378. }
  379. break;
  380. case SelectedType.PDFEdit:
  381. if (isHover)
  382. {
  383. colorBrush = DrawParam.PDFEditRectFillHoverBrush;
  384. pen = editHoverPen;//DrawParam.PDFEditRectLineHoverPen;
  385. }
  386. else
  387. {
  388. if (isSelected)
  389. {
  390. colorBrush = DrawParam.SPDFEditRectFillBrush;
  391. pen = DrawParam.SPDFEditRectLinePen;
  392. }
  393. else
  394. {
  395. colorBrush = DrawParam.PDFEditRectFillBrush;
  396. //init Color
  397. if (showCreatTextRect)
  398. {
  399. pen = DrawParam.PDFEditRectLinePen;
  400. }
  401. else
  402. {
  403. pen = editPen;
  404. }
  405. // editPen; //editPen;//// DrawParam.PDFEditRectLinePen;
  406. }
  407. }
  408. break;
  409. default:
  410. break;
  411. }
  412. }
  413. public void SetShowCreatTextRect(bool ShowCreatTextRect)
  414. {
  415. showCreatTextRect = ShowCreatTextRect;
  416. }
  417. public void SetEditPen(Pen editPen = null, Pen editHoverPen = null)
  418. {
  419. if (editPen == null)
  420. {
  421. this.editPen = DrawParam.PDFEditRectLinePen;
  422. }
  423. else
  424. {
  425. this.editPen = new Pen(editPen.Brush, editPen.Thickness);
  426. }
  427. if (editHoverPen == null)
  428. {
  429. this.editHoverPen = DrawParam.PDFEditRectLineHoverPen;
  430. }
  431. else
  432. {
  433. this.editHoverPen = editHoverPen;
  434. }
  435. }
  436. public virtual void ClearDraw()
  437. {
  438. SetDrawRect = drawRect = new Rect();
  439. rotateAngle = 0;
  440. drawDC = RenderOpen();
  441. drawDC?.Close();
  442. drawDC = null;
  443. }
  444. /// <summary>
  445. /// Hide the drawing
  446. /// </summary>
  447. public virtual void HideDraw()
  448. {
  449. drawDC = RenderOpen();
  450. drawDC?.Close();
  451. }
  452. public void SetRect(Rect newRect, double zoom)
  453. {
  454. if (newRect == Rect.Empty || newRect == null)
  455. {
  456. return;
  457. }
  458. newRect = new Rect((newRect.X - rectPadding * zoom), (newRect.Y - rectPadding * zoom), (newRect.Width + 2 * rectPadding * zoom), (newRect.Height + 2 * rectPadding * zoom));
  459. currentZoom = zoom;
  460. SetDrawRect = drawRect = newRect;
  461. drawCenterPoint = new Point(drawRect.Left + drawRect.Width / 2, drawRect.Top + drawRect.Height / 2);
  462. }
  463. /// <summary>
  464. /// Get the original set Rect, not the calculated fill
  465. /// </summary>
  466. /// <param name="newRect">
  467. /// The new rect to set
  468. /// </param>
  469. public Rect GetRect()
  470. {
  471. 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));
  472. return rect;
  473. }
  474. public int GetRotation()
  475. {
  476. return rotateAngle;
  477. }
  478. public void SetRotation(int rotationAngle)
  479. {
  480. this.rotateAngle = rotationAngle;
  481. }
  482. public void SetRectPadding(double rectPadding)
  483. {
  484. this.rectPadding = rectPadding;
  485. }
  486. public double GetRectPadding()
  487. {
  488. return rectPadding;
  489. }
  490. public Rect GetDrawRect()
  491. {
  492. return drawRect;
  493. }
  494. /// <summary>
  495. /// Obtain cropped and actual region margin
  496. /// </summary>
  497. /// <returns></returns>
  498. public Thickness GetClipThickness()
  499. {
  500. return clipThickness;
  501. }
  502. /// <summary>
  503. /// Get ClipRect
  504. /// </summary>
  505. /// <returns></returns>
  506. public Rect GetClipRect()
  507. {
  508. Rect drawrect = new Rect(0, 0, 0, 0);
  509. drawrect.X = SetDrawRect.X - clipThickness.Left * currentZoom;
  510. drawrect.Y = SetDrawRect.Y - clipThickness.Top * currentZoom;
  511. drawrect.Width = SetDrawRect.Width - clipThickness.Right * currentZoom + clipThickness.Left * currentZoom;
  512. drawrect.Height = SetDrawRect.Height - clipThickness.Bottom * currentZoom + clipThickness.Top * currentZoom;
  513. return drawrect;
  514. }
  515. /// <summary>
  516. /// Set cropping and actual area margins
  517. /// </summary>
  518. /// <returns></returns>
  519. public void SetClipThickness(Thickness rect)
  520. {
  521. try
  522. {
  523. Rect drawrect = new Rect(0, 0, 0, 0);
  524. drawrect.X = SetDrawRect.X - rect.Left * currentZoom;
  525. drawrect.Y = SetDrawRect.Y - rect.Top * currentZoom;
  526. drawrect.Width = SetDrawRect.Width - rect.Right * currentZoom + rect.Left * currentZoom;
  527. drawrect.Height = SetDrawRect.Height - rect.Bottom * currentZoom + rect.Top * currentZoom;
  528. drawRect = drawrect;
  529. clipThickness = rect;
  530. }
  531. catch { }
  532. }
  533. public void SetMaxRect(Rect rect)
  534. {
  535. maxRect = rect;
  536. }
  537. public Rect GetMaxRect()
  538. {
  539. return maxRect;
  540. }
  541. public void SetAnnotData(AnnotData annotData, CPDFViewer viewer)
  542. {
  543. SetIgnorePoints(new List<PointControlType>());
  544. SetIsProportionalScaling(false);
  545. isProportionalScaling = false;
  546. switch (annotData.AnnotType)
  547. {
  548. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  549. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  550. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  551. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  552. case C_ANNOTATION_TYPE.C_ANNOTATION_RICHMEDIA:
  553. case C_ANNOTATION_TYPE.C_ANNOTATION_MOVIE:
  554. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  555. DisableAll();
  556. break;
  557. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  558. case C_ANNOTATION_TYPE.C_ANNOTATION_SOUND:
  559. SetIgnorePointsAll();
  560. break;
  561. case C_ANNOTATION_TYPE.C_ANNOTATION_STAMP:
  562. SetIsProportionalScaling(true);
  563. break;
  564. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  565. SetIgnorePointsAll();
  566. break;
  567. default:
  568. break;
  569. }
  570. SetMaxRect(annotData.PaintOffset);
  571. if(annotData.AnnotType == C_ANNOTATION_TYPE.C_ANNOTATION_STAMP)
  572. {
  573. CRect sourceRect = new CRect();
  574. annotData.Annot.GetSourceRect(ref sourceRect);
  575. if (!sourceRect.IsEmpty)
  576. {
  577. RenderData renderData = viewer.GetCurrentRenderPageForIndex(annotData.PageIndex);
  578. 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);
  579. Rect rotateRect = zoomRect;
  580. rotateRect.X += renderData.PageBound.X - renderData.CropLeft * annotData.CurrentZoom;
  581. rotateRect.Y += renderData.PageBound.Y - renderData.CropTop * annotData.CurrentZoom;
  582. SetRect(rotateRect, annotData.CurrentZoom);
  583. rotateAngle = -(annotData.Annot as CPDFStampAnnotation).AnnotationRotator.GetRotation();
  584. pageRotation = annotData.Annot.Page.Rotation;
  585. }
  586. else
  587. {
  588. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  589. }
  590. }
  591. else
  592. {
  593. SetRect(annotData.PaintRect, annotData.CurrentZoom);
  594. }
  595. //SetRotation(annotData.Rotation);
  596. if (selectedRectData != null)
  597. {
  598. selectedRectData = new SelectedAnnotData();
  599. }
  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.MiddlBottom);
  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.MiddlBottom);
  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.MiddlBottom);
  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.MiddlBottom)
  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. }