DrawObject.cs 18 KB

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