DrawSquareA.cs 13 KB

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