Slider.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "Slider.h"
  2. namespace gameplay
  3. {
  4. // Fraction of slider to scroll when mouse scrollwheel is used.
  5. static const float SCROLL_FRACTION = 0.1f;
  6. Slider::Slider() : _minImage(NULL), _maxImage(NULL), _trackImage(NULL), _markerImage(NULL)
  7. {
  8. }
  9. Slider::~Slider()
  10. {
  11. }
  12. Slider* Slider::create(const char* id, Theme::Style* style)
  13. {
  14. GP_ASSERT(style);
  15. Slider* slider = new Slider();
  16. if (id)
  17. slider->_id = id;
  18. slider->setStyle(style);
  19. return slider;
  20. }
  21. Slider* Slider::create(Theme::Style* style, Properties* properties)
  22. {
  23. GP_ASSERT(properties);
  24. Slider* slider = new Slider();
  25. slider->initialize(style, properties);
  26. slider->_min = properties->getFloat("min");
  27. slider->_max = properties->getFloat("max");
  28. slider->_value = properties->getFloat("value");
  29. slider->_step = properties->getFloat("step");
  30. return slider;
  31. }
  32. void Slider::setMin(float min)
  33. {
  34. _min = min;
  35. }
  36. float Slider::getMin()
  37. {
  38. return _min;
  39. }
  40. void Slider::setMax(float max)
  41. {
  42. _max = max;
  43. }
  44. float Slider::getMax()
  45. {
  46. return _max;
  47. }
  48. void Slider::setStep(float step)
  49. {
  50. _step = step;
  51. }
  52. float Slider::getStep()
  53. {
  54. return _step;
  55. }
  56. float Slider::getValue()
  57. {
  58. return _value;
  59. }
  60. void Slider::setValue(float value)
  61. {
  62. _value = MATH_CLAMP(value, _min, _max);
  63. }
  64. void Slider::addListener(Control::Listener* listener, int eventFlags)
  65. {
  66. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  67. {
  68. GP_ERROR("TEXT_CHANGED event is not applicable to Slider.");
  69. }
  70. Control::addListener(listener, eventFlags);
  71. }
  72. bool Slider::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  73. {
  74. if (!isEnabled())
  75. {
  76. return false;
  77. }
  78. switch (evt)
  79. {
  80. case Touch::TOUCH_PRESS:
  81. _state = Control::ACTIVE;
  82. // Fall through to calculate new value.
  83. case Touch::TOUCH_MOVE:
  84. case Touch::TOUCH_RELEASE:
  85. if (_state == ACTIVE &&
  86. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  87. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  88. {
  89. // Horizontal case.
  90. const Theme::Border& border = getBorder(_state);
  91. const Theme::Padding& padding = getPadding();
  92. const Rectangle& minCapRegion = _minImage->getRegion();
  93. const Rectangle& maxCapRegion = _maxImage->getRegion();
  94. float markerPosition = ((float)x - maxCapRegion.width - border.left - padding.left) /
  95. (_bounds.width - border.left - border.right - padding.left - padding.right - minCapRegion.width - maxCapRegion.width);
  96. if (markerPosition > 1.0f)
  97. {
  98. markerPosition = 1.0f;
  99. }
  100. else if (markerPosition < 0.0f)
  101. {
  102. markerPosition = 0.0f;
  103. }
  104. float oldValue = _value;
  105. _value = (markerPosition * (_max - _min)) + _min;
  106. if (_step > 0.0f)
  107. {
  108. float stepDistance = _step / (_max - _min);
  109. int numSteps = round(_value / _step);
  110. _value = _step * numSteps;
  111. }
  112. // Call the callback if our value changed.
  113. if (_value != oldValue)
  114. {
  115. notifyListeners(Listener::VALUE_CHANGED);
  116. }
  117. _dirty = true;
  118. }
  119. if (evt == Touch::TOUCH_RELEASE)
  120. {
  121. _state = FOCUS;
  122. _dirty = true;
  123. }
  124. break;
  125. }
  126. if (evt == Touch::TOUCH_MOVE)
  127. return _consumeInputEvents;
  128. else
  129. return Control::touchEvent(evt, x, y, contactIndex);
  130. }
  131. bool Slider::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  132. {
  133. if (!isEnabled())
  134. {
  135. return false;
  136. }
  137. switch (evt)
  138. {
  139. case Mouse::MOUSE_PRESS_LEFT_BUTTON:
  140. return touchEvent(Touch::TOUCH_PRESS, x, y, 0);
  141. case Mouse::MOUSE_MOVE:
  142. return touchEvent(Touch::TOUCH_MOVE, x, y, 0);
  143. case Mouse::MOUSE_RELEASE_LEFT_BUTTON:
  144. return touchEvent(Touch::TOUCH_RELEASE, x, y, 0);
  145. case Mouse::MOUSE_WHEEL:
  146. {
  147. if (_state == FOCUS)
  148. {
  149. float total = _max - _min;
  150. float oldValue = _value;
  151. _value += (total * SCROLL_FRACTION) * wheelDelta;
  152. if (_value > _max)
  153. _value = _max;
  154. else if (_value < _min)
  155. _value = _min;
  156. if (_value != oldValue)
  157. {
  158. notifyListeners(Listener::VALUE_CHANGED);
  159. }
  160. _dirty = true;
  161. return _consumeInputEvents;
  162. }
  163. break;
  164. }
  165. default:
  166. break;
  167. }
  168. return false;
  169. }
  170. void Slider::update(const Control* container, const Vector2& offset)
  171. {
  172. Label::update(container, offset);
  173. _minImage = getImage("minCap", _state);
  174. _maxImage = getImage("maxCap", _state);
  175. _markerImage = getImage("marker", _state);
  176. _trackImage = getImage("track", _state);
  177. }
  178. void Slider::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  179. {
  180. GP_ASSERT(spriteBatch);
  181. GP_ASSERT(_minImage);
  182. GP_ASSERT(_maxImage);
  183. GP_ASSERT(_markerImage);
  184. GP_ASSERT(_trackImage);
  185. // TODO: Vertical slider.
  186. // The slider is drawn in the center of the control (perpendicular to orientation).
  187. // The track is stretched according to orientation.
  188. // Caps and marker are not stretched.
  189. const Theme::Border& border = getBorder(_state);
  190. const Theme::Padding& padding = getPadding();
  191. const Rectangle& minCapRegion = _minImage->getRegion();
  192. const Rectangle& maxCapRegion = _maxImage->getRegion();
  193. const Rectangle& markerRegion = _markerImage->getRegion();
  194. const Rectangle& trackRegion = _trackImage->getRegion();
  195. const Theme::UVs& minCap = _minImage->getUVs();
  196. const Theme::UVs& maxCap = _maxImage->getUVs();
  197. const Theme::UVs& marker = _markerImage->getUVs();
  198. const Theme::UVs& track = _trackImage->getUVs();
  199. Vector4 minCapColor = _minImage->getColor();
  200. Vector4 maxCapColor = _maxImage->getColor();
  201. Vector4 markerColor = _markerImage->getColor();
  202. Vector4 trackColor = _trackImage->getColor();
  203. minCapColor.w *= _opacity;
  204. maxCapColor.w *= _opacity;
  205. markerColor.w *= _opacity;
  206. trackColor.w *= _opacity;
  207. // Draw order: track, caps, marker.
  208. float midY = _viewportBounds.y + (_viewportBounds.height) * 0.5f;
  209. Vector2 pos(_viewportBounds.x, midY - trackRegion.height * 0.5f);
  210. spriteBatch->draw(pos.x, pos.y, _viewportBounds.width, trackRegion.height, track.u1, track.v1, track.u2, track.v2, trackColor, _viewportClipBounds);
  211. pos.y = midY - minCapRegion.height * 0.5f;
  212. pos.x -= minCapRegion.width * 0.5f;
  213. spriteBatch->draw(pos.x, pos.y, minCapRegion.width, minCapRegion.height, minCap.u1, minCap.v1, minCap.u2, minCap.v2, minCapColor, _viewportClipBounds);
  214. pos.x = _viewportBounds.x + _viewportBounds.width - maxCapRegion.width * 0.5f;
  215. spriteBatch->draw(pos.x, pos.y, maxCapRegion.width, maxCapRegion.height, maxCap.u1, maxCap.v1, maxCap.u2, maxCap.v2, maxCapColor, _viewportClipBounds);
  216. // Percent across.
  217. float markerPosition = (_value - _min) / (_max - _min);
  218. markerPosition *= _viewportBounds.width - minCapRegion.width * 0.5f - maxCapRegion.width * 0.5f - markerRegion.width;
  219. pos.x = _viewportBounds.x + minCapRegion.width * 0.5f + markerPosition;
  220. pos.y = midY - markerRegion.height / 2.0f;
  221. spriteBatch->draw(pos.x, pos.y, markerRegion.width, markerRegion.height, marker.u1, marker.v1, marker.u2, marker.v2, markerColor, _viewportClipBounds);
  222. }
  223. const char* Slider::getType() const
  224. {
  225. return "slider";
  226. }
  227. }