SelectedRect.protected.cs 67 KB

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