DrawTwoArrowLine.cs 12 KB

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