ImageCutDraw.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using SmartCoalApplication.Annotation.Enum;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Globalization;
  7. using System.Runtime.Serialization;
  8. using System.Windows.Forms;
  9. namespace SmartCoalApplication.Annotation.Label
  10. {
  11. using PointList = List<Point>;
  12. /// <summary>
  13. /// 编辑->图片裁剪
  14. /// </summary>
  15. public class ImageCutDraw : DrawObject
  16. {
  17. private const string entryRectangle = "Rect";
  18. public ImageCutDraw() : this(0, 0, 1, 1)
  19. {
  20. }
  21. public ImageCutDraw(int x, int y, int width, int height) : base()
  22. {
  23. this.objectType = DrawClass.Other;
  24. this.drawToolType = DrawToolType.ImageCut;
  25. rectangle.X = x;
  26. rectangle.Y = y;
  27. rectangle.Width = width;
  28. rectangle.Height = height;
  29. Initialize();
  30. }
  31. /// <summary>
  32. /// Clone this instance
  33. /// </summary>
  34. public override DrawObject Clone()
  35. {
  36. ImageCutDraw drawRectangle = new ImageCutDraw();
  37. drawRectangle.objectType = DrawClass.Other;
  38. drawRectangle.drawToolType = DrawToolType.ImageCut;
  39. drawRectangle.ISurfaceBox = ISurfaceBox;
  40. drawRectangle.rectangle = this.rectangle;
  41. FillDrawObjectFields(drawRectangle);
  42. return drawRectangle;
  43. }
  44. /// <summary>
  45. /// Draw rectangle
  46. /// </summary>
  47. /// <param name="g"></param>
  48. public override void Draw(Graphics g)
  49. {
  50. Pen pen = new Pen(Color, PenWidth);
  51. pen.DashStyle = DashStyle.Dash;
  52. g.DrawRectangle(pen, DrawRectangle.GetNormalizedRectangle(Rectangle));
  53. pen.Dispose();
  54. }
  55. protected void SetRectangle(int x, int y, int width, int height)
  56. {
  57. rectangle.X = x;
  58. rectangle.Y = y;
  59. rectangle.Width = width;
  60. rectangle.Height = height;
  61. }
  62. /// <summary>
  63. /// Get number of handles
  64. /// </summary>
  65. public override int HandleCount
  66. {
  67. get
  68. {
  69. return 8;
  70. }
  71. }
  72. /// <summary>
  73. /// Get handle pointscroll by 1-based number
  74. /// </summary>
  75. /// <param name="handleNumber"></param>
  76. /// <returns></returns>
  77. public override PointF GetHandle(int handleNumber)
  78. {
  79. float x, y, xCenter, yCenter;
  80. xCenter = rectangle.X + rectangle.Width / 2;
  81. yCenter = rectangle.Y + rectangle.Height / 2;
  82. x = rectangle.X;
  83. y = rectangle.Y;
  84. switch (handleNumber)
  85. {
  86. case 1:
  87. x = rectangle.X;
  88. y = rectangle.Y;
  89. break;
  90. case 2:
  91. x = xCenter;
  92. y = rectangle.Y;
  93. break;
  94. case 3:
  95. x = rectangle.Right;
  96. y = rectangle.Y;
  97. break;
  98. case 4:
  99. x = rectangle.Right;
  100. y = yCenter;
  101. break;
  102. case 5:
  103. x = rectangle.Right;
  104. y = rectangle.Bottom;
  105. break;
  106. case 6:
  107. x = xCenter;
  108. y = rectangle.Bottom;
  109. break;
  110. case 7:
  111. x = rectangle.X;
  112. y = rectangle.Bottom;
  113. break;
  114. case 8:
  115. x = rectangle.X;
  116. y = yCenter;
  117. break;
  118. }
  119. return new PointF(x, y);
  120. }
  121. /// <summary>
  122. /// Hit test.
  123. /// Return value: -1 - no hit
  124. /// 0 - hit anywhere
  125. /// > 1 - handle number
  126. /// </summary>
  127. /// <param name="pointscroll"></param>
  128. /// <returns></returns>
  129. public override int HitTest(Point point)
  130. {
  131. if (Selected)
  132. {
  133. for (int i = 1; i <= HandleCount; i++)
  134. {
  135. if (GetHandleRectangle(i).Contains(point))
  136. return i;
  137. }
  138. }
  139. if (PointInObject(point))
  140. return 0;
  141. return -1;
  142. }
  143. protected override bool PointInObject(Point point)
  144. {
  145. return rectangle.Contains(point);
  146. }
  147. /// <summary>
  148. /// Get cursor for the handle
  149. /// </summary>
  150. /// <param name="handleNumber"></param>
  151. /// <returns></returns>
  152. public override Cursor GetHandleCursor(int handleNumber)
  153. {
  154. switch (handleNumber)
  155. {
  156. case 1:
  157. return Cursors.SizeNWSE;
  158. case 2:
  159. return Cursors.SizeNS;
  160. case 3:
  161. return Cursors.SizeNESW;
  162. case 4:
  163. return Cursors.SizeWE;
  164. case 5:
  165. return Cursors.SizeNWSE;
  166. case 6:
  167. return Cursors.SizeNS;
  168. case 7:
  169. return Cursors.SizeNESW;
  170. case 8:
  171. return Cursors.SizeWE;
  172. default:
  173. return Cursors.Default;
  174. }
  175. }
  176. /// <summary>
  177. /// Move handle to new pointscroll (resizing)
  178. /// </summary>
  179. /// <param name="pointscroll"></param>
  180. /// <param name="handleNumber"></param>
  181. public override void MoveHandleTo(Point point, int handleNumber)
  182. {
  183. int left = (int)Rectangle.Left;
  184. int top = (int)Rectangle.Top;
  185. int right = (int)Rectangle.Right;
  186. int bottom = (int)Rectangle.Bottom;
  187. switch (handleNumber)
  188. {
  189. case 1:
  190. left = point.X;
  191. top = point.Y;
  192. break;
  193. case 2:
  194. top = point.Y;
  195. break;
  196. case 3:
  197. right = point.X;
  198. top = point.Y;
  199. break;
  200. case 4:
  201. right = point.X;
  202. break;
  203. case 5:
  204. right = point.X;
  205. bottom = point.Y;
  206. break;
  207. case 6:
  208. bottom = point.Y;
  209. break;
  210. case 7:
  211. left = point.X;
  212. bottom = point.Y;
  213. break;
  214. case 8:
  215. left = point.X;
  216. break;
  217. }
  218. SetRectangle(left, top, right - left, bottom - top);
  219. }
  220. public override bool IntersectsWith(Rectangle rectangle)
  221. {
  222. return Rectangle.IntersectsWith(rectangle);
  223. }
  224. /// <summary>
  225. /// Move object
  226. /// </summary>
  227. /// <param name="deltaX"></param>
  228. /// <param name="deltaY"></param>
  229. public override void Move(int deltaX, int deltaY)
  230. {
  231. int x = ISurfaceBox.UnscaleScalar(deltaX);
  232. int y = ISurfaceBox.UnscaleScalar(deltaY);
  233. rectangle.X += x;
  234. rectangle.Y += y;
  235. }
  236. public override void Dump()
  237. {
  238. base.Dump();
  239. }
  240. /// <summary>
  241. /// Normalize rectangle
  242. /// </summary>
  243. public override void Normalize()
  244. {
  245. rectangle = DrawRectangle.GetNormalizedRectangle(rectangle);
  246. }
  247. /// <summary>
  248. /// Save objevt to serialization stream
  249. /// </summary>
  250. /// <param name="info"></param>
  251. /// <param name="orderNumber"></param>
  252. public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
  253. {
  254. info.AddValue(
  255. String.Format(CultureInfo.InvariantCulture,
  256. "{0}{1}",
  257. entryRectangle, orderNumber),
  258. rectangle);
  259. base.SaveToStream(info, orderNumber);
  260. }
  261. /// <summary>
  262. /// LOad object from serialization stream
  263. /// </summary>
  264. /// <param name="info"></param>
  265. /// <param name="orderNumber"></param>
  266. public override void LoadFromStream(SerializationInfo info, int orderNumber)
  267. {
  268. rectangle = (Rectangle)info.GetValue(
  269. String.Format(CultureInfo.InvariantCulture,
  270. "{0}{1}",
  271. entryRectangle, orderNumber),
  272. typeof(Rectangle));
  273. base.LoadFromStream(info, orderNumber);
  274. }
  275. #region Helper Functions
  276. public static Rectangle GetNormalizedRectangle(int x1, int y1, int x2, int y2)
  277. {
  278. if (x2 < x1)
  279. {
  280. int tmp = x2;
  281. x2 = x1;
  282. x1 = tmp;
  283. }
  284. if (y2 < y1)
  285. {
  286. int tmp = y2;
  287. y2 = y1;
  288. y1 = tmp;
  289. }
  290. return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  291. }
  292. public static Rectangle GetNormalizedRectangle(Point p1, Point p2)
  293. {
  294. return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);
  295. }
  296. public static Rectangle GetNormalizedRectangle(Rectangle r)
  297. {
  298. return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
  299. }
  300. #endregion
  301. public override RectangleF GetBoundingBox()
  302. {
  303. return rectangle;
  304. }
  305. }
  306. }