DrawRectangle.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.SettingModel;
  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. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  12. namespace PaintDotNet.Annotation.Label
  13. {
  14. /// <summary>
  15. /// 标注->多边形->矩形
  16. /// </summary>
  17. public class DrawRectangle : DrawObject
  18. {
  19. /// <summary>
  20. /// 标注样式信息model
  21. /// </summary>
  22. public PolygonRectangle labelRectStyleModel;
  23. public DrawRectangle(ISurfaceBox surfaceBox, int x, int y, int width, int height) : base()
  24. {
  25. this.objectType = DrawClass.Label;
  26. this.drawToolType = DrawToolType.DrawRectangle;
  27. this.labelRectStyleModel = surfaceBox.GetLabelStyleModel().polygonRectangle;
  28. this.rectangle.X = x;
  29. this.rectangle.Y = y;
  30. this.rectangle.Width = width;
  31. this.rectangle.Height = height;
  32. Initialize();
  33. }
  34. public DrawRectangle(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  35. {
  36. this.objectType = DrawClass.Label;
  37. this.drawToolType = DrawToolType.DrawRectangle;
  38. this.ISurfaceBox = surfaceBox;
  39. labelRectStyleModel = (PolygonRectangle)parentStyleModel;
  40. rectangle.X = points[0].X;
  41. rectangle.Y = points[0].Y;
  42. rectangle.Width = points[1].X - points[0].X;
  43. rectangle.Height = points[1].Y - points[0].Y;
  44. }
  45. /// <summary>
  46. /// Clone this instance
  47. /// </summary>
  48. public override DrawObject Clone()
  49. {
  50. DrawRectangle drawRectangle = new DrawRectangle(ISurfaceBox, 0, 0, 1, 0);
  51. drawRectangle.objectType = DrawClass.Label;
  52. drawRectangle.drawToolType = DrawToolType.DrawRectangle;
  53. drawRectangle.ISurfaceBox = this.ISurfaceBox;
  54. drawRectangle.rectangle = this.rectangle;
  55. FillDrawObjectFields(drawRectangle);
  56. return drawRectangle;
  57. }
  58. public override DrawObject Clone(ISurfaceBox surfaceBox)
  59. {
  60. DrawRectangle drawRectangle = new DrawRectangle(surfaceBox, 0, 0, 1, 0);
  61. drawRectangle.objectType = DrawClass.Label;
  62. drawRectangle.drawToolType = DrawToolType.DrawRectangle;
  63. drawRectangle.ISurfaceBox = surfaceBox;
  64. drawRectangle.rectangle = this.rectangle;
  65. drawRectangle.labelRectStyleModel = this.labelRectStyleModel;
  66. FillDrawObjectFields(drawRectangle);
  67. return drawRectangle;
  68. }
  69. /// <summary>
  70. /// Draw rectangle
  71. /// </summary>
  72. /// <param name="g"></param>
  73. public override void Draw(Graphics g)
  74. {
  75. Color color = Color.FromArgb(this.labelRectStyleModel.lineColor);
  76. Pen pen = new Pen(color, PenWidth);
  77. int penWidth = this.labelRectStyleModel.lineWidth;
  78. pen.DashStyle = (DashStyle)this.labelRectStyleModel.lineStyle;
  79. pen.Width = penWidth;
  80. Color fillColor = Color.FromArgb(this.labelRectStyleModel.fillColor);
  81. SolidBrush fillBrush = new SolidBrush(fillColor);
  82. g.FillRectangle(fillBrush, DrawRectangle.GetNormalizedRectangle(Rectangle));
  83. g.DrawRectangle(pen, DrawRectangle.GetNormalizedRectangle(Rectangle));
  84. fillBrush.Dispose();
  85. pen.Dispose();
  86. }
  87. protected void SetRectangle(float x, float y, float width, float height)
  88. {
  89. this.rectangle.X = x;
  90. this.rectangle.Y = y;
  91. this.rectangle.Width = width;
  92. this.rectangle.Height = height;
  93. }
  94. /// <summary>
  95. /// Get number of handles
  96. /// </summary>
  97. public override int HandleCount
  98. {
  99. get
  100. {
  101. return 8;
  102. }
  103. }
  104. /// <summary>
  105. /// Get handle pointscroll by 1-based number
  106. /// </summary>
  107. /// <param name="handleNumber"></param>
  108. /// <returns></returns>
  109. public override PointF GetHandle(int handleNumber)
  110. {
  111. float x, y, xCenter, yCenter;
  112. xCenter = this.rectangle.X + this.rectangle.Width / 2;
  113. yCenter = this.rectangle.Y + this.rectangle.Height / 2;
  114. x = this.rectangle.X;
  115. y = this.rectangle.Y;
  116. switch (handleNumber)
  117. {
  118. case 1:
  119. x = this.rectangle.X;
  120. y = this.rectangle.Y;
  121. break;
  122. case 2:
  123. x = xCenter;
  124. y = this.rectangle.Y;
  125. break;
  126. case 3:
  127. x = this.rectangle.Right;
  128. y = this.rectangle.Y;
  129. break;
  130. case 4:
  131. x = this.rectangle.Right;
  132. y = yCenter;
  133. break;
  134. case 5:
  135. x = this.rectangle.Right;
  136. y = this.rectangle.Bottom;
  137. break;
  138. case 6:
  139. x = xCenter;
  140. y = this.rectangle.Bottom;
  141. break;
  142. case 7:
  143. x = this.rectangle.X;
  144. y = this.rectangle.Bottom;
  145. break;
  146. case 8:
  147. x = this.rectangle.X;
  148. y = yCenter;
  149. break;
  150. }
  151. return new PointF(x, y);
  152. }
  153. /// <summary>
  154. /// Hit test.
  155. /// Return value: -1 - no hit
  156. /// 0 - hit anywhere
  157. /// > 1 - handle number
  158. /// </summary>
  159. /// <param name="pointscroll"></param>
  160. /// <returns></returns>
  161. public override int HitTest(Point point)
  162. {
  163. if (Selected)
  164. {
  165. for (int i = 1; i <= HandleCount; i++)
  166. {
  167. if (GetHandleRectangle(i).Contains(point))
  168. return i;
  169. }
  170. }
  171. if (PointInObject(point))
  172. return 0;
  173. return -1;
  174. }
  175. protected override bool PointInObject(Point point)
  176. {
  177. return rectangle.Contains(point);
  178. }
  179. /// <summary>
  180. /// Get cursor for the handle
  181. /// </summary>
  182. /// <param name="handleNumber"></param>
  183. /// <returns></returns>
  184. public override Cursor GetHandleCursor(int handleNumber)
  185. {
  186. switch (handleNumber)
  187. {
  188. case 1:
  189. return Cursors.SizeNWSE;
  190. case 2:
  191. return Cursors.SizeNS;
  192. case 3:
  193. return Cursors.SizeNESW;
  194. case 4:
  195. return Cursors.SizeWE;
  196. case 5:
  197. return Cursors.SizeNWSE;
  198. case 6:
  199. return Cursors.SizeNS;
  200. case 7:
  201. return Cursors.SizeNESW;
  202. case 8:
  203. return Cursors.SizeWE;
  204. default:
  205. return Cursors.Default;
  206. }
  207. }
  208. /// <summary>
  209. /// Move handle to new pointscroll (resizing)
  210. /// </summary>
  211. /// <param name="pointscroll"></param>
  212. /// <param name="handleNumber"></param>
  213. public override void MoveHandleTo(Point point, int handleNumber)
  214. {
  215. float left = Rectangle.Left;
  216. float top = Rectangle.Top;
  217. float right = Rectangle.Right;
  218. float bottom = Rectangle.Bottom;
  219. switch (handleNumber)
  220. {
  221. case 1:
  222. left = point.X;
  223. top = point.Y;
  224. break;
  225. case 2:
  226. top = point.Y;
  227. break;
  228. case 3:
  229. right = point.X;
  230. top = point.Y;
  231. break;
  232. case 4:
  233. right = point.X;
  234. break;
  235. case 5:
  236. right = point.X;
  237. bottom = point.Y;
  238. break;
  239. case 6:
  240. bottom = point.Y;
  241. break;
  242. case 7:
  243. left = point.X;
  244. bottom = point.Y;
  245. break;
  246. case 8:
  247. left = point.X;
  248. break;
  249. }
  250. SetRectangle(left, top, right - left, bottom - top);
  251. }
  252. public override bool IntersectsWith(Rectangle rectangle)
  253. {
  254. return Rectangle.IntersectsWith(rectangle);
  255. }
  256. /// <summary>
  257. /// Move object
  258. /// </summary>
  259. /// <param name="deltaX"></param>
  260. /// <param name="deltaY"></param>
  261. public override void Move(int deltaX, int deltaY)
  262. {
  263. int x = ISurfaceBox.UnscaleScalar(deltaX);
  264. int y = ISurfaceBox.UnscaleScalar(deltaY);
  265. rectangle.X += x;
  266. rectangle.Y += y;
  267. }
  268. public override RectangleF GetBoundingBox()
  269. {
  270. return rectangle;
  271. }
  272. public override List<PointF> GetPoints()
  273. {
  274. List<PointF> points = new List<PointF>();
  275. points.Add(new PointF(rectangle.X, rectangle.Y));
  276. points.Add(new PointF(rectangle.Right, rectangle.Bottom));
  277. return points;
  278. }
  279. public override ParentStyleModel GetStyle()
  280. {
  281. return labelRectStyleModel;
  282. }
  283. }
  284. }