DrawLineSegment.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Runtime.Serialization;
  8. using System.Windows.Forms;
  9. using PaintDotNet.Annotation.Enum;
  10. using PaintDotNet.Base.SettingModel;
  11. namespace PaintDotNet.Annotation.Label
  12. {
  13. /// <summary>
  14. /// 标注->直线->线段
  15. /// </summary>
  16. public class DrawLineSegment : DrawObject
  17. {
  18. private const string entryStart = "Start";
  19. private const string entryEnd = "End";
  20. /// <summary>
  21. /// Graphic objects for hit test
  22. /// </summary>
  23. private GraphicsPath areaPath = null;
  24. private Pen areaPen = null;
  25. private Region areaRegion = null;
  26. // Create the outline for our custom end cap.
  27. GraphicsPath hPath = new GraphicsPath();
  28. // Construct the hook-shaped end cap.
  29. CustomLineCap HookCap;
  30. /// <summary>
  31. /// 标注样式信息model
  32. /// </summary>
  33. private LabelStyleModel.LineChildLineSegment labelLineSegment;
  34. public LabelStyleModel.LineChildLineSegment LabelLineSegment
  35. {
  36. set
  37. {
  38. this.labelLineSegment = value;
  39. }
  40. }
  41. public DrawLineSegment(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
  42. {
  43. this.objectType = DrawClass.Label;
  44. this.drawToolType = DrawToolType.DrawLineSegment;
  45. labelLineSegment = surfaceBox.GetLabelStyleModel().lineChildLineSegment;
  46. startPoint.X = x1;
  47. startPoint.Y = y1;
  48. endPoint.X = x2;
  49. endPoint.Y = y2;
  50. //自定义线形的线帽
  51. hPath.AddLine(new PointF(-10, 0), new PointF(10, 0));
  52. HookCap = new CustomLineCap(null, hPath);
  53. HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
  54. /*//求出亮点之间的距离
  55. int xy = (startPoint.X - endPoint.X) * (startPoint.X - endPoint.X) + (startPoint.Y - endPoint.Y) * (startPoint.Y - endPoint.Y);
  56. int k = (int)Math.Sqrt(xy);
  57. //没有则创建这个文件
  58. StreamWriter sw = File.AppendText("e:\\temp\\testtxt.txt");
  59. sw.Write(DateTime.Now + " : " + xy + "\r\n");
  60. sw.Flush();
  61. sw.Close();
  62. sw.Dispose();*/
  63. Initialize();
  64. }
  65. public DrawLineSegment(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  66. {
  67. this.objectType = DrawClass.Label;
  68. this.drawToolType = DrawToolType.DrawLineSegment;
  69. this.ISurfaceBox = surfaceBox;
  70. labelLineSegment = (LabelStyleModel.LineChildLineSegment)parentStyleModel;
  71. startPoint.X = points[0].X;
  72. startPoint.Y = points[0].Y;
  73. endPoint.X = points[1].X;
  74. endPoint.Y = points[1].Y;
  75. hPath.AddLine(new PointF(-10, 0), new PointF(10, 0));
  76. HookCap = new CustomLineCap(null, hPath);
  77. HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
  78. }
  79. /// <summary>
  80. /// Clone this instance
  81. /// </summary>
  82. public override DrawObject Clone()
  83. {
  84. DrawLineSegment drawLineSegment = new DrawLineSegment(ISurfaceBox, 0, 0, 1, 0);
  85. drawLineSegment.objectType = DrawClass.Label;
  86. drawLineSegment.drawToolType = DrawToolType.DrawLineSegment;
  87. drawLineSegment.ISurfaceBox = this.ISurfaceBox;
  88. drawLineSegment.startPoint = this.startPoint;
  89. drawLineSegment.endPoint = this.endPoint;
  90. drawLineSegment.hPath.AddLine(new PointF(-10, 0), new PointF(10, 0));
  91. drawLineSegment.HookCap = new CustomLineCap(null, hPath);
  92. drawLineSegment.HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
  93. FillDrawObjectFields(drawLineSegment);
  94. return drawLineSegment;
  95. }
  96. public override DrawObject Clone(ISurfaceBox surfaceBox)
  97. {
  98. DrawLineSegment drawLineSegment = new DrawLineSegment(surfaceBox, 0, 0, 1, 0);
  99. drawLineSegment.objectType = DrawClass.Label;
  100. drawLineSegment.drawToolType = DrawToolType.DrawLineSegment;
  101. drawLineSegment.ISurfaceBox = surfaceBox;
  102. drawLineSegment.startPoint = this.startPoint;
  103. drawLineSegment.endPoint = this.endPoint;
  104. drawLineSegment.hPath.AddLine(new PointF(-10, 0), new PointF(10, 0));
  105. drawLineSegment.HookCap = new CustomLineCap(null, hPath);
  106. drawLineSegment.HookCap.SetStrokeCaps(LineCap.Round, LineCap.Round);
  107. drawLineSegment.labelLineSegment = this.labelLineSegment;
  108. FillDrawObjectFields(drawLineSegment);
  109. return drawLineSegment;
  110. }
  111. public override void Draw(Graphics g)
  112. {
  113. g.SmoothingMode = SmoothingMode.AntiAlias;
  114. Color color = Color.FromArgb(this.labelLineSegment.lineColor);
  115. Pen pen = new Pen(color, this.labelLineSegment.lineWidth);
  116. pen.DashStyle = (DashStyle)this.labelLineSegment.lineStyle;
  117. pen.CustomStartCap = HookCap;
  118. pen.CustomEndCap = HookCap;
  119. g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  120. pen.Dispose();
  121. }
  122. public override int HandleCount
  123. {
  124. get
  125. {
  126. return 2;
  127. }
  128. }
  129. /// <summary>
  130. /// Get handle pointscroll by 1-based number
  131. /// </summary>
  132. /// <param name="handleNumber"></param>
  133. /// <returns></returns>
  134. public override PointF GetHandle(int handleNumber)
  135. {
  136. if (handleNumber == 1)
  137. return startPoint;
  138. else
  139. return endPoint;
  140. }
  141. /// <summary>
  142. /// Hit test.
  143. /// Return value: -1 - no hit
  144. /// 0 - hit anywhere
  145. /// > 1 - handle number
  146. /// </summary>
  147. /// <param name="pointscroll"></param>
  148. /// <returns></returns>
  149. public override int HitTest(Point point)
  150. {
  151. if (Selected)
  152. {
  153. for (int i = 1; i <= HandleCount; i++)
  154. {
  155. if (GetHandleRectangle(i).Contains(point))
  156. return i;
  157. }
  158. }
  159. if (PointInObject(point))
  160. return 0;
  161. return -1;
  162. }
  163. protected override bool PointInObject(Point point)
  164. {
  165. CreateObjects();
  166. return AreaRegion.IsVisible(point);
  167. }
  168. public override bool IntersectsWith(Rectangle rectangle)
  169. {
  170. CreateObjects();
  171. return AreaRegion.IsVisible(rectangle);
  172. }
  173. public override Cursor GetHandleCursor(int handleNumber)
  174. {
  175. switch (handleNumber)
  176. {
  177. case 1:
  178. case 2:
  179. return Cursors.SizeAll;
  180. default:
  181. return Cursors.Default;
  182. }
  183. }
  184. public override void MoveHandleTo(Point point, int handleNumber)
  185. {
  186. if (handleNumber == 1)
  187. startPoint = point;
  188. else
  189. endPoint = point;
  190. Invalidate();
  191. }
  192. public override void Move(int deltaX, int deltaY)
  193. {
  194. int x = ISurfaceBox.UnscaleScalar(deltaX);
  195. int y = ISurfaceBox.UnscaleScalar(deltaY);
  196. startPoint.X += x;
  197. startPoint.Y += y;
  198. endPoint.X += x;
  199. endPoint.Y += y;
  200. Invalidate();
  201. }
  202. public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  203. {
  204. info.AddValue(
  205. String.Format(CultureInfo.InvariantCulture,
  206. "{0}{1}",
  207. entryStart, orderNumber),
  208. startPoint);
  209. info.AddValue(
  210. String.Format(CultureInfo.InvariantCulture,
  211. "{0}{1}",
  212. entryEnd, orderNumber),
  213. endPoint);
  214. base.SaveToStream(info, orderNumber);
  215. }
  216. public override void LoadFromStream(SerializationInfo info, int orderNumber)
  217. {
  218. startPoint = (Point)info.GetValue(
  219. String.Format(CultureInfo.InvariantCulture,
  220. "{0}{1}",
  221. entryStart, orderNumber),
  222. typeof(Point));
  223. endPoint = (Point)info.GetValue(
  224. String.Format(CultureInfo.InvariantCulture,
  225. "{0}{1}",
  226. entryEnd, orderNumber),
  227. typeof(Point));
  228. base.LoadFromStream(info, orderNumber);
  229. }
  230. /// <summary>
  231. /// Invalidate object.
  232. /// When object is invalidated, path used for hit test
  233. /// is released and should be created again.
  234. /// </summary>
  235. protected void Invalidate()
  236. {
  237. if (AreaPath != null)
  238. {
  239. AreaPath.Dispose();
  240. AreaPath = null;
  241. }
  242. if (AreaPen != null)
  243. {
  244. AreaPen.Dispose();
  245. AreaPen = null;
  246. }
  247. if (AreaRegion != null)
  248. {
  249. AreaRegion.Dispose();
  250. AreaRegion = null;
  251. }
  252. }
  253. /// <summary>
  254. /// Create graphic objects used from hit test.
  255. /// </summary>
  256. protected virtual void CreateObjects()
  257. {
  258. if (AreaPath != null)
  259. return;
  260. // Create path which contains wide line
  261. // for easy mouse selection
  262. AreaPath = new GraphicsPath();
  263. AreaPen = new Pen(Color.Black, 7);
  264. AreaPath.AddLine(startPoint.X-1, startPoint.Y-1, endPoint.X+1, endPoint.Y+1);
  265. AreaPath.Widen(AreaPen);
  266. // Create region from the path
  267. AreaRegion = new Region(AreaPath);
  268. }
  269. protected GraphicsPath AreaPath
  270. {
  271. get
  272. {
  273. return areaPath;
  274. }
  275. set
  276. {
  277. areaPath = value;
  278. }
  279. }
  280. protected Pen AreaPen
  281. {
  282. get
  283. {
  284. return areaPen;
  285. }
  286. set
  287. {
  288. areaPen = value;
  289. }
  290. }
  291. protected Region AreaRegion
  292. {
  293. get
  294. {
  295. return areaRegion;
  296. }
  297. set
  298. {
  299. areaRegion = value;
  300. }
  301. }
  302. public override RectangleF GetBoundingBox()
  303. {
  304. RectangleF rectangle = GetNotmalizedRectangle(startPoint, endPoint);
  305. return rectangle;
  306. }
  307. public static RectangleF GetNotmalizedRectangle(PointF a, PointF b)
  308. {
  309. RectangleF rect = new RectangleF();
  310. if (a.X < b.X)
  311. {
  312. rect.X = a.X;
  313. rect.Width = b.X - a.X;
  314. }
  315. else
  316. {
  317. rect.X = b.X;
  318. rect.Width = a.X - b.X;
  319. }
  320. if (a.Y < b.Y)
  321. {
  322. rect.Y = a.Y;
  323. rect.Height = b.Y - a.Y;
  324. }
  325. else
  326. {
  327. rect.Y = b.Y;
  328. rect.Height = a.Y - b.Y;
  329. }
  330. return rect;
  331. }
  332. /// <summary>
  333. /// Draw tracker for selected object
  334. /// </summary>
  335. /// <param name="g"></param>
  336. public override void DrawTracker(Graphics g)
  337. {
  338. if (!Selected)
  339. return;
  340. SolidBrush brush = new SolidBrush(Color.FromArgb(MarkpointAreaColor));
  341. Pen pen = new Pen(Color.FromArgb(MarkpointLineColor), MarkpointLineWidth);
  342. for (int i = 1; i <= HandleCount; i++)
  343. {
  344. switch (MarkpointStyle)
  345. {
  346. case 0:
  347. g.FillRectangle(brush, GetHandleRectangle(i));
  348. g.DrawRectangle(pen, GetHandleRectangle(i));
  349. break;
  350. case 1:
  351. g.FillEllipse(brush, GetHandleRectangle(i));
  352. g.DrawEllipse(pen, GetHandleRectangle(i));
  353. break;
  354. case 2:
  355. g.FillPolygon(brush, GetHandlePoint(i));
  356. g.DrawPolygon(pen, GetHandlePoint(i));
  357. break;
  358. }
  359. }
  360. brush.Dispose();
  361. pen.Dispose();
  362. }
  363. public override List<PointF> GetPoints()
  364. {
  365. List<PointF> points = new List<PointF>();
  366. points.Add(startPoint);
  367. points.Add(endPoint);
  368. return points;
  369. }
  370. public override ParentStyleModel GetStyle()
  371. {
  372. return labelLineSegment;
  373. }
  374. }
  375. }