ViewRectangle.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 PointF((float)(value + Points[0].X), 0), 4);
  24. }
  25. }
  26. public override double Height
  27. {
  28. get => Rectangle.Height;
  29. set
  30. {
  31. MoveHandleTo(new PointF(0, (float)(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. MoveHandleTo((PointF)point, handleNumber);
  99. }
  100. public override void MoveHandleTo(PointF point, int handleNumber)
  101. {
  102. if (handleNumber % 2 == 0)
  103. MoveEdge(point, handleNumber / 2 - 1);
  104. else
  105. MoveCorner(point, handleNumber / 2);
  106. OnPropertyChanged();
  107. }
  108. private void MoveCorner(PointF point, int i)
  109. {
  110. Points[i] = point;
  111. if (i % 2 == 0)
  112. {
  113. Points[(i + 1) % 4] = new PointF(Points[(i + 1) % 4].X, point.Y);
  114. Points[(i + 3) % 4] = new PointF(point.X, Points[(i + 3) % 4].Y);
  115. }
  116. else
  117. {
  118. Points[(i + 1) % 4] = new PointF(point.X, Points[(i + 1) % 4].Y);
  119. Points[(i + 3) % 4] = new PointF(Points[(i + 3) % 4].X, point.Y);
  120. }
  121. }
  122. private void MoveEdge(PointF point, int i)
  123. {
  124. var tempk = i % 2 != 0 ? K : -1 / K;
  125. this.Points[i] = BasicCalculationHelper.GetDropFeet(Points[(i + 3) % 4], tempk, point);
  126. this.Points[(i + 1) % 4] = BasicCalculationHelper.GetDropFeet(Points[(i + 2) % 4], tempk, point);
  127. }
  128. #endregion
  129. #region Clone
  130. public override DrawObject Clone()
  131. {
  132. return Clone(ISurfaceBox);
  133. }
  134. public override DrawObject Clone(ISurfaceBox surfaceBox)
  135. {
  136. ViewRectangle drawRectangle = new ViewRectangle(Points);
  137. drawRectangle.ISurfaceBox = surfaceBox;
  138. drawRectangle.combineMode = combineMode;
  139. drawRectangle.Color = Color;
  140. drawRectangle.ID = ID;
  141. FillDrawObjectFields(drawRectangle);
  142. return drawRectangle;
  143. }
  144. #endregion
  145. }
  146. }