DrawLocationCross.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using PaintDotNet.Annotation.Enum;
  10. namespace PaintDotNet.Annotation.ImageCollect
  11. {
  12. public class DrawLocationCross : DrawObject
  13. {
  14. private int m_width;
  15. private int m_height;
  16. private int m_z;
  17. private Dictionary<int, object> points;
  18. public DrawLocationCross(ISurfaceBox surfaceBox, int x, int y, int width, int height, int z) : base()
  19. {
  20. this.objectType = DrawClass.Stitch;
  21. this.drawToolType = DrawToolType.DrawLocationCross;
  22. this.points = new Dictionary<int, object>();
  23. m_z = z;
  24. m_width = width;
  25. m_height = height;
  26. rectangle.X = x;
  27. rectangle.Y = y;
  28. rectangle.Width = width;
  29. rectangle.Height = height;
  30. Initialize();
  31. }
  32. public override DrawObject Clone()
  33. {
  34. DrawLocationCross drawRectangle = new DrawLocationCross(ISurfaceBox, 0, 0, 1, 0, 0);
  35. drawRectangle.objectType = DrawClass.Stitch;
  36. drawRectangle.drawToolType = DrawToolType.DrawLocationCross;
  37. drawRectangle.ISurfaceBox = this.ISurfaceBox;
  38. drawRectangle.rectangle = this.rectangle;
  39. FillDrawObjectFields(drawRectangle);
  40. return drawRectangle;
  41. }
  42. public override RectangleF GetBoundingBox()
  43. {
  44. return rectangle;
  45. }
  46. public override void Draw(Graphics g)
  47. {
  48. //样式暂时写死,没有对应样式设置
  49. int dashStyle = 0; //线形
  50. int thickness = 5; //粗细
  51. int color = -59111; //颜色
  52. Pen pen = new Pen(new SolidBrush(Color.FromArgb(color)), thickness);
  53. pen.DashStyle = (DashStyle)dashStyle;
  54. float lineLength = m_width < m_height ? m_width * 0.3f : m_height * 0.3f;//线长
  55. float x = rectangle.X + (m_width - lineLength) / 2;
  56. float y = rectangle.Y + (m_height - lineLength) / 2;
  57. g.DrawLine(pen, x, rectangle.Y + m_height / 2, x + lineLength, rectangle.Y + m_height / 2);
  58. g.DrawLine(pen, rectangle.X + m_width / 2, y, rectangle.X + m_width / 2, y + lineLength);
  59. if(this.points.Count > 0)
  60. {
  61. this.points[0] = this.ISurfaceBox.ScalePointToRulerPoint(new PointF(rectangle.X + m_width / 2, rectangle.Y + m_height / 2));
  62. }
  63. else
  64. {
  65. this.points.Add(0, this.ISurfaceBox.ScalePointToRulerPoint(new PointF(rectangle.X + m_width / 2, rectangle.Y + m_height / 2)));
  66. this.points.Add(1, m_z);
  67. }
  68. }
  69. protected void SetRectangle(float x, float y, float width, float height)
  70. {
  71. this.rectangle.X = x;
  72. this.rectangle.Y = y;
  73. this.rectangle.Width = width;
  74. this.rectangle.Height = height;
  75. }
  76. /// <summary>
  77. /// Get number of handles
  78. /// </summary>
  79. public override int HandleCount
  80. {
  81. get
  82. {
  83. return 8;
  84. }
  85. }
  86. /// <summary>
  87. /// Get handle pointscroll by 1-based number
  88. /// </summary>
  89. /// <param name="handleNumber"></param>
  90. /// <returns></returns>
  91. public override PointF GetHandle(int handleNumber)
  92. {
  93. float x, y, xCenter, yCenter;
  94. xCenter = this.rectangle.X + this.rectangle.Width / 2;
  95. yCenter = this.rectangle.Y + this.rectangle.Height / 2;
  96. x = this.rectangle.X;
  97. y = this.rectangle.Y;
  98. switch (handleNumber)
  99. {
  100. case 1:
  101. x = this.rectangle.X;
  102. y = this.rectangle.Y;
  103. break;
  104. case 2:
  105. x = xCenter;
  106. y = this.rectangle.Y;
  107. break;
  108. case 3:
  109. x = this.rectangle.Right;
  110. y = this.rectangle.Y;
  111. break;
  112. case 4:
  113. x = this.rectangle.Right;
  114. y = yCenter;
  115. break;
  116. case 5:
  117. x = this.rectangle.Right;
  118. y = this.rectangle.Bottom;
  119. break;
  120. case 6:
  121. x = xCenter;
  122. y = this.rectangle.Bottom;
  123. break;
  124. case 7:
  125. x = this.rectangle.X;
  126. y = this.rectangle.Bottom;
  127. break;
  128. case 8:
  129. x = this.rectangle.X;
  130. y = yCenter;
  131. break;
  132. }
  133. return new PointF(x, y);
  134. }
  135. public override Rectangle GetHandleRectangle(int handleNumber)
  136. {
  137. PointF point = GetHandle(handleNumber);
  138. return new Rectangle((int)(point.X - 100), (int)(point.Y - 100), 201, 201);
  139. }
  140. /// <summary>
  141. /// Hit test.
  142. /// Return value: -1 - no hit
  143. /// 0 - hit anywhere
  144. /// > 1 - handle number
  145. /// </summary>
  146. /// <param name="pointscroll"></param>
  147. /// <returns></returns>
  148. public override int HitTest(Point point)
  149. {
  150. if (Selected)
  151. {
  152. for (int i = 1; i <= HandleCount; i++)
  153. {
  154. if (GetHandleRectangle(i).Contains(point))
  155. return i;
  156. }
  157. }
  158. if (PointInObject(point))
  159. return 0;
  160. return -1;
  161. }
  162. protected override bool PointInObject(Point point)
  163. {
  164. return rectangle.Contains(point);
  165. }
  166. /// <summary>
  167. /// Get cursor for the handle
  168. /// </summary>
  169. /// <param name="handleNumber"></param>
  170. /// <returns></returns>
  171. public override Cursor GetHandleCursor(int handleNumber)
  172. {
  173. return null;
  174. }
  175. /// <summary>
  176. /// Move handle to new pointscroll (resizing)
  177. /// </summary>
  178. /// <param name="pointscroll"></param>
  179. /// <param name="handleNumber"></param>
  180. public override void MoveHandleTo(Point point, int handleNumber)
  181. {
  182. return;
  183. }
  184. public override bool IntersectsWith(Rectangle rectangle)
  185. {
  186. return Rectangle.IntersectsWith(rectangle);
  187. }
  188. /// <summary>
  189. /// Move object
  190. /// </summary>
  191. /// <param name="deltaX"></param>
  192. /// <param name="deltaY"></param>
  193. public override void Move(int deltaX, int deltaY)
  194. {
  195. int x = ISurfaceBox.UnscaleScalar(deltaX);
  196. int y = ISurfaceBox.UnscaleScalar(deltaY);
  197. rectangle.X += x;
  198. rectangle.Y += y;
  199. }
  200. public override List<PointF> GetPoints()
  201. {
  202. List<PointF> points = new List<PointF>();
  203. points.Add(new PointF(rectangle.X, rectangle.Y));
  204. points.Add(new PointF(rectangle.Right, rectangle.Bottom));
  205. return points;
  206. }
  207. public List<PointF> GetRulerPoints()
  208. {
  209. List<PointF> points = new List<PointF>();
  210. points.Add(this.ISurfaceBox.ScalePointToRulerPoint(new PointF(rectangle.X, rectangle.Y)));
  211. points.Add(this.ISurfaceBox.ScalePointToRulerPoint(new PointF(rectangle.Right, rectangle.Bottom)));
  212. return points;
  213. }
  214. public Dictionary<int, object> GetViewPoints()
  215. {
  216. return this.points;
  217. }
  218. public void SetZPostion(Dictionary<int, object> point)
  219. {
  220. this.points = point;
  221. }
  222. }
  223. }