DrawEllipse.cs 9.6 KB

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