Slider.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. Slider::Slider() : _min(0.0f), _max(0.0f), _step(0.0f), _value(0.0f), _delta(0.0f), _minImage(NULL),
  10. _maxImage(NULL), _trackImage(NULL), _markerImage(NULL), _valueTextVisible(false),
  11. _valueTextAlignment(Font::ALIGN_BOTTOM_HCENTER), _valueTextPrecision(0), _valueText(""), _gamepadValue(0.0f)
  12. {
  13. _canFocus = true;
  14. }
  15. Slider::~Slider()
  16. {
  17. }
  18. Slider* Slider::create(const char* id, Theme::Style* style)
  19. {
  20. Slider* slider = new Slider();
  21. slider->_id = id ? id : "";
  22. slider->initialize("Slider", style, NULL);
  23. return slider;
  24. }
  25. Control* Slider::create(Theme::Style* style, Properties* properties)
  26. {
  27. Slider* slider = new Slider();
  28. slider->initialize("Slider", style, properties);
  29. return slider;
  30. }
  31. void Slider::initialize(const char* typeName, Theme::Style* style, Properties* properties)
  32. {
  33. Label::initialize(typeName, style, properties);
  34. if (properties)
  35. {
  36. _min = properties->getFloat("min");
  37. _max = properties->getFloat("max");
  38. _value = properties->getFloat("value");
  39. _step = properties->getFloat("step");
  40. _valueTextVisible = properties->getBool("valueTextVisible");
  41. _valueTextPrecision = properties->getInt("valueTextPrecision");
  42. if (properties->exists("valueTextAlignment"))
  43. {
  44. _valueTextAlignment = Font::getJustify(properties->getString("valueTextAlignment"));
  45. }
  46. }
  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. static bool isScrollable(Container* container)
  193. {
  194. if (container->getScroll() != Container::SCROLL_NONE)
  195. return true;
  196. Container* parent = static_cast<Container*>(container->getParent());
  197. return parent ? isScrollable(parent) : false;
  198. }
  199. bool Slider::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  200. {
  201. switch (evt)
  202. {
  203. case Mouse::MOUSE_WHEEL:
  204. {
  205. if (hasFocus() && !isScrollable(_parent))
  206. {
  207. float total = _max - _min;
  208. float oldValue = _value;
  209. _value += (total * SCROLLWHEEL_FRACTION) * wheelDelta;
  210. if (_value > _max)
  211. _value = _max;
  212. else if (_value < _min)
  213. _value = _min;
  214. if (_step > 0.0f)
  215. {
  216. int numSteps = round(_value / _step);
  217. _value = _step * numSteps;
  218. }
  219. if (_value != oldValue)
  220. {
  221. notifyListeners(Control::Listener::VALUE_CHANGED);
  222. }
  223. _dirty = true;
  224. return true;
  225. }
  226. break;
  227. }
  228. default:
  229. break;
  230. }
  231. // Return false to fall through to touch handling
  232. return false;
  233. }
  234. bool Slider::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  235. {
  236. bool eventConsumed = false;
  237. switch (evt)
  238. {
  239. case Gamepad::JOYSTICK_EVENT:
  240. {
  241. // The right analog stick can be used to change a slider's value.
  242. if (analogIndex == 1)
  243. {
  244. Vector2 joy;
  245. gamepad->getJoystickValues(analogIndex, &joy);
  246. _gamepadValue = _value;
  247. _delta = joy.x;
  248. _dirty = true;
  249. return true;
  250. }
  251. break;
  252. }
  253. }
  254. return Label::gamepadEvent(evt, gamepad, analogIndex);
  255. }
  256. bool Slider::keyEvent(Keyboard::KeyEvent evt, int key)
  257. {
  258. switch (evt)
  259. {
  260. case Keyboard::KEY_PRESS:
  261. switch (key)
  262. {
  263. case Keyboard::KEY_LEFT_ARROW:
  264. if (_step > 0.0f)
  265. {
  266. _value = std::max(_value - _step, _min);
  267. }
  268. else
  269. {
  270. _value = std::max(_value - (_max - _min) * MOVE_FRACTION, _min);
  271. }
  272. _dirty = true;
  273. return true;
  274. case Keyboard::KEY_RIGHT_ARROW:
  275. if (_step > 0.0f)
  276. {
  277. _value = std::min(_value + _step, _max);
  278. }
  279. else
  280. {
  281. _value = std::min(_value + (_max - _min) * MOVE_FRACTION, _max);
  282. }
  283. _dirty = true;
  284. return true;
  285. }
  286. break;
  287. }
  288. return Control::keyEvent(evt, key);
  289. }
  290. void Slider::update(const Control* container, const Vector2& offset)
  291. {
  292. Label::update(container, offset);
  293. Control::State state = getState();
  294. _minImage = getImage("minCap", state);
  295. _maxImage = getImage("maxCap", state);
  296. _markerImage = getImage("marker", state);
  297. _trackImage = getImage("track", state);
  298. char s[32];
  299. sprintf(s, "%.*f", _valueTextPrecision, _value);
  300. _valueText = s;
  301. if (_delta != 0.0f)
  302. {
  303. float oldValue = _value;
  304. float total = _max - _min;
  305. if (_step > 0.0f)
  306. {
  307. _gamepadValue += (total * MOVE_FRACTION) * _delta;
  308. int numSteps = round(_gamepadValue / _step);
  309. _value = _step * numSteps;
  310. }
  311. else
  312. {
  313. _value += (total * MOVE_FRACTION) * _delta;
  314. }
  315. if (_value > _max)
  316. _value = _max;
  317. else if (_value < _min)
  318. _value = _min;
  319. if (_value != oldValue)
  320. {
  321. notifyListeners(Control::Listener::VALUE_CHANGED);
  322. }
  323. }
  324. if (_autoHeight == Control::AUTO_SIZE_FIT)
  325. {
  326. float height = _minImage->getRegion().height;
  327. height = std::max(height, _maxImage->getRegion().height);
  328. height = std::max(height, _markerImage->getRegion().height);
  329. height = std::max(height, _trackImage->getRegion().height);
  330. height += _bounds.height;
  331. if (_valueTextVisible && _font)
  332. height += getFontSize(state);
  333. setHeight(height);
  334. }
  335. }
  336. unsigned int Slider::drawImages(Form* form, const Rectangle& clip)
  337. {
  338. if (!(_minImage && _maxImage && _markerImage && _trackImage))
  339. return 0;
  340. // TODO: Vertical slider.
  341. // The slider is drawn in the center of the control (perpendicular to orientation).
  342. // The track is stretched according to orientation.
  343. // Caps and marker are not stretched.
  344. const Rectangle& minCapRegion = _minImage->getRegion();
  345. const Rectangle& maxCapRegion = _maxImage->getRegion();
  346. const Rectangle& markerRegion = _markerImage->getRegion();
  347. const Rectangle& trackRegion = _trackImage->getRegion();
  348. const Theme::UVs& minCap = _minImage->getUVs();
  349. const Theme::UVs& maxCap = _maxImage->getUVs();
  350. const Theme::UVs& marker = _markerImage->getUVs();
  351. const Theme::UVs& track = _trackImage->getUVs();
  352. Vector4 minCapColor = _minImage->getColor();
  353. Vector4 maxCapColor = _maxImage->getColor();
  354. Vector4 markerColor = _markerImage->getColor();
  355. Vector4 trackColor = _trackImage->getColor();
  356. minCapColor.w *= _opacity;
  357. maxCapColor.w *= _opacity;
  358. markerColor.w *= _opacity;
  359. trackColor.w *= _opacity;
  360. SpriteBatch* batch = _style->getTheme()->getSpriteBatch();
  361. startBatch(form, batch);
  362. // Draw order: track, caps, marker.
  363. float midY = _viewportBounds.y + (_viewportBounds.height) * 0.5f;
  364. Vector2 pos(_viewportBounds.x, midY - trackRegion.height * 0.5f);
  365. batch->draw(pos.x, pos.y, _viewportBounds.width, trackRegion.height, track.u1, track.v1, track.u2, track.v2, trackColor, _viewportClipBounds);
  366. pos.y = midY - minCapRegion.height * 0.5f;
  367. pos.x -= minCapRegion.width * 0.5f;
  368. batch->draw(pos.x, pos.y, minCapRegion.width, minCapRegion.height, minCap.u1, minCap.v1, minCap.u2, minCap.v2, minCapColor, _viewportClipBounds);
  369. pos.x = _viewportBounds.x + _viewportBounds.width - maxCapRegion.width * 0.5f;
  370. batch->draw(pos.x, pos.y, maxCapRegion.width, maxCapRegion.height, maxCap.u1, maxCap.v1, maxCap.u2, maxCap.v2, maxCapColor, _viewportClipBounds);
  371. // Percent across.
  372. float markerPosition = (_value - _min) / (_max - _min);
  373. markerPosition *= _viewportBounds.width - minCapRegion.width * 0.5f - maxCapRegion.width * 0.5f - markerRegion.width;
  374. pos.x = _viewportBounds.x + minCapRegion.width * 0.5f + markerPosition;
  375. pos.y = midY - markerRegion.height / 2.0f;
  376. batch->draw(pos.x, pos.y, markerRegion.width, markerRegion.height, marker.u1, marker.v1, marker.u2, marker.v2, markerColor, _viewportClipBounds);
  377. finishBatch(form, batch);
  378. return 4;
  379. }
  380. unsigned int Slider::drawText(Form* form, const Rectangle& clip)
  381. {
  382. unsigned int drawCalls = Label::drawText(form, clip);
  383. if (_valueTextVisible && _font)
  384. {
  385. Control::State state = getState();
  386. SpriteBatch* batch = _font->getSpriteBatch();
  387. startBatch(form, batch);
  388. _font->drawText(_valueText.c_str(), _textBounds, _textColor, getFontSize(state), _valueTextAlignment, true, getTextRightToLeft(state), &_viewportClipBounds);
  389. finishBatch(form, batch);
  390. ++drawCalls;
  391. }
  392. return drawCalls;
  393. }
  394. const char* Slider::getType() const
  395. {
  396. return "slider";
  397. }
  398. }