Slider.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "Slider.h"
  2. namespace gameplay
  3. {
  4. static std::vector<Slider*> __sliders;
  5. Slider::Slider()
  6. {
  7. }
  8. Slider::~Slider()
  9. {
  10. }
  11. Slider* Slider::create(Theme::Style* style, Properties* properties)
  12. {
  13. Slider* slider = new Slider();
  14. slider->_style = style;
  15. slider->_id = properties->getId();
  16. properties->getVector2("position", &slider->_position);
  17. properties->getVector2("size", &slider->_size);
  18. slider->_min = properties->getFloat("min");
  19. slider->_max = properties->getFloat("max");
  20. slider->_value = properties->getFloat("value");
  21. slider->_step = properties->getFloat("step");
  22. slider->_text = properties->getString("text");
  23. __sliders.push_back(slider);
  24. return slider;
  25. }
  26. Slider* Slider::create(const char* id, float min, float max, float default, float step)
  27. {
  28. Slider* slider = new Slider();
  29. return slider;
  30. }
  31. Slider* Slider::getSlider(const char* id)
  32. {
  33. std::vector<Slider*>::const_iterator it;
  34. for (it = __sliders.begin(); it < __sliders.end(); it++)
  35. {
  36. Slider* slider = *it;
  37. if (strcmp(id, slider->getID()) == 0)
  38. {
  39. return slider;
  40. }
  41. }
  42. return NULL;
  43. }
  44. void Slider::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  45. {
  46. Button::touchEvent(evt, x, y, contactIndex);
  47. if (_state == STATE_ACTIVE &&
  48. x > 0 && x <= _size.x &&
  49. y > 0 && y <= _size.y)
  50. {
  51. // Horizontal case.
  52. Theme::Border border = _style->getBorder();
  53. Theme::Padding padding = _style->getPadding();
  54. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  55. Theme::SliderIcon* icon = overlay->getSliderIcon();
  56. float markerPosition = ((float)x - icon->rightCapSize.x - border.left - padding.left) /
  57. (_size.x - border.left - border.right - padding.left - padding.right - icon->leftCapSize.x - icon->rightCapSize.x);
  58. if (markerPosition > 1.0f)
  59. {
  60. markerPosition = 1.0f;
  61. }
  62. else if (markerPosition < 0.0f)
  63. {
  64. markerPosition = 0.0f;
  65. }
  66. _value = markerPosition * _max;
  67. if (_step > 0.0f)
  68. {
  69. float stepDistance = _step / (_max - _min);
  70. int numSteps = round(_value / _step);
  71. _value = _step * numSteps;
  72. }
  73. }
  74. }
  75. void Slider::drawSprites(SpriteBatch* spriteBatch, const Vector2& position)
  76. {
  77. // TODO: Vertical slider.
  78. // The slider is drawn in the center of the control (perpendicular to orientation).
  79. // The track is stretched according to orientation.
  80. // Caps and marker are not stretched.
  81. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  82. Theme::SliderIcon* icon = overlay->getSliderIcon();
  83. if (icon)
  84. {
  85. Theme::Border border = _style->getBorder();
  86. Theme::Padding padding = _style->getPadding();
  87. // Draw order: track, caps, marker.
  88. float midY = position.y + _position.y + (_size.y - border.bottom - padding.bottom) / 2.0f;
  89. Vector2 pos(position.x + _position.x + border.left + padding.left, midY - icon->trackSize.y / 2.0f);
  90. spriteBatch->draw(pos.x, pos.y, _size.x, icon->trackSize.y, icon->track.u1, icon->track.v1, icon->track.u2, icon->track.v2, overlay->getBorderColor());
  91. pos.y = midY - icon->leftCapSize.y / 2.0f;
  92. spriteBatch->draw(pos.x, pos.y, icon->leftCapSize.x, icon->leftCapSize.y, icon->leftCap.u1, icon->leftCap.v1, icon->leftCap.u2, icon->leftCap.v2, overlay->getBorderColor());
  93. pos.x = position.x + _position.x + _size.x - border.left - padding.left - icon->rightCapSize.x;
  94. spriteBatch->draw(pos.x, pos.y, icon->rightCapSize.x, icon->rightCapSize.y, icon->rightCap.u1, icon->rightCap.v1, icon->rightCap.u2, icon->rightCap.v2, overlay->getBorderColor());
  95. // Percent across.
  96. float markerPosition = _value / (_max - _min);
  97. markerPosition *= _size.x - border.left - padding.left - border.right - padding.right - icon->leftCapSize.x - icon->rightCapSize.x - icon->markerSize.x;
  98. pos.x = position.x + _position.x + border.left + padding.left + icon->leftCapSize.x + markerPosition;
  99. pos.y = midY - icon->markerSize.y / 2.0f;
  100. spriteBatch->draw(pos.x, pos.y, icon->markerSize.x, icon->markerSize.y, icon->marker.u1, icon->marker.v1, icon->marker.u2, icon->marker.v2, overlay->getBorderColor());
  101. }
  102. }
  103. void Slider::drawText(const Vector2& position)
  104. {
  105. }
  106. }