Slider.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include "Slider.h"
  2. #include "Game.h"
  3. namespace gameplay
  4. {
  5. // Fraction of slider to scroll when mouse scrollwheel is used.
  6. static const float SCROLLWHEEL_FRACTION = 0.1f;
  7. // Fraction of slider to scroll for a delta of 1.0f when a gamepad or keyboard is used.
  8. static const float MOVE_FRACTION = 0.005f;
  9. // Distance that a slider must be moved before it starts consuming input events,
  10. // e.g. to prevent its parent container from scrolling at the same time.
  11. static const float SLIDER_THRESHOLD = 5.0f;
  12. Slider::Slider() : _min(0.0f), _max(0.0f), _step(0.0f), _value(0.0f), _delta(0.0f), _minImage(NULL),
  13. _maxImage(NULL), _trackImage(NULL), _markerImage(NULL), _valueTextVisible(false),
  14. _valueTextAlignment(Font::ALIGN_BOTTOM_HCENTER), _valueTextPrecision(0), _valueText(""),
  15. _selectButtonDown(false), _directionButtonDown(false), _gamepadValue(0.0f)
  16. {
  17. _canFocus = true;
  18. }
  19. Slider::~Slider()
  20. {
  21. }
  22. Slider* Slider::create(const char* id, Theme::Style* style)
  23. {
  24. GP_ASSERT(style);
  25. Slider* slider = new Slider();
  26. if (id)
  27. slider->_id = id;
  28. slider->setStyle(style);
  29. return slider;
  30. }
  31. Control* Slider::create(Theme::Style* style, Properties* properties)
  32. {
  33. GP_ASSERT(properties);
  34. Slider* slider = new Slider();
  35. slider->initialize(style, properties);
  36. slider->_min = properties->getFloat("min");
  37. slider->_max = properties->getFloat("max");
  38. slider->_value = properties->getFloat("value");
  39. slider->_step = properties->getFloat("step");
  40. slider->_valueTextVisible = properties->getBool("valueTextVisible");
  41. slider->_valueTextPrecision = properties->getInt("valueTextPrecision");
  42. if (properties->exists("valueTextAlignment"))
  43. {
  44. slider->_valueTextAlignment = Font::getJustify(properties->getString("valueTextAlignment"));
  45. }
  46. return slider;
  47. }
  48. void Slider::setMin(float min)
  49. {
  50. if (_min != _min)
  51. {
  52. _dirty = true;
  53. }
  54. _min = min;
  55. }
  56. float Slider::getMin() const
  57. {
  58. return _min;
  59. }
  60. void Slider::setMax(float max)
  61. {
  62. if (max != _max)
  63. {
  64. _dirty = true;
  65. }
  66. _max = max;
  67. }
  68. float Slider::getMax() const
  69. {
  70. return _max;
  71. }
  72. void Slider::setStep(float step)
  73. {
  74. _step = step;
  75. }
  76. float Slider::getStep() const
  77. {
  78. return _step;
  79. }
  80. float Slider::getValue() const
  81. {
  82. return _value;
  83. }
  84. void Slider::setValue(float value)
  85. {
  86. float oldValue = _value;
  87. _value = MATH_CLAMP(value, _min, _max);
  88. if (_value != value)
  89. {
  90. _dirty = true;
  91. }
  92. }
  93. void Slider::setValueTextVisible(bool valueTextVisible)
  94. {
  95. if (valueTextVisible != _valueTextVisible)
  96. {
  97. _dirty = true;
  98. }
  99. _valueTextVisible = valueTextVisible;
  100. }
  101. bool Slider::isValueTextVisible() const
  102. {
  103. return _valueTextVisible;
  104. }
  105. void Slider::setValueTextAlignment(Font::Justify alignment)
  106. {
  107. if (alignment != _valueTextAlignment)
  108. {
  109. _dirty = true;
  110. }
  111. _valueTextAlignment = alignment;
  112. }
  113. Font::Justify Slider::getValueTextAlignment() const
  114. {
  115. return _valueTextAlignment;
  116. }
  117. void Slider::setValueTextPrecision(unsigned int precision)
  118. {
  119. if (precision != _valueTextPrecision)
  120. {
  121. _dirty = true;
  122. }
  123. _valueTextPrecision = precision;
  124. }
  125. unsigned int Slider::getValueTextPrecision() const
  126. {
  127. return _valueTextPrecision;
  128. }
  129. void Slider::addListener(Control::Listener* listener, int eventFlags)
  130. {
  131. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  132. {
  133. GP_ERROR("TEXT_CHANGED event is not applicable to Slider.");
  134. }
  135. Control::addListener(listener, eventFlags);
  136. }
  137. void Slider::updateValue(int x, int y)
  138. {
  139. State state = getState();
  140. // If the point lies within this slider, update the value of the slider accordingly
  141. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  142. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  143. {
  144. // Horizontal case.
  145. const Theme::Border& border = getBorder(state);
  146. const Theme::Padding& padding = getPadding();
  147. const Rectangle& minCapRegion = _minImage->getRegion();
  148. const Rectangle& maxCapRegion = _maxImage->getRegion();
  149. float markerPosition = ((float)x - maxCapRegion.width - border.left - padding.left) /
  150. (_bounds.width - border.left - border.right - padding.left - padding.right - minCapRegion.width - maxCapRegion.width);
  151. if (markerPosition > 1.0f)
  152. {
  153. markerPosition = 1.0f;
  154. }
  155. else if (markerPosition < 0.0f)
  156. {
  157. markerPosition = 0.0f;
  158. }
  159. float oldValue = _value;
  160. _value = (markerPosition * (_max - _min)) + _min;
  161. if (_step > 0.0f)
  162. {
  163. int numSteps = round(_value / _step);
  164. _value = _step * numSteps;
  165. }
  166. // Call the callback if our value changed.
  167. if (_value != oldValue)
  168. {
  169. notifyListeners(Control::Listener::VALUE_CHANGED);
  170. }
  171. _dirty = true;
  172. }
  173. }
  174. bool Slider::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  175. {
  176. State state = getState();
  177. switch (evt)
  178. {
  179. case Touch::TOUCH_PRESS:
  180. updateValue(x, y);
  181. return true;
  182. case Touch::TOUCH_MOVE:
  183. if (state == ACTIVE)
  184. {
  185. updateValue(x, y);
  186. return true;
  187. }
  188. break;
  189. }
  190. return Control::touchEvent(evt, x, y, contactIndex);
  191. }
  192. bool Slider::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  193. {
  194. switch (evt)
  195. {
  196. case Mouse::MOUSE_WHEEL:
  197. {
  198. if (hasFocus())
  199. {
  200. float total = _max - _min;
  201. float oldValue = _value;
  202. _value += (total * SCROLLWHEEL_FRACTION) * wheelDelta;
  203. if (_value > _max)
  204. _value = _max;
  205. else if (_value < _min)
  206. _value = _min;
  207. if (_step > 0.0f)
  208. {
  209. int numSteps = round(_value / _step);
  210. _value = _step * numSteps;
  211. }
  212. if (_value != oldValue)
  213. {
  214. notifyListeners(Control::Listener::VALUE_CHANGED);
  215. }
  216. _dirty = true;
  217. return true;
  218. }
  219. break;
  220. }
  221. default:
  222. break;
  223. }
  224. // Return false to fall through to touch handling
  225. return false;
  226. }
  227. bool Slider::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  228. {
  229. bool eventConsumed = false;
  230. switch (evt)
  231. {
  232. case Gamepad::JOYSTICK_EVENT:
  233. {
  234. // The right analog stick can be used to change a slider's value.
  235. if (analogIndex == 1)
  236. {
  237. Vector2 joy;
  238. gamepad->getJoystickValues(analogIndex, &joy);
  239. _gamepadValue = _value;
  240. _delta = joy.x;
  241. _dirty = true;
  242. return true;
  243. }
  244. break;
  245. }
  246. }
  247. return Label::gamepadEvent(evt, gamepad, analogIndex);
  248. }
  249. bool Slider::keyEvent(Keyboard::KeyEvent evt, int key)
  250. {
  251. switch (evt)
  252. {
  253. case Keyboard::KEY_PRESS:
  254. switch (key)
  255. {
  256. case Keyboard::KEY_LEFT_ARROW:
  257. if (_step > 0.0f)
  258. {
  259. _value = std::max(_value - _step, _min);
  260. }
  261. else
  262. {
  263. _value = std::max(_value - (_max - _min) * MOVE_FRACTION, _min);
  264. }
  265. _dirty = true;
  266. return true;
  267. case Keyboard::KEY_RIGHT_ARROW:
  268. if (_step > 0.0f)
  269. {
  270. _value = std::min(_value + _step, _max);
  271. }
  272. else
  273. {
  274. _value = std::min(_value + (_max - _min) * MOVE_FRACTION, _max);
  275. }
  276. _dirty = true;
  277. return true;
  278. }
  279. break;
  280. }
  281. return Control::keyEvent(evt, key);
  282. }
  283. void Slider::update(const Control* container, const Vector2& offset)
  284. {
  285. Label::update(container, offset);
  286. Control::State state = getState();
  287. _minImage = getImage("minCap", state);
  288. _maxImage = getImage("maxCap", state);
  289. _markerImage = getImage("marker", state);
  290. _trackImage = getImage("track", state);
  291. char s[32];
  292. sprintf(s, "%.*f", _valueTextPrecision, _value);
  293. _valueText = s;
  294. if (_delta != 0.0f)
  295. {
  296. float oldValue = _value;
  297. float total = _max - _min;
  298. if (_step > 0.0f)
  299. {
  300. _gamepadValue += (total * MOVE_FRACTION) * _delta;
  301. int numSteps = round(_gamepadValue / _step);
  302. _value = _step * numSteps;
  303. }
  304. else
  305. {
  306. _value += (total * MOVE_FRACTION) * _delta;
  307. }
  308. if (_value > _max)
  309. _value = _max;
  310. else if (_value < _min)
  311. _value = _min;
  312. if (_value != oldValue)
  313. {
  314. notifyListeners(Control::Listener::VALUE_CHANGED);
  315. }
  316. }
  317. if (_autoHeight == Control::AUTO_SIZE_FIT)
  318. {
  319. float height = _minImage->getRegion().height;
  320. height = std::max(height, _maxImage->getRegion().height);
  321. height = std::max(height, _markerImage->getRegion().height);
  322. height = std::max(height, _trackImage->getRegion().height);
  323. height += _bounds.height;
  324. if (_valueTextVisible && _font)
  325. height += getFontSize(state);
  326. setHeight(height);
  327. }
  328. }
  329. void Slider::draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needsClear, bool cleared, float targetHeight)
  330. {
  331. if (needsClear)
  332. {
  333. GL_ASSERT( glEnable(GL_SCISSOR_TEST) );
  334. GL_ASSERT( glScissor(_clearBounds.x, targetHeight - _clearBounds.y - _clearBounds.height, _clearBounds.width, _clearBounds.height) );
  335. Game::getInstance()->clear(Game::CLEAR_COLOR, Vector4::zero(), 1.0f, 0);
  336. GL_ASSERT( glDisable(GL_SCISSOR_TEST) );
  337. }
  338. if (!_visible)
  339. {
  340. _dirty = false;
  341. return;
  342. }
  343. spriteBatch->start();
  344. drawBorder(spriteBatch, clip);
  345. drawImages(spriteBatch, clip);
  346. spriteBatch->finish();
  347. drawText(clip);
  348. if (_delta == 0.0f)
  349. {
  350. _dirty = false;
  351. }
  352. }
  353. void Slider::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  354. {
  355. GP_ASSERT(spriteBatch);
  356. GP_ASSERT(_minImage);
  357. GP_ASSERT(_maxImage);
  358. GP_ASSERT(_markerImage);
  359. GP_ASSERT(_trackImage);
  360. // TODO: Vertical slider.
  361. // The slider is drawn in the center of the control (perpendicular to orientation).
  362. // The track is stretched according to orientation.
  363. // Caps and marker are not stretched.
  364. const Rectangle& minCapRegion = _minImage->getRegion();
  365. const Rectangle& maxCapRegion = _maxImage->getRegion();
  366. const Rectangle& markerRegion = _markerImage->getRegion();
  367. const Rectangle& trackRegion = _trackImage->getRegion();
  368. const Theme::UVs& minCap = _minImage->getUVs();
  369. const Theme::UVs& maxCap = _maxImage->getUVs();
  370. const Theme::UVs& marker = _markerImage->getUVs();
  371. const Theme::UVs& track = _trackImage->getUVs();
  372. Vector4 minCapColor = _minImage->getColor();
  373. Vector4 maxCapColor = _maxImage->getColor();
  374. Vector4 markerColor = _markerImage->getColor();
  375. Vector4 trackColor = _trackImage->getColor();
  376. minCapColor.w *= _opacity;
  377. maxCapColor.w *= _opacity;
  378. markerColor.w *= _opacity;
  379. trackColor.w *= _opacity;
  380. // Draw order: track, caps, marker.
  381. float midY = _viewportBounds.y + (_viewportBounds.height) * 0.5f;
  382. Vector2 pos(_viewportBounds.x, midY - trackRegion.height * 0.5f);
  383. spriteBatch->draw(pos.x, pos.y, _viewportBounds.width, trackRegion.height, track.u1, track.v1, track.u2, track.v2, trackColor, _viewportClipBounds);
  384. pos.y = midY - minCapRegion.height * 0.5f;
  385. pos.x -= minCapRegion.width * 0.5f;
  386. spriteBatch->draw(pos.x, pos.y, minCapRegion.width, minCapRegion.height, minCap.u1, minCap.v1, minCap.u2, minCap.v2, minCapColor, _viewportClipBounds);
  387. pos.x = _viewportBounds.x + _viewportBounds.width - maxCapRegion.width * 0.5f;
  388. spriteBatch->draw(pos.x, pos.y, maxCapRegion.width, maxCapRegion.height, maxCap.u1, maxCap.v1, maxCap.u2, maxCap.v2, maxCapColor, _viewportClipBounds);
  389. // Percent across.
  390. float markerPosition = (_value - _min) / (_max - _min);
  391. markerPosition *= _viewportBounds.width - minCapRegion.width * 0.5f - maxCapRegion.width * 0.5f - markerRegion.width;
  392. pos.x = _viewportBounds.x + minCapRegion.width * 0.5f + markerPosition;
  393. pos.y = midY - markerRegion.height / 2.0f;
  394. spriteBatch->draw(pos.x, pos.y, markerRegion.width, markerRegion.height, marker.u1, marker.v1, marker.u2, marker.v2, markerColor, _viewportClipBounds);
  395. }
  396. void Slider::drawText(const Rectangle& clip)
  397. {
  398. Label::drawText(clip);
  399. if (_valueTextVisible && _font)
  400. {
  401. Control::State state = getState();
  402. _font->start();
  403. _font->drawText(_valueText.c_str(), _textBounds, _textColor, getFontSize(state), _valueTextAlignment, true, getTextRightToLeft(state), &_viewportClipBounds);
  404. _font->finish();
  405. }
  406. }
  407. const char* Slider::getType() const
  408. {
  409. return "slider";
  410. }
  411. }