SnapObstacle.cs 4.5 KB

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