DrawOneArrowLine.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 DrawOneArrowLine : 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.OneWayArrow labelOneArrowStyleModel;
  30. public LabelStyleModel.OneWayArrow LabelOneArrowStyleModel
  31. {
  32. set
  33. {
  34. this.labelOneArrowStyleModel = value;
  35. }
  36. }
  37. public DrawOneArrowLine(ISurfaceBox surfaceBox, int x1, int y1, int x2, int y2) : base()
  38. {
  39. this.objectType = DrawClass.Label;
  40. this.drawToolType = DrawToolType.DrawOneArrowLine;
  41. this.labelOneArrowStyleModel = surfaceBox.GetLabelStyleModel().oneWayArrowModel;
  42. startPoint.X = x1;
  43. startPoint.Y = y1;
  44. endPoint.X = x2;
  45. endPoint.Y = y2;
  46. Initialize();
  47. }
  48. public DrawOneArrowLine(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  49. {
  50. this.objectType = DrawClass.Label;
  51. this.drawToolType = DrawToolType.DrawOneArrowLine;
  52. this.ISurfaceBox = surfaceBox;
  53. labelOneArrowStyleModel = (LabelStyleModel.OneWayArrow)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. DrawOneArrowLine drawOneArrowLine = new DrawOneArrowLine(this.ISurfaceBox,0,0,0,0);
  65. drawOneArrowLine.objectType = DrawClass.Label;
  66. drawOneArrowLine.drawToolType = DrawToolType.DrawOneArrowLine;
  67. drawOneArrowLine.ISurfaceBox = this.ISurfaceBox;
  68. drawOneArrowLine.startPoint = this.startPoint;
  69. drawOneArrowLine.endPoint = this.endPoint;
  70. FillDrawObjectFields(drawOneArrowLine);
  71. return drawOneArrowLine;
  72. }
  73. public override DrawObject Clone(ISurfaceBox surfaceBox)
  74. {
  75. DrawOneArrowLine drawOneArrowLine = new DrawOneArrowLine(surfaceBox, 0, 0, 0, 0);
  76. drawOneArrowLine.objectType = DrawClass.Label;
  77. drawOneArrowLine.drawToolType = DrawToolType.DrawOneArrowLine;
  78. drawOneArrowLine.ISurfaceBox = surfaceBox;
  79. drawOneArrowLine.startPoint = this.startPoint;
  80. drawOneArrowLine.endPoint = this.endPoint;
  81. drawOneArrowLine.labelOneArrowStyleModel = this.labelOneArrowStyleModel;
  82. FillDrawObjectFields(drawOneArrowLine);
  83. return drawOneArrowLine;
  84. }
  85. public override void Draw(Graphics g)
  86. {
  87. g.SmoothingMode = SmoothingMode.AntiAlias;
  88. Color color = Color.FromArgb(this.labelOneArrowStyleModel.lineColor);
  89. Pen pen = new Pen(color, this.labelOneArrowStyleModel.lineWidth);
  90. pen.DashStyle = (DashStyle)this.labelOneArrowStyleModel.lineStyle;
  91. pen.CustomEndCap = lineCap;
  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 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. public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  176. {
  177. info.AddValue(
  178. String.Format(CultureInfo.InvariantCulture,
  179. "{0}{1}",
  180. entryStart, orderNumber),
  181. startPoint);
  182. info.AddValue(
  183. String.Format(CultureInfo.InvariantCulture,
  184. "{0}{1}",
  185. entryEnd, orderNumber),
  186. endPoint);
  187. base.SaveToStream(info, orderNumber);
  188. }
  189. public override void LoadFromStream(SerializationInfo info, int orderNumber)
  190. {
  191. startPoint = (Point)info.GetValue(
  192. String.Format(CultureInfo.InvariantCulture,
  193. "{0}{1}",
  194. entryStart, orderNumber),
  195. typeof(Point));
  196. endPoint = (Point)info.GetValue(
  197. String.Format(CultureInfo.InvariantCulture,
  198. "{0}{1}",
  199. entryEnd, orderNumber),
  200. typeof(Point));
  201. base.LoadFromStream(info, orderNumber);
  202. }
  203. /// <summary>
  204. /// Invalidate object.
  205. /// When object is invalidated, path used for hit test
  206. /// is released and should be created again.
  207. /// </summary>
  208. protected void Invalidate()
  209. {
  210. if (AreaPath != null)
  211. {
  212. AreaPath.Dispose();
  213. AreaPath = null;
  214. }
  215. if (AreaPen != null)
  216. {
  217. AreaPen.Dispose();
  218. AreaPen = null;
  219. }
  220. if (AreaRegion != null)
  221. {
  222. AreaRegion.Dispose();
  223. AreaRegion = null;
  224. }
  225. }
  226. /// <summary>
  227. /// Create graphic objects used from hit test.
  228. /// </summary>
  229. protected virtual void CreateObjects()
  230. {
  231. if (AreaPath != null)
  232. return;
  233. // Create path which contains wide line
  234. // for easy mouse selection
  235. AreaPath = new GraphicsPath();
  236. AreaPen = new Pen(Color.Black, 7);
  237. AreaPath.AddLine(startPoint.X-1, startPoint.Y-1, endPoint.X+1, endPoint.Y+1);
  238. AreaPath.Widen(AreaPen);
  239. // Create region from the path
  240. AreaRegion = new Region(AreaPath);
  241. }
  242. protected GraphicsPath AreaPath
  243. {
  244. get
  245. {
  246. return areaPath;
  247. }
  248. set
  249. {
  250. areaPath = value;
  251. }
  252. }
  253. protected Pen AreaPen
  254. {
  255. get
  256. {
  257. return areaPen;
  258. }
  259. set
  260. {
  261. areaPen = value;
  262. }
  263. }
  264. protected Region AreaRegion
  265. {
  266. get
  267. {
  268. return areaRegion;
  269. }
  270. set
  271. {
  272. areaRegion = value;
  273. }
  274. }
  275. public override RectangleF GetBoundingBox()
  276. {
  277. RectangleF rectangle = GetNotmalizedRectangle(startPoint, endPoint);
  278. return rectangle;
  279. }
  280. public static RectangleF GetNotmalizedRectangle(PointF a, PointF b)
  281. {
  282. RectangleF rect = new RectangleF();
  283. if (a.X < b.X)
  284. {
  285. rect.X = a.X;
  286. rect.Width = b.X - a.X;
  287. }
  288. else
  289. {
  290. rect.X = b.X;
  291. rect.Width = a.X - b.X;
  292. }
  293. if (a.Y < b.Y)
  294. {
  295. rect.Y = a.Y;
  296. rect.Height = b.Y - a.Y;
  297. }
  298. else
  299. {
  300. rect.Y = b.Y;
  301. rect.Height = a.Y - b.Y;
  302. }
  303. return rect;
  304. }
  305. /// <summary>
  306. /// Draw tracker for selected object
  307. /// </summary>
  308. /// <param name="g"></param>
  309. public override void DrawTracker(Graphics g)
  310. {
  311. if (!Selected)
  312. return;
  313. SolidBrush brush = new SolidBrush(Color.Black);
  314. for (int i = 1; i <= HandleCount; i++)
  315. {
  316. g.FillRectangle(brush, GetHandleRectangle(i));
  317. }
  318. brush.Dispose();
  319. }
  320. public override List<PointF> GetPoints()
  321. {
  322. List<PointF> points = new List<PointF>();
  323. points.Add(startPoint);
  324. points.Add(endPoint);
  325. return points;
  326. }
  327. public override ParentStyleModel GetStyle()
  328. {
  329. return labelOneArrowStyleModel;
  330. }
  331. }
  332. }