Slider.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef SLIDER_H_
  2. #define SLIDER_H_
  3. #include "Base.h"
  4. #include "Theme.h"
  5. #include "Properties.h"
  6. #include "Button.h"
  7. #include "Touch.h"
  8. namespace gameplay
  9. {
  10. /**
  11. * A slider consists of a marker that can slide along a track between two end-caps.
  12. * The following properties are available for sliders:
  13. @verbatim
  14. slider
  15. {
  16. style = <styleID> // A Style from the Theme.
  17. position = <x, y> // Position of the Control on-screen, measured in pixels.
  18. size = <width, height> // Size of the Control, measured in pixels.
  19. min = <float> // The value of the left- / bottom-most point on the slider.
  20. max = <float> // The value of the right- / top-most point on the slider.
  21. value = <float> // The default position of the marker.
  22. step = <float> // If greater than 0, force the marker to snap to discrete multiples of 'step'.
  23. text = <string> // Text to display above, below or alongside the slider (depending on the style).
  24. consumeEvents = <bool> // Whether the slider propagates input events to the Game's input event handler. Default is true.
  25. // TODO: orientation = <HORIZONTAL or VERTICAL> // Determines whether a slider is stretched along its width or its height
  26. }
  27. @endverbatim
  28. */
  29. class Slider : public Label
  30. {
  31. friend class Container;
  32. public:
  33. /**
  34. * Create a new slider control.
  35. *
  36. * @param id The control's ID.
  37. * @param style The control's style.
  38. *
  39. * @return The new slider.
  40. * @script{create}
  41. */
  42. static Slider* create(const char* id, Theme::Style* style);
  43. /**
  44. * Set the minimum value that can be set on this slider.
  45. *
  46. * @param min The new minimum.
  47. */
  48. void setMin(float min);
  49. /**
  50. * Get the minimum value that can be set on this slider.
  51. *
  52. * @return The minimum value that can be set on this slider.
  53. */
  54. float getMin();
  55. /**
  56. * Set the maximum value that can be set on this slider.
  57. *
  58. * @param max The new maximum.
  59. */
  60. void setMax(float max);
  61. /**
  62. * Get the maximum value that can be set on this slider.
  63. *
  64. * @return The maximum value that can be set on this slider.
  65. */
  66. float getMax();
  67. /**
  68. * Set this slider's step size. If this is greater than zero, the marker
  69. * will snap to discrete multiples of the step size.
  70. *
  71. * @param step The new step size.
  72. */
  73. void setStep(float step);
  74. /**
  75. * Get this slider's step size.
  76. *
  77. * @return This slider's step size.
  78. */
  79. float getStep();
  80. /**
  81. * Set this slider's value. The new value will be clamped to fit within
  82. * the slider's minimum and maximum values.
  83. *
  84. * @param value The new value.
  85. */
  86. void setValue(float value);
  87. /**
  88. * Get this slider's current value.
  89. *
  90. * @return This slider's current value.
  91. */
  92. float getValue();
  93. /**
  94. * @see Control::getType
  95. */
  96. const char* getType() const;
  97. /**
  98. * Add a listener to be notified of specific events affecting
  99. * this control. Event types can be OR'ed together.
  100. * E.g. To listen to touch-press and touch-release events,
  101. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  102. * as the second parameter.
  103. *
  104. * @param listener The listener to add.
  105. * @param eventFlags The events to listen for.
  106. */
  107. void addListener(Control::Listener* listener, int eventFlags);
  108. protected:
  109. /**
  110. * Constructor.
  111. */
  112. Slider();
  113. /**
  114. * Destructor.
  115. */
  116. ~Slider();
  117. /**
  118. * Create a slider with a given style and properties.
  119. *
  120. * @param style The style to apply to this slider.
  121. * @param properties The properties to set on this slider.
  122. *
  123. * @return The new slider.
  124. */
  125. static Slider* create(Theme::Style* style, Properties* properties);
  126. /**
  127. * Touch callback on touch events. Controls return true if they consume the touch event.
  128. *
  129. * @param evt The touch event that occurred.
  130. * @param x The x position of the touch in pixels. Left edge is zero.
  131. * @param y The y position of the touch in pixels. Top edge is zero.
  132. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  133. *
  134. * @return Whether the touch event was consumed by the control.
  135. *
  136. * @see Touch::TouchEvent
  137. */
  138. bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  139. /**
  140. * Mouse callback on mouse events.
  141. *
  142. * @param evt The mouse event that occurred.
  143. * @param x The x position of the mouse in pixels. Left edge is zero.
  144. * @param y The y position of the mouse in pixels. Top edge is zero.
  145. * @param wheelDelta The number of mouse wheel ticks. Positive is up (forward), negative is down (backward).
  146. *
  147. * @return True if the mouse event is consumed or false if it is not consumed.
  148. *
  149. * @see Mouse::MouseEvent
  150. */
  151. bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  152. /**
  153. * Draw the images associated with this control.
  154. *
  155. * @param spriteBatch The sprite batch containing this control's icons.
  156. * @param clip The clipping rectangle of this control's parent container.
  157. */
  158. void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  159. /**
  160. * Called when a slider's properties change. Updates this slider's internal rendering
  161. * properties, such as its text viewport.
  162. *
  163. * @param container This slider's parent container.
  164. * @param offset The scroll offset of this slider's parent container.
  165. */
  166. void update(const Control* container, const Vector2& offset);
  167. /**
  168. * The minimum value for the Slider.
  169. */
  170. float _min;
  171. /**
  172. * The maximum value for the Slider
  173. */
  174. float _max;
  175. /**
  176. * The Slider's step size.
  177. */
  178. float _step;
  179. /**
  180. * The Slider's current value.
  181. */
  182. float _value;
  183. /**
  184. * The X coordinate of the first touch event in a sequence.
  185. */
  186. float _originalX;
  187. /**
  188. * The Slider's original value at the start of a sequence of touch events.
  189. */
  190. float _originalValue;
  191. /**
  192. * The Slider's original setting of _consumeInputEvents at the start of a sequence of touch events.
  193. */
  194. bool _originalConsumeInputEvents;
  195. /**
  196. * Whether the Slider's current movement has been cancelled, e.g. because the user is scrolling the parent container.
  197. */
  198. bool _moveCancelled;
  199. /**
  200. * The image for the minimum slider value.
  201. */
  202. Theme::ThemeImage* _minImage;
  203. /**
  204. * The image for the maximum slider value.
  205. */
  206. Theme::ThemeImage* _maxImage;
  207. /**
  208. * The image for the slider track.
  209. */
  210. Theme::ThemeImage* _trackImage;
  211. /**
  212. * The image for the slider marker.
  213. */
  214. Theme::ThemeImage* _markerImage;
  215. private:
  216. /**
  217. * Constructor.
  218. */
  219. Slider(const Slider& copy);
  220. };
  221. }
  222. #endif