Slider.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. void Slider::setMin(float min)
  52. {
  53. _min = min;
  54. }
  55. float Slider::getMin() const
  56. {
  57. return _min;
  58. }
  59. void Slider::setMax(float max)
  60. {
  61. _max = max;
  62. }
  63. float Slider::getMax() const
  64. {
  65. return _max;
  66. }
  67. void Slider::setStep(float step)
  68. {
  69. _step = step;
  70. }
  71. float Slider::getStep() const
  72. {
  73. return _step;
  74. }
  75. float Slider::getValue() const
  76. {
  77. return _value;
  78. }
  79. void Slider::setValue(float value)
  80. {
  81. value = MATH_CLAMP(value, _min, _max);
  82. if (value != _value)
  83. {
  84. _value = value;
  85. notifyListeners(Control::Listener::VALUE_CHANGED);
  86. }
  87. // Always update value text if it's visible
  88. if (_valueTextVisible)
  89. {
  90. char s[32];
  91. sprintf(s, "%.*f", _valueTextPrecision, _value);
  92. _valueText = s;
  93. }
  94. }
  95. void Slider::setValueTextVisible(bool valueTextVisible)
  96. {
  97. if (valueTextVisible != _valueTextVisible)
  98. {
  99. _valueTextVisible = valueTextVisible;
  100. if (_autoSize & AUTO_SIZE_HEIGHT)
  101. setDirty(DIRTY_BOUNDS);
  102. }
  103. }
  104. bool Slider::isValueTextVisible() const
  105. {
  106. return _valueTextVisible;
  107. }
  108. void Slider::setValueTextAlignment(Font::Justify alignment)
  109. {
  110. _valueTextAlignment = alignment;
  111. }
  112. Font::Justify Slider::getValueTextAlignment() const
  113. {
  114. return _valueTextAlignment;
  115. }
  116. void Slider::setValueTextPrecision(unsigned int precision)
  117. {
  118. _valueTextPrecision = precision;
  119. }
  120. unsigned int Slider::getValueTextPrecision() const
  121. {
  122. return _valueTextPrecision;
  123. }
  124. void Slider::addListener(Control::Listener* listener, int eventFlags)
  125. {
  126. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  127. {
  128. GP_ERROR("TEXT_CHANGED event is not applicable to Slider.");
  129. }
  130. Control::addListener(listener, eventFlags);
  131. }
  132. void Slider::updateValue(int x, int y)
  133. {
  134. State state = getState();
  135. // Horizontal case.
  136. const Theme::Border& border = getBorder(state);
  137. const Theme::Padding& padding = getPadding();
  138. const Rectangle& minCapRegion = _minImage->getRegion();
  139. const Rectangle& maxCapRegion = _maxImage->getRegion();
  140. const Rectangle& markerRegion = _markerImage->getRegion();
  141. float markerPosition = x / (_viewportBounds.width - markerRegion.width);
  142. if (markerPosition > 1.0f)
  143. {
  144. markerPosition = 1.0f;
  145. }
  146. else if (markerPosition < 0.0f)
  147. {
  148. markerPosition = 0.0f;
  149. }
  150. float value = (markerPosition * (_max - _min)) + _min;
  151. if (_step > 0.0f)
  152. {
  153. int numSteps = round(value / _step);
  154. value = _step * numSteps;
  155. }
  156. setValue(value);
  157. }
  158. bool Slider::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  159. {
  160. State state = getState();
  161. switch (evt)
  162. {
  163. case Touch::TOUCH_PRESS:
  164. updateValue(x, y);
  165. return true;
  166. case Touch::TOUCH_MOVE:
  167. if (state == ACTIVE)
  168. {
  169. updateValue(x, y);
  170. return true;
  171. }
  172. break;
  173. }
  174. return Control::touchEvent(evt, x, y, contactIndex);
  175. }
  176. static bool isScrollable(Container* container)
  177. {
  178. if (container->getScroll() != Container::SCROLL_NONE)
  179. return true;
  180. Container* parent = static_cast<Container*>(container->getParent());
  181. return parent ? isScrollable(parent) : false;
  182. }
  183. bool Slider::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  184. {
  185. switch (evt)
  186. {
  187. case Mouse::MOUSE_WHEEL:
  188. {
  189. if (hasFocus() && !isScrollable(_parent))
  190. {
  191. float total = _max - _min;
  192. float value = _value + (total * SCROLLWHEEL_FRACTION) * wheelDelta;
  193. if (_step > 0.0f)
  194. {
  195. int numSteps = round(value / _step);
  196. value = _step * numSteps;
  197. }
  198. setValue(value);
  199. return true;
  200. }
  201. break;
  202. }
  203. default:
  204. break;
  205. }
  206. // Return false to fall through to touch handling
  207. return false;
  208. }
  209. bool Slider::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  210. {
  211. switch (evt)
  212. {
  213. case Gamepad::JOYSTICK_EVENT:
  214. {
  215. // The right analog stick can be used to change a slider's value.
  216. if (analogIndex == 1)
  217. {
  218. Vector2 joy;
  219. gamepad->getJoystickValues(analogIndex, &joy);
  220. _gamepadValue = _value;
  221. _delta = joy.x;
  222. return true;
  223. }
  224. break;
  225. }
  226. }
  227. return Label::gamepadEvent(evt, gamepad, analogIndex);
  228. }
  229. bool Slider::keyEvent(Keyboard::KeyEvent evt, int key)
  230. {
  231. switch (evt)
  232. {
  233. case Keyboard::KEY_PRESS:
  234. switch (key)
  235. {
  236. case Keyboard::KEY_LEFT_ARROW:
  237. if (_step > 0.0f)
  238. {
  239. setValue(std::max(_value - _step, _min));
  240. }
  241. else
  242. {
  243. setValue(std::max(_value - (_max - _min) * MOVE_FRACTION, _min));
  244. }
  245. return true;
  246. case Keyboard::KEY_RIGHT_ARROW:
  247. if (_step > 0.0f)
  248. {
  249. setValue(std::min(_value + _step, _max));
  250. }
  251. else
  252. {
  253. setValue(std::min(_value + (_max - _min) * MOVE_FRACTION, _max));
  254. }
  255. return true;
  256. }
  257. break;
  258. }
  259. return Control::keyEvent(evt, key);
  260. }
  261. void Slider::update(float elapsedTime)
  262. {
  263. Label::update(elapsedTime);
  264. if (_delta != 0.0f)
  265. {
  266. float total = _max - _min;
  267. if (_step > 0.0f)
  268. {
  269. _gamepadValue += (total * MOVE_FRACTION) * _delta;
  270. int numSteps = round(_gamepadValue / _step);
  271. setValue(_step * numSteps);
  272. }
  273. else
  274. {
  275. setValue(_value + (total * MOVE_FRACTION) * _delta);
  276. }
  277. }
  278. }
  279. void Slider::updateBounds(const Vector2& offset)
  280. {
  281. Label::updateBounds(offset);
  282. Control::State state = getState();
  283. _minImage = getImage("minCap", state);
  284. _maxImage = getImage("maxCap", state);
  285. _markerImage = getImage("marker", state);
  286. _trackImage = getImage("track", state);
  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(state);
  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. const char* Slider::getType() const
  415. {
  416. return "slider";
  417. }
  418. }