ViewBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.CommTool;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8. /*
  9. * 视场,
  10. * 全部图形构成基于点阵(Points)拟合.
  11. * 图形变换,对外属性均依赖于点阵的换算.
  12. * 点阵集合继承DrawObject 的 pointArrayList 字段
  13. * 全部接口数值皆为原始数值并未进行缩放处理.
  14. */
  15. namespace PaintDotNet.Annotation.FieldView
  16. {
  17. public abstract class ViewBase : DrawObject
  18. {
  19. /// <summary>
  20. /// 鼠标形状
  21. /// </summary>
  22. public static Cursor m_rotateCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationRotateHandle.cur"));
  23. public static Cursor m_resizeCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationResizeHandle.cur"));
  24. #region Points
  25. public virtual List<PointF> Points
  26. {
  27. get
  28. {
  29. if (pointArrayList == null)
  30. pointArrayList = new List<PointF>();
  31. return pointArrayList;
  32. }
  33. set
  34. {
  35. pointArrayList = new List<PointF>();
  36. foreach (var p in value)
  37. {
  38. pointArrayList.Add(p);
  39. }
  40. }
  41. }
  42. public void AddPoint(int x, int y)
  43. {
  44. AddPoint(new PointF(x, y));
  45. }
  46. protected void AddPoint(PointF point)
  47. {
  48. Points.Add(point);
  49. }
  50. public override List<PointF> GetPoints()
  51. {
  52. return Points;
  53. }
  54. #endregion
  55. #region Bounding Rectangle
  56. public override RectangleF Rectangle
  57. {
  58. get
  59. {
  60. return GetBoundingBox();
  61. }
  62. }
  63. /// <summary>
  64. /// 外接矩形
  65. /// </summary>
  66. public override RectangleF GetBoundingBox()
  67. {
  68. int minx = 0, maxx = 0, miny = 0, maxy = 0;
  69. for (int i = 0; i < Points.Count; i++)
  70. {
  71. if (i == 0)
  72. {
  73. minx = maxx = (int)(Points[i].X);
  74. miny = maxy = (int)(Points[i].Y);
  75. }
  76. else
  77. {
  78. if (Points[i].X > maxx) maxx = (int)(Points[i].X);
  79. if (Points[i].X < minx) minx = (int)(Points[i].X);
  80. if (Points[i].Y > maxy) maxy = (int)(Points[i].Y);
  81. if (Points[i].Y < miny) miny = (int)(Points[i].Y);
  82. }
  83. }
  84. return new Rectangle(minx, miny, maxx - minx, maxy - miny);
  85. }
  86. #endregion
  87. #region Draw
  88. /// <summary>
  89. /// 指定如何组合不同的剪辑区域
  90. /// 用于存储是合并视场、还是剪切视场
  91. /// </summary>
  92. public CombineMode combineMode;
  93. /// <summary>
  94. /// Draw rectangle
  95. /// </summary>
  96. /// <param name="g"></param>
  97. public override void Draw(Graphics g)
  98. {
  99. Region reg = g.Clip;
  100. ViewBase view = null;
  101. for (int i = ISurfaceBox.GraphicsList.Count - 1; ;)
  102. {
  103. if (ISurfaceBox.GraphicsList[i].objectType == DrawClass.View)
  104. {
  105. view = (ViewBase)ISurfaceBox.GraphicsList[i];
  106. if (view.combineMode == CombineMode.Union)
  107. {
  108. break;
  109. }
  110. }
  111. i--;
  112. if (i < 0)
  113. {
  114. view.combineMode = CombineMode.Union;
  115. break;
  116. }
  117. }
  118. //获取所有合并的视场
  119. Region union = GetUnionOrDeleteView(CombineMode.Union);
  120. //获取所有删除的视场
  121. Region delete = GetUnionOrDeleteView(CombineMode.Complement);
  122. if (union != null)
  123. {
  124. reg.Xor(union);
  125. }
  126. if (delete != null)
  127. {
  128. reg.Union(delete);
  129. }
  130. SolidBrush sb = new SolidBrush(Color.FromArgb(127, 255, 255, 255));
  131. g.FillRegion(sb, reg);
  132. }
  133. /// <summary>
  134. /// 获取所有合并或删除的视场
  135. /// </summary>
  136. /// <returns></returns>
  137. private Region GetUnionOrDeleteView(CombineMode combineMode)
  138. {
  139. Region region = null;
  140. if (ISurfaceBox.GraphicsList != null && ISurfaceBox.GraphicsList.Count > 0)
  141. {
  142. for (int i = 0; i < ISurfaceBox.GraphicsList.Count; i++)
  143. {
  144. if (ISurfaceBox.GraphicsList[i].objectType == DrawClass.View)
  145. {
  146. ViewBase view = (ViewBase)ISurfaceBox.GraphicsList[i];
  147. GraphicsPath path = new GraphicsPath();
  148. switch (view.drawToolType)
  149. {
  150. case DrawToolType.ViewOval:
  151. case DrawToolType.ViewCircle:
  152. path.AddClosedCurve(ISurfaceBox.GraphicsList[i].GetPoints().ToArray());
  153. break;
  154. case DrawToolType.ViewSquare:
  155. case DrawToolType.ViewRectangle:
  156. case DrawToolType.ViewTriangle:
  157. case DrawToolType.ViewPolygon:
  158. case DrawToolType.ViewTriangleEx:
  159. case DrawToolType.ViewRectangleEx:
  160. if ((ISurfaceBox.GraphicsList[i]).GetPoints().Count > 2)
  161. {
  162. path.AddPolygon((ISurfaceBox.GraphicsList[i]).GetPoints().ToArray());
  163. }
  164. break;
  165. }
  166. if (view.combineMode == combineMode)
  167. {
  168. if (region == null)
  169. region = new Region(path);
  170. else
  171. region.Union(path);
  172. }
  173. }
  174. }
  175. }
  176. return region;
  177. }
  178. public override void DrawTracker(Graphics g)
  179. {
  180. if (!Selected)
  181. return;
  182. SolidBrush brush = new SolidBrush(Color.FromArgb(184, Color.Black));
  183. if (Rotatable && Points.Count > 2)
  184. {
  185. for (int i = 1; i <= HandleCount - 1; i++)
  186. {
  187. g.FillRectangle(brush, GetHandleRectangle(i));
  188. }
  189. brush = new SolidBrush(Color.Red);
  190. g.FillEllipse(brush, GetHandleRectangle(HandleCount));
  191. brush.Dispose();
  192. }
  193. else
  194. {
  195. for (int i = 1; i <= HandleCount; i++)
  196. {
  197. g.FillRectangle(brush, GetHandleRectangle(i));
  198. }
  199. }
  200. }
  201. #endregion
  202. #region Rotate
  203. protected PointF _lastPoint;
  204. protected PointF _lastCenter;
  205. protected bool _isRotating;
  206. /// <summary>
  207. /// 设置可旋转性,用于显示旋转Handle
  208. /// </summary>
  209. public bool Rotatable = false;
  210. /// <summary>
  211. /// 图形斜率,用于旋转
  212. /// </summary>
  213. public double K
  214. {
  215. get;
  216. set;
  217. } = 0;
  218. public virtual void SetK() { }
  219. public virtual PointF Center
  220. {
  221. get
  222. {
  223. var centerX = Rectangle.X + Math.Abs(Rectangle.Width / 2);
  224. var centerY = Rectangle.Y + Math.Abs(Rectangle.Height / 2);
  225. return new PointF(centerX, centerY);
  226. }
  227. set { }
  228. }
  229. protected virtual void StartRotate()
  230. {
  231. _isRotating = true;
  232. _lastCenter = Center;
  233. }
  234. protected virtual void EndRotate()
  235. {
  236. _isRotating = false;
  237. SetK();
  238. }
  239. protected virtual void MoveRotate(PointF point)
  240. {
  241. if (!_isRotating)
  242. {
  243. StartRotate();
  244. _lastPoint = point;
  245. }
  246. double angle = BasicCalculationHelper.CalculateAngle(_lastCenter.X, _lastCenter.Y, _lastPoint.X, _lastPoint.Y, point.X, point.Y);
  247. _lastPoint = point;
  248. for (int i = 0; i < Points.Count; i++)
  249. {
  250. Points[i] = BasicCalculationHelper.GetAnglePoint(Points[i], _lastCenter, angle);
  251. }
  252. }
  253. /// <summary>
  254. /// 标准化外接矩形
  255. /// </summary>
  256. public override void Normalize()
  257. {
  258. if (_isRotating)
  259. EndRotate();
  260. }
  261. #endregion
  262. #region Select
  263. /// <summary>
  264. /// Hit test.
  265. /// Return value: -1 - no hit
  266. /// 0 - hit anywhere
  267. /// > 1 - handle number
  268. /// </summary>
  269. /// <param name="pointscroll"></param>
  270. /// <returns></returns>
  271. public override int HitTest(Point point)
  272. {
  273. if (Selected)
  274. {
  275. for (int i = 1; i <= HandleCount; i++)
  276. {
  277. if (GetHandleRectangle(i).Contains(point))
  278. return i;
  279. }
  280. }
  281. if (PointInObject(point))
  282. return 0;
  283. return -1;
  284. }
  285. protected override bool PointInObject(Point point)
  286. {
  287. int counter = 0;
  288. int i;
  289. double xinters;
  290. PointF p1, p2;
  291. int pointCount = Points.Count;
  292. p1 = Points[0];
  293. for (i = 1; i <= pointCount; i++)
  294. {
  295. p2 = Points[i % pointCount];
  296. if (point.Y > Math.Min(p1.Y, p2.Y)//校验点的Y大于线段端点的最小Y
  297. && point.Y <= Math.Max(p1.Y, p2.Y))//校验点的Y小于线段端点的最大Y
  298. {
  299. if (point.X <= Math.Max(p1.X, p2.X))//校验点的X小于等线段端点的最大X(使用校验点的左射线判断).
  300. {
  301. if (p1.Y != p2.Y)//线段不平行于X轴
  302. {
  303. xinters = (point.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
  304. if (p1.X == p2.X || point.X <= xinters)
  305. {
  306. counter++;
  307. }
  308. }
  309. }
  310. }
  311. p1 = p2;
  312. }
  313. return counter % 2 != 0;
  314. }
  315. public override bool IntersectsWith(Rectangle rectangle)
  316. {
  317. return Rectangle.IntersectsWith(rectangle);
  318. }
  319. #endregion
  320. public override void Move(int deltaX, int deltaY)
  321. {
  322. for (int i = 0; i < Points.Count; i++)
  323. {
  324. PointF p = Points[i];
  325. p.X += ISurfaceBox.UnscaleScalar(deltaX);
  326. p.Y += ISurfaceBox.UnscaleScalar(deltaY);
  327. Points[i] = p;
  328. }
  329. OnPropertyChanged();
  330. }
  331. /// <summary>
  332. /// 获取视场面积
  333. /// </summary>
  334. /// <returns></returns>
  335. public virtual double Getacreage()
  336. {
  337. var count = Points.Count;
  338. double area0 = 0;
  339. double area1 = 0;
  340. for (int i = 0; i < count; i++)
  341. {
  342. var x = Points[i].X;
  343. var y = i + 1 < count ? Points[i + 1].Y : Points[0].Y;
  344. area0 += x * y;
  345. x = Points[i].Y;
  346. y = i + 1 < count ? Points[i + 1].X : Points[0].X;
  347. area1 += x * y;
  348. }
  349. return Math.Abs(0.5 * (area0 - area1));
  350. }
  351. }
  352. }