DrawObject.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. using SmartCoalApplication.Annotation.Enum;
  2. using SmartCoalApplication.Base;
  3. using SmartCoalApplication.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 SmartCoalApplication.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. /// <summary>
  103. /// 是否可以移动
  104. /// </summary>
  105. public bool canMove = true;
  106. /// <summary>
  107. /// 是否点中 点
  108. /// </summary>
  109. public bool isHitTest = false;
  110. public virtual RectangleF Rectangle
  111. {
  112. get
  113. {
  114. return rectangle;
  115. }
  116. set
  117. {
  118. rectangle = value;
  119. OnPropertyChanged();
  120. }
  121. }
  122. public virtual double Height
  123. { get; set; }
  124. public virtual double Width
  125. { get; set; }
  126. /// <summary>
  127. /// 公开出去的自定义事件
  128. /// 当对象本身属性有所改变的时候
  129. /// 比如边长,原点等有变化
  130. /// </summary>
  131. public event EventHandler PropertyChanged;
  132. protected virtual void OnPropertyChanged()
  133. {
  134. if (PropertyChanged != null)
  135. {
  136. PropertyChanged(this, EventArgs.Empty);
  137. }
  138. }
  139. /// <summary>
  140. /// 选中状态改变的事件
  141. /// </summary>
  142. public event EventHandler SelectedChanged;
  143. protected virtual void OnSelectedChanged()
  144. {
  145. if (SelectedChanged != null)
  146. {
  147. SelectedChanged(this, EventArgs.Empty);
  148. }
  149. }
  150. public ISurfaceBox ISurfaceBox
  151. {
  152. set
  153. {
  154. this.surfaceBox = value;
  155. }
  156. get
  157. {
  158. return this.surfaceBox;
  159. }
  160. }
  161. public DrawObject()
  162. {
  163. id = this.GetHashCode();
  164. }
  165. /// <summary>
  166. /// Selection flag
  167. /// </summary>
  168. public bool Selected
  169. {
  170. get
  171. {
  172. return selected;
  173. }
  174. set
  175. {
  176. selected = value;
  177. if (selected) OnSelectedChanged();
  178. }
  179. }
  180. /// <summary>
  181. /// Color
  182. /// </summary>
  183. public Color Color
  184. {
  185. get
  186. {
  187. return color;
  188. }
  189. set
  190. {
  191. color = value;
  192. }
  193. }
  194. /// <summary>
  195. /// Pen width
  196. /// </summary>
  197. public int PenWidth
  198. {
  199. get
  200. {
  201. return penWidth;
  202. }
  203. set
  204. {
  205. penWidth = value;
  206. }
  207. }
  208. /// <summary>
  209. /// Number of handles
  210. /// </summary>
  211. public virtual int HandleCount
  212. {
  213. get
  214. {
  215. return 0;
  216. }
  217. }
  218. /// <summary>
  219. /// Object ID
  220. /// </summary>
  221. public int ID
  222. {
  223. get { return id; }
  224. set { id = value; }
  225. }
  226. /// <summary>
  227. /// Last used color
  228. /// </summary>
  229. public static Color LastUsedColor
  230. {
  231. get
  232. {
  233. return lastUsedColor;
  234. }
  235. set
  236. {
  237. lastUsedColor = value;
  238. }
  239. }
  240. /// <summary>
  241. /// Last used pen width
  242. /// </summary>
  243. public static int LastUsedPenWidth
  244. {
  245. get
  246. {
  247. return lastUsedPenWidth;
  248. }
  249. set
  250. {
  251. lastUsedPenWidth = value;
  252. }
  253. }
  254. #region 虚函数
  255. /// <summary>
  256. /// Clone this instance.
  257. /// </summary>
  258. public abstract DrawObject Clone();
  259. public virtual DrawObject Clone(ISurfaceBox ISurfaceBox) { return null; }
  260. /// <summary>
  261. /// Draw object
  262. /// </summary>
  263. /// <param name="g"></param>
  264. public virtual void Draw(Graphics g)
  265. {
  266. }
  267. /// <summary>
  268. /// Draw object
  269. /// </summary>
  270. /// <param name="g"></param>
  271. public virtual void DrawPicture(Graphics g, RectangleF rectangle)
  272. {
  273. }
  274. public abstract RectangleF GetBoundingBox();
  275. /// <summary>
  276. /// Get handle pointscroll by 1-based number
  277. /// </summary>
  278. /// <param name="handleNumber"></param>
  279. /// <returns></returns>
  280. public virtual PointF GetHandle(int handleNumber)
  281. {
  282. return new PointF(0, 0);
  283. }
  284. /// <summary>
  285. /// Get handle rectangle by 1-based number
  286. /// </summary>
  287. /// <param name="handleNumber"></param>
  288. /// <returns></returns>
  289. public virtual Rectangle GetHandleRectangle(int handleNumber)
  290. {
  291. PointF point = GetHandle(handleNumber);
  292. int tempLess;
  293. int tempPlus;
  294. if (smallRectangleWidth == 0)
  295. {
  296. tempLess = (int)(6 * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
  297. tempPlus = (int)(12 * (this.surfaceBox.ScaleRatio < 1 ? 1 / this.surfaceBox.ScaleRatio : 1));
  298. }
  299. else
  300. {
  301. tempLess = smallRectangleWidth / 2;
  302. tempPlus = smallRectangleWidth;
  303. }
  304. return new Rectangle((int)(point.X - tempLess), (int)(point.Y - tempLess), tempPlus, tempPlus);
  305. }
  306. /// <summary>
  307. /// Draw tracker for selected object
  308. /// </summary>
  309. /// <param name="g"></param>
  310. public virtual void DrawTracker(Graphics g)
  311. {
  312. if (!Selected)
  313. return;
  314. SolidBrush brush = new SolidBrush(Color.Black);
  315. for (int i = 1; i <= HandleCount; i++)
  316. {
  317. g.FillRectangle(brush, GetHandleRectangle(i));
  318. }
  319. brush.Dispose();
  320. }
  321. /// <summary>
  322. /// Hit test.
  323. /// Return value: -1 - no hit
  324. /// 0 - hit anywhere
  325. /// > 1 - handle number
  326. /// </summary>
  327. /// <param name="pointscroll"></param>
  328. /// <returns></returns>
  329. public virtual int HitTest(Point point)
  330. {
  331. return -1;
  332. }
  333. /// <summary>
  334. /// 获取鼠标按下时的坐标
  335. /// </summary>
  336. /// <param name="point"></param>
  337. public virtual void MouseDownPoint(Point point)
  338. {
  339. }
  340. public virtual void MouseUp(bool up)
  341. {
  342. }
  343. /// <summary>
  344. /// Test whether pointscroll is inside of the object
  345. /// </summary>
  346. /// <param name="pointscroll"></param>
  347. /// <returns></returns>
  348. protected virtual bool PointInObject(Point point)
  349. {
  350. return false;
  351. }
  352. /// <summary>
  353. /// Get cursor for the handle
  354. /// </summary>
  355. /// <param name="handleNumber"></param>
  356. /// <returns></returns>
  357. public virtual Cursor GetHandleCursor(int handleNumber)
  358. {
  359. return Cursors.Default;
  360. }
  361. /// <summary>
  362. /// Test whether object intersects with rectangle
  363. /// </summary>
  364. /// <param name="rectangle"></param>
  365. /// <returns></returns>
  366. public virtual bool IntersectsWith(Rectangle rectangle)
  367. {
  368. return false;
  369. }
  370. /// <summary>
  371. /// Move object
  372. /// </summary>
  373. /// <param name="deltaX"></param>
  374. /// <param name="deltaY"></param>
  375. public virtual void Move(int deltaX, int deltaY)
  376. {
  377. OnPropertyChanged();
  378. }
  379. /// <summary>
  380. /// Move handle to the pointscroll
  381. /// </summary>
  382. /// <param name="pointscroll"></param>
  383. /// <param name="handleNumber"></param>
  384. public virtual void MoveHandleTo(Point point, int handleNumber)
  385. {
  386. OnPropertyChanged();
  387. }
  388. public virtual void MoveHandleTo(PointF point, int handleNumber)
  389. {
  390. OnPropertyChanged();
  391. }
  392. /// <summary>
  393. /// Dump (for debugging)
  394. /// </summary>
  395. public virtual void Dump()
  396. {
  397. Trace.WriteLine(this.GetType().Name);
  398. Trace.WriteLine("Selected = " +
  399. selected.ToString(CultureInfo.InvariantCulture)
  400. + " ID = " + id.ToString(CultureInfo.InvariantCulture));
  401. }
  402. /// <summary>
  403. /// Normalize object.
  404. /// Call this function in the end of object resizing.
  405. /// </summary>
  406. public virtual void Normalize()
  407. {
  408. this.Rectangle = GetNormalizedRectangleF(this.Rectangle);
  409. }
  410. public static RectangleF GetNormalizedRectangleF(RectangleF r)
  411. {
  412. return GetNormalizedRectangleF(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
  413. }
  414. public static RectangleF GetNormalizedRectangleF(PointF p1, PointF p2)
  415. {
  416. return GetNormalizedRectangleF(p1.X, p1.Y, p2.X, p2.Y);
  417. }
  418. public static RectangleF GetNormalizedRectangleF(float x1, float y1, float x2, float y2)
  419. {
  420. if (x2 < x1)
  421. {
  422. float tmp = x2;
  423. x2 = x1;
  424. x1 = tmp;
  425. }
  426. if (y2 < y1)
  427. {
  428. float tmp = y2;
  429. y2 = y1;
  430. y1 = tmp;
  431. }
  432. return new RectangleF(x1, y1, (x2 - x1), (y2 - y1));
  433. }
  434. public static Rectangle GetNormalizedRectangle(RectangleF r)
  435. {
  436. return GetNormalizedRectangle(r.X, r.Y, r.X + r.Width, r.Y + r.Height);
  437. }
  438. public static Rectangle GetNormalizedRectangle(PointF p1, PointF p2)
  439. {
  440. return GetNormalizedRectangle(p1.X, p1.Y, p2.X, p2.Y);
  441. }
  442. public static Rectangle GetNormalizedRectangle(float x1, float y1, float x2, float y2)
  443. {
  444. if (x2 < x1)
  445. {
  446. float tmp = x2;
  447. x2 = x1;
  448. x1 = tmp;
  449. }
  450. if (y2 < y1)
  451. {
  452. float tmp = y2;
  453. y2 = y1;
  454. y1 = tmp;
  455. }
  456. return new Rectangle((int)x1, (int)y1, (int)(x2 - x1), (int)(y2 - y1));
  457. }
  458. /// <summary>
  459. /// Save object to serialization stream
  460. /// </summary>
  461. /// <param name="info"></param>
  462. /// <param name="orderNumber"></param>
  463. public virtual void SaveToStream(SerializationInfo info, int orderNumber)
  464. {
  465. info.AddValue(
  466. String.Format(CultureInfo.InvariantCulture,
  467. "{0}{1}",
  468. entryColor, orderNumber),
  469. Color.ToArgb());
  470. info.AddValue(
  471. String.Format(CultureInfo.InvariantCulture,
  472. "{0}{1}",
  473. entryPenWidth, orderNumber),
  474. PenWidth);
  475. }
  476. /// <summary>
  477. /// Load object from serialization stream
  478. /// </summary>
  479. /// <param name="info"></param>
  480. /// <param name="orderNumber"></param>
  481. public virtual void LoadFromStream(SerializationInfo info, int orderNumber)
  482. {
  483. int n = info.GetInt32(
  484. String.Format(CultureInfo.InvariantCulture,
  485. "{0}{1}",
  486. entryColor, orderNumber));
  487. Color = Color.FromArgb(n);
  488. PenWidth = info.GetInt32(
  489. String.Format(CultureInfo.InvariantCulture,
  490. "{0}{1}",
  491. entryPenWidth, orderNumber));
  492. id = this.GetHashCode();
  493. }
  494. /// <summary>
  495. /// 用于返回组成标注/测量/视场的所有的点的集合
  496. /// 暂时没有想到有特殊的数据需要处理,想到再说
  497. /// </summary>
  498. /// <returns></returns>
  499. public virtual List<PointF> GetPoints()
  500. {
  501. return null;
  502. }
  503. public virtual ParentStyleModel GetStyle()
  504. {
  505. return null;
  506. }
  507. public virtual string GetContent()
  508. {
  509. return "";
  510. }
  511. #endregion
  512. #region 其它方法
  513. /// <summary>
  514. /// Initialization
  515. /// </summary>
  516. protected void Initialize()
  517. {
  518. color = lastUsedColor;
  519. penWidth = LastUsedPenWidth;
  520. }
  521. /// <summary>
  522. /// Copy fields from this instance to cloned instance drawObject.
  523. /// Called from Clone functions of derived classes.
  524. /// </summary>
  525. protected void FillDrawObjectFields(DrawObject drawObject)
  526. {
  527. drawObject.selected = this.selected;
  528. drawObject.color = this.color;
  529. drawObject.penWidth = this.penWidth;
  530. drawObject.ID = this.ID;
  531. }
  532. #endregion
  533. }
  534. }