DrawSmudgePolygon.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.Windows.Forms;
  9. namespace PaintDotNet.Annotation.Label
  10. {
  11. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  12. using PointList = List<PointF>;
  13. /// <summary>
  14. /// 污迹处理->多边形
  15. /// </summary>
  16. public class DrawSmudgePolygon : DrawObject
  17. {
  18. /// <summary>
  19. /// 鼠标形状
  20. /// </summary>
  21. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
  22. /// <summary>
  23. /// 点集合
  24. /// </summary>
  25. private PointList pointArray;
  26. /// <summary>
  27. /// 标注样式信息model
  28. /// </summary>
  29. public PolygonPolygon polygonStyle;
  30. private RectangleF rectangleF;
  31. public DrawSmudgePolygon() : base()
  32. {
  33. this.objectType = DrawClass.Label;
  34. this.drawToolType = DrawToolType.DrawSmudgePolygon;
  35. pointArray = new PointList();
  36. Initialize();
  37. }
  38. public DrawSmudgePolygon(ISurfaceBox surfaceBox, int x1, int y1) : base()//DrawSmudgePolygon
  39. {
  40. this.objectType = DrawClass.Label;
  41. this.drawToolType = DrawToolType.DrawSmudgePolygon;
  42. polygonStyle = surfaceBox.GetLabelStyleModel().polygonPolygon;
  43. pointArray = new PointList();
  44. startPoint.X = x1;
  45. startPoint.Y = y1;
  46. pointArray.Add(new PointF(x1, y1));
  47. Initialize();
  48. }
  49. public DrawSmudgePolygon(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  50. {
  51. this.objectType = DrawClass.Label;
  52. this.drawToolType = DrawToolType.DrawSmudgePolygon;
  53. this.ISurfaceBox = surfaceBox;
  54. polygonStyle = (PolygonPolygon)parentStyleModel;
  55. pointArray = points;
  56. startPoint = points[points.Count - 2];
  57. endPoint = points[points.Count - 1];
  58. }
  59. /// <summary>
  60. /// Clone this instance
  61. /// </summary>
  62. public override DrawObject Clone()
  63. {
  64. DrawSmudgePolygon drawSmudgePolygon = new DrawSmudgePolygon();
  65. drawSmudgePolygon.objectType = DrawClass.Label;
  66. drawSmudgePolygon.drawToolType = DrawToolType.DrawSmudgePolygon;
  67. drawSmudgePolygon.ISurfaceBox = this.ISurfaceBox;
  68. foreach (PointF p in this.pointArray)
  69. {
  70. drawSmudgePolygon.pointArray.Add(p);
  71. }
  72. drawSmudgePolygon.startPoint = this.startPoint;
  73. drawSmudgePolygon.endPoint = this.endPoint;
  74. FillDrawObjectFields(drawSmudgePolygon);
  75. return drawSmudgePolygon;
  76. }
  77. public override DrawObject Clone(ISurfaceBox surfaceBox)
  78. {
  79. DrawSmudgePolygon drawSmudgePolygon = new DrawSmudgePolygon();
  80. drawSmudgePolygon.objectType = DrawClass.Label;
  81. drawSmudgePolygon.drawToolType = DrawToolType.DrawSmudgePolygon;
  82. drawSmudgePolygon.ISurfaceBox = surfaceBox;
  83. foreach (PointF p in this.pointArray)
  84. {
  85. drawSmudgePolygon.pointArray.Add(p);
  86. }
  87. drawSmudgePolygon.startPoint = this.startPoint;
  88. drawSmudgePolygon.endPoint = this.endPoint;
  89. drawSmudgePolygon.polygonStyle = this.polygonStyle;
  90. FillDrawObjectFields(drawSmudgePolygon);
  91. return drawSmudgePolygon;
  92. }
  93. public override void Draw(Graphics g)
  94. {
  95. if (HandleCount >= 3)
  96. {
  97. g.SmoothingMode = SmoothingMode.AntiAlias;
  98. Color color = Color.FromArgb(this.polygonStyle.lineColor);
  99. Pen pen = new Pen(color, PenWidth);
  100. int penWidth = this.polygonStyle.lineWidth;
  101. pen.DashStyle = (DashStyle)this.polygonStyle.lineStyle;
  102. pen.Width = penWidth;
  103. //Color fillColor = Color.FromArgb(this.polygonStyle.fillColor);
  104. //SolidBrush fillBrush = new SolidBrush(fillColor);
  105. //g.FillPolygon(fillBrush, pointArray.ToArray());
  106. g.DrawPolygon(pen, pointArray.ToArray());
  107. pen.Dispose();
  108. //fillBrush.Dispose();
  109. }
  110. }
  111. public void AddPoint(PointF point)
  112. {
  113. pointArray.Add(point);
  114. }
  115. public override int HandleCount
  116. {
  117. get
  118. {
  119. return pointArray.Count;
  120. }
  121. }
  122. /// <summary>
  123. /// Get handle pointscroll by 1-based number
  124. /// </summary>
  125. /// <param name="handleNumber"></param>
  126. /// <returns></returns>
  127. public override PointF GetHandle(int handleNumber)
  128. {
  129. if (handleNumber < 1)
  130. handleNumber = 1;
  131. if (handleNumber > pointArray.Count)
  132. handleNumber = pointArray.Count;
  133. return pointArray[handleNumber - 1];
  134. }
  135. public override Cursor GetHandleCursor(int handleNumber)
  136. {
  137. return handleCursor;
  138. }
  139. public override void MoveHandleTo(Point point, int handleNumber)
  140. {
  141. if (handleNumber < 1)
  142. handleNumber = 1;
  143. if (handleNumber > pointArray.Count)
  144. handleNumber = pointArray.Count;
  145. pointArray[handleNumber - 1] = point;
  146. }
  147. public override void Move(int deltaX, int deltaY)
  148. {
  149. int n = pointArray.Count;
  150. PointF point;
  151. for (int i = 0; i < n; i++)
  152. {
  153. point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
  154. pointArray[i] = point;
  155. }
  156. }
  157. /// <summary>
  158. /// Draw tracker for selected object
  159. /// </summary>
  160. /// <param name="g"></param>
  161. public override void DrawTracker(Graphics g)
  162. {
  163. if (!Selected)
  164. return;
  165. SolidBrush brush = new SolidBrush(Color.Black);
  166. for (int i = 1; i <= HandleCount; i++)
  167. {
  168. g.FillRectangle(brush, GetHandleRectangle(i));
  169. }
  170. brush.Dispose();
  171. rectangleF = GetBoundingBox();
  172. g.DrawRectangle(new Pen(Color.White), rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
  173. }
  174. /// <summary>
  175. /// Hit test.
  176. /// Return value: -1 - no hit
  177. /// 0 - hit anywhere
  178. /// > 1 - handle number
  179. /// </summary>
  180. /// <param name="pointscroll"></param>
  181. /// <returns></returns>
  182. public override int HitTest(Point point)
  183. {
  184. if (Selected)
  185. {
  186. for (int i = 1; i <= HandleCount; i++)
  187. {
  188. if (GetHandleRectangle(i).Contains(point))
  189. return i;
  190. }
  191. }
  192. if (PointInObject(point))
  193. return 0;
  194. return -1;
  195. }
  196. protected override bool PointInObject(Point point)
  197. {
  198. int counter = 0;
  199. int i;
  200. double xinters;
  201. PointF p1, p2;
  202. int pointCount = pointArray.Count;
  203. p1 = pointArray[0];
  204. for (i = 1; i <= pointCount; i++)
  205. {
  206. p2 = pointArray[i % pointCount];
  207. if (point.Y > Math.Min(p1.Y, p2.Y)//校验点的Y大于线段端点的最小Y
  208. && point.Y <= Math.Max(p1.Y, p2.Y))//校验点的Y小于线段端点的最大Y
  209. {
  210. if (point.X <= Math.Max(p1.X, p2.X))//校验点的X小于等线段端点的最大X(使用校验点的左射线判断).
  211. {
  212. if (p1.Y != p2.Y)//线段不平行于X轴
  213. {
  214. xinters = (point.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
  215. if (p1.X == p2.X || point.X <= xinters)
  216. {
  217. counter++;
  218. }
  219. }
  220. }
  221. }
  222. p1 = p2;
  223. }
  224. if (counter % 2 == 0)
  225. {
  226. return false;
  227. }
  228. else
  229. {
  230. return true;
  231. }
  232. }
  233. public override bool IntersectsWith(Rectangle rectangle)
  234. {
  235. return rectangleF.IntersectsWith(rectangle);
  236. }
  237. public override RectangleF GetBoundingBox()
  238. {
  239. RectangleF rectangle;
  240. float minx = 0, maxx = 0, miny = 0, maxy = 0;
  241. for (int i = 0; i < pointArray.Count; i++)
  242. {
  243. if (i == 0)
  244. {
  245. minx = maxx = pointArray[i].X;
  246. miny = maxy = pointArray[i].Y;
  247. }
  248. else
  249. {
  250. if (pointArray[i].X > maxx) maxx = pointArray[i].X;
  251. if (pointArray[i].X < minx) minx = pointArray[i].X;
  252. if (pointArray[i].Y > maxy) maxy = pointArray[i].Y;
  253. if (pointArray[i].Y < miny) miny = pointArray[i].Y;
  254. }
  255. }
  256. rectangle = new RectangleF(minx, miny, maxx - minx, maxy - miny);
  257. return rectangle;
  258. }
  259. internal void setNextPoint(PointF p)
  260. {
  261. AddPoint(p);
  262. startPoint = endPoint;
  263. endPoint = p;
  264. }
  265. internal void setEndPoint(PointF p)
  266. {
  267. endPoint = p;
  268. }
  269. public override List<PointF> GetPoints()
  270. {
  271. return pointArray;
  272. }
  273. public override ParentStyleModel GetStyle()
  274. {
  275. return polygonStyle;
  276. }
  277. }
  278. }