DrawArtworkRectangle.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using PaintDotNet.Annotation.Enum;
  9. using PaintDotNet.Base.SettingModel;
  10. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  11. namespace PaintDotNet.Annotation.ImageCollect
  12. {
  13. public class DrawArtworkRectangle : DrawObject
  14. {
  15. /// <summary>
  16. /// 标注样式信息model
  17. /// </summary>
  18. public PolygonRectangle labelRectStyleModel;
  19. public Pen pen;
  20. public DrawArtworkRectangle(ISurfaceBox surfaceBox, int id, Pen pen, Rectangle rect) : base()
  21. {
  22. this.ID = id;
  23. this.objectType = DrawClass.Label;
  24. this.drawToolType = DrawToolType.DrawArtworkRectangle;
  25. this.ISurfaceBox = surfaceBox;
  26. this.pen = pen;
  27. this.rectangle = rect;
  28. }
  29. /// <summary>
  30. /// Clone this instance
  31. /// </summary>
  32. public override DrawObject Clone()
  33. {
  34. return null;
  35. }
  36. public override DrawObject Clone(ISurfaceBox surfaceBox)
  37. {
  38. return null;
  39. }
  40. /// <summary>
  41. /// Draw rectangle
  42. /// </summary>
  43. /// <param name="g"></param>
  44. public override void Draw(Graphics g)
  45. {
  46. g.DrawRectangle(this.pen, GetNormalizedRectangle(Rectangle));
  47. //pen.Dispose();
  48. }
  49. protected void SetRectangle(float x, float y, float width, float height)
  50. {
  51. this.rectangle.X = x;
  52. this.rectangle.Y = y;
  53. this.rectangle.Width = width;
  54. this.rectangle.Height = height;
  55. }
  56. public override Rectangle GetHandleRectangle(int handleNumber)
  57. {
  58. PointF point = GetHandle(handleNumber);
  59. return new Rectangle((int)(point.X), (int)(point.Y), 4, 4);
  60. }
  61. public override void DrawTracker(Graphics g)
  62. {
  63. if (!Selected)
  64. return;
  65. SolidBrush brush = new SolidBrush(pen.Color);
  66. for (int i = 1; i <= HandleCount; i++)
  67. {
  68. g.FillRectangle(brush, GetHandleRectangle(i));
  69. }
  70. brush.Dispose();
  71. }
  72. /// <summary>
  73. /// Get number of handles
  74. /// </summary>
  75. public override int HandleCount
  76. {
  77. get
  78. {
  79. return 8;
  80. }
  81. }
  82. /// <summary>
  83. /// Get handle pointscroll by 1-based number
  84. /// </summary>
  85. /// <param name="handleNumber"></param>
  86. /// <returns></returns>
  87. public override PointF GetHandle(int handleNumber)
  88. {
  89. float x, y, xCenter, yCenter;
  90. xCenter = this.rectangle.X + this.rectangle.Width / 2;
  91. yCenter = this.rectangle.Y + this.rectangle.Height / 2;
  92. x = this.rectangle.X;
  93. y = this.rectangle.Y;
  94. switch (handleNumber)
  95. {
  96. case 1:
  97. x = this.rectangle.X;
  98. y = this.rectangle.Y;
  99. break;
  100. case 2:
  101. x = xCenter - 2;
  102. y = this.rectangle.Y;
  103. break;
  104. case 3:
  105. x = this.rectangle.Right -4;
  106. y = this.rectangle.Y;
  107. break;
  108. case 4:
  109. x = this.rectangle.Right - 4;
  110. y = yCenter - 2;
  111. break;
  112. case 5:
  113. x = this.rectangle.Right - 4;
  114. y = this.rectangle.Bottom - 4;
  115. break;
  116. case 6:
  117. x = xCenter - 2;
  118. y = this.rectangle.Bottom - 4;
  119. break;
  120. case 7:
  121. x = this.rectangle.X;
  122. y = this.rectangle.Bottom - 4;
  123. break;
  124. case 8:
  125. x = this.rectangle.X;
  126. y = yCenter - 2;
  127. break;
  128. }
  129. return new PointF(x, y);
  130. }
  131. /// <summary>
  132. /// Hit test.
  133. /// Return value: -1 - no hit
  134. /// 0 - hit anywhere
  135. /// > 1 - handle number
  136. /// </summary>
  137. /// <param name="pointscroll"></param>
  138. /// <returns></returns>
  139. public override int HitTest(Point point)
  140. {
  141. if (Selected)
  142. {
  143. //for (int i = 1; i <= HandleCount; i++)
  144. //{
  145. // if (GetHandleRectangle(i).Contains(point))
  146. // return i;
  147. //}
  148. }
  149. if (PointInObject(point))
  150. return 0;
  151. return -1;
  152. }
  153. protected override bool PointInObject(Point point)
  154. {
  155. return rectangle.Contains(point);
  156. }
  157. /// <summary>
  158. /// Get cursor for the handle
  159. /// </summary>
  160. /// <param name="handleNumber"></param>
  161. /// <returns></returns>
  162. public override Cursor GetHandleCursor(int handleNumber)
  163. {
  164. switch (handleNumber)
  165. {
  166. case 1:
  167. return Cursors.SizeNWSE;
  168. case 2:
  169. return Cursors.SizeNS;
  170. case 3:
  171. return Cursors.SizeNESW;
  172. case 4:
  173. return Cursors.SizeWE;
  174. case 5:
  175. return Cursors.SizeNWSE;
  176. case 6:
  177. return Cursors.SizeNS;
  178. case 7:
  179. return Cursors.SizeNESW;
  180. case 8:
  181. return Cursors.SizeWE;
  182. default:
  183. return Cursors.Default;
  184. }
  185. }
  186. /// <summary>
  187. /// Move handle to new pointscroll (resizing)
  188. /// </summary>
  189. /// <param name="pointscroll"></param>
  190. /// <param name="handleNumber"></param>
  191. public override void MoveHandleTo(Point point, int handleNumber)
  192. {
  193. //float left = Rectangle.Left;
  194. //float top = Rectangle.Top;
  195. //float right = Rectangle.Right;
  196. //float bottom = Rectangle.Bottom;
  197. //switch (handleNumber)
  198. //{
  199. // case 1:
  200. // left = point.X;
  201. // top = point.Y;
  202. // break;
  203. // case 2:
  204. // top = point.Y;
  205. // break;
  206. // case 3:
  207. // right = point.X;
  208. // top = point.Y;
  209. // break;
  210. // case 4:
  211. // right = point.X;
  212. // break;
  213. // case 5:
  214. // right = point.X;
  215. // bottom = point.Y;
  216. // break;
  217. // case 6:
  218. // bottom = point.Y;
  219. // break;
  220. // case 7:
  221. // left = point.X;
  222. // bottom = point.Y;
  223. // break;
  224. // case 8:
  225. // left = point.X;
  226. // break;
  227. //}
  228. //SetRectangle(left, top, right - left, bottom - top);
  229. }
  230. public override bool IntersectsWith(Rectangle rectangle)
  231. {
  232. return Rectangle.IntersectsWith(rectangle);
  233. }
  234. /// <summary>
  235. /// Move object
  236. /// </summary>
  237. /// <param name="deltaX"></param>
  238. /// <param name="deltaY"></param>
  239. public override void Move(int deltaX, int deltaY)
  240. {
  241. int x = ISurfaceBox.UnscaleScalar(deltaX);
  242. int y = ISurfaceBox.UnscaleScalar(deltaY);
  243. rectangle.X += x;
  244. rectangle.Y += y;
  245. }
  246. public override RectangleF GetBoundingBox()
  247. {
  248. return rectangle;
  249. }
  250. public override List<PointF> GetPoints()
  251. {
  252. List<PointF> points = new List<PointF>();
  253. points.Add(new PointF(rectangle.X, rectangle.Y));
  254. points.Add(new PointF(rectangle.Right, rectangle.Bottom));
  255. return points;
  256. }
  257. public override ParentStyleModel GetStyle()
  258. {
  259. return labelRectStyleModel;
  260. }
  261. }
  262. }