Slider.cpp 13 KB

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