SelectedRect.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.Tool.Help;
  4. using ComPDFKit.Tool.SettingParam;
  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 canRotation = 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 CommonHelper.RotationCursor;
  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 canRotation)
  603. {
  604. this.canRotation = canRotation;
  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 (canRotation)
  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 (canRotation)
  862. {
  863. // rotationPoint 围绕centerPoint旋转angle度
  864. 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),
  865. centerPoint.Y + (rotationPoint.X - centerPoint.X) * Math.Sin(rotateAngle * Math.PI / 180) + (rotationPoint.Y - centerPoint.Y) * Math.Cos(rotateAngle * Math.PI / 180));
  866. Vector checkVector1 = point - hitRotationPoint;
  867. if (checkVector1.Length < pointSize)
  868. {
  869. return PointControlType.Rotate;
  870. }
  871. }
  872. break;
  873. case DrawPointType.Crop:
  874. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  875. if (cropRect.Contains(point))
  876. {
  877. return (PointControlType)i;
  878. }
  879. break;
  880. default:
  881. break;
  882. }
  883. }
  884. if (drawRect.Contains(point))
  885. {
  886. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  887. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  888. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  889. if (rect.Contains(point))
  890. {
  891. if (!ignoreList.Contains(PointControlType.Body))
  892. {
  893. return PointControlType.Body;
  894. }
  895. }
  896. if (!ignoreList.Contains(PointControlType.Body))
  897. {
  898. return PointControlType.Line;
  899. }
  900. }
  901. }
  902. return PointControlType.None;
  903. }
  904. /// <summary>
  905. /// The position of the points in the cropping box
  906. /// </summary>
  907. /// <param name="point"></param>
  908. /// <param name="isIgnore"></param>
  909. /// <returns></returns>
  910. public PointControlType GetHitCropControlIndex(Point point, bool isIgnore = true)
  911. {
  912. List<Point> controlCurrentPoints = GetControlPoint(drawRect);
  913. HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
  914. if (hitResult != null && hitResult.VisualHit is DrawingVisual)
  915. {
  916. List<PointControlType> ignoreList = GetIgnorePoints();
  917. List<Point> IgnorePointsList = new List<Point>();
  918. foreach (PointControlType type in ignoreList)
  919. {
  920. if ((int)type < controlCurrentPoints.Count)
  921. {
  922. IgnorePointsList.Add(controlCurrentPoints[(int)type]);
  923. }
  924. }
  925. for (int i = 0; i < controlCurrentPoints.Count; i++)
  926. {
  927. Point checkPoint = controlCurrentPoints[i];
  928. if (isIgnore && IgnorePointsList.Contains(checkPoint))
  929. {
  930. continue;
  931. }
  932. switch (currentDrawPointType)
  933. {
  934. case DrawPointType.Circle:
  935. Vector checkVector = checkPoint - point;
  936. if (checkVector.Length < pointSize)
  937. {
  938. return (PointControlType)i;
  939. }
  940. break;
  941. case DrawPointType.Square:
  942. Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  943. if (checkRect.Contains(point))
  944. {
  945. return (PointControlType)i;
  946. }
  947. break;
  948. case DrawPointType.Crop:
  949. Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
  950. if (cropRect.Contains(point))
  951. {
  952. return (PointControlType)i;
  953. }
  954. break;
  955. default:
  956. break;
  957. }
  958. }
  959. if (drawRect.Contains(point))
  960. {
  961. double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
  962. double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
  963. Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
  964. if (rect.Contains(point))
  965. {
  966. if (!ignoreList.Contains(PointControlType.Body))
  967. {
  968. return PointControlType.Body;
  969. }
  970. }
  971. if (!ignoreList.Contains(PointControlType.Body))
  972. {
  973. return PointControlType.Line;
  974. }
  975. }
  976. }
  977. return PointControlType.None;
  978. }
  979. }
  980. }