Slider.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "Slider.h"
  2. namespace gameplay
  3. {
  4. Slider::Slider()
  5. {
  6. }
  7. Slider::~Slider()
  8. {
  9. }
  10. Slider* Slider::create(Theme::Style* style, Properties* properties)
  11. {
  12. Slider* slider = new Slider();
  13. slider->init(style, properties);
  14. slider->_min = properties->getFloat("min");
  15. slider->_max = properties->getFloat("max");
  16. slider->_value = properties->getFloat("value");
  17. slider->_step = properties->getFloat("step");
  18. return slider;
  19. }
  20. void Slider::setMin(float min)
  21. {
  22. _min = min;
  23. }
  24. float Slider::getMin()
  25. {
  26. return _min;
  27. }
  28. void Slider::setMax(float max)
  29. {
  30. _max = max;
  31. }
  32. float Slider::getMax()
  33. {
  34. return _max;
  35. }
  36. void Slider::setStep(float step)
  37. {
  38. _step = step;
  39. }
  40. float Slider::getStep()
  41. {
  42. return _step;
  43. }
  44. float Slider::getValue()
  45. {
  46. return _value;
  47. }
  48. void Slider::setValue(float value)
  49. {
  50. _value = MATH_CLAMP(value, _min, _max);
  51. }
  52. void Slider::addListener(Control::Listener* listener, int eventFlags)
  53. {
  54. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  55. {
  56. WARN("TEXT_CHANGED event is not applicable to Slider.");
  57. eventFlags &= ~Listener::TEXT_CHANGED;
  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 > 0 && x <= _bounds.width &&
  76. y > 0 && y <= _bounds.height)
  77. {
  78. // Horizontal case.
  79. Theme::Border border;
  80. Theme::ContainerRegion* containerRegion = _style->getOverlay(getOverlayType())->getContainerRegion();
  81. if (containerRegion)
  82. {
  83. border = containerRegion->getBorder();
  84. }
  85. Theme::Padding padding = _style->getPadding();
  86. const Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  87. const Theme::SliderIcon* icon = overlay->getSliderIcon();
  88. const Vector2 minCapSize = icon->getMinCapSize();
  89. const Vector2 maxCapSize = icon->getMaxCapSize();
  90. float markerPosition = ((float)x - maxCapSize.x - border.left - padding.left) /
  91. (_bounds.width - border.left - border.right - padding.left - padding.right - minCapSize.x - maxCapSize.x);
  92. if (markerPosition > 1.0f)
  93. {
  94. markerPosition = 1.0f;
  95. }
  96. else if (markerPosition < 0.0f)
  97. {
  98. markerPosition = 0.0f;
  99. }
  100. float oldValue = _value;
  101. _value = markerPosition * _max;
  102. if (_step > 0.0f)
  103. {
  104. float stepDistance = _step / (_max - _min);
  105. int numSteps = round(_value / _step);
  106. _value = _step * numSteps;
  107. }
  108. // Call the callback if our value changed.
  109. if (_value != oldValue)
  110. {
  111. alertListeners(Listener::VALUE_CHANGED);
  112. }
  113. _dirty = true;
  114. }
  115. }
  116. return Control::touchEvent(evt, x, y, contactIndex);
  117. }
  118. void Slider::drawSprites(SpriteBatch* spriteBatch, const Rectangle& clip)
  119. {
  120. // TODO: Vertical slider.
  121. // The slider is drawn in the center of the control (perpendicular to orientation).
  122. // The track is stretched according to orientation.
  123. // Caps and marker are not stretched.
  124. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  125. Theme::SliderIcon* icon = overlay->getSliderIcon();
  126. if (icon)
  127. {
  128. Theme::Border border;
  129. Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  130. if (containerRegion)
  131. {
  132. border = containerRegion->getBorder();
  133. }
  134. Theme::Padding padding = _style->getPadding();
  135. const Vector2 minCapSize = icon->getMinCapSize();
  136. const Vector2 maxCapSize = icon->getMaxCapSize();
  137. const Vector2 markerSize = icon->getMarkerSize();
  138. const Vector2 trackSize = icon->getTrackSize();
  139. const Theme::UVs minCap = icon->getMinCapUVs();
  140. const Theme::UVs maxCap = icon->getMaxCapUVs();
  141. const Theme::UVs marker = icon->getMarkerUVs();
  142. const Theme::UVs track = icon->getTrackUVs();
  143. const Vector4 color = icon->getColor();
  144. // Draw order: track, caps, marker.
  145. float midY = clip.y + _bounds.y + (_bounds.height - border.bottom - padding.bottom) / 2.0f;
  146. Vector2 pos(clip.x + _bounds.x + border.left + padding.left, midY - trackSize.y / 2.0f);
  147. spriteBatch->draw(pos.x, pos.y, _bounds.width, trackSize.y, track.u1, track.v1, track.u2, track.v2, color);
  148. pos.y = midY - minCapSize.y * 0.5f;
  149. pos.x -= minCapSize.x * 0.5f;
  150. spriteBatch->draw(pos.x, pos.y, minCapSize.x, minCapSize.y, minCap.u1, minCap.v1, minCap.u2, minCap.v2, color);
  151. pos.x = clip.x + _bounds.x + _bounds.width - border.right - padding.right - maxCapSize.x * 0.5f;
  152. spriteBatch->draw(pos.x, pos.y, maxCapSize.x, maxCapSize.y, maxCap.u1, maxCap.v1, maxCap.u2, maxCap.v2, color);
  153. // Percent across.
  154. float markerPosition = _value / (_max - _min);
  155. markerPosition *= _bounds.width - border.left - padding.left - border.right - padding.right - minCapSize.x * 0.5f - maxCapSize.x * 0.5f - markerSize.x;
  156. pos.x = clip.x + _bounds.x + border.left + padding.left + minCapSize.x * 0.5f + markerPosition;
  157. pos.y = midY - markerSize.y / 2.0f;
  158. spriteBatch->draw(pos.x, pos.y, markerSize.x, markerSize.y, marker.u1, marker.v1, marker.u2, marker.v2, color);
  159. }
  160. }
  161. }