SelectedRect.cs 41 KB

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