DrawScratchTreatmentLine.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.Other
  12. {
  13. /// <summary>
  14. /// 工具->划痕处理
  15. /// </summary>
  16. public class DrawScratchTreatmentLine : 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 DrawScratchTreatmentLine(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
  38. {
  39. this.objectType = DrawClass.Label;
  40. this.drawToolType = DrawToolType.DrawScratchTreatmentLine;
  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 DrawScratchTreatmentLine(ISurfaceBox surfaceBox, List<Point> points, ParentStyleModel parentStyleModel, Object content) : base()
  49. {
  50. this.objectType = DrawClass.Label;
  51. this.drawToolType = DrawToolType.DrawScratchTreatmentLine;
  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. DrawScratchTreatmentLine drawLine = new DrawScratchTreatmentLine(ISurfaceBox, 0, 0, 1, 0);
  65. drawLine.objectType = DrawClass.Label;
  66. drawLine.drawToolType = DrawToolType.DrawScratchTreatmentLine;
  67. drawLine.ISurfaceBox = this.ISurfaceBox;
  68. drawLine.startPoint = this.startPoint;
  69. drawLine.endPoint = this.endPoint;
  70. FillDrawObjectFields(drawLine);
  71. return drawLine;
  72. }
  73. public override DrawObject Clone(ISurfaceBox surfaceBox)
  74. {
  75. DrawScratchTreatmentLine drawLine = new DrawScratchTreatmentLine(surfaceBox, 0, 0, 1, 0);
  76. drawLine.objectType = DrawClass.Label;
  77. drawLine.drawToolType = DrawToolType.DrawScratchTreatmentLine;
  78. drawLine.ISurfaceBox = surfaceBox;
  79. drawLine.startPoint = this.startPoint;
  80. drawLine.endPoint = this.endPoint;
  81. drawLine.labelLineStyleModel = this.labelLineStyleModel;
  82. FillDrawObjectFields(drawLine);
  83. return drawLine;
  84. }
  85. public override void Draw(Graphics g)
  86. {
  87. g.SmoothingMode = SmoothingMode.AntiAlias;
  88. Color color = Color.Black;// Color.FromArgb(this.labelLineStyleModel.lineColor);
  89. Pen pen = new Pen(color, 1/*this.labelLineStyleModel.lineWidth*/);
  90. pen.DashStyle = DashStyle.Solid;// (DashStyle)this.labelLineStyleModel.lineStyle;
  91. g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  92. pen.Dispose();
  93. }
  94. public override int HandleCount
  95. {
  96. get
  97. {
  98. return 2;
  99. }
  100. }
  101. /// <summary>
  102. /// Get handle pointscroll by 1-based number
  103. /// </summary>
  104. /// <param name="handleNumber"></param>
  105. /// <returns></returns>
  106. public override PointF GetHandle(int handleNumber)
  107. {
  108. if (handleNumber == 1)
  109. return startPoint;
  110. else
  111. return endPoint;
  112. }
  113. /// <summary>
  114. /// Hit test.
  115. /// Return value: -1 - no hit
  116. /// 0 - hit anywhere
  117. /// > 1 - handle number
  118. /// </summary>
  119. /// <param name="pointscroll"></param>
  120. /// <returns></returns>
  121. public override int HitTest(Point point)
  122. {
  123. if (Selected)
  124. {
  125. for (int i = 1; i <= HandleCount; i++)
  126. {
  127. if (GetHandleRectangle(i).Contains(point))
  128. return i;
  129. }
  130. }
  131. if (PointInObject(point))
  132. return 0;
  133. return -1;
  134. }
  135. protected override bool PointInObject(Point point)
  136. {
  137. CreateObjects();
  138. return AreaRegion.IsVisible(point);
  139. }
  140. public override bool IntersectsWith(Rectangle rectangle)
  141. {
  142. CreateObjects();
  143. return AreaRegion.IsVisible(rectangle);
  144. }
  145. public override Cursor GetHandleCursor(int handleNumber)
  146. {
  147. switch (handleNumber)
  148. {
  149. case 1:
  150. case 2:
  151. return handleCursor;// Cursors.SizeAll;
  152. default:
  153. return Cursors.Default;
  154. }
  155. }
  156. public override void MoveHandleTo(Point point, int handleNumber)
  157. {
  158. if (handleNumber == 1)
  159. startPoint = point;
  160. else
  161. endPoint = point;
  162. Invalidate();
  163. }
  164. public override void Move(int deltaX, int deltaY)
  165. {
  166. int x = ISurfaceBox.UnscaleScalar(deltaX);
  167. int y = ISurfaceBox.UnscaleScalar(deltaY);
  168. startPoint.X += x;
  169. startPoint.Y += y;
  170. endPoint.X += x;
  171. endPoint.Y += y;
  172. Invalidate();
  173. }
  174. /// <summary>
  175. /// Invalidate object.
  176. /// When object is invalidated, path used for hit test
  177. /// is released and should be created again.
  178. /// </summary>
  179. protected void Invalidate()
  180. {
  181. if (AreaPath != null)
  182. {
  183. AreaPath.Dispose();
  184. AreaPath = null;
  185. }
  186. if (AreaPen != null)
  187. {
  188. AreaPen.Dispose();
  189. AreaPen = null;
  190. }
  191. if (AreaRegion != null)
  192. {
  193. AreaRegion.Dispose();
  194. AreaRegion = null;
  195. }
  196. }
  197. /// <summary>
  198. /// Create graphic objects used from hit test.
  199. /// </summary>
  200. protected virtual void CreateObjects()
  201. {
  202. if (AreaPath != null)
  203. return;
  204. // Create path which contains wide line
  205. // for easy mouse selection
  206. AreaPath = new GraphicsPath();
  207. AreaPen = new Pen(Color.Black, 7);
  208. AreaPath.AddLine(startPoint.X-1, startPoint.Y-1, endPoint.X+1, endPoint.Y+1);
  209. AreaPath.Widen(AreaPen);
  210. // Create region from the path
  211. AreaRegion = new Region(AreaPath);
  212. }
  213. protected GraphicsPath AreaPath
  214. {
  215. get
  216. {
  217. return areaPath;
  218. }
  219. set
  220. {
  221. areaPath = value;
  222. }
  223. }
  224. protected Pen AreaPen
  225. {
  226. get
  227. {
  228. return areaPen;
  229. }
  230. set
  231. {
  232. areaPen = value;
  233. }
  234. }
  235. protected Region AreaRegion
  236. {
  237. get
  238. {
  239. return areaRegion;
  240. }
  241. set
  242. {
  243. areaRegion = value;
  244. }
  245. }
  246. public override RectangleF GetBoundingBox()
  247. {
  248. RectangleF rectangle = GetNotmalizedRectangle(startPoint, endPoint);
  249. return rectangle;
  250. }
  251. public static RectangleF GetNotmalizedRectangle(PointF a, PointF b)
  252. {
  253. RectangleF rect = new RectangleF();
  254. if (a.X < b.X)
  255. {
  256. rect.X = a.X;
  257. rect.Width = b.X - a.X;
  258. }
  259. else
  260. {
  261. rect.X = b.X;
  262. rect.Width = a.X - b.X;
  263. }
  264. if (a.Y < b.Y)
  265. {
  266. rect.Y = a.Y;
  267. rect.Height = b.Y - a.Y;
  268. }
  269. else
  270. {
  271. rect.Y = b.Y;
  272. rect.Height = a.Y - b.Y;
  273. }
  274. return rect;
  275. }
  276. /// <summary>
  277. /// Draw tracker for selected object
  278. /// </summary>
  279. /// <param name="g"></param>
  280. public override void DrawTracker(Graphics g)
  281. {
  282. if (!Selected)
  283. return;
  284. SolidBrush brush = new SolidBrush(Color.Black);
  285. for (int i = 1; i <= HandleCount; i++)
  286. {
  287. g.FillRectangle(brush, GetHandleRectangle(i));
  288. }
  289. brush.Dispose();
  290. }
  291. public override List<PointF> GetPoints()
  292. {
  293. List<PointF> points = new List<PointF>();
  294. points.Add(startPoint);
  295. points.Add(endPoint);
  296. return points;
  297. }
  298. public override ParentStyleModel GetStyle()
  299. {
  300. return labelLineStyleModel;
  301. }
  302. }
  303. }