ViewRectangle.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.CommTool;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Globalization;
  9. using System.Runtime.Serialization;
  10. using System.Windows.Forms;
  11. namespace PaintDotNet.Annotation.FieldView
  12. {
  13. /// <summary>
  14. /// 矩形视场绘制
  15. /// </summary>
  16. public class ViewRectangle : ViewBase
  17. {
  18. public override double Width
  19. {
  20. get => Rectangle.Width;
  21. set
  22. {
  23. MoveHandleTo(new Point((int)(value + Points[0].X), 0), 4);
  24. }
  25. }
  26. public override double Height
  27. {
  28. get => Rectangle.Height;
  29. set
  30. {
  31. MoveHandleTo(new Point(0, (int)(value + Points[0].Y)), 6);
  32. }
  33. }
  34. #region Constructor
  35. public ViewRectangle()
  36. {
  37. objectType = DrawClass.View;
  38. drawToolType = DrawToolType.ViewRectangle;
  39. Initialize();
  40. }
  41. public ViewRectangle(int x, int y) : this()
  42. {
  43. for (int i = 0; i < 4; i++) AddPoint(x, y);
  44. }
  45. public ViewRectangle(int x, int y, int width, int height) : this()
  46. {
  47. AddPoint(new PointF(x, y));
  48. AddPoint(new PointF(x + width, y));
  49. AddPoint(new PointF(x + width, y + height));
  50. AddPoint(new PointF(x, y + height));
  51. }
  52. public ViewRectangle(List<PointF> points) : this()
  53. {
  54. Points = points;
  55. }
  56. #endregion
  57. #region Handle
  58. public override int HandleCount
  59. {
  60. get
  61. {
  62. return 8;
  63. }
  64. }
  65. public override PointF GetHandle(int handleIndex)
  66. {
  67. var count = Points.Count;
  68. var x = Points[(handleIndex / 2) % count].X + Points[(handleIndex - 1) / 2].X;
  69. var y = Points[(handleIndex / 2) % count].Y + Points[(handleIndex - 1) / 2].Y;
  70. return new PointF(x / 2, y / 2);
  71. }
  72. public override Cursor GetHandleCursor(int handleNumber)
  73. {
  74. switch (handleNumber)
  75. {
  76. case 1:
  77. return Cursors.SizeNWSE;
  78. case 2:
  79. return Cursors.SizeNS;
  80. case 3:
  81. return Cursors.SizeNESW;
  82. case 4:
  83. return Cursors.SizeWE;
  84. case 5:
  85. return Cursors.SizeNWSE;
  86. case 6:
  87. return Cursors.SizeNS;
  88. case 7:
  89. return Cursors.SizeNESW;
  90. case 8:
  91. return Cursors.SizeWE;
  92. default:
  93. return Cursors.Default;
  94. }
  95. }
  96. public override void MoveHandleTo(Point point, int handleNumber)
  97. {
  98. if (handleNumber % 2 == 0)
  99. MoveEdge(point, handleNumber / 2 - 1);
  100. else
  101. MoveCorner(point, handleNumber / 2);
  102. OnPropertyChanged();
  103. }
  104. private void MoveCorner(Point point, int i)
  105. {
  106. Points[i] = point;
  107. if (i % 2 == 0)
  108. {
  109. Points[(i + 1) % 4] = new PointF(Points[(i + 1) % 4].X, point.Y);
  110. Points[(i + 3) % 4] = new PointF(point.X, Points[(i + 3) % 4].Y);
  111. }
  112. else
  113. {
  114. Points[(i + 1) % 4] = new PointF(point.X, Points[(i + 1) % 4].Y);
  115. Points[(i + 3) % 4] = new PointF(Points[(i + 3) % 4].X, point.Y);
  116. }
  117. }
  118. private void MoveEdge(Point point, int i)
  119. {
  120. var tempk = i % 2 != 0 ? K : -1 / K;
  121. this.Points[i] = BasicCalculationHelper.GetDropFeet(Points[(i + 3) % 4], tempk, point);
  122. this.Points[(i + 1) % 4] = BasicCalculationHelper.GetDropFeet(Points[(i + 2) % 4], tempk, point);
  123. }
  124. #endregion
  125. #region Clone
  126. public override DrawObject Clone()
  127. {
  128. return Clone(ISurfaceBox);
  129. }
  130. public override DrawObject Clone(ISurfaceBox surfaceBox)
  131. {
  132. ViewRectangle drawRectangle = new ViewRectangle(Points);
  133. drawRectangle.ISurfaceBox = surfaceBox;
  134. drawRectangle.combineMode = combineMode;
  135. drawRectangle.Color = Color;
  136. drawRectangle.ID = ID;
  137. FillDrawObjectFields(drawRectangle);
  138. return drawRectangle;
  139. }
  140. #endregion
  141. }
  142. }