SelectedRect.protected.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Media;
  6. namespace ComPDFKit.Tool.DrawTool
  7. {
  8. public partial class SelectedRect
  9. {
  10. #region Properties
  11. /// <summary>
  12. /// Current control point drawing style.
  13. /// </summary>
  14. protected DrawPointType currentDrawPointType
  15. {
  16. get;
  17. set;
  18. }
  19. /// <summary>
  20. /// Current drag drawing style.
  21. /// </summary>
  22. protected DrawMoveType currentDrawMoveType { get; set; }
  23. /// <summary>
  24. /// Current click hit control point.
  25. /// </summary>
  26. protected PointControlType hitControlType
  27. {
  28. get;
  29. set;
  30. }
  31. /// <summary>
  32. /// Mouse down position information.
  33. /// </summary>
  34. protected Point mouseDownPoint { get; set; }
  35. /// <summary>
  36. /// Whether the mouse is pressed.
  37. /// </summary>
  38. protected bool isMouseDown { get; set; }
  39. protected bool isInRotate { get; set; } = false;
  40. protected bool isInScaling { get; set; } = false;
  41. /// <summary>
  42. /// Whether proportional scaling is required.
  43. /// </summary>
  44. protected bool isProportionalScaling { get; set; } = false;
  45. /// <summary>
  46. /// Current control point size.
  47. /// </summary>
  48. protected int pointSize { get; set; } = 4;
  49. /// <summary>
  50. /// Rectangular minimum width.
  51. /// </summary>
  52. public int RectMinWidth { get; set; } = 10;
  53. /// <summary>
  54. /// Rectangular minimum height.
  55. /// </summary>
  56. public int RectMinHeight { get; set; } = 10;
  57. /// <summary>
  58. /// Current set of ignore points.
  59. /// </summary>
  60. protected List<PointControlType> ignorePoints { get; set; } = new List<PointControlType>();
  61. /// <summary>
  62. /// Current set of drawing rectangles (original data).
  63. /// </summary>
  64. protected Rect SetDrawRect
  65. {
  66. get;
  67. set;
  68. } = new Rect(0, 0, 0, 0);
  69. /// <summary>
  70. /// Current drawing rectangle (calculated during operation).
  71. /// </summary>
  72. protected Rect drawRect
  73. {
  74. get;
  75. set;
  76. } = new Rect(0, 0, 0, 0);
  77. /// <summary>
  78. /// Maximum range that can be drawn.
  79. /// </summary>
  80. protected Rect maxRect { get; set; } = new Rect(0, 0, 0, 0);
  81. /// <summary>
  82. /// Current center point of the drawing rectangle.
  83. /// </summary>
  84. protected Point drawCenterPoint { get; private set; } = new Point(0, 0);
  85. /// <summary>
  86. /// When the mouse is pressed, the cached rectangle.
  87. /// </summary>
  88. protected Rect cacheRect { get; set; } = new Rect(0, 0, 0, 0);
  89. /// <summary>
  90. /// Current control point coordinates.
  91. /// </summary>
  92. protected List<Point> controlPoints { get; set; } = new List<Point>();
  93. protected Point centerPoint = new Point();
  94. protected Point rotationPoint = new Point();
  95. protected Point dragRotationPoint = new Point();
  96. /// <summary>
  97. /// Move offset during movement.
  98. /// </summary>
  99. protected Point moveOffset { get; set; } = new Point(0, 0);
  100. /// <summary>
  101. /// Current drawing rectangle (calculated during operation).
  102. /// </summary>
  103. protected Thickness clipThickness = new Thickness(0, 0, 0, 0);
  104. private Pen editPen { get; set; } = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2) { DashStyle = DashStyles.Dash };
  105. private Pen editHoverPen { get; set; } = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2) { DashStyle = DashStyles.Dash };
  106. private bool showCreatTextRect = false;
  107. protected bool isOutSideScaling = false;
  108. /// <summary>
  109. /// Current actual display width and height of PDFVIewer.
  110. /// </summary>
  111. protected double PDFViewerActualWidth { get; set; } = 0;
  112. protected double PDFViewerActualHeight { get; set; } = 0;
  113. protected int rotateAngle { get; set; } = 0;
  114. protected int pageRotation { get; set; } = 0;
  115. protected double rectPadding = 6;
  116. protected double currentZoom = 1;
  117. protected SelectedAnnotData selectedRectData = new SelectedAnnotData();
  118. protected bool disable = false;
  119. protected List<Point> rotateControlPoints = new List<Point>();
  120. protected Rect rotateRect = new Rect();
  121. #endregion
  122. #region Functions
  123. /// <summary>
  124. /// Calcuate the control points
  125. /// </summary>
  126. /// <param name="currentRect">
  127. /// Control points in the target rectangle
  128. /// </param>
  129. protected void CalcControlPoint(Rect currentRect)
  130. {
  131. controlPoints.Clear();
  132. centerPoint.X = (int)(currentRect.Left + currentRect.Right) / 2;
  133. centerPoint.Y = (int)(currentRect.Top + currentRect.Bottom) / 2;
  134. controlPoints.Add(new Point(currentRect.Left, currentRect.Top));
  135. controlPoints.Add(new Point(currentRect.Left, centerPoint.Y));
  136. controlPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  137. controlPoints.Add(new Point(centerPoint.X, currentRect.Bottom));
  138. controlPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  139. controlPoints.Add(new Point(currentRect.Right, centerPoint.Y));
  140. controlPoints.Add(new Point(currentRect.Right, currentRect.Top));
  141. controlPoints.Add(new Point(centerPoint.X, currentRect.Top));
  142. if (canRotate)
  143. {
  144. rotationPoint = new Point(centerPoint.X, currentRect.Top - 30);
  145. switch (pageRotation)
  146. {
  147. case 0:
  148. rotationPoint = new Point(centerPoint.X, currentRect.Top - 30);
  149. break;
  150. case 1:
  151. rotationPoint = new Point(currentRect.Right + 30, centerPoint.Y);
  152. break;
  153. case 2:
  154. rotationPoint = new Point(centerPoint.X, currentRect.Bottom + 30);
  155. break;
  156. case 3:
  157. rotationPoint = new Point(currentRect.Left - 30, centerPoint.Y);
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. }
  164. protected List<Point> GetControlPoint(Rect currentRect)
  165. {
  166. List<Point> controlCurrentPoints = new List<Point>();
  167. controlCurrentPoints.Clear();
  168. int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
  169. int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
  170. controlCurrentPoints.Add(new Point(currentRect.Left, currentRect.Top));
  171. controlCurrentPoints.Add(new Point(currentRect.Left, centerY));
  172. controlCurrentPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  173. controlCurrentPoints.Add(new Point(centerX, currentRect.Bottom));
  174. controlCurrentPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  175. controlCurrentPoints.Add(new Point(currentRect.Right, centerY));
  176. controlCurrentPoints.Add(new Point(currentRect.Right, currentRect.Top));
  177. controlCurrentPoints.Add(new Point(centerX, currentRect.Top));
  178. return controlCurrentPoints;
  179. }
  180. /// <summary>
  181. /// Calcuate the offset of the current rectangle in the maximum rectangle range
  182. /// </summary>
  183. /// <param name="currentRect">
  184. /// The rectangle cached when pressed
  185. /// </param>
  186. /// <param name="offsetPoint">
  187. /// Equivalent to the offset value when pressed
  188. /// </param>
  189. /// <param name="maxRect">
  190. /// The maximum rectangle range.
  191. /// </param>
  192. /// <returns></returns>
  193. protected Point CalcMoveBound(Rect currentRect, Point offsetPoint, Rect maxRect)
  194. {
  195. double cLeft = currentRect.Left;
  196. double cRight = currentRect.Right;
  197. double cUp = currentRect.Top;
  198. double cDown = currentRect.Bottom;
  199. double TmpLeft = cLeft + offsetPoint.X;
  200. double TmpRight = cRight + offsetPoint.X;
  201. double TmpUp = cUp + offsetPoint.Y;
  202. double TmpDown = cDown + offsetPoint.Y;
  203. if (TmpLeft < maxRect.Left)
  204. {
  205. TmpRight = (cRight - cLeft) + maxRect.Left;
  206. TmpLeft = maxRect.Left;
  207. }
  208. if (TmpUp < maxRect.Top)
  209. {
  210. TmpDown = (cDown - cUp) + maxRect.Top;
  211. TmpUp = maxRect.Top;
  212. }
  213. if (TmpRight > maxRect.Right)
  214. {
  215. TmpLeft = maxRect.Right - (cRight - cLeft);
  216. TmpRight = maxRect.Right;
  217. }
  218. if (TmpDown > maxRect.Bottom)
  219. {
  220. TmpUp = maxRect.Bottom - (cDown - cUp);
  221. TmpDown = maxRect.Bottom;
  222. }
  223. offsetPoint = new Point(TmpLeft - cLeft, TmpUp - cUp);
  224. return offsetPoint;
  225. }
  226. /// <summary>
  227. /// Calculate the movement of the hit point
  228. /// </summary>
  229. /// <param name="mousePoint">
  230. /// Current mouse position
  231. /// </param>
  232. /// <returns>
  233. /// Whether the movement is successful
  234. /// </returns>
  235. protected bool CalcHitPointMove(Point mousePoint)
  236. {
  237. if (isMouseDown == false || hitControlType == PointControlType.None)
  238. {
  239. return false;
  240. }
  241. if (hitControlType == PointControlType.Rotate)
  242. {
  243. SetRotateByMousePoint(mousePoint);
  244. return false;
  245. }
  246. if (!isOutSideScaling)
  247. {
  248. if (selectedRectData.rotationAngle != 0)
  249. {
  250. return RotateScaling(mousePoint);
  251. }
  252. else
  253. {
  254. return NormalScaling(mousePoint);
  255. }
  256. }
  257. else
  258. {
  259. return OutSideScaling(mousePoint);
  260. }
  261. }
  262. public void SetOutSideScaling(bool IsOutSideScaling)
  263. {
  264. isOutSideScaling = IsOutSideScaling;
  265. }
  266. private Size GetProportionalScalingSize(double width, double height)
  267. {
  268. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  269. double minWidth = RectMinWidth + 2 * rectPadding * currentZoom;
  270. if (minWidth > width || minHeight > height)
  271. {
  272. if (cacheRect.Width >= cacheRect.Height)
  273. {
  274. width = cacheRect.Width * minHeight / cacheRect.Height;
  275. height = minHeight;
  276. }
  277. else
  278. {
  279. height = cacheRect.Height * minWidth / cacheRect.Width;
  280. width = minWidth;
  281. }
  282. }
  283. return new Size(width, height);
  284. }
  285. private void SetRotateByMousePoint(Point mousePoint)
  286. {
  287. dragRotationPoint = mousePoint;
  288. Vector moveVector = (mousePoint - centerPoint);
  289. rotateAngle = (int)(Math.Atan2(moveVector.X, -moveVector.Y) * 180 / Math.PI) - pageRotation * 90;
  290. }
  291. /// <summary>
  292. /// Draw the algorithm in the form of normal scaling (drag a point, only scale in one direction).
  293. /// </summary>
  294. /// <param name="mousePoint">Current mouse position.</param>
  295. /// <returns></returns>
  296. protected bool NormalScaling(Point mousePoint)
  297. {
  298. try
  299. {
  300. double left = 0, right = 0, top = 0, bottom = 0;
  301. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  302. double minWidth = RectMinWidth + 2 * rectPadding * currentZoom;
  303. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  304. Point moveVector = (Point)(mousePoint - centerPoint);
  305. moveVector = ProportionalScalingOffsetPos(moveVector);
  306. switch (hitControlType)
  307. {
  308. case PointControlType.LeftTop:
  309. {
  310. left = centerPoint.X + moveVector.X;
  311. right = cacheRect.Right;
  312. top = centerPoint.Y + moveVector.Y;
  313. bottom = cacheRect.Bottom;
  314. if (isProportionalScaling)
  315. {
  316. Size size = GetProportionalScalingSize(right - left, bottom - top);
  317. left = right - size.Width;
  318. top = bottom - size.Height;
  319. if (left < maxRect.Left)
  320. {
  321. double tmpWidth = right - left;
  322. left = maxRect.Left;
  323. double width = right - left;
  324. double height = (bottom - top) * width / tmpWidth;
  325. top = bottom - height;
  326. }
  327. if (top < maxRect.Top)
  328. {
  329. double tmpHeight = bottom - top;
  330. top = maxRect.Top;
  331. double height = bottom - top;
  332. double width = (right - left) * height / tmpHeight;
  333. left = right - width;
  334. }
  335. }
  336. else
  337. {
  338. if (left + minWidth > right)
  339. {
  340. left = right - minWidth;
  341. }
  342. if (top + minHeight > bottom)
  343. {
  344. top = bottom - minHeight;
  345. }
  346. }
  347. }
  348. break;
  349. case PointControlType.LeftMiddle:
  350. {
  351. left = centerPoint.X + moveVector.X;
  352. right = cacheRect.Right;
  353. top = cacheRect.Top;
  354. bottom = cacheRect.Bottom;
  355. if (left + minWidth > right)
  356. {
  357. left = right - minWidth;
  358. }
  359. }
  360. break;
  361. case PointControlType.LeftBottom:
  362. {
  363. left = centerPoint.X + moveVector.X;
  364. right = cacheRect.Right;
  365. top = cacheRect.Top;
  366. bottom = centerPoint.Y + moveVector.Y;
  367. if (isProportionalScaling)
  368. {
  369. Size size = GetProportionalScalingSize(right - left, bottom - top);
  370. left = right - size.Width;
  371. bottom = top + size.Height;
  372. if (left < maxRect.Left)
  373. {
  374. double tmpWidth = right - left;
  375. left = maxRect.Left;
  376. double width = right - left;
  377. double height = (bottom - top) * width / tmpWidth;
  378. bottom = top + height;
  379. }
  380. if (bottom > maxRect.Bottom)
  381. {
  382. double tmpHeight = bottom - top;
  383. bottom = maxRect.Bottom;
  384. double height = bottom - top;
  385. double width = (right - left) * height / tmpHeight;
  386. left = right - width;
  387. }
  388. }
  389. else
  390. {
  391. if (left + minWidth > right)
  392. {
  393. left = right - minWidth;
  394. }
  395. if (top + minHeight > bottom)
  396. {
  397. bottom = top + minHeight;
  398. }
  399. }
  400. }
  401. break;
  402. case PointControlType.MiddleBottom:
  403. {
  404. left = cacheRect.Left;
  405. right = cacheRect.Right;
  406. top = cacheRect.Top;
  407. bottom = centerPoint.Y + moveVector.Y;
  408. if (top + minHeight > bottom)
  409. {
  410. bottom = top + minHeight;
  411. }
  412. }
  413. break;
  414. case PointControlType.RightBottom:
  415. {
  416. left = cacheRect.Left;
  417. right = centerPoint.X + moveVector.X;
  418. top = cacheRect.Top;
  419. bottom = centerPoint.Y + moveVector.Y;
  420. if (isProportionalScaling)
  421. {
  422. Size size = GetProportionalScalingSize(right - left, bottom - top);
  423. right = left + size.Width;
  424. bottom = top + size.Height;
  425. if (right > maxRect.Right)
  426. {
  427. double tmpWidth = right - left;
  428. right = maxRect.Right;
  429. double width = right - left;
  430. double height = (bottom - top) * width / tmpWidth;
  431. bottom = top + height;
  432. }
  433. if (bottom > maxRect.Bottom)
  434. {
  435. double tmpHeight = bottom - top;
  436. bottom = maxRect.Bottom;
  437. double height = bottom - top;
  438. double width = (right - left) * height / tmpHeight;
  439. right = left + width;
  440. }
  441. }
  442. else
  443. {
  444. if (left + minWidth > right)
  445. {
  446. right = left + minWidth;
  447. }
  448. if (top + minHeight > bottom)
  449. {
  450. bottom = top + minHeight;
  451. }
  452. }
  453. }
  454. break;
  455. case PointControlType.RightMiddle:
  456. {
  457. left = cacheRect.Left;
  458. right = centerPoint.X + moveVector.X;
  459. top = cacheRect.Top;
  460. bottom = cacheRect.Bottom;
  461. if (left + minWidth > right)
  462. {
  463. right = left + minWidth;
  464. }
  465. }
  466. break;
  467. case PointControlType.RightTop:
  468. {
  469. left = cacheRect.Left;
  470. right = centerPoint.X + moveVector.X;
  471. top = centerPoint.Y + moveVector.Y;
  472. bottom = cacheRect.Bottom;
  473. if (isProportionalScaling)
  474. {
  475. Size size = GetProportionalScalingSize(right - left, bottom - top);
  476. right = left + size.Width;
  477. top = bottom - size.Height;
  478. if (right > maxRect.Right)
  479. {
  480. double tmpWidth = right - left;
  481. right = maxRect.Right;
  482. double width = right - left;
  483. double height = (bottom - top) * width / tmpWidth;
  484. top = bottom - height;
  485. }
  486. if (top < maxRect.Top)
  487. {
  488. double tmpHeight = bottom - top;
  489. top = maxRect.Top;
  490. double height = bottom - top;
  491. double width = (right - left) * height / tmpHeight;
  492. right = left + width;
  493. }
  494. }
  495. else
  496. {
  497. if (left + minWidth > right)
  498. {
  499. right = left + minWidth;
  500. }
  501. if (top + minHeight > bottom)
  502. {
  503. top = bottom - minHeight;
  504. }
  505. }
  506. }
  507. break;
  508. case PointControlType.MiddleTop:
  509. {
  510. left = cacheRect.Left;
  511. right = cacheRect.Right;
  512. top = centerPoint.Y + moveVector.Y;
  513. bottom = cacheRect.Bottom;
  514. if (top + minHeight > bottom)
  515. {
  516. top = bottom - minHeight;
  517. }
  518. }
  519. break;
  520. case PointControlType.Body:
  521. case PointControlType.Line:
  522. {
  523. Point offsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), maxRect);
  524. left = cacheRect.Left + offsetPos.X;
  525. right = cacheRect.Right + offsetPos.X;
  526. top = cacheRect.Top + offsetPos.Y;
  527. bottom = cacheRect.Bottom + offsetPos.Y;
  528. }
  529. break;
  530. default:
  531. break;
  532. }
  533. if (left < maxRect.Left)
  534. {
  535. left = maxRect.Left;
  536. }
  537. if (top < maxRect.Top)
  538. {
  539. top = maxRect.Top;
  540. }
  541. if (right > maxRect.Right)
  542. {
  543. right = maxRect.Right;
  544. }
  545. if (bottom > maxRect.Bottom)
  546. {
  547. bottom = maxRect.Bottom;
  548. }
  549. drawRect = new Rect(left, top, right - left, bottom - top);
  550. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  551. return true;
  552. }
  553. catch (Exception ex)
  554. {
  555. }
  556. return false;
  557. }
  558. protected bool RotateScaling(Point mouseMovePoint)
  559. {
  560. Point rotatePoint = new Point();
  561. Point hitControlUIPos = new Point();
  562. if (hitControlType < PointControlType.Body)
  563. {
  564. hitControlUIPos = rotateControlPoints[(int)hitControlType];
  565. }
  566. hitControlUIPos = GetRotateUIPoint(hitControlUIPos);
  567. Point centerPoint = new Point((rotateRect.Left + rotateRect.Right) / 2, (rotateRect.Top + rotateRect.Bottom) / 2);
  568. Vector moveVector = mouseMovePoint - mouseDownPoint;
  569. Vector hitVector = hitControlUIPos - centerPoint;
  570. Rect tmpRect = cacheRect;
  571. if (hitControlType == PointControlType.LeftTop
  572. || hitControlType == PointControlType.LeftBottom
  573. || hitControlType == PointControlType.RightTop
  574. || hitControlType == PointControlType.RightBottom)
  575. {
  576. if (isProportionalScaling)
  577. {
  578. double vectorAngle = Vector.AngleBetween(moveVector, hitVector);
  579. double newLenght = Math.Cos(Math.PI / 180.0 * vectorAngle) * moveVector.Length;
  580. hitVector.Normalize();
  581. hitVector.X *= newLenght;
  582. hitVector.Y *= newLenght;
  583. rotatePoint = new Point(hitControlUIPos.X + hitVector.X, hitControlUIPos.Y + hitVector.Y);
  584. }
  585. }
  586. switch (hitControlType)
  587. {
  588. case PointControlType.LeftTop:
  589. {
  590. Point rightBottomPoint = rotateControlPoints[(int)PointControlType.RightBottom];
  591. Point rightBottomUIPos = GetRotateUIPoint(rightBottomPoint);
  592. centerPoint = new Point((rotatePoint.X + rightBottomUIPos.X) / 2, (rotatePoint.Y + rightBottomUIPos.Y) / 2);
  593. Matrix rotateMatrix = new Matrix();
  594. rotateMatrix.RotateAt(-rotateAngle, centerPoint.X, centerPoint.Y);
  595. Point leftTopPoint = rotateMatrix.Transform(rotatePoint);
  596. rightBottomPoint = rotateMatrix.Transform(rightBottomUIPos);
  597. tmpRect = new Rect(leftTopPoint, rightBottomPoint);
  598. }
  599. break;
  600. case PointControlType.LeftBottom:
  601. {
  602. Point rightTopPoint = rotateControlPoints[(int)PointControlType.RightTop];
  603. Point rightTopUIPos = GetRotateUIPoint(rightTopPoint);
  604. centerPoint = new Point((rotatePoint.X + rightTopUIPos.X) / 2, (rotatePoint.Y + rightTopUIPos.Y) / 2);
  605. Matrix rotateMatrix = new Matrix();
  606. rotateMatrix.RotateAt(-rotateAngle, centerPoint.X, centerPoint.Y);
  607. Point leftBottomPoint = rotateMatrix.Transform(rotatePoint);
  608. rightTopPoint = rotateMatrix.Transform(rightTopUIPos);
  609. tmpRect = new Rect(leftBottomPoint, rightTopPoint);
  610. }
  611. break;
  612. case PointControlType.RightTop:
  613. {
  614. Point leftBottomPoint = rotateControlPoints[(int)PointControlType.LeftBottom];
  615. Point leftBottomUIPos = GetRotateUIPoint(leftBottomPoint);
  616. centerPoint = new Point((rotatePoint.X + leftBottomUIPos.X) / 2, (rotatePoint.Y + leftBottomUIPos.Y) / 2);
  617. Matrix rotateMatrix = new Matrix();
  618. rotateMatrix.RotateAt(-rotateAngle, centerPoint.X, centerPoint.Y);
  619. Point rightTopPoint = rotateMatrix.Transform(rotatePoint);
  620. leftBottomPoint = rotateMatrix.Transform(leftBottomUIPos);
  621. tmpRect = new Rect(leftBottomPoint, rightTopPoint);
  622. }
  623. break;
  624. case PointControlType.RightBottom:
  625. {
  626. Point leftTopPoint = rotateControlPoints[(int)PointControlType.LeftTop];
  627. Point leftTopUIPos = GetRotateUIPoint(leftTopPoint);
  628. centerPoint = new Point((rotatePoint.X + leftTopUIPos.X) / 2, (rotatePoint.Y + leftTopUIPos.Y) / 2);
  629. Matrix rotateMatrix = new Matrix();
  630. rotateMatrix.RotateAt(-rotateAngle, centerPoint.X, centerPoint.Y);
  631. Point rightBottomPoint = rotateMatrix.Transform(rotatePoint);
  632. leftTopPoint = rotateMatrix.Transform(leftTopUIPos);
  633. tmpRect = new Rect(leftTopPoint, rightBottomPoint);
  634. }
  635. break;
  636. case PointControlType.Body:
  637. case PointControlType.Line:
  638. {
  639. Point offsetPos = (Point)(mouseMovePoint - mouseDownPoint);
  640. double left = cacheRect.Left + offsetPos.X;
  641. double right = cacheRect.Right + offsetPos.X;
  642. double top = cacheRect.Top + offsetPos.Y;
  643. double bottom = cacheRect.Bottom + offsetPos.Y;
  644. tmpRect = new Rect(new Point(left,top), new Point(right,bottom));
  645. }
  646. break;
  647. default:
  648. break;
  649. }
  650. List<Point> tempPoints = new List<Point>
  651. {
  652. new Point(tmpRect.Left, tmpRect.Top),
  653. new Point(tmpRect.Right, tmpRect.Top),
  654. new Point(tmpRect.Right, tmpRect.Bottom),
  655. new Point(tmpRect.Left, tmpRect.Bottom)
  656. };
  657. List<Point> boundPoint = new List<Point>();
  658. Point center = new Point((tmpRect.Left + tmpRect.Right) / 2, (tmpRect.Top + tmpRect.Bottom) / 2);
  659. foreach (Point point in tempPoints)
  660. {
  661. float x = (float)(center.X + (point.X - center.X) * Math.Cos(rotateAngle * Math.PI / 180) - (point.Y - center.Y) * Math.Sin(rotateAngle * Math.PI / 180));
  662. float y = (float)(center.Y + (point.X - center.X) * Math.Sin(rotateAngle * Math.PI / 180) + (point.Y - center.Y) * Math.Cos(rotateAngle * Math.PI / 180));
  663. boundPoint.Add(new Point(x, y));
  664. }
  665. Rect boundRect = new Rect(new Point(boundPoint.Min(p => p.X), boundPoint.Min(p => p.Y)), new Point(boundPoint.Max(p => p.X), boundPoint.Max(p => p.Y)));
  666. if (maxRect.Contains(boundRect))
  667. {
  668. drawRect = tmpRect;
  669. }
  670. else
  671. {
  672. if(hitControlType == PointControlType.Body || hitControlType == PointControlType.Line)
  673. {
  674. Point boundRectCenterPos = new Point((boundRect.Left + boundRect.Right) / 2, (boundRect.Top + boundRect.Bottom) / 2);
  675. Point maxRectCenterPos = new Point((maxRect.Left + maxRect.Right) / 2, (maxRect.Top + maxRect.Bottom) / 2);
  676. Vector moveCenterVector = boundRectCenterPos - maxRectCenterPos;
  677. Point moveOffsetPos = new Point(0, 0);
  678. if (Math.Abs(moveCenterVector.X) > (maxRect.Width - boundRect.Width) / 2)
  679. {
  680. double moveLength = maxRectCenterPos.X - boundRectCenterPos.X;
  681. moveOffsetPos.X = Math.Abs(moveLength) - (maxRect.Width - boundRect.Width) / 2;
  682. if (moveLength < 0)
  683. {
  684. moveOffsetPos.X *= -1;
  685. }
  686. }
  687. if (Math.Abs(moveCenterVector.Y) > (maxRect.Height - boundRect.Height) / 2)
  688. {
  689. double moveLength = maxRectCenterPos.Y - boundRectCenterPos.Y;
  690. moveOffsetPos.Y = Math.Abs(moveLength) - (maxRect.Height - boundRect.Height) / 2;
  691. if (moveLength < 0)
  692. {
  693. moveOffsetPos.Y *= -1;
  694. }
  695. }
  696. drawRect = new Rect(tmpRect.Left + moveOffsetPos.X, tmpRect.Top + moveOffsetPos.Y, tmpRect.Width, tmpRect.Height);
  697. }
  698. }
  699. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  700. return true;
  701. }
  702. private Point GetRotateUIPoint(Point point)
  703. {
  704. Point centerPoint = new Point((rotateRect.Left + rotateRect.Right) / 2, (rotateRect.Top + rotateRect.Bottom) / 2);
  705. Matrix rotateMatrix = new Matrix();
  706. rotateMatrix.RotateAt(rotateAngle, centerPoint.X, centerPoint.Y);
  707. return rotateMatrix.Transform(point);
  708. }
  709. /// <summary>
  710. /// Provisional logic, to be further improved, not yet used: Draw the algorithm in the form of normal scaling (drag a point, only scale in one direction).
  711. /// </summary>
  712. /// <param name="mousePoint">Current mouse position.</param>
  713. /// <returns></returns>
  714. protected bool OutSideScaling(Point mousePoint)
  715. {
  716. try
  717. {
  718. double left = 0, right = 0, top = 0, bottom = 0;
  719. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  720. double minWidth = RectMinWidth + 2 * rectPadding * currentZoom;
  721. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  722. Point moveVector = (Point)(mousePoint - centerPoint);
  723. moveVector = ProportionalScalingOffsetPos(moveVector);
  724. switch (hitControlType)
  725. {
  726. case PointControlType.LeftTop:
  727. {
  728. left = centerPoint.X + moveVector.X;
  729. right = cacheRect.Right;
  730. top = centerPoint.Y + moveVector.Y;
  731. bottom = cacheRect.Bottom;
  732. if (isProportionalScaling)
  733. {
  734. Size size = GetProportionalScalingSize(right - left, bottom - top);
  735. left = right - size.Width;
  736. top = bottom - size.Height;
  737. if (left < maxRect.Left)
  738. {
  739. double tmpWidth = right - left;
  740. left = maxRect.Left;
  741. double width = right - left;
  742. double height = (bottom - top) * width / tmpWidth;
  743. top = bottom - height;
  744. }
  745. if (top < maxRect.Top)
  746. {
  747. double tmpHeight = bottom - top;
  748. top = maxRect.Top;
  749. double height = bottom - top;
  750. double width = (right - left) * height / tmpHeight;
  751. left = right - width;
  752. }
  753. }
  754. else
  755. {
  756. if (left + minWidth > right)
  757. {
  758. left = right - minWidth;
  759. }
  760. if (top + minHeight > bottom)
  761. {
  762. top = bottom - minHeight;
  763. }
  764. }
  765. }
  766. break;
  767. case PointControlType.LeftMiddle:
  768. {
  769. left = centerPoint.X + moveVector.X;
  770. right = cacheRect.Right;
  771. top = cacheRect.Top;
  772. bottom = cacheRect.Bottom;
  773. if (left + minWidth > right)
  774. {
  775. left = right - minWidth;
  776. }
  777. }
  778. break;
  779. case PointControlType.LeftBottom:
  780. {
  781. left = centerPoint.X + moveVector.X;
  782. right = cacheRect.Right;
  783. top = cacheRect.Top;
  784. bottom = centerPoint.Y + moveVector.Y;
  785. if (isProportionalScaling)
  786. {
  787. Size size = GetProportionalScalingSize(right - left, bottom - top);
  788. left = right - size.Width;
  789. bottom = top + size.Height;
  790. if (left < maxRect.Left)
  791. {
  792. double tmpWidth = right - left;
  793. left = maxRect.Left;
  794. double width = right - left;
  795. double height = (bottom - top) * width / tmpWidth;
  796. bottom = top + height;
  797. }
  798. if (bottom > maxRect.Bottom)
  799. {
  800. double tmpHeight = bottom - top;
  801. bottom = maxRect.Bottom;
  802. double height = bottom - top;
  803. double width = (right - left) * height / tmpHeight;
  804. left = right - width;
  805. }
  806. }
  807. else
  808. {
  809. if (left + minWidth > right)
  810. {
  811. left = right - minWidth;
  812. }
  813. if (top + minHeight > bottom)
  814. {
  815. bottom = top + minHeight;
  816. }
  817. }
  818. }
  819. break;
  820. case PointControlType.MiddleBottom:
  821. {
  822. left = cacheRect.Left;
  823. right = cacheRect.Right;
  824. top = cacheRect.Top;
  825. bottom = centerPoint.Y + moveVector.Y;
  826. if (top + minHeight > bottom)
  827. {
  828. bottom = top + minHeight;
  829. }
  830. }
  831. break;
  832. case PointControlType.RightBottom:
  833. {
  834. left = cacheRect.Left;
  835. right = centerPoint.X + moveVector.X;
  836. top = cacheRect.Top;
  837. bottom = centerPoint.Y + moveVector.Y;
  838. if (isProportionalScaling)
  839. {
  840. Size size = GetProportionalScalingSize(right - left, bottom - top);
  841. right = left + size.Width;
  842. bottom = top + size.Height;
  843. if (right > maxRect.Right)
  844. {
  845. double tmpWidth = right - left;
  846. right = maxRect.Right;
  847. double width = right - left;
  848. double height = (bottom - top) * width / tmpWidth;
  849. bottom = top + height;
  850. }
  851. if (bottom > maxRect.Bottom)
  852. {
  853. double tmpHeight = bottom - top;
  854. bottom = maxRect.Bottom;
  855. double height = bottom - top;
  856. double width = (right - left) * height / tmpHeight;
  857. right = left + width;
  858. }
  859. }
  860. else
  861. {
  862. if (left + minWidth > right)
  863. {
  864. right = left + minWidth;
  865. }
  866. if (top + minHeight > bottom)
  867. {
  868. bottom = top + minHeight;
  869. }
  870. }
  871. }
  872. break;
  873. case PointControlType.RightMiddle:
  874. {
  875. left = cacheRect.Left;
  876. right = centerPoint.X + moveVector.X;
  877. top = cacheRect.Top;
  878. bottom = cacheRect.Bottom;
  879. if (left + minWidth > right)
  880. {
  881. right = left + minWidth;
  882. }
  883. }
  884. break;
  885. case PointControlType.RightTop:
  886. {
  887. left = cacheRect.Left;
  888. right = centerPoint.X + moveVector.X;
  889. top = centerPoint.Y + moveVector.Y;
  890. bottom = cacheRect.Bottom;
  891. if (isProportionalScaling)
  892. {
  893. Size size = GetProportionalScalingSize(right - left, bottom - top);
  894. right = left + size.Width;
  895. top = bottom - size.Height;
  896. if (right > maxRect.Right)
  897. {
  898. double tmpWidth = right - left;
  899. right = maxRect.Right;
  900. double width = right - left;
  901. double height = (bottom - top) * width / tmpWidth;
  902. top = bottom - height;
  903. }
  904. if (top < maxRect.Top)
  905. {
  906. double tmpHeight = bottom - top;
  907. top = maxRect.Top;
  908. double height = bottom - top;
  909. double width = (right - left) * height / tmpHeight;
  910. right = left + width;
  911. }
  912. }
  913. else
  914. {
  915. if (left + minWidth > right)
  916. {
  917. right = left + minWidth;
  918. }
  919. if (top + minHeight > bottom)
  920. {
  921. top = bottom - minHeight;
  922. }
  923. }
  924. }
  925. break;
  926. case PointControlType.MiddleTop:
  927. {
  928. left = cacheRect.Left;
  929. right = cacheRect.Right;
  930. top = centerPoint.Y + moveVector.Y;
  931. bottom = cacheRect.Bottom;
  932. if (top + minHeight > bottom)
  933. {
  934. top = bottom - minHeight;
  935. }
  936. }
  937. break;
  938. case PointControlType.Body:
  939. case PointControlType.Line:
  940. {
  941. double newleft = maxRect.Left - SetDrawRect.Width + 10;
  942. double newright = maxRect.Right + SetDrawRect.Width - 10;
  943. double newtop = maxRect.Top - SetDrawRect.Height + 10;
  944. double newbottom = maxRect.Bottom + SetDrawRect.Height - 10;
  945. if (newleft < 0)
  946. {
  947. newleft = 0;
  948. }
  949. Rect newMaxRect = new Rect(newleft, newtop, newright - newleft, newbottom - newtop);
  950. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), newMaxRect);
  951. left = cacheRect.Left + OffsetPos.X;
  952. right = cacheRect.Right + OffsetPos.X;
  953. top = cacheRect.Top + OffsetPos.Y;
  954. bottom = cacheRect.Bottom + OffsetPos.Y;
  955. }
  956. break;
  957. default:
  958. break;
  959. }
  960. //if (left < maxRect.Left)
  961. //{
  962. // left = maxRect.Left;
  963. //}
  964. //if (top < maxRect.Top)
  965. //{
  966. // top = maxRect.Top;
  967. //}
  968. if (right > maxRect.Right + SetDrawRect.Width - 10)
  969. {
  970. if (left > maxRect.Right - 10)
  971. {
  972. left = maxRect.Right - 10;
  973. }
  974. right = maxRect.Right + SetDrawRect.Width - 10;
  975. }
  976. if (bottom > maxRect.Bottom + SetDrawRect.Height - 10)
  977. {
  978. if (top > maxRect.Bottom - 10)
  979. {
  980. top = maxRect.Bottom - 10;
  981. }
  982. bottom = maxRect.Bottom + SetDrawRect.Height - 10;
  983. }
  984. drawRect = new Rect(left, top, right - left, bottom - top);
  985. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  986. return true;
  987. }
  988. catch (Exception ex)
  989. {
  990. }
  991. return false;
  992. }
  993. /// <summary>
  994. /// Proportional scaling offset calibration
  995. /// </summary>
  996. /// <param name="movePoint">
  997. /// The current movement point
  998. /// </param>
  999. /// <returns>
  1000. /// The offset point after the proportional scaling
  1001. /// </returns>
  1002. protected Point ProportionalScalingOffsetPos(Point movePoint)
  1003. {
  1004. if (isProportionalScaling)
  1005. {
  1006. Point offsetPos = movePoint;
  1007. double ratioX = cacheRect.Width > 0 ? cacheRect.Height / cacheRect.Width : 1;
  1008. double ratioY = cacheRect.Height > 0 ? cacheRect.Width / cacheRect.Height : 1;
  1009. switch (hitControlType)
  1010. {
  1011. case PointControlType.LeftTop:
  1012. case PointControlType.RightBottom:
  1013. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  1014. break;
  1015. case PointControlType.LeftBottom:
  1016. case PointControlType.RightTop:
  1017. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  1018. break;
  1019. case PointControlType.LeftMiddle:
  1020. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  1021. break;
  1022. case PointControlType.RightMiddle:
  1023. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  1024. break;
  1025. case PointControlType.MiddleBottom:
  1026. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? 1 : -1), movePoint.Y);
  1027. break;
  1028. case PointControlType.MiddleTop:
  1029. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? -1 : 1), movePoint.Y);
  1030. break;
  1031. default:
  1032. break;
  1033. }
  1034. return offsetPos;
  1035. }
  1036. else
  1037. {
  1038. return movePoint;
  1039. }
  1040. }
  1041. /// <summary>
  1042. /// Inner drawing circle point
  1043. /// </summary>
  1044. /// <param name="drawingContext">
  1045. /// Drawing context
  1046. /// </param>
  1047. /// <param name="ignoreList">
  1048. /// Collection of positions that need to be drawn
  1049. /// </param>
  1050. /// <param name="PointSize">
  1051. /// Size of the point
  1052. /// </param>
  1053. /// <param name="PointPen">
  1054. /// Brush for drawing points
  1055. /// </param>
  1056. /// <param name="BorderBrush">
  1057. /// Border brush for drawing points
  1058. /// </param>
  1059. protected void DrawCirclePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  1060. {
  1061. GeometryGroup controlGroup = new GeometryGroup();
  1062. controlGroup.FillRule = FillRule.Nonzero;
  1063. List<Point> ignorePointsList = new List<Point>();
  1064. // Get specific points
  1065. foreach (PointControlType type in ignoreList)
  1066. {
  1067. if ((int)type < controlPoints.Count)
  1068. {
  1069. ignorePointsList.Add(controlPoints[(int)type]);
  1070. }
  1071. }
  1072. for (int i = 0; i < controlPoints.Count; i++)
  1073. {
  1074. Point controlPoint = controlPoints[i];
  1075. if (ignorePointsList.Contains(controlPoint))
  1076. {
  1077. continue;
  1078. }
  1079. EllipseGeometry circlPoint = new EllipseGeometry(controlPoint, PointSize, PointSize);
  1080. controlGroup.Children.Add(circlPoint);
  1081. }
  1082. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  1083. }
  1084. /// <summary>
  1085. /// Inner drawing square
  1086. /// </summary>
  1087. /// <param name="drawingContext">
  1088. /// Drawing context
  1089. /// </param>
  1090. /// <param name="ControlPoints">
  1091. /// Collection of positions that need to be drawn
  1092. /// </param>
  1093. /// <param name="PointSize">
  1094. /// Size of the point
  1095. /// </param>
  1096. /// <param name="PointPen">
  1097. /// Brush for drawing points
  1098. /// </param>
  1099. /// <param name="BorderBrush">
  1100. /// Border brush for drawing points
  1101. /// </param>
  1102. protected void DrawSquarePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  1103. {
  1104. RotateTransform rotateTransform = new RotateTransform(rotateAngle, centerPoint.X, centerPoint.Y);
  1105. if (canRotate && !isInScaling)
  1106. {
  1107. Point currentRotationPoint = isInRotate ? dragRotationPoint : rotationPoint;
  1108. double angleInRadians = rotateAngle * (Math.PI / 180);
  1109. double sinValue = Math.Sin(angleInRadians);
  1110. double cosValue = Math.Cos(angleInRadians);
  1111. double rotatedX = 0;
  1112. double rotatedY = 0;
  1113. switch (pageRotation)
  1114. {
  1115. case 0:
  1116. rotatedX = currentRotationPoint.X - pointSize * sinValue;
  1117. rotatedY = currentRotationPoint.Y + pointSize * cosValue;
  1118. break;
  1119. case 1:
  1120. rotatedX = currentRotationPoint.X - pointSize * cosValue;
  1121. rotatedY = currentRotationPoint.Y + pointSize * sinValue;
  1122. break;
  1123. case 2:
  1124. rotatedX = currentRotationPoint.X + pointSize * sinValue;
  1125. rotatedY = currentRotationPoint.Y - pointSize * cosValue;
  1126. break;
  1127. case 3:
  1128. rotatedX = currentRotationPoint.X + pointSize * sinValue;
  1129. rotatedY = currentRotationPoint.Y - pointSize * cosValue;
  1130. break;
  1131. default:
  1132. break;
  1133. }
  1134. GeometryGroup rotateGroup = new GeometryGroup();
  1135. LineGeometry moveLineGeometry = new LineGeometry(centerPoint, new Point(rotatedX, rotatedY));
  1136. EllipseGeometry ellipseGeometry = new EllipseGeometry(currentRotationPoint, PointSize, pointSize);
  1137. rotateGroup.Children.Add(moveLineGeometry);
  1138. rotateGroup.Children.Add(ellipseGeometry);
  1139. if (!isInRotate)
  1140. {
  1141. rotateGroup.Children.Remove(moveLineGeometry);
  1142. LineGeometry stopLineGeometry = new LineGeometry(centerPoint, new Point(currentRotationPoint.X, currentRotationPoint.Y + pointSize));
  1143. switch (pageRotation)
  1144. {
  1145. case 0:
  1146. stopLineGeometry = new LineGeometry(centerPoint, new Point(currentRotationPoint.X, currentRotationPoint.Y + pointSize));
  1147. break;
  1148. case 1:
  1149. stopLineGeometry = new LineGeometry(centerPoint, new Point(currentRotationPoint.X - pointSize, currentRotationPoint.Y ));
  1150. break;
  1151. case 2:
  1152. stopLineGeometry = new LineGeometry(centerPoint, new Point(currentRotationPoint.X, currentRotationPoint.Y - pointSize));
  1153. break;
  1154. case 3:
  1155. stopLineGeometry = new LineGeometry(centerPoint, new Point(currentRotationPoint.X + pointSize, currentRotationPoint.Y));
  1156. break;
  1157. default:
  1158. break;
  1159. }
  1160. rotateGroup.Children.Add(stopLineGeometry);
  1161. drawingContext.PushTransform(rotateTransform);
  1162. }
  1163. drawingContext?.DrawGeometry(BorderBrush, PointPen, rotateGroup);
  1164. if (!isInRotate)
  1165. {
  1166. drawingContext.Pop();
  1167. }
  1168. }
  1169. if (!isInRotate)
  1170. {
  1171. GeometryGroup controlGroup = new GeometryGroup();
  1172. controlGroup.FillRule = FillRule.Nonzero;
  1173. List<Point> ignorePointsList = new List<Point>();
  1174. // Get specific points
  1175. foreach (PointControlType type in ignoreList)
  1176. {
  1177. if ((int)type < controlPoints.Count)
  1178. {
  1179. ignorePointsList.Add(controlPoints[(int)type]);
  1180. }
  1181. }
  1182. for (int i = 0; i < controlPoints.Count; i++)
  1183. {
  1184. Point controlPoint = controlPoints[i];
  1185. if (ignorePointsList.Contains(controlPoint))
  1186. {
  1187. continue;
  1188. }
  1189. RectangleGeometry rectPoint = new RectangleGeometry(new Rect(controlPoint.X - PointSize, controlPoint.Y - PointSize,
  1190. PointSize * 2, PointSize * 2), 1, 1);
  1191. controlGroup.Children.Add(rectPoint);
  1192. }
  1193. drawingContext.PushTransform(rotateTransform);
  1194. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  1195. drawingContext.Pop();
  1196. }
  1197. }
  1198. protected void DrawCropPoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  1199. {
  1200. //GeometryGroup controlGroup = new GeometryGroup();
  1201. //controlGroup.FillRule = FillRule.Nonzero;
  1202. clipThickness.Left = (SetDrawRect.Left - drawRect.Left) / currentZoom;
  1203. clipThickness.Top = (SetDrawRect.Top - drawRect.Top) / currentZoom;
  1204. clipThickness.Right = (SetDrawRect.Right - drawRect.Right) / currentZoom;
  1205. clipThickness.Bottom = (SetDrawRect.Bottom - drawRect.Bottom) / currentZoom;
  1206. List<Point> controlCurrentPoints = GetControlPoint(drawRect);
  1207. CombinedGeometry controlGroup = new CombinedGeometry();
  1208. RectangleGeometry paintGeometry = new RectangleGeometry();
  1209. paintGeometry.Rect = SetDrawRect;
  1210. controlGroup.Geometry1 = paintGeometry;
  1211. RectangleGeometry moveGeometry = new RectangleGeometry();
  1212. Rect clippedBorder = drawRect;
  1213. if (clippedBorder.IsEmpty == false)
  1214. {
  1215. moveGeometry.Rect = drawRect;
  1216. }
  1217. controlGroup.Geometry2 = moveGeometry;
  1218. controlGroup.GeometryCombineMode = GeometryCombineMode.Exclude;
  1219. //Left Top Corner
  1220. if (!ignoreList.Contains(PointControlType.LeftTop))
  1221. {
  1222. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[0].X - PointSize, controlCurrentPoints[0].Y - PointSize, PointSize, PointSize * 4));
  1223. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[0].X - PointSize, controlCurrentPoints[0].Y - PointSize, PointSize * 4, PointSize));
  1224. }
  1225. //Left Center
  1226. if (!ignoreList.Contains(PointControlType.LeftMiddle))
  1227. {
  1228. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[1].X - PointSize, (controlCurrentPoints[1].Y + controlCurrentPoints[1].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
  1229. }
  1230. //Left Bottom Corner
  1231. if (!ignoreList.Contains(PointControlType.LeftBottom))
  1232. {
  1233. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[2].X - PointSize, controlCurrentPoints[2].Y - PointSize * 3, PointSize, PointSize * 4));
  1234. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[2].X - PointSize, controlCurrentPoints[2].Y, PointSize * 4, PointSize));
  1235. }
  1236. //Bottom Center
  1237. if (!ignoreList.Contains(PointControlType.MiddleBottom))
  1238. {
  1239. drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlCurrentPoints[3].X + controlCurrentPoints[3].X - PointSize * 5) / 2, controlCurrentPoints[3].Y, PointSize * 5, PointSize));
  1240. }
  1241. //Bottom Right Corner
  1242. if (!ignoreList.Contains(PointControlType.RightBottom))
  1243. {
  1244. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[4].X, controlCurrentPoints[4].Y - PointSize * 3, PointSize, PointSize * 4));
  1245. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[4].X - PointSize * 3, controlCurrentPoints[4].Y, PointSize * 4, PointSize));
  1246. }
  1247. //Right Center
  1248. if (!ignoreList.Contains(PointControlType.RightMiddle))
  1249. {
  1250. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[5].X, (controlCurrentPoints[5].Y + controlCurrentPoints[5].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
  1251. }
  1252. //Right Top Corner
  1253. if (!ignoreList.Contains(PointControlType.RightTop))
  1254. {
  1255. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[6].X, controlCurrentPoints[6].Y - PointSize, PointSize, PointSize * 4));
  1256. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[6].X - PointSize * 4, controlCurrentPoints[6].Y - PointSize, PointSize * 4, PointSize));
  1257. }
  1258. //Top Center
  1259. if (!ignoreList.Contains(PointControlType.MiddleTop))
  1260. {
  1261. drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlCurrentPoints[7].X + controlCurrentPoints[7].X - PointSize * 5) / 2, controlCurrentPoints[7].Y - PointSize, PointSize * 5, PointSize));
  1262. }
  1263. BorderBrush = new SolidColorBrush(Color.FromArgb(0x3F, 0x00, 0x00, 0x00));
  1264. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  1265. }
  1266. /// <summary>
  1267. /// Draw the reference line in the moving state
  1268. /// </summary>
  1269. /// <param name="drawDc">
  1270. /// Draw context handle
  1271. /// </param>
  1272. /// <param name="controltype">
  1273. /// Current selected control point type
  1274. /// </param>
  1275. /// <param name="activePen">
  1276. /// Brush for drawing lines
  1277. /// </param>
  1278. /// <param name="moveBrush">
  1279. /// Brush for drawing rectangles
  1280. /// </param>
  1281. /// <param name="moveRect">
  1282. /// Current rectangle to draw
  1283. /// </param>
  1284. protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect, Pen RectPen = null)
  1285. {
  1286. switch (controltype)
  1287. {
  1288. case PointControlType.LeftTop:
  1289. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1290. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  1291. break;
  1292. case PointControlType.LeftMiddle:
  1293. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  1294. break;
  1295. case PointControlType.LeftBottom:
  1296. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1297. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  1298. break;
  1299. case PointControlType.MiddleBottom:
  1300. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1301. break;
  1302. case PointControlType.RightBottom:
  1303. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1304. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  1305. break;
  1306. case PointControlType.RightMiddle:
  1307. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  1308. break;
  1309. case PointControlType.RightTop:
  1310. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1311. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  1312. break;
  1313. case PointControlType.MiddleTop:
  1314. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1315. break;
  1316. case PointControlType.Rotate:
  1317. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(moveRect.Left, moveRect.Top));
  1318. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1319. drawDc?.DrawLine(activePen, new Point(moveRect.Left, moveRect.Top), new Point(moveRect.Left, 0));
  1320. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Top), new Point(moveRect.Right, 0));
  1321. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(moveRect.Left, moveRect.Bottom));
  1322. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1323. drawDc?.DrawLine(activePen, new Point(moveRect.Left, moveRect.Bottom), new Point(moveRect.Left, PDFViewerActualHeight));
  1324. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Bottom), new Point(moveRect.Right, PDFViewerActualHeight));
  1325. break;
  1326. case PointControlType.Body:
  1327. case PointControlType.Line:
  1328. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(moveRect.Left, moveRect.Top));
  1329. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1330. drawDc?.DrawLine(activePen, new Point(moveRect.Left, moveRect.Top), new Point(moveRect.Left, 0));
  1331. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Top), new Point(moveRect.Right, 0));
  1332. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(moveRect.Left, moveRect.Bottom));
  1333. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1334. drawDc?.DrawLine(activePen, new Point(moveRect.Left, moveRect.Bottom), new Point(moveRect.Left, PDFViewerActualHeight));
  1335. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Bottom), new Point(moveRect.Right, PDFViewerActualHeight));
  1336. break;
  1337. default:
  1338. break;
  1339. }
  1340. drawDc?.DrawRectangle(moveBrush, RectPen, moveRect);
  1341. }
  1342. /// <summary>
  1343. /// Notify the event during/after the drawing data
  1344. /// </summary>
  1345. /// <param name="isFinish">
  1346. /// Identifies whether the data change is complete
  1347. /// </param>
  1348. protected void InvokeDataChangEvent(bool isFinish)
  1349. {
  1350. selectedRectData.Square = GetRect();
  1351. selectedRectData.rotationAngle = rotateAngle;
  1352. if (isFinish)
  1353. {
  1354. DataChanged?.Invoke(this, selectedRectData);
  1355. }
  1356. else
  1357. {
  1358. DataChanging?.Invoke(this, selectedRectData);
  1359. }
  1360. }
  1361. /// <summary>
  1362. /// Align the rectangle drawing
  1363. /// </summary>
  1364. /// <param name="RectMovePoint">
  1365. /// Move distance required for the aligned algorithm to obtain the rectangle
  1366. /// </param>
  1367. private void DrawAlignRect(Point RectMovePoint)
  1368. {
  1369. double TmpLeft, TmpRight, TmpUp, TmpDown;
  1370. Point OffsetPos = CalcMoveBound(drawRect, RectMovePoint, maxRect);
  1371. TmpLeft = drawRect.Left + OffsetPos.X;
  1372. TmpRight = drawRect.Right + OffsetPos.X;
  1373. TmpUp = drawRect.Top + OffsetPos.Y;
  1374. TmpDown = drawRect.Bottom + OffsetPos.Y;
  1375. SetDrawRect = drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  1376. Draw();
  1377. }
  1378. /// <summary>
  1379. /// Get the current set of ignore points
  1380. /// </summary>
  1381. /// <returns>
  1382. /// Data set of ignored points
  1383. /// </returns>
  1384. private List<PointControlType> GetIgnorePoints()
  1385. {
  1386. List<PointControlType> IgnorePointsList = new List<PointControlType>();
  1387. foreach (PointControlType type in ignorePoints)
  1388. {
  1389. IgnorePointsList.Add(type);
  1390. }
  1391. return IgnorePointsList;
  1392. }
  1393. #endregion
  1394. }
  1395. }