DrawObject.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base;
  3. using PaintDotNet.Base.SettingModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Globalization;
  9. using System.Runtime.Serialization;
  10. using System.Windows.Forms;
  11. namespace PaintDotNet.Annotation
  12. {
  13. public abstract class DrawObject
  14. {
  15. /// <summary>
  16. /// 目标被选择状态
  17. /// </summary>
  18. private bool selected;
  19. /// <summary>
  20. /// 鼠标按下状态, 静态,所有现存的公用
  21. /// </summary>
  22. public static bool mouseStatus = false;
  23. public int smallRectangleWidth = MarkPointRect.markPointRectWidth;
  24. /// <summary>
  25. /// 点集合
  26. /// </summary>
  27. public List<PointF> pointArrayList;
  28. /// <summary>
  29. /// 颜色
  30. /// </summary>
  31. private Color color;
  32. /// <summary>
  33. /// 线宽
  34. /// </summary>
  35. private int penWidth;
  36. /// <summary>
  37. /// 用于撤消-重做
  38. /// </summary>
  39. int id;
  40. /// <summary>
  41. /// 上次使用的属性值
  42. /// 也许可以不用??
  43. /// </summary>
  44. private static Color lastUsedColor = Color.Black;
  45. /// <summary>
  46. /// 上次使用的线宽
  47. /// 也许可以不用??
  48. /// </summary>
  49. private static int lastUsedPenWidth = 1;
  50. /// <summary>
  51. /// 用于序列化-颜色
  52. /// </summary>
  53. private const string entryColor = "Color";
  54. /// <summary>
  55. /// 用于序列化-线宽
  56. /// </summary>
  57. private const string entryPenWidth = "PenWidth";
  58. /// <summary>
  59. /// 画板
  60. /// </summary>
  61. private ISurfaceBox surfaceBox;
  62. /// <summary>
  63. /// 主要用于区分是标注、测量、视场、其它
  64. /// </summary>
  65. public DrawClass objectType;
  66. /// <summary>
  67. /// 用于标注自身是什么工具
  68. /// </summary>
  69. public DrawToolType drawToolType;
  70. /// <summary>
  71. /// 外接矩形,或者是计算的矩形
  72. /// </summary>
  73. protected RectangleF rectangle;
  74. /// <summary>
  75. /// 起始点
  76. /// </summary>
  77. public PointF startPoint;
  78. /// <summary>
  79. /// 物理长度
  80. /// </summary>
  81. public double length = 0;
  82. /// <summary>
  83. /// 结束点
  84. /// </summary>
  85. public PointF endPoint;
  86. /// <summary>
  87. /// 判断辅助线方向的点
  88. /// </summary>
  89. public Point judgeP;
  90. /// <summary>
  91. /// 文本框内容
  92. /// </summary>
  93. public string textboxMessage;
  94. /// <summary>
  95. /// 目标点状态 0:添加 1:选择 2:删除
  96. /// </summary>
  97. public int CompoundRatioSelected;
  98. /// <summary>
  99. /// 删除状态 0:不删除 1:删除
  100. /// </summary>
  101. public int delete;
  102. public bool isLocked = false;
  103. public virtual RectangleF Rectangle
  104. {
  105. get
  106. {
  107. return rectangle;
  108. }
  109. set
  110. {
  111. rectangle = value;
  112. OnPropertyChanged();
  113. }
  114. }
  115. public virtual double Height
  116. { get; set; }
  117. public virtual double Width
  118. { get; set; }
  119. /// <summary>
  120. /// 公开出去的自定义事件
  121. /// 当对象本身属性有所改变的时候
  122. /// 比如边长,原点等有变化
  123. /// </summary>
  124. public event EventHandler PropertyChanged;
  125. protected virtual void OnPropertyChanged()
  126. {
  127. if (PropertyChanged != null)
  128. {
  129. PropertyChanged(this, EventArgs.Empty);
  130. }
  131. }
  132. /// <summary>
  133. /// 选中状态改变的事件
  134. /// </summary>
  135. public event EventHandler SelectedChanged;
  136. protected virtual void OnSelectedChanged()
  137. {
  138. if (SelectedChanged != null)
  139. {
  140. SelectedChanged(this, EventArgs.Empty);
  141. }
  142. }
  143. public ISurfaceBox ISurfaceBox
  144. {
  145. set
  146. {
  147. this.surfaceBox = value;
  148. }
  149. get
  150. {
  151. return this.surfaceBox;
  152. }
  153. }
  154. public DrawObject()
  155. {
  156. id = this.GetHashCode();
  157. }
  158. /// <summary>
  159. /// Selection flag
  160. /// </summary>
  161. public bool Selected
  162. {
  163. get
  164. {
  165. return selected;
  166. }
  167. set
  168. {
  169. selected = value;
  170. if (selected) OnSelectedChanged();
  171. }
  172. }
  173. /// <summary>
  174. /// Color
  175. /// </summary>
  176. public Color Color
  177. {
  178. get
  179. {
  180. return color;
  181. }
  182. set
  183. {
  184. color = value;
  185. }
  186. }
  187. /// <summary>
  188. /// Pen width
  189. /// </summary>
  190. public int PenWidth
  191. {
  192. get
  193. {
  194. return penWidth;
  195. }
  196. set
  197. {
  198. penWidth = value;
  199. }
  200. }
  201. /// <summary>
  202. /// Number of handles
  203. /// </summary>
  204. public virtual int HandleCount
  205. {
  206. get
  207. {
  208. return 0;
  209. }
  210. }
  211. /// <summary>
  212. /// Object ID
  213. /// </summary>
  214. public int ID
  215. {
  216. get { return id; }
  217. set { id = value; }
  218. }
  219. /// <summary>
  220. /// Last used color
  221. /// </summary>
  222. public static Color LastUsedColor
  223. {
  224. get
  225. {
  226. return lastUsedColor;
  227. }
  228. set
  229. {
  230. lastUsedColor = value;
  231. }
  232. }
  233. /// <summary>
  234. /// Last used pen width
  235. /// </summary>
  236. public static int LastUsedPenWidth
  237. {
  238. get
  239. {
  240. return lastUsedPenWidth;
  241. }
  242. set
  243. {
  244. lastUsedPenWidth = value;
  245. }
  246. }
  247. #region 虚函数
  248. /// <summary>
  249. /// Clone this instance.
  250. /// </summary>
  251. public abstract DrawObject Clone();
  252. public virtual DrawObject Clone(ISurfaceBox ISurfaceBox) { return null; }
  253. /// <summary>
  254. /// Draw object
  255. /// </summary>
  256. /// <param name="g"></param>
  257. public virtual void Draw(Graphics g)
  258. {
  259. }
  260. public abstract RectangleF GetBoundingBox();
  261. /// <summary>
  262. /// Get handle pointscroll by 1-based number
  263. /// </summary>
  264. /// <param name="handleNumber"></param>
  265. /// <returns></returns>
  266. public virtual PointF GetHandle(int handleNumber)
  267. {
  268. return new PointF(0, 0);
  269. }
  270. /// <summary>
  271. /// Get handle rectangle by 1-based number
  272. /// </summary>
  273. /// <param name="handleNumber"></param>
  274. /// <returns></returns>
  275. public virtual Rectangle GetHandleRectangle(int handleNumber)
  276. {
  277. PointF point = GetHandle(handleNumber);
  278. int tempLess;
  279. int tempPlus;
  280. if (smallRectangleWidth == 0)
  281. {
  282. tempLess = (int)(6 * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
  283. tempPlus = (int)(12 * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
  284. }
  285. else
  286. {
  287. tempLess = smallRectangleWidth / 2;
  288. tempPlus = smallRectangleWidth;
  289. }
  290. return new Rectangle((int)(point.X - tempLess), (int)(point.Y - tempLess), tempPlus, tempPlus);
  291. }
  292. /// <summary>
  293. /// Draw tracker for selected object
  294. /// </summary>
  295. /// <param name="g"></param>
  296. public virtual void DrawTracker(Graphics g)
  297. {
  298. if (!Selected)
  299. return;
  300. SolidBrush brush = new SolidBrush(Color.Black);
  301. for (int i = 1; i <= HandleCount; i++)
  302. {
  303. g.FillRectangle(brush, GetHandleRectangle(i));
  304. }
  305. brush.Dispose();
  306. }
  307. /// <summary>
  308. /// Hit test.
  309. /// Return value: -1 - no hit
  310. /// 0 - hit anywhere
  311. /// > 1 - handle number
  312. /// </summary>
  313. /// <param name="pointscroll"></param>
  314. /// <returns></returns>
  315. public virtual int HitTest(Point point)
  316. {
  317. return -1;
  318. }
  319. /// <summary>
  320. /// 获取鼠标按下时的坐标
  321. /// </summary>
  322. /// <param name="point"></param>
  323. public virtual void MouseDownPoint(Point point)
  324. {
  325. }
  326. public virtual void MouseUp(bool up)
  327. {
  328. }
  329. /// <summary>
  330. /// Test whether pointscroll is inside of the object
  331. /// </summary>
  332. /// <param name="pointscroll"></param>
  333. /// <returns></returns>
  334. protected virtual bool PointInObject(Point point)
  335. {
  336. return false;
  337. }
  338. /// <summary>
  339. /// Get cursor for the handle
  340. /// </summary>
  341. /// <param name="handleNumber"></param>
  342. /// <returns></returns>
  343. public virtual Cursor GetHandleCursor(int handleNumber)
  344. {
  345. return Cursors.Default;
  346. }
  347. /// <summary>
  348. /// Test whether object intersects with rectangle
  349. /// </summary>
  350. /// <param name="rectangle"></param>
  351. /// <returns></returns>
  352. public virtual bool IntersectsWith(Rectangle rectangle)
  353. {
  354. return false;
  355. }
  356. /// <summary>
  357. /// Move object
  358. /// </summary>
  359. /// <param name="deltaX"></param>
  360. /// <param name="deltaY"></param>
  361. public virtual void Move(int deltaX, int deltaY)
  362. {
  363. OnPropertyChanged();
  364. }
  365. /// <summary>
  366. /// Move handle to the pointscroll
  367. /// </summary>
  368. /// <param name="pointscroll"></param>
  369. /// <param name="handleNumber"></param>
  370. public virtual void MoveHandleTo(Point point, int handleNumber)
  371. {
  372. OnPropertyChanged();
  373. }
  374. public virtual void MoveHandleTo(PointF point, int handleNumber)
  375. {
  376. OnPropertyChanged();
  377. }
  378. /// <summary>
  379. /// Dump (for debugging)
  380. /// </summary>
  381. public virtual void Dump()
  382. {
  383. Trace.WriteLine(this.GetType().Name);
  384. Trace.WriteLine("Selected = " +
  385. selected.ToString(CultureInfo.InvariantCulture)
  386. + " ID = " + id.ToString(CultureInfo.InvariantCulture));
  387. }
  388. /// <summary>
  389. /// Normalize object.
  390. /// Call this function in the end of object resizing.
  391. /// </summary>
  392. public virtual void Normalize()
  393. {
  394. this.Rectangle = GetNormalizedRectangleF(this.Rectangle);
  395. }
  396. public static RectangleF GetNormalizedRectangleF(RectangleF r)
  397. {
  398. return GetNormalizedRectangleF(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
  399. }
  400. public static RectangleF GetNormalizedRectangleF(PointF p1, PointF p2)
  401. {
  402. return GetNormalizedRectangleF(p1.X, p1.Y, p2.X, p2.Y);
  403. }
  404. public static RectangleF GetNormalizedRectangleF(float x1, float y1, float x2, float y2)
  405. {
  406. if (x2 < x1)
  407. {
  408. float tmp = x2;
  409. x2 = x1;
  410. x1 = tmp;
  411. }
  412. if (y2 < y1)
  413. {
  414. float tmp = y2;
  415. y2 = y1;
  416. y1 = tmp;
  417. }
  418. return new RectangleF(x1, y1, (x2 - x1), (y2 - y1));
  419. }
  420. public static Rectangle GetNormalizedRectangle(RectangleF r)
  421. {
  422. return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
  423. }
  424. public static Rectangle GetNormalizedRectangle(PointF p1, PointF p2)
  425. {
  426. return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);
  427. }
  428. public static Rectangle GetNormalizedRectangle(float x1, float y1, float x2, float y2)
  429. {
  430. if (x2 < x1)
  431. {
  432. float tmp = x2;
  433. x2 = x1;
  434. x1 = tmp;
  435. }
  436. if (y2 < y1)
  437. {
  438. float tmp = y2;
  439. y2 = y1;
  440. y1 = tmp;
  441. }
  442. return new Rectangle((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1));
  443. }
  444. /// <summary>
  445. /// Save object to serialization stream
  446. /// </summary>
  447. /// <param name="info"></param>
  448. /// <param name="orderNumber"></param>
  449. public virtual void SaveToStream(SerializationInfo info, int orderNumber)
  450. {
  451. info.AddValue(
  452. String.Format(CultureInfo.InvariantCulture,
  453. "{0}{1}",
  454. entryColor, orderNumber),
  455. Color.ToArgb());
  456. info.AddValue(
  457. String.Format(CultureInfo.InvariantCulture,
  458. "{0}{1}",
  459. entryPenWidth, orderNumber),
  460. PenWidth);
  461. }
  462. /// <summary>
  463. /// Load object from serialization stream
  464. /// </summary>
  465. /// <param name="info"></param>
  466. /// <param name="orderNumber"></param>
  467. public virtual void LoadFromStream(SerializationInfo info, int orderNumber)
  468. {
  469. int n = info.GetInt32(
  470. String.Format(CultureInfo.InvariantCulture,
  471. "{0}{1}",
  472. entryColor, orderNumber));
  473. Color = Color.FromArgb(n);
  474. PenWidth = info.GetInt32(
  475. String.Format(CultureInfo.InvariantCulture,
  476. "{0}{1}",
  477. entryPenWidth, orderNumber));
  478. id = this.GetHashCode();
  479. }
  480. /// <summary>
  481. /// 用于返回组成标注/测量/视场的所有的点的集合
  482. /// 暂时没有想到有特殊的数据需要处理,想到再说
  483. /// </summary>
  484. /// <returns></returns>
  485. public virtual List<PointF> GetPoints()
  486. {
  487. return null;
  488. }
  489. public virtual ParentStyleModel GetStyle()
  490. {
  491. return null;
  492. }
  493. public virtual string GetContent()
  494. {
  495. return "";
  496. }
  497. #endregion
  498. #region 其它方法
  499. /// <summary>
  500. /// Initialization
  501. /// </summary>
  502. protected void Initialize()
  503. {
  504. color = lastUsedColor;
  505. penWidth = LastUsedPenWidth;
  506. }
  507. /// <summary>
  508. /// Copy fields from this instance to cloned instance drawObject.
  509. /// Called from Clone functions of derived classes.
  510. /// </summary>
  511. protected void FillDrawObjectFields(DrawObject drawObject)
  512. {
  513. drawObject.selected = this.selected;
  514. drawObject.color = this.color;
  515. drawObject.penWidth = this.penWidth;
  516. drawObject.ID = this.ID;
  517. }
  518. #endregion
  519. }
  520. }