Slider.cpp 6.2 KB

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