SelectedRect.protected.cs 65 KB

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