DrawCircle.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 DrawCircle : DrawObject
  18. {
  19. private const string entryRectangle = "Rect";
  20. /// <summary>
  21. /// 标注样式信息model
  22. /// </summary>
  23. public CircleModel labelCircleStyleModel;
  24. public DrawCircle(ISurfaceBox surfaceBox, int x, int y, int width, int height) : base()
  25. {
  26. this.objectType = DrawClass.Label;
  27. this.drawToolType = DrawToolType.DrawCircle;
  28. labelCircleStyleModel = surfaceBox.GetLabelStyleModel().circleModel;
  29. rectangle.X = x;
  30. rectangle.Y = y;
  31. rectangle.Width = width;
  32. rectangle.Height = height;
  33. Initialize();
  34. }
  35. public DrawCircle(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  36. {
  37. this.objectType = DrawClass.Label;
  38. this.drawToolType = DrawToolType.DrawCircle;
  39. this.ISurfaceBox = surfaceBox;
  40. labelCircleStyleModel = (CircleModel)parentStyleModel;
  41. rectangle.X = points[0].X;
  42. rectangle.Y = points[0].Y;
  43. rectangle.Width = points[1].X - points[0].X;
  44. rectangle.Height = points[1].Y - points[0].Y;
  45. }
  46. /// <summary>
  47. /// Clone this instance
  48. /// </summary>
  49. public override DrawObject Clone()
  50. {
  51. DrawCircle drawRectangle = new DrawCircle(ISurfaceBox, 0, 0, 1, 0);
  52. drawRectangle.objectType = DrawClass.Label;
  53. drawRectangle.drawToolType = DrawToolType.DrawCircle;
  54. drawRectangle.ISurfaceBox = this.ISurfaceBox;
  55. drawRectangle.rectangle = this.rectangle;
  56. FillDrawObjectFields(drawRectangle);
  57. return drawRectangle;
  58. }
  59. public override DrawObject Clone(ISurfaceBox surfaceBox)
  60. {
  61. DrawCircle drawRectangle = new DrawCircle(surfaceBox, 0, 0, 1, 0);
  62. drawRectangle.objectType = DrawClass.Label;
  63. drawRectangle.drawToolType = DrawToolType.DrawCircle;
  64. drawRectangle.ISurfaceBox = surfaceBox;
  65. drawRectangle.rectangle = this.rectangle;
  66. drawRectangle.labelCircleStyleModel = this.labelCircleStyleModel;
  67. FillDrawObjectFields(drawRectangle);
  68. return drawRectangle;
  69. }
  70. /// <summary>
  71. /// Draw rectangle
  72. /// </summary>
  73. /// <param name="g"></param>
  74. public override void Draw(Graphics g)
  75. {
  76. Color color = Color.FromArgb(this.labelCircleStyleModel.lineColor);
  77. Pen pen = new Pen(color, PenWidth);
  78. int penWidth = this.labelCircleStyleModel.lineWidth;
  79. pen.DashStyle = (DashStyle)this.labelCircleStyleModel.lineStyle;
  80. pen.Width = penWidth;
  81. Color fillColor = Color.FromArgb(this.labelCircleStyleModel.fillColor);
  82. SolidBrush fillBrush = new SolidBrush(fillColor);
  83. float x1 = Rectangle.X;
  84. float y1 = Rectangle.Y;
  85. float x2 = Rectangle.Right;
  86. float y2 = Rectangle.Bottom;
  87. if (x2 < x1)
  88. {
  89. float tmp = x2;
  90. x2 = x1;
  91. x1 = tmp;
  92. }
  93. if (y2 < y1)
  94. {
  95. float tmp = y2;
  96. y2 = y1;
  97. y1 = tmp;
  98. }
  99. float radius = (x2 - x1) < (y2 - y1) ? (x2 - x1) : (y2 - y1);
  100. g.FillEllipse(fillBrush, new RectangleF(x1, y1, radius, radius));
  101. g.DrawEllipse(pen, new RectangleF(x1, y1, radius, radius));
  102. fillBrush.Dispose();
  103. pen.Dispose();
  104. }
  105. protected void SetRectangle(float x, float y, float width, float height, int handleNumber)
  106. {
  107. this.rectangle.X = x;
  108. this.rectangle.Y = y;
  109. if (handleNumber == 2 || handleNumber == 6)
  110. {
  111. this.rectangle.Width = height;
  112. this.rectangle.Height = height;
  113. }
  114. else
  115. {
  116. this.rectangle.Width = width;
  117. this.rectangle.Height = width;
  118. }
  119. }
  120. /// <summary>
  121. /// Get number of handles
  122. /// </summary>
  123. public override int HandleCount
  124. {
  125. get
  126. {
  127. return 8;
  128. }
  129. }
  130. /// <summary>
  131. /// Get handle pointscroll by 1-based number
  132. /// </summary>
  133. /// <param name="handleNumber"></param>
  134. /// <returns></returns>
  135. public override PointF GetHandle(int handleNumber)
  136. {
  137. float x, y, xCenter, yCenter;
  138. xCenter = rectangle.X + rectangle.Width / 2;
  139. yCenter = rectangle.Y + rectangle.Height / 2;
  140. x = rectangle.X;
  141. y = rectangle.Y;
  142. switch (handleNumber)
  143. {
  144. case 1:
  145. x = rectangle.X;
  146. y = rectangle.Y;
  147. break;
  148. case 2:
  149. x = xCenter;
  150. y = rectangle.Y;
  151. break;
  152. case 3:
  153. x = rectangle.Right;
  154. y = rectangle.Y;
  155. break;
  156. case 4:
  157. x = rectangle.Right;
  158. y = yCenter;
  159. break;
  160. case 5:
  161. x = rectangle.Right;
  162. y = rectangle.Bottom;
  163. break;
  164. case 6:
  165. x = xCenter;
  166. y = rectangle.Bottom;
  167. break;
  168. case 7:
  169. x = rectangle.X;
  170. y = rectangle.Bottom;
  171. break;
  172. case 8:
  173. x = rectangle.X;
  174. y = yCenter;
  175. break;
  176. }
  177. return new PointF(x, y);
  178. }
  179. /// <summary>
  180. /// Hit test.
  181. /// Return value: -1 - no hit
  182. /// 0 - hit anywhere
  183. /// > 1 - handle number
  184. /// </summary>
  185. /// <param name="pointscroll"></param>
  186. /// <returns></returns>
  187. public override int HitTest(Point point)
  188. {
  189. if (Selected)
  190. {
  191. for (int i = 1; i <= HandleCount; i++)
  192. {
  193. if (GetHandleRectangle(i).Contains(point))
  194. return i;
  195. }
  196. }
  197. if (PointInObject(point))
  198. return 0;
  199. return -1;
  200. }
  201. protected override bool PointInObject(Point point)
  202. {
  203. return rectangle.Contains(point);
  204. }
  205. /// <summary>
  206. /// Get cursor for the handle
  207. /// </summary>
  208. /// <param name="handleNumber"></param>
  209. /// <returns></returns>
  210. public override Cursor GetHandleCursor(int handleNumber)
  211. {
  212. switch (handleNumber)
  213. {
  214. case 1:
  215. return Cursors.SizeNWSE;
  216. case 2:
  217. return Cursors.SizeNS;
  218. case 3:
  219. return Cursors.SizeNESW;
  220. case 4:
  221. return Cursors.SizeWE;
  222. case 5:
  223. return Cursors.SizeNWSE;
  224. case 6:
  225. return Cursors.SizeNS;
  226. case 7:
  227. return Cursors.SizeNESW;
  228. case 8:
  229. return Cursors.SizeWE;
  230. default:
  231. return Cursors.Default;
  232. }
  233. }
  234. /// <summary>
  235. /// Move handle to new pointscroll (resizing)
  236. /// </summary>
  237. /// <param name="pointscroll"></param>
  238. /// <param name="handleNumber"></param>
  239. public override void MoveHandleTo(Point point, int handleNumber)
  240. {
  241. float left = Rectangle.Left;
  242. float top = Rectangle.Top;
  243. float right = Rectangle.Right;
  244. float bottom = Rectangle.Bottom;
  245. switch (handleNumber)
  246. {
  247. case 1:
  248. left = point.X;
  249. top = point.Y;
  250. break;
  251. case 2:
  252. top = point.Y;
  253. break;
  254. case 3:
  255. right = point.X;
  256. top = point.Y;
  257. break;
  258. case 4:
  259. right = point.X;
  260. break;
  261. case 5:
  262. right = point.X;
  263. bottom = point.Y;
  264. break;
  265. case 6:
  266. bottom = point.Y;
  267. break;
  268. case 7:
  269. left = point.X;
  270. bottom = point.Y;
  271. break;
  272. case 8:
  273. left = point.X;
  274. break;
  275. }
  276. SetRectangle(left, top, right - left, bottom - top, handleNumber);
  277. }
  278. public override bool IntersectsWith(Rectangle rectangle)
  279. {
  280. return Rectangle.IntersectsWith(rectangle);
  281. }
  282. /// <summary>
  283. /// Move object
  284. /// </summary>
  285. /// <param name="deltaX"></param>
  286. /// <param name="deltaY"></param>
  287. public override void Move(int deltaX, int deltaY)
  288. {
  289. int x = ISurfaceBox.UnscaleScalar(deltaX);
  290. int y = ISurfaceBox.UnscaleScalar(deltaY);
  291. rectangle.X += x;
  292. rectangle.Y += y;
  293. }
  294. public override void Dump()
  295. {
  296. base.Dump();
  297. Trace.WriteLine("rectangle.X = " + rectangle.X.ToString(CultureInfo.InvariantCulture));
  298. Trace.WriteLine("rectangle.Y = " + rectangle.Y.ToString(CultureInfo.InvariantCulture));
  299. Trace.WriteLine("rectangle.Width = " + rectangle.Width.ToString(CultureInfo.InvariantCulture));
  300. Trace.WriteLine("rectangle.Height = " + rectangle.Height.ToString(CultureInfo.InvariantCulture));
  301. }
  302. /// <summary>
  303. /// Normalize rectangle
  304. /// </summary>
  305. public override void Normalize()
  306. {
  307. rectangle = DrawRectangle.GetNormalizedRectangle(rectangle);
  308. }
  309. /// <summary>
  310. /// Save objevt to serialization stream
  311. /// </summary>
  312. /// <param name="info"></param>
  313. /// <param name="orderNumber"></param>
  314. public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  315. {
  316. info.AddValue(
  317. String.Format(CultureInfo.InvariantCulture,
  318. "{0}{1}",
  319. entryRectangle, orderNumber),
  320. rectangle);
  321. base.SaveToStream(info, orderNumber);
  322. }
  323. /// <summary>
  324. /// LOad object from serialization stream
  325. /// </summary>
  326. /// <param name="info"></param>
  327. /// <param name="orderNumber"></param>
  328. public override void LoadFromStream(SerializationInfo info, int orderNumber)
  329. {
  330. rectangle = (Rectangle)info.GetValue(
  331. String.Format(CultureInfo.InvariantCulture,
  332. "{0}{1}",
  333. entryRectangle, orderNumber),
  334. typeof(Rectangle));
  335. base.LoadFromStream(info, orderNumber);
  336. }
  337. #region Helper Functions
  338. public static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2)
  339. {
  340. if (x2 < x1)
  341. {
  342. int tmp = x2;
  343. x2 = x1;
  344. x1 = tmp;
  345. }
  346. if (y2 < y1)
  347. {
  348. int tmp = y2;
  349. y2 = y1;
  350. y1 = tmp;
  351. }
  352. return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  353. }
  354. public static Rectangle GetNormalizedRectangle(Point p1, Point p2)
  355. {
  356. return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);
  357. }
  358. public static Rectangle GetNormalizedRectangle(Rectangle r)
  359. {
  360. return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
  361. }
  362. #endregion
  363. public override RectangleF GetBoundingBox()
  364. {
  365. return rectangle;
  366. }
  367. public override List<PointF> GetPoints()
  368. {
  369. List<PointF> points = new List<PointF>();
  370. points.Add(new PointF(rectangle.X, rectangle.Y));
  371. points.Add(new PointF(rectangle.Right, rectangle.Bottom));
  372. return points;
  373. }
  374. public override ParentStyleModel GetStyle()
  375. {
  376. return labelCircleStyleModel;
  377. }
  378. }
  379. }