SelectedRect.protected.cs 62 KB

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