WidgetSlider.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "WidgetSlider.h"
  29. #include "../../../Include/RmlUi/Core/Elements/ElementFormControl.h"
  30. #include "../../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../../Include/RmlUi/Core/Factory.h"
  32. #include "../../../Include/RmlUi/Core/Input.h"
  33. #include "../../../Include/RmlUi/Core/Profiling.h"
  34. #include "../Clock.h"
  35. namespace Rml {
  36. static const float DEFAULT_REPEAT_DELAY = 0.5f;
  37. static const float DEFAULT_REPEAT_PERIOD = 0.1f;
  38. WidgetSlider::WidgetSlider(ElementFormControl* _parent)
  39. {
  40. parent = _parent;
  41. orientation = HORIZONTAL;
  42. track = nullptr;
  43. bar = nullptr;
  44. arrows[0] = nullptr;
  45. arrows[1] = nullptr;
  46. bar_position = 0;
  47. bar_drag_anchor = 0;
  48. arrow_timers[0] = -1;
  49. arrow_timers[1] = -1;
  50. last_update_time = 0;
  51. value = 0;
  52. min_value = 0;
  53. max_value = 100;
  54. step = 1;
  55. }
  56. WidgetSlider::~WidgetSlider()
  57. {
  58. if (bar != nullptr)
  59. {
  60. parent->RemoveChild(bar);
  61. }
  62. if (track != nullptr)
  63. {
  64. parent->RemoveChild(track);
  65. }
  66. parent->RemoveEventListener(EventId::Blur, this);
  67. parent->RemoveEventListener(EventId::Focus, this);
  68. parent->RemoveEventListener(EventId::Keydown, this, true);
  69. parent->RemoveEventListener(EventId::Mousedown, this);
  70. parent->RemoveEventListener(EventId::Mouseup, this);
  71. parent->RemoveEventListener(EventId::Mouseout, this);
  72. parent->RemoveEventListener(EventId::Drag, this);
  73. parent->RemoveEventListener(EventId::Dragstart, this);
  74. parent->RemoveEventListener(EventId::Dragend, this);
  75. for (int i = 0; i < 2; i++)
  76. {
  77. if (arrows[i] != nullptr)
  78. {
  79. parent->RemoveChild(arrows[i]);
  80. }
  81. }
  82. }
  83. // Initialises the slider to a given orientation.
  84. bool WidgetSlider::Initialise()
  85. {
  86. Property drag_property = Property(Style::Drag::Drag);
  87. parent->SetProperty(PropertyId::Drag, drag_property);
  88. // Create all of our child elements as standard elements, and abort if we can't create them.
  89. ElementPtr track_element = Factory::InstanceElement(parent, "*", "slidertrack", XMLAttributes());
  90. ElementPtr bar_element = Factory::InstanceElement(parent, "*", "sliderbar", XMLAttributes());
  91. ElementPtr arrow0_element = Factory::InstanceElement(parent, "*", "sliderarrowdec", XMLAttributes());
  92. ElementPtr arrow1_element = Factory::InstanceElement(parent, "*", "sliderarrowinc", XMLAttributes());
  93. if (!track_element || !bar_element || !arrow0_element || !arrow1_element)
  94. {
  95. return false;
  96. }
  97. // Add them as non-DOM elements.
  98. track = parent->AppendChild(std::move(track_element), false);
  99. bar = parent->AppendChild(std::move(bar_element), false);
  100. arrows[0] = parent->AppendChild(std::move(arrow0_element), false);
  101. arrows[1] = parent->AppendChild(std::move(arrow1_element), false);
  102. arrows[0]->SetProperty(PropertyId::Drag, drag_property);
  103. arrows[1]->SetProperty(PropertyId::Drag, drag_property);
  104. // Attach the listeners
  105. // All listeners are attached to parent, ensuring that we don't get duplicate events when it bubbles from child to parent
  106. parent->AddEventListener(EventId::Blur, this);
  107. parent->AddEventListener(EventId::Focus, this);
  108. parent->AddEventListener(EventId::Keydown, this, true);
  109. parent->AddEventListener(EventId::Mousedown, this);
  110. parent->AddEventListener(EventId::Mouseup, this);
  111. parent->AddEventListener(EventId::Mouseout, this);
  112. parent->AddEventListener(EventId::Drag, this);
  113. parent->AddEventListener(EventId::Dragstart, this);
  114. parent->AddEventListener(EventId::Dragend, this);
  115. return true;
  116. }
  117. // Updates the key repeats for the increment / decrement arrows.
  118. void WidgetSlider::Update()
  119. {
  120. for (int i = 0; i < 2; i++)
  121. {
  122. bool updated_time = false;
  123. float delta_time = 0;
  124. if (arrow_timers[i] > 0)
  125. {
  126. if (!updated_time)
  127. {
  128. double current_time = Clock::GetElapsedTime();
  129. delta_time = float(current_time - last_update_time);
  130. last_update_time = current_time;
  131. }
  132. arrow_timers[i] -= delta_time;
  133. while (arrow_timers[i] <= 0)
  134. {
  135. arrow_timers[i] += DEFAULT_REPEAT_PERIOD;
  136. SetBarPosition(i == 0 ? OnLineDecrement() : OnLineIncrement());
  137. }
  138. }
  139. }
  140. }
  141. // Sets the position of the bar.
  142. void WidgetSlider::SetBarPosition(float _bar_position)
  143. {
  144. bar_position = Math::Clamp(_bar_position, 0.0f, 1.0f);
  145. PositionBar();
  146. }
  147. // Returns the current position of the bar.
  148. float WidgetSlider::GetBarPosition()
  149. {
  150. return bar_position;
  151. }
  152. // Sets the orientation of the slider.
  153. void WidgetSlider::SetOrientation(Orientation _orientation)
  154. {
  155. orientation = _orientation;
  156. }
  157. // Returns the slider's orientation.
  158. WidgetSlider::Orientation WidgetSlider::GetOrientation() const
  159. {
  160. return orientation;
  161. }
  162. // Sets the dimensions to the size of the slider.
  163. void WidgetSlider::GetDimensions(Vector2f& dimensions) const
  164. {
  165. switch (orientation)
  166. {
  167. case VERTICAL: dimensions.x = 16; dimensions.y = 256; break;
  168. case HORIZONTAL: dimensions.x = 256; dimensions.y = 16; break;
  169. }
  170. }
  171. void WidgetSlider::SetValue(float target_value)
  172. {
  173. float num_steps = (target_value - min_value) / step;
  174. float new_value = min_value + Math::RoundFloat(num_steps) * step;
  175. if (new_value != value)
  176. SetBarPosition(SetValueInternal(new_value));
  177. }
  178. float WidgetSlider::GetValue() const
  179. {
  180. return value;
  181. }
  182. // Sets the minimum value of the slider.
  183. void WidgetSlider::SetMinValue(float _min_value)
  184. {
  185. min_value = _min_value;
  186. }
  187. // Sets the maximum value of the slider.
  188. void WidgetSlider::SetMaxValue(float _max_value)
  189. {
  190. max_value = _max_value;
  191. }
  192. // Sets the slider's value increment.
  193. void WidgetSlider::SetStep(float _step)
  194. {
  195. // Can't have a zero step!
  196. if (_step == 0)
  197. return;
  198. step = _step;
  199. }
  200. // Formats the slider's elements.
  201. void WidgetSlider::FormatElements()
  202. {
  203. RMLUI_ZoneScopedNC("RangeOnResize", 0x228044);
  204. Vector2f box = GetParent()->GetBox().GetSize();
  205. WidgetSlider::FormatElements(box, GetOrientation() == VERTICAL ? box.y : box.x);
  206. }
  207. // Lays out and resizes the internal elements.
  208. void WidgetSlider::FormatElements(const Vector2f& containing_block, float slider_length, float bar_length)
  209. {
  210. int length_axis = orientation == VERTICAL ? 1 : 0;
  211. // Build the box for the containing slider element. As the containing block is not guaranteed to have a defined
  212. // height, we must use the width for both axes.
  213. Box parent_box;
  214. ElementUtilities::BuildBox(parent_box, Vector2f(containing_block.x, containing_block.x), parent);
  215. // Set the length of the slider.
  216. Vector2f content = parent_box.GetSize();
  217. content[length_axis] = slider_length;
  218. parent_box.SetContent(content);
  219. // Generate the initial dimensions for the track. It'll need to be cut down to fit the arrows.
  220. Box track_box;
  221. ElementUtilities::BuildBox(track_box, parent_box.GetSize(), track);
  222. content = track_box.GetSize();
  223. content[length_axis] = slider_length -= orientation == VERTICAL ? (track_box.GetCumulativeEdge(Box::CONTENT, Box::TOP) + track_box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM)) :
  224. (track_box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) + track_box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT));
  225. // If no height has been explicitly specified for the track, it'll be initialised to -1 as per normal block
  226. // elements. We'll fix that up here.
  227. if (orientation == HORIZONTAL &&
  228. content.y < 0)
  229. content.y = parent_box.GetSize().y;
  230. // Now we size the arrows.
  231. for (int i = 0; i < 2; i++)
  232. {
  233. Box arrow_box;
  234. ElementUtilities::BuildBox(arrow_box, parent_box.GetSize(), arrows[i]);
  235. // Clamp the size to (0, 0).
  236. Vector2f arrow_size = arrow_box.GetSize();
  237. if (arrow_size.x < 0 ||
  238. arrow_size.y < 0)
  239. arrow_box.SetContent(Vector2f(0, 0));
  240. arrows[i]->SetBox(arrow_box);
  241. // Shrink the track length by the arrow size.
  242. content[length_axis] -= arrow_box.GetSize(Box::MARGIN)[length_axis];
  243. }
  244. // Now the track has been sized, we can fix everything into position.
  245. track_box.SetContent(content);
  246. track->SetBox(track_box);
  247. if (orientation == VERTICAL)
  248. {
  249. Vector2f offset(arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::LEFT), arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::TOP));
  250. arrows[0]->SetOffset(offset, parent);
  251. offset.x = track->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  252. offset.y += arrows[0]->GetBox().GetSize(Box::BORDER).y + arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM) + track->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  253. track->SetOffset(offset, parent);
  254. offset.x = arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  255. offset.y += track->GetBox().GetSize(Box::BORDER).y + track->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM) + arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  256. arrows[1]->SetOffset(offset, parent);
  257. }
  258. else
  259. {
  260. Vector2f offset(arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::LEFT), arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::TOP));
  261. arrows[0]->SetOffset(offset, parent);
  262. offset.x += arrows[0]->GetBox().GetSize(Box::BORDER).x + arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::RIGHT) + track->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  263. offset.y = track->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  264. track->SetOffset(offset, parent);
  265. offset.x += track->GetBox().GetSize(Box::BORDER).x + track->GetBox().GetEdge(Box::MARGIN, Box::RIGHT) + arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  266. offset.y = arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  267. arrows[1]->SetOffset(offset, parent);
  268. }
  269. FormatBar(bar_length);
  270. if (parent->IsDisabled())
  271. {
  272. // Propagate disabled state to child elements
  273. bar->SetPseudoClass("disabled", true);
  274. track->SetPseudoClass("disabled", true);
  275. arrows[0]->SetPseudoClass("disabled", true);
  276. arrows[1]->SetPseudoClass("disabled", true);
  277. }
  278. }
  279. // Lays out and positions the bar element.
  280. void WidgetSlider::FormatBar(float bar_length)
  281. {
  282. Box bar_box;
  283. ElementUtilities::BuildBox(bar_box, parent->GetBox().GetSize(), bar);
  284. auto& computed = bar->GetComputedValues();
  285. Vector2f bar_box_content = bar_box.GetSize();
  286. if (orientation == HORIZONTAL)
  287. {
  288. if (computed.height.type == Style::Height::Auto)
  289. bar_box_content.y = parent->GetBox().GetSize().y;
  290. }
  291. if (bar_length >= 0)
  292. {
  293. Vector2f track_size = track->GetBox().GetSize();
  294. if (orientation == VERTICAL)
  295. {
  296. float track_length = track_size.y - (bar_box.GetCumulativeEdge(Box::CONTENT, Box::TOP) + bar_box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM));
  297. if (computed.height.type == Style::Height::Auto)
  298. {
  299. bar_box_content.y = track_length * bar_length;
  300. // Check for 'min-height' restrictions.
  301. float min_track_length = ResolveValue(computed.min_height, track_length);
  302. bar_box_content.y = Math::Max(min_track_length, bar_box_content.y);
  303. // Check for 'max-height' restrictions.
  304. float max_track_length = ResolveValue(computed.max_height, track_length);
  305. if (max_track_length > 0)
  306. bar_box_content.y = Math::Min(max_track_length, bar_box_content.y);
  307. }
  308. // Make sure we haven't gone further than we're allowed to (min-height may have made us too big).
  309. bar_box_content.y = Math::Min(bar_box_content.y, track_length);
  310. }
  311. else
  312. {
  313. float track_length = track_size.x - (bar_box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) + bar_box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT));
  314. if (computed.width.type == Style::Width::Auto)
  315. {
  316. bar_box_content.x = track_length * bar_length;
  317. // Check for 'min-width' restrictions.
  318. float min_track_length = ResolveValue(computed.min_width, track_length);
  319. bar_box_content.x = Math::Max(min_track_length, bar_box_content.x);
  320. // Check for 'max-width' restrictions.
  321. float max_track_length = ResolveValue(computed.max_width, track_length);
  322. if (max_track_length > 0)
  323. bar_box_content.x = Math::Min(max_track_length, bar_box_content.x);
  324. }
  325. // Make sure we haven't gone further than we're allowed to (min-width may have made us too big).
  326. bar_box_content.x = Math::Min(bar_box_content.x, track_length);
  327. }
  328. }
  329. // Set the new dimensions on the bar to re-decorate it.
  330. bar_box.SetContent(bar_box_content);
  331. bar->SetBox(bar_box);
  332. // Now that it's been resized, re-position it.
  333. PositionBar();
  334. }
  335. // Returns the widget's parent element.
  336. Element* WidgetSlider::GetParent() const
  337. {
  338. return parent;
  339. }
  340. // Handles events coming through from the slider's components.
  341. void WidgetSlider::ProcessEvent(Event& event)
  342. {
  343. if (parent->IsDisabled())
  344. return;
  345. switch (event.GetId())
  346. {
  347. case EventId::Mousedown:
  348. {
  349. if (event.GetTargetElement() == parent || event.GetTargetElement() == track)
  350. {
  351. float mouse_position, bar_halfsize;
  352. if (orientation == HORIZONTAL)
  353. {
  354. mouse_position = event.GetParameter< float >("mouse_x", 0);
  355. bar_halfsize = 0.5f * bar->GetBox().GetSize(Box::BORDER).x;
  356. }
  357. else
  358. {
  359. mouse_position = event.GetParameter< float >("mouse_y", 0);
  360. bar_halfsize = 0.5f * bar->GetBox().GetSize(Box::BORDER).y;
  361. }
  362. float new_bar_position = AbsolutePositionToBarPosition(mouse_position - bar_halfsize);
  363. SetBarPosition(OnBarChange(new_bar_position));
  364. }
  365. else if (event.GetTargetElement() == arrows[0])
  366. {
  367. arrow_timers[0] = DEFAULT_REPEAT_DELAY;
  368. last_update_time = Clock::GetElapsedTime();
  369. SetBarPosition(OnLineDecrement());
  370. }
  371. else if (event.GetTargetElement() == arrows[1])
  372. {
  373. arrow_timers[1] = DEFAULT_REPEAT_DELAY;
  374. last_update_time = Clock::GetElapsedTime();
  375. SetBarPosition(OnLineIncrement());
  376. }
  377. }
  378. break;
  379. case EventId::Mouseup:
  380. case EventId::Mouseout:
  381. {
  382. if (event.GetTargetElement() == arrows[0])
  383. arrow_timers[0] = -1;
  384. else if (event.GetTargetElement() == arrows[1])
  385. arrow_timers[1] = -1;
  386. }
  387. break;
  388. case EventId::Dragstart:
  389. {
  390. if (event.GetTargetElement() == parent)
  391. {
  392. bar->SetPseudoClass("active", true);
  393. if (orientation == HORIZONTAL)
  394. bar_drag_anchor = event.GetParameter< float >("mouse_x", 0) - bar->GetAbsoluteOffset().x;
  395. else
  396. bar_drag_anchor = event.GetParameter< float >("mouse_y", 0) - bar->GetAbsoluteOffset().y;
  397. }
  398. }
  399. break;
  400. case EventId::Drag:
  401. {
  402. if (event.GetTargetElement() == parent)
  403. {
  404. float new_bar_offset = event.GetParameter< float >((orientation == HORIZONTAL ? "mouse_x" : "mouse_y"), 0) - bar_drag_anchor;
  405. float new_bar_position = AbsolutePositionToBarPosition(new_bar_offset);
  406. SetBarPosition(OnBarChange(new_bar_position));
  407. }
  408. }
  409. break;
  410. case EventId::Dragend:
  411. {
  412. if (event.GetTargetElement() == parent)
  413. {
  414. bar->SetPseudoClass("active", false);
  415. }
  416. }
  417. break;
  418. case EventId::Keydown:
  419. {
  420. Input::KeyIdentifier key_identifier = (Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
  421. switch (key_identifier)
  422. {
  423. case Input::KI_LEFT:
  424. if (orientation == HORIZONTAL) SetBarPosition(OnLineDecrement());
  425. break;
  426. case Input::KI_UP:
  427. if (orientation == VERTICAL) SetBarPosition(OnLineDecrement());
  428. break;
  429. case Input::KI_RIGHT:
  430. if (orientation == HORIZONTAL) SetBarPosition(OnLineIncrement());
  431. break;
  432. case Input::KI_DOWN:
  433. if (orientation == VERTICAL) SetBarPosition(OnLineIncrement());
  434. break;
  435. default:
  436. break;
  437. }
  438. }
  439. break;
  440. case EventId::Focus:
  441. {
  442. if (event.GetTargetElement() == parent)
  443. bar->SetPseudoClass("focus", true);
  444. }
  445. break;
  446. case EventId::Blur:
  447. {
  448. if (event.GetTargetElement() == parent)
  449. bar->SetPseudoClass("focus", false);
  450. }
  451. break;
  452. default:
  453. break;
  454. }
  455. }
  456. // Called when the slider's bar position is set or dragged.
  457. float WidgetSlider::OnBarChange(float bar_position)
  458. {
  459. float new_value = min_value + bar_position * (max_value - min_value);
  460. int num_steps = Math::RoundToInteger((new_value - value) / step);
  461. return SetValueInternal(value + num_steps * step);
  462. }
  463. // Called when the slider is incremented by one 'line'.
  464. float WidgetSlider::OnLineIncrement()
  465. {
  466. return SetValueInternal(value + step);
  467. }
  468. // Called when the slider is decremented by one 'line'.
  469. float WidgetSlider::OnLineDecrement()
  470. {
  471. return SetValueInternal(value - step);
  472. }
  473. float WidgetSlider::AbsolutePositionToBarPosition(float absolute_position) const
  474. {
  475. float new_bar_position = bar_position;
  476. if (orientation == HORIZONTAL)
  477. {
  478. const float edge_left = bar->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  479. const float edge_right = bar->GetBox().GetEdge(Box::MARGIN, Box::RIGHT);
  480. float traversable_track_length = track->GetBox().GetSize(Box::CONTENT).x - bar->GetBox().GetSize(Box::BORDER).x - edge_left - edge_right;
  481. if (traversable_track_length > 0)
  482. {
  483. float traversable_track_origin = track->GetAbsoluteOffset().x + edge_left;
  484. new_bar_position = (absolute_position - traversable_track_origin) / traversable_track_length;
  485. new_bar_position = Math::Clamp(new_bar_position, 0.0f, 1.0f);
  486. }
  487. }
  488. else
  489. {
  490. const float edge_top = bar->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  491. const float edge_bottom = bar->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM);
  492. float traversable_track_length = track->GetBox().GetSize(Box::CONTENT).y - bar->GetBox().GetSize(Box::BORDER).y - edge_top - edge_bottom;
  493. if (traversable_track_length > 0)
  494. {
  495. float traversable_track_origin = track->GetAbsoluteOffset().y + edge_top;
  496. new_bar_position = (absolute_position - traversable_track_origin) / traversable_track_length;
  497. new_bar_position = Math::Clamp(new_bar_position, 0.0f, 1.0f);
  498. }
  499. }
  500. return new_bar_position;
  501. }
  502. void WidgetSlider::PositionBar()
  503. {
  504. const Vector2f track_dimensions = track->GetBox().GetSize();
  505. const Vector2f bar_dimensions = bar->GetBox().GetSize(Box::BORDER);
  506. if (orientation == VERTICAL)
  507. {
  508. const float edge_top = bar->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  509. const float edge_bottom = bar->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM);
  510. float traversable_track_length = track_dimensions.y - bar_dimensions.y - edge_top - edge_bottom;
  511. bar->SetOffset(
  512. Vector2f(
  513. bar->GetBox().GetEdge(Box::MARGIN, Box::LEFT),
  514. track->GetRelativeOffset().y + edge_top + traversable_track_length * bar_position
  515. ),
  516. parent
  517. );
  518. }
  519. else
  520. {
  521. const float edge_left = bar->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  522. const float edge_right = bar->GetBox().GetEdge(Box::MARGIN, Box::RIGHT);
  523. float traversable_track_length = track_dimensions.x - bar_dimensions.x - edge_left - edge_right;
  524. bar->SetOffset(
  525. Vector2f(
  526. track->GetRelativeOffset().x + edge_left + traversable_track_length * bar_position,
  527. bar->GetBox().GetEdge(Box::MARGIN, Box::TOP)
  528. ),
  529. parent
  530. );
  531. }
  532. }
  533. // Clamps the new value, sets it on the slider.
  534. float WidgetSlider::SetValueInternal(float new_value)
  535. {
  536. if (min_value < max_value)
  537. {
  538. value = Math::Clamp(new_value, min_value, max_value);
  539. }
  540. else if (min_value > max_value)
  541. {
  542. value = Math::Clamp(new_value, max_value, min_value);
  543. }
  544. else
  545. {
  546. value = min_value;
  547. return 0;
  548. }
  549. Dictionary parameters;
  550. parameters["value"] = value;
  551. GetParent()->DispatchEvent(EventId::Change, parameters);
  552. // TODO: This might not be the safest approach as this will call SetValue(),
  553. // thus, a slight mismatch will result in infinite recursion.
  554. GetParent()->SetAttribute("value", value);
  555. return (value - min_value) / (max_value - min_value);
  556. }
  557. } // namespace Rml