SnapObstacle.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Drawing;
  3. namespace PaintDotNet
  4. {
  5. public abstract class SnapObstacle
  6. {
  7. public const int DefaultSnapProximity = 15;
  8. public const int DefaultSnapDistance = 3;
  9. private string name;
  10. protected Rectangle previousBounds; // for BoundsChanged event
  11. protected Rectangle bounds;
  12. private SnapRegion snapRegion;
  13. private bool stickyEdges;
  14. private int snapProximity;
  15. private int snapDistance;
  16. private bool enabled;
  17. private bool enableSave;
  18. public string Name
  19. {
  20. get
  21. {
  22. return this.name;
  23. }
  24. }
  25. /// <summary>
  26. /// Gets the bounds of this snap obstacle, defined in coordinates relative to its container.
  27. /// </summary>
  28. public Rectangle Bounds
  29. {
  30. get
  31. {
  32. return this.bounds;
  33. }
  34. }
  35. protected virtual void OnBoundsChangeRequested(Rectangle newBounds, ref bool handled)
  36. {
  37. }
  38. public bool RequestBoundsChange(Rectangle newBounds)
  39. {
  40. bool handled = false;
  41. OnBoundsChangeRequested(newBounds, ref handled);
  42. return handled;
  43. }
  44. public SnapRegion SnapRegion
  45. {
  46. get
  47. {
  48. return this.snapRegion;
  49. }
  50. }
  51. /// <summary>
  52. /// Gets whether or not this obstacle has "sticky" edges.
  53. /// </summary>
  54. /// <remarks>
  55. /// If an obstacle has sticky edges, than any obstacle that is snapped on
  56. /// to it will move with this obstacle.
  57. /// </remarks>
  58. public bool StickyEdges
  59. {
  60. get
  61. {
  62. return this.stickyEdges;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets how close another obstacle must be to snap to this one, in pixels
  67. /// </summary>
  68. public int SnapProximity
  69. {
  70. get
  71. {
  72. return this.snapProximity;
  73. }
  74. }
  75. /// <summary>
  76. /// Gets how close another obstacle will be parked when it snaps to this one, in pixels.
  77. /// </summary>
  78. public int SnapDistance
  79. {
  80. get
  81. {
  82. return this.snapDistance;
  83. }
  84. }
  85. public bool Enabled
  86. {
  87. get
  88. {
  89. return this.enabled;
  90. }
  91. set
  92. {
  93. this.enabled = value;
  94. }
  95. }
  96. public bool EnableSave
  97. {
  98. get
  99. {
  100. return this.enableSave;
  101. }
  102. set
  103. {
  104. this.enableSave = value;
  105. }
  106. }
  107. /// <summary>
  108. /// Raised before the Bounds is changed.
  109. /// </summary>
  110. /// <remarks>
  111. /// The Data property of the event args is the value that Bounds is being set to.
  112. /// </remarks>
  113. public event EventHandler<EventArgs<Rectangle>> BoundsChanging;
  114. protected virtual void OnBoundsChanging()
  115. {
  116. if (BoundsChanging != null)
  117. {
  118. BoundsChanging(this, new EventArgs<Rectangle>(this.Bounds));
  119. }
  120. }
  121. /// <summary>
  122. /// Raised after the Bounds is changed.
  123. /// </summary>
  124. /// <remarks>
  125. /// The Data property of the event args is the value that Bounds was just changed from.
  126. /// </remarks>
  127. public event EventHandler<EventArgs<Rectangle>> BoundsChanged;
  128. protected virtual void OnBoundsChanged()
  129. {
  130. if (BoundsChanged != null)
  131. {
  132. BoundsChanged(this, new EventArgs<Rectangle>(this.previousBounds));
  133. }
  134. }
  135. internal SnapObstacle(string name, Rectangle bounds, SnapRegion snapRegion, bool stickyEdges)
  136. : this(name, bounds, snapRegion, stickyEdges, DefaultSnapProximity, DefaultSnapDistance)
  137. {
  138. }
  139. internal SnapObstacle(string name, Rectangle bounds, SnapRegion snapRegion, bool stickyEdges, int snapProximity, int snapDistance)
  140. {
  141. this.name = name;
  142. this.bounds = bounds;
  143. this.previousBounds = bounds;
  144. this.snapRegion = snapRegion;
  145. this.stickyEdges = stickyEdges;
  146. this.snapProximity = snapProximity;
  147. this.snapDistance = snapDistance;
  148. this.enabled = true;
  149. this.enableSave = true;
  150. }
  151. }
  152. }