ViewRectangle.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using SmartCoalApplication.Annotation.Enum;
  2. using SmartCoalApplication.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 SmartCoalApplication.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(ISurfaceBox surfaceBox,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. /// <summary>
  97. /// Hit test.
  98. /// Return value: -1 - no hit
  99. /// 0 - hit anywhere
  100. /// > 1 - handle number
  101. /// </summary>
  102. /// <param name="pointscroll"></param>
  103. /// <returns></returns>
  104. public override int HitTest(Point point)
  105. {
  106. if (!this.canMove) return -1;
  107. if (Selected)
  108. {
  109. for (int i = 1; i <= HandleCount; i++)
  110. {
  111. if (GetHandleRectangle(i).Contains(point))
  112. return i;
  113. }
  114. }
  115. if (PointInObject(point))
  116. return 0;
  117. return -1;
  118. }
  119. public override void MoveHandleTo(Point point, int handleNumber)
  120. {
  121. if (handleNumber % 2 == 0)
  122. MoveEdge(point, handleNumber / 2 - 1);
  123. else
  124. MoveCorner(point, handleNumber / 2);
  125. OnPropertyChanged();
  126. }
  127. private void MoveCorner(Point point, int i)
  128. {
  129. Points[i] = point;
  130. if (i % 2 == 0)
  131. {
  132. Points[(i + 1) % 4] = new PointF(Points[(i + 1) % 4].X, point.Y);
  133. Points[(i + 3) % 4] = new PointF(point.X, Points[(i + 3) % 4].Y);
  134. }
  135. else
  136. {
  137. Points[(i + 1) % 4] = new PointF(point.X, Points[(i + 1) % 4].Y);
  138. Points[(i + 3) % 4] = new PointF(Points[(i + 3) % 4].X, point.Y);
  139. }
  140. }
  141. private void MoveEdge(Point point, int i)
  142. {
  143. var tempk = i % 2 != 0 ? K : -1 / K;
  144. this.Points[i] = BasicCalculationHelper.GetDropFeet(Points[(i + 3) % 4], tempk, point);
  145. this.Points[(i + 1) % 4] = BasicCalculationHelper.GetDropFeet(Points[(i + 2) % 4], tempk, point);
  146. }
  147. #endregion
  148. #region Clone
  149. public override DrawObject Clone()
  150. {
  151. return Clone(ISurfaceBox);
  152. }
  153. public override DrawObject Clone(ISurfaceBox surfaceBox)
  154. {
  155. ViewRectangle drawRectangle = new ViewRectangle(Points);
  156. drawRectangle.ISurfaceBox = surfaceBox;
  157. drawRectangle.combineMode = combineMode;
  158. drawRectangle.Color = Color;
  159. drawRectangle.canMove = canMove;
  160. drawRectangle.ID = ID;
  161. FillDrawObjectFields(drawRectangle);
  162. return drawRectangle;
  163. }
  164. #endregion
  165. }
  166. }