ViewSquare.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using PaintDotNet.Annotation.Enum;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Globalization;
  8. using System.Runtime.Serialization;
  9. using System.Windows.Forms;
  10. namespace PaintDotNet.Annotation.FieldView
  11. {
  12. /// <summary>
  13. /// 正方形视场绘制
  14. /// </summary>
  15. public class ViewSquare : ViewBase
  16. {
  17. public override double Width
  18. {
  19. get { return Rectangle.Width; }
  20. set
  21. {
  22. MoveHandleTo(new PointF((float)(Points[0].X + value), (float)(Points[0].Y + value)), 3);
  23. }
  24. }
  25. #region Constructor
  26. public ViewSquare()
  27. {
  28. objectType = DrawClass.View;
  29. drawToolType = DrawToolType.ViewSquare;
  30. Initialize();
  31. }
  32. public ViewSquare(int x, int y) : this()
  33. {
  34. for (int i = 0; i < 4; i++) AddPoint(x, y);
  35. }
  36. public ViewSquare(List<PointF> points) : this()
  37. {
  38. Points = points;
  39. }
  40. #endregion
  41. #region Handles
  42. public override int HandleCount => Points.Count;
  43. public override PointF GetHandle(int handleIndex)
  44. {
  45. return Points[handleIndex - 1];
  46. }
  47. public override Cursor GetHandleCursor(int handleNumber)
  48. {
  49. switch (handleNumber)
  50. {
  51. case 1:
  52. return Cursors.SizeNWSE;
  53. case 2:
  54. return Cursors.SizeNESW;
  55. case 3:
  56. return Cursors.SizeNWSE;
  57. case 4:
  58. return Cursors.SizeNESW;
  59. default:
  60. return Cursors.Default;
  61. }
  62. }
  63. public override void MoveHandleTo(Point point, int handleNumber)
  64. {
  65. MoveHandleTo((PointF)point, handleNumber);
  66. }
  67. public override void MoveHandleTo(PointF point, int handleNumber)
  68. {
  69. MoveCorner(point, handleNumber - 1);
  70. OnPropertyChanged();
  71. }
  72. private void MoveCorner(PointF point, int i)
  73. {
  74. var p0 = Points[(i + 2) % 4];
  75. var lenX = point.X - p0.X;
  76. var lenY = point.Y - p0.Y;
  77. var len = Math.Max(Math.Abs(lenX), Math.Abs(lenY));
  78. lenX = lenX > 0 ? len : -len;
  79. lenY = lenY > 0 ? len : -len;
  80. Points[i] = new PointF(p0.X + lenX, p0.Y + lenY);
  81. if (i % 2 == 0)
  82. {
  83. Points[(i + 1) % 4] = new PointF(p0.X + lenX, p0.Y);
  84. Points[(i + 3) % 4] = new PointF(p0.X, p0.Y + lenY);
  85. }
  86. else
  87. {
  88. Points[(i + 3) % 4] = new PointF(p0.X + lenX, p0.Y);
  89. Points[(i + 1) % 4] = new PointF(p0.X, p0.Y + lenY);
  90. }
  91. }
  92. #endregion
  93. #region Clone
  94. public override DrawObject Clone()
  95. {
  96. return Clone(ISurfaceBox);
  97. }
  98. /// <summary>
  99. /// Clone this instance
  100. /// </summary>
  101. public override DrawObject Clone(ISurfaceBox surfaceBox)
  102. {
  103. ViewSquare drawRectangle = new ViewSquare(Points);
  104. drawRectangle.ISurfaceBox = surfaceBox;
  105. drawRectangle.combineMode = this.combineMode;
  106. drawRectangle.Color = Color;
  107. drawRectangle.ID = ID;
  108. FillDrawObjectFields(drawRectangle);
  109. return drawRectangle;
  110. }
  111. #endregion
  112. }
  113. }