DrawLine.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using PaintDotNet.Base.SettingModel;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Globalization;
  6. using System.Runtime.Serialization;
  7. using System.Windows.Forms;
  8. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  9. using PaintDotNet.Annotation.Enum;
  10. using System.Collections.Generic;
  11. namespace PaintDotNet.Annotation.Label
  12. {
  13. /// <summary>
  14. /// 标注->直线->直线
  15. /// </summary>
  16. public class DrawLine : DrawObject
  17. {
  18. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
  19. /// <summary>
  20. /// 可能最后用不到
  21. /// </summary>
  22. private const string entryStart = "Start";
  23. /// <summary>
  24. /// 可能最后用不到
  25. /// </summary>
  26. private const string entryEnd = "End";
  27. /// <summary>
  28. /// Graphic objects for hit test
  29. /// </summary>
  30. private GraphicsPath areaPath = null;
  31. private Pen areaPen = null;
  32. private Region areaRegion = null;
  33. /// <summary>
  34. /// 标注样式信息model
  35. /// </summary>
  36. public LineChildLine labelLineStyleModel;
  37. public DrawLine(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
  38. {
  39. this.objectType = DrawClass.Label;
  40. this.drawToolType = DrawToolType.DrawLine;
  41. labelLineStyleModel = surfaceBox.GetLabelStyleModel().lineChildLine;
  42. startPoint.X = x1;
  43. startPoint.Y = y1;
  44. endPoint.X = x2;
  45. endPoint.Y = y2;
  46. Initialize();
  47. }
  48. public DrawLine(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  49. {
  50. this.objectType = DrawClass.Label;
  51. this.drawToolType = DrawToolType.DrawLine;
  52. this.ISurfaceBox = surfaceBox;
  53. labelLineStyleModel = (LineChildLine)parentStyleModel;
  54. startPoint.X = points[0].X;
  55. startPoint.Y = points[0].Y;
  56. endPoint.X = points[1].X;
  57. endPoint.Y = points[1].Y;
  58. }
  59. /// <summary>
  60. /// Clone this instance
  61. /// </summary>
  62. public override DrawObject Clone()
  63. {
  64. DrawLine drawLine = new DrawLine(ISurfaceBox, 0, 0, 1, 0);
  65. drawLine.objectType = DrawClass.Label;
  66. drawLine.drawToolType = DrawToolType.DrawLine;
  67. drawLine.ISurfaceBox = this.ISurfaceBox;
  68. drawLine.startPoint = this.startPoint;
  69. drawLine.endPoint = this.endPoint;
  70. drawLine.labelLineStyleModel = this.labelLineStyleModel;
  71. FillDrawObjectFields(drawLine);
  72. return drawLine;
  73. }
  74. public override DrawObject Clone(ISurfaceBox surfaceBox)
  75. {
  76. DrawLine drawLine = new DrawLine(surfaceBox, 0, 0, 1, 0);
  77. drawLine.objectType = DrawClass.Label;
  78. drawLine.drawToolType = DrawToolType.DrawLine;
  79. drawLine.ISurfaceBox = surfaceBox;
  80. drawLine.startPoint = this.startPoint;
  81. drawLine.endPoint = this.endPoint;
  82. drawLine.labelLineStyleModel = this.labelLineStyleModel;
  83. FillDrawObjectFields(drawLine);
  84. return drawLine;
  85. }
  86. public override void Draw(Graphics g)
  87. {
  88. g.SmoothingMode = SmoothingMode.AntiAlias;
  89. Color color = Color.FromArgb(this.labelLineStyleModel.lineColor);
  90. Pen pen = new Pen(color, this.labelLineStyleModel.lineWidth);
  91. pen.DashStyle = (DashStyle)this.labelLineStyleModel.lineStyle;
  92. g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  93. pen.Dispose();
  94. }
  95. public override int HandleCount
  96. {
  97. get
  98. {
  99. return 2;
  100. }
  101. }
  102. /// <summary>
  103. /// Get handle pointscroll by 1-based number
  104. /// </summary>
  105. /// <param name="handleNumber"></param>
  106. /// <returns></returns>
  107. public override PointF GetHandle(int handleNumber)
  108. {
  109. if (handleNumber == 1)
  110. return startPoint;
  111. else
  112. return endPoint;
  113. }
  114. /// <summary>
  115. /// Hit test.
  116. /// Return value: -1 - no hit
  117. /// 0 - hit anywhere
  118. /// > 1 - handle number
  119. /// </summary>
  120. /// <param name="pointscroll"></param>
  121. /// <returns></returns>
  122. public override int HitTest(Point point)
  123. {
  124. if (Selected)
  125. {
  126. for (int i = 1; i <= HandleCount; i++)
  127. {
  128. if (GetHandleRectangle(i).Contains(point))
  129. return i;
  130. }
  131. }
  132. if (PointInObject(point))
  133. return 0;
  134. return -1;
  135. }
  136. protected override bool PointInObject(Point point)
  137. {
  138. CreateObjects();
  139. return AreaRegion.IsVisible(point);
  140. }
  141. public override bool IntersectsWith(Rectangle rectangle)
  142. {
  143. CreateObjects();
  144. return AreaRegion.IsVisible(rectangle);
  145. }
  146. public override Cursor GetHandleCursor(int handleNumber)
  147. {
  148. switch (handleNumber)
  149. {
  150. case 1:
  151. case 2:
  152. return handleCursor;// Cursors.SizeAll;
  153. default:
  154. return Cursors.Default;
  155. }
  156. }
  157. public override void MoveHandleTo(Point point, int handleNumber)
  158. {
  159. if (handleNumber == 1)
  160. startPoint = point;
  161. else
  162. endPoint = point;
  163. Invalidate();
  164. }
  165. public override void Move(int deltaX, int deltaY)
  166. {
  167. int x = ISurfaceBox.UnscaleScalar(deltaX);
  168. int y = ISurfaceBox.UnscaleScalar(deltaY);
  169. startPoint.X += x;
  170. startPoint.Y += y;
  171. endPoint.X += x;
  172. endPoint.Y += y;
  173. Invalidate();
  174. }
  175. /// <summary>
  176. /// Invalidate object.
  177. /// When object is invalidated, path used for hit test
  178. /// is released and should be created again.
  179. /// </summary>
  180. protected void Invalidate()
  181. {
  182. if (AreaPath != null)
  183. {
  184. AreaPath.Dispose();
  185. AreaPath = null;
  186. }
  187. if (AreaPen != null)
  188. {
  189. AreaPen.Dispose();
  190. AreaPen = null;
  191. }
  192. if (AreaRegion != null)
  193. {
  194. AreaRegion.Dispose();
  195. AreaRegion = null;
  196. }
  197. }
  198. /// <summary>
  199. /// Create graphic objects used from hit test.
  200. /// </summary>
  201. protected virtual void CreateObjects()
  202. {
  203. if (AreaPath != null)
  204. return;
  205. // Create path which contains wide line
  206. // for easy mouse selection
  207. AreaPath = new GraphicsPath();
  208. AreaPen = new Pen(Color.Black, 7);
  209. AreaPath.AddLine(startPoint.X-1, startPoint.Y-1, endPoint.X+1, endPoint.Y+1);
  210. AreaPath.Widen(AreaPen);
  211. // Create region from the path
  212. AreaRegion = new Region(AreaPath);
  213. }
  214. protected GraphicsPath AreaPath
  215. {
  216. get
  217. {
  218. return areaPath;
  219. }
  220. set
  221. {
  222. areaPath = value;
  223. }
  224. }
  225. protected Pen AreaPen
  226. {
  227. get
  228. {
  229. return areaPen;
  230. }
  231. set
  232. {
  233. areaPen = value;
  234. }
  235. }
  236. protected Region AreaRegion
  237. {
  238. get
  239. {
  240. return areaRegion;
  241. }
  242. set
  243. {
  244. areaRegion = value;
  245. }
  246. }
  247. public override RectangleF GetBoundingBox()
  248. {
  249. RectangleF rectangle = GetNotmalizedRectangle(startPoint, endPoint);
  250. return rectangle;
  251. }
  252. public static RectangleF GetNotmalizedRectangle(PointF a, PointF b)
  253. {
  254. RectangleF rect = new RectangleF();
  255. if (a.X < b.X)
  256. {
  257. rect.X = a.X;
  258. rect.Width = b.X - a.X;
  259. }
  260. else
  261. {
  262. rect.X = b.X;
  263. rect.Width = a.X - b.X;
  264. }
  265. if (a.Y < b.Y)
  266. {
  267. rect.Y = a.Y;
  268. rect.Height = b.Y - a.Y;
  269. }
  270. else
  271. {
  272. rect.Y = b.Y;
  273. rect.Height = a.Y - b.Y;
  274. }
  275. return rect;
  276. }
  277. /// <summary>
  278. /// Draw tracker for selected object
  279. /// </summary>
  280. /// <param name="g"></param>
  281. public override void DrawTracker(Graphics g)
  282. {
  283. if (!Selected)
  284. return;
  285. SolidBrush brush = new SolidBrush(Color.FromArgb(MarkpointAreaColor));
  286. Pen pen = new Pen(Color.FromArgb(MarkpointLineColor), MarkpointLineWidth);
  287. for (int i = 1; i <= HandleCount; i++)
  288. {
  289. switch (MarkpointStyle)
  290. {
  291. case 0:
  292. g.FillRectangle(brush, GetHandleRectangle(i));
  293. g.DrawRectangle(pen, GetHandleRectangle(i));
  294. break;
  295. case 1:
  296. g.FillEllipse(brush, GetHandleRectangle(i));
  297. g.DrawEllipse(pen, GetHandleRectangle(i));
  298. break;
  299. case 2:
  300. g.FillPolygon(brush, GetHandlePoint(i));
  301. g.DrawPolygon(pen, GetHandlePoint(i));
  302. break;
  303. }
  304. }
  305. brush.Dispose();
  306. pen.Dispose();
  307. }
  308. public override List<PointF> GetPoints()
  309. {
  310. List<PointF> points = new List<PointF>();
  311. points.Add(startPoint);
  312. points.Add(endPoint);
  313. return points;
  314. }
  315. public override ParentStyleModel GetStyle()
  316. {
  317. return labelLineStyleModel;
  318. }
  319. }
  320. }