WidgetSlider.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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-2023 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/ComputedValues.h"
  30. #include "../../../Include/RmlUi/Core/Context.h"
  31. #include "../../../Include/RmlUi/Core/ElementUtilities.h"
  32. #include "../../../Include/RmlUi/Core/Elements/ElementFormControl.h"
  33. #include "../../../Include/RmlUi/Core/Factory.h"
  34. #include "../../../Include/RmlUi/Core/Input.h"
  35. #include "../../../Include/RmlUi/Core/Profiling.h"
  36. #include "../Clock.h"
  37. namespace Rml {
  38. static const float DEFAULT_REPEAT_DELAY = 0.5f;
  39. static const float DEFAULT_REPEAT_PERIOD = 0.1f;
  40. WidgetSlider::WidgetSlider(ElementFormControl* _parent)
  41. {
  42. parent = _parent;
  43. orientation = HORIZONTAL;
  44. track = nullptr;
  45. bar = nullptr;
  46. progress = nullptr;
  47. arrows[0] = nullptr;
  48. arrows[1] = nullptr;
  49. bar_position = 0;
  50. bar_drag_anchor = 0;
  51. arrow_timers[0] = -1;
  52. arrow_timers[1] = -1;
  53. last_update_time = 0;
  54. value = 0;
  55. min_value = 0;
  56. max_value = 100;
  57. step = 1;
  58. }
  59. WidgetSlider::~WidgetSlider()
  60. {
  61. if (bar)
  62. parent->RemoveChild(bar);
  63. if (track && progress)
  64. track->RemoveChild(progress);
  65. if (track)
  66. parent->RemoveChild(track);
  67. parent->RemoveEventListener(EventId::Blur, this);
  68. parent->RemoveEventListener(EventId::Focus, this);
  69. parent->RemoveEventListener(EventId::Keydown, this, true);
  70. parent->RemoveEventListener(EventId::Mousedown, this);
  71. parent->RemoveEventListener(EventId::Mouseup, this);
  72. parent->RemoveEventListener(EventId::Mouseout, this);
  73. parent->RemoveEventListener(EventId::Drag, this);
  74. parent->RemoveEventListener(EventId::Dragstart, this);
  75. parent->RemoveEventListener(EventId::Dragend, this);
  76. for (int i = 0; i < 2; i++)
  77. {
  78. if (arrows[i])
  79. parent->RemoveChild(arrows[i]);
  80. }
  81. }
  82. bool WidgetSlider::Initialise()
  83. {
  84. track = As<Element*>(parent->AppendChild(Factory::InstanceNode("*", "slidertrack"), false));
  85. progress = As<Element*>(track->AppendChild(Factory::InstanceNode("*", "sliderprogress"), false));
  86. bar = As<Element*>(parent->AppendChild(Factory::InstanceNode("*", "sliderbar"), false));
  87. arrows[0] = As<Element*>(parent->AppendChild(Factory::InstanceNode("*", "sliderarrowdec"), false));
  88. arrows[1] = As<Element*>(parent->AppendChild(Factory::InstanceNode("*", "sliderarrowinc"), false));
  89. const Property drag_property = Property(Style::Drag::Drag);
  90. track->SetProperty(PropertyId::Drag, drag_property);
  91. progress->SetProperty(PropertyId::Drag, drag_property);
  92. bar->SetProperty(PropertyId::Drag, drag_property);
  93. // Attach the listeners
  94. // All listeners are attached to parent, ensuring that we don't get duplicate events when it bubbles from child to parent
  95. parent->AddEventListener(EventId::Blur, this);
  96. parent->AddEventListener(EventId::Focus, this);
  97. parent->AddEventListener(EventId::Keydown, this, true);
  98. parent->AddEventListener(EventId::Mousedown, this);
  99. parent->AddEventListener(EventId::Mouseup, this);
  100. parent->AddEventListener(EventId::Mouseout, this);
  101. parent->AddEventListener(EventId::Drag, this);
  102. parent->AddEventListener(EventId::Dragstart, this);
  103. parent->AddEventListener(EventId::Dragend, this);
  104. return true;
  105. }
  106. void WidgetSlider::Update()
  107. {
  108. if (!std::any_of(std::begin(arrow_timers), std::end(arrow_timers), [](float timer) { return timer > 0; }))
  109. return;
  110. const double current_time = Clock::GetElapsedTime();
  111. const float delta_time = float(current_time - last_update_time);
  112. last_update_time = current_time;
  113. for (int i = 0; i < 2; i++)
  114. {
  115. if (arrow_timers[i] > 0)
  116. {
  117. arrow_timers[i] -= delta_time;
  118. while (arrow_timers[i] <= 0)
  119. {
  120. arrow_timers[i] += DEFAULT_REPEAT_PERIOD;
  121. SetBarPosition(i == 0 ? OnLineDecrement() : OnLineIncrement());
  122. }
  123. if (Context* ctx = parent->GetContext())
  124. ctx->RequestNextUpdate(arrow_timers[i]);
  125. }
  126. }
  127. }
  128. void WidgetSlider::SetBarPosition(float _bar_position)
  129. {
  130. bar_position = Math::Clamp(_bar_position, 0.0f, 1.0f);
  131. PositionBar();
  132. ResizeProgress();
  133. }
  134. float WidgetSlider::GetBarPosition()
  135. {
  136. return bar_position;
  137. }
  138. void WidgetSlider::SetOrientation(Orientation _orientation)
  139. {
  140. orientation = _orientation;
  141. }
  142. WidgetSlider::Orientation WidgetSlider::GetOrientation() const
  143. {
  144. return orientation;
  145. }
  146. void WidgetSlider::GetDimensions(Vector2f& dimensions) const
  147. {
  148. switch (orientation)
  149. {
  150. case VERTICAL:
  151. dimensions.x = 16;
  152. dimensions.y = 256;
  153. break;
  154. case HORIZONTAL:
  155. dimensions.x = 256;
  156. dimensions.y = 16;
  157. break;
  158. }
  159. }
  160. void WidgetSlider::SetValue(float target_value)
  161. {
  162. float num_steps = (target_value - min_value) / step;
  163. float new_value = min_value + Math::Round(num_steps) * step;
  164. if (new_value != value)
  165. SetBarPosition(SetValueInternal(new_value));
  166. }
  167. float WidgetSlider::GetValue() const
  168. {
  169. return value;
  170. }
  171. void WidgetSlider::SetMinValue(float _min_value)
  172. {
  173. if (_min_value != min_value)
  174. {
  175. min_value = _min_value;
  176. SetBarPosition(SetValueInternal(value, false));
  177. }
  178. }
  179. void WidgetSlider::SetMaxValue(float _max_value)
  180. {
  181. if (_max_value != max_value)
  182. {
  183. max_value = _max_value;
  184. SetBarPosition(SetValueInternal(value, false));
  185. }
  186. }
  187. void WidgetSlider::SetStep(float _step)
  188. {
  189. // Can't have a zero step!
  190. if (_step == 0)
  191. return;
  192. step = _step;
  193. }
  194. void WidgetSlider::FormatElements()
  195. {
  196. RMLUI_ZoneScopedNC("RangeOnResize", 0x228044);
  197. Vector2f box = GetParent()->GetBox().GetSize();
  198. WidgetSlider::FormatElements(box, GetOrientation() == VERTICAL ? box.y : box.x);
  199. }
  200. void WidgetSlider::FormatElements(const Vector2f containing_block, float slider_length)
  201. {
  202. int length_axis = orientation == VERTICAL ? 1 : 0;
  203. // Build the box for the containing slider element.
  204. Box parent_box;
  205. ElementUtilities::BuildBox(parent_box, containing_block, parent);
  206. // Set the length of the slider.
  207. Vector2f content = parent_box.GetSize();
  208. content[length_axis] = slider_length;
  209. parent_box.SetContent(content);
  210. // Generate the initial dimensions for the track. It'll need to be cut down to fit the arrows.
  211. Box track_box;
  212. ElementUtilities::BuildBox(track_box, parent_box.GetSize(), track);
  213. content = track_box.GetSize();
  214. content[length_axis] = slider_length -= orientation == VERTICAL
  215. ? (track_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Top) + track_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Bottom))
  216. : (track_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Left) + track_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Right));
  217. // If no height has been explicitly specified for the track, it'll be initialised to -1 as per normal block
  218. // elements. We'll fix that up here.
  219. if (orientation == HORIZONTAL && content.y < 0)
  220. content.y = parent_box.GetSize().y;
  221. // Now we size the arrows.
  222. for (int i = 0; i < 2; i++)
  223. {
  224. Box arrow_box;
  225. ElementUtilities::BuildBox(arrow_box, parent_box.GetSize(), arrows[i]);
  226. // Clamp the size to (0, 0).
  227. Vector2f arrow_size = arrow_box.GetSize();
  228. if (arrow_size.x < 0 || arrow_size.y < 0)
  229. arrow_box.SetContent(Vector2f(0, 0));
  230. arrows[i]->SetBox(arrow_box);
  231. // Shrink the track length by the arrow size.
  232. content[length_axis] -= arrow_box.GetSize(BoxArea::Margin)[length_axis];
  233. }
  234. // Now the track has been sized, we can fix everything into position.
  235. track_box.SetContent(content);
  236. track->SetBox(track_box);
  237. if (orientation == VERTICAL)
  238. {
  239. Vector2f offset(arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left), arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top));
  240. arrows[0]->SetOffset(offset, parent);
  241. offset.x = track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  242. offset.y += arrows[0]->GetBox().GetSize(BoxArea::Border).y + arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Bottom) +
  243. track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  244. track->SetOffset(offset, parent);
  245. offset.x = arrows[1]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  246. offset.y += track->GetBox().GetSize(BoxArea::Border).y + track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Bottom) +
  247. arrows[1]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  248. arrows[1]->SetOffset(offset, parent);
  249. }
  250. else
  251. {
  252. Vector2f offset(arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left), arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top));
  253. arrows[0]->SetOffset(offset, parent);
  254. offset.x += arrows[0]->GetBox().GetSize(BoxArea::Border).x + arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Right) +
  255. track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  256. offset.y = track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  257. track->SetOffset(offset, parent);
  258. offset.x += track->GetBox().GetSize(BoxArea::Border).x + track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Right) +
  259. arrows[1]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  260. offset.y = arrows[1]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  261. arrows[1]->SetOffset(offset, parent);
  262. }
  263. FormatBar();
  264. FormatProgress();
  265. if (parent->IsDisabled())
  266. {
  267. // Propagate disabled state to child elements
  268. bar->SetPseudoClass("disabled", true);
  269. track->SetPseudoClass("disabled", true);
  270. progress->SetPseudoClass("disabled", true);
  271. arrows[0]->SetPseudoClass("disabled", true);
  272. arrows[1]->SetPseudoClass("disabled", true);
  273. }
  274. }
  275. void WidgetSlider::FormatBar()
  276. {
  277. Box bar_box;
  278. ElementUtilities::BuildBox(bar_box, parent->GetBox().GetSize(), bar);
  279. auto& computed = bar->GetComputedValues();
  280. Vector2f bar_box_content = bar_box.GetSize();
  281. if (orientation == HORIZONTAL)
  282. {
  283. if (computed.height().type == Style::Height::Auto)
  284. bar_box_content.y = parent->GetBox().GetSize().y;
  285. }
  286. // Set the new dimensions on the bar to re-decorate it.
  287. bar_box.SetContent(bar_box_content);
  288. bar->SetBox(bar_box);
  289. // Now that it's been resized, re-position it.
  290. PositionBar();
  291. }
  292. void WidgetSlider::FormatProgress()
  293. {
  294. Box progress_box;
  295. ElementUtilities::BuildBox(progress_box, parent->GetBox().GetSize(), progress);
  296. auto& computed = progress->GetComputedValues();
  297. Vector2f progress_box_content = progress_box.GetSize();
  298. if (orientation == HORIZONTAL)
  299. {
  300. if (computed.height().type == Style::Height::Auto)
  301. progress_box_content.y = track->GetBox().GetSize().y;
  302. }
  303. // Set the new dimensions on the progress element to re-decorate it.
  304. progress_box.SetContent(progress_box_content);
  305. progress->SetBox(progress_box);
  306. Vector2f offset = track->GetRelativeOffset();
  307. offset.x += progress->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  308. offset.y += progress->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  309. progress->SetOffset(offset, parent);
  310. ResizeProgress();
  311. }
  312. Element* WidgetSlider::GetParent() const
  313. {
  314. return parent;
  315. }
  316. void WidgetSlider::ProcessEvent(Event& event)
  317. {
  318. if (parent->IsDisabled())
  319. return;
  320. switch (event.GetId())
  321. {
  322. case EventId::Mousedown:
  323. {
  324. // Only respond to primary mouse button.
  325. if (event.GetParameter("button", -1) != 0)
  326. break;
  327. if (event.GetTargetElement() == track || event.GetTargetElement() == progress)
  328. {
  329. float mouse_position, bar_halfsize;
  330. if (orientation == HORIZONTAL)
  331. {
  332. mouse_position = event.GetParameter<float>("mouse_x", 0);
  333. bar_halfsize = 0.5f * bar->GetBox().GetSize(BoxArea::Border).x;
  334. }
  335. else
  336. {
  337. mouse_position = event.GetParameter<float>("mouse_y", 0);
  338. bar_halfsize = 0.5f * bar->GetBox().GetSize(BoxArea::Border).y;
  339. }
  340. float new_bar_position = AbsolutePositionToBarPosition(mouse_position - bar_halfsize);
  341. SetBarPosition(OnBarChange(new_bar_position));
  342. }
  343. else if (event.GetTargetElement() == arrows[0])
  344. {
  345. arrow_timers[0] = DEFAULT_REPEAT_DELAY;
  346. last_update_time = Clock::GetElapsedTime();
  347. SetBarPosition(OnLineDecrement());
  348. }
  349. else if (event.GetTargetElement() == arrows[1])
  350. {
  351. arrow_timers[1] = DEFAULT_REPEAT_DELAY;
  352. last_update_time = Clock::GetElapsedTime();
  353. SetBarPosition(OnLineIncrement());
  354. }
  355. }
  356. break;
  357. case EventId::Mouseup:
  358. case EventId::Mouseout:
  359. {
  360. if (event.GetTargetElement() == arrows[0])
  361. arrow_timers[0] = -1;
  362. else if (event.GetTargetElement() == arrows[1])
  363. arrow_timers[1] = -1;
  364. }
  365. break;
  366. case EventId::Dragstart:
  367. {
  368. if (event.GetTargetElement() == bar || event.GetTargetElement() == track || event.GetTargetElement() == progress)
  369. {
  370. bar->SetPseudoClass("active", true);
  371. if (orientation == HORIZONTAL)
  372. bar_drag_anchor = event.GetParameter<float>("mouse_x", 0) - bar->GetAbsoluteOffset().x;
  373. else
  374. bar_drag_anchor = event.GetParameter<float>("mouse_y", 0) - bar->GetAbsoluteOffset().y;
  375. }
  376. }
  377. break;
  378. case EventId::Drag:
  379. {
  380. if (event.GetTargetElement() == bar || event.GetTargetElement() == track || event.GetTargetElement() == progress)
  381. {
  382. float new_bar_offset = event.GetParameter<float>((orientation == HORIZONTAL ? "mouse_x" : "mouse_y"), 0) - bar_drag_anchor;
  383. float new_bar_position = AbsolutePositionToBarPosition(new_bar_offset);
  384. SetBarPosition(OnBarChange(new_bar_position));
  385. }
  386. }
  387. break;
  388. case EventId::Dragend:
  389. {
  390. if (event.GetTargetElement() == bar || event.GetTargetElement() == track || event.GetTargetElement() == progress)
  391. {
  392. bar->SetPseudoClass("active", false);
  393. }
  394. }
  395. break;
  396. case EventId::Keydown:
  397. {
  398. const Input::KeyIdentifier key_identifier = (Input::KeyIdentifier)event.GetParameter<int>("key_identifier", 0);
  399. const bool increment =
  400. (key_identifier == Input::KI_RIGHT && orientation == HORIZONTAL) || (key_identifier == Input::KI_DOWN && orientation == VERTICAL);
  401. const bool decrement =
  402. (key_identifier == Input::KI_LEFT && orientation == HORIZONTAL) || (key_identifier == Input::KI_UP && orientation == VERTICAL);
  403. if (increment || decrement)
  404. {
  405. SetBarPosition(decrement ? OnLineDecrement() : OnLineIncrement());
  406. event.StopPropagation();
  407. }
  408. }
  409. break;
  410. case EventId::Focus:
  411. {
  412. if (event.GetTargetElement() == parent)
  413. bar->SetPseudoClass("focus", true);
  414. }
  415. break;
  416. case EventId::Blur:
  417. {
  418. if (event.GetTargetElement() == parent)
  419. bar->SetPseudoClass("focus", false);
  420. }
  421. break;
  422. default: break;
  423. }
  424. }
  425. float WidgetSlider::OnBarChange(float bar_position)
  426. {
  427. float new_value = min_value + bar_position * (max_value - min_value);
  428. int num_steps = Math::RoundToInteger((new_value - value) / step);
  429. return SetValueInternal(value + num_steps * step);
  430. }
  431. float WidgetSlider::OnLineIncrement()
  432. {
  433. return SetValueInternal(value + step);
  434. }
  435. float WidgetSlider::OnLineDecrement()
  436. {
  437. return SetValueInternal(value - step);
  438. }
  439. float WidgetSlider::AbsolutePositionToBarPosition(float absolute_position) const
  440. {
  441. float new_bar_position = bar_position;
  442. if (orientation == HORIZONTAL)
  443. {
  444. const float edge_left = bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  445. const float edge_right = bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Right);
  446. float traversable_track_length =
  447. track->GetBox().GetSize(BoxArea::Content).x - bar->GetBox().GetSize(BoxArea::Border).x - edge_left - edge_right;
  448. if (traversable_track_length > 0)
  449. {
  450. float traversable_track_origin = track->GetAbsoluteOffset().x + edge_left;
  451. new_bar_position = (absolute_position - traversable_track_origin) / traversable_track_length;
  452. new_bar_position = Math::Clamp(new_bar_position, 0.0f, 1.0f);
  453. }
  454. }
  455. else
  456. {
  457. const float edge_top = bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  458. const float edge_bottom = bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Bottom);
  459. float traversable_track_length =
  460. track->GetBox().GetSize(BoxArea::Content).y - bar->GetBox().GetSize(BoxArea::Border).y - edge_top - edge_bottom;
  461. if (traversable_track_length > 0)
  462. {
  463. float traversable_track_origin = track->GetAbsoluteOffset().y + edge_top;
  464. new_bar_position = (absolute_position - traversable_track_origin) / traversable_track_length;
  465. new_bar_position = Math::Clamp(new_bar_position, 0.0f, 1.0f);
  466. }
  467. }
  468. return new_bar_position;
  469. }
  470. void WidgetSlider::PositionBar()
  471. {
  472. const Vector2f track_dimensions = track->GetBox().GetSize();
  473. const Vector2f bar_dimensions = bar->GetBox().GetSize(BoxArea::Border);
  474. if (orientation == VERTICAL)
  475. {
  476. const float edge_top = bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  477. const float edge_bottom = bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Bottom);
  478. const float traversable_track_length = track_dimensions.y - bar_dimensions.y - edge_top - edge_bottom;
  479. const Vector2f offset = {
  480. bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left),
  481. track->GetRelativeOffset().y + edge_top + traversable_track_length * bar_position,
  482. };
  483. bar->SetOffset(offset.Round(), parent);
  484. }
  485. else
  486. {
  487. const float edge_left = bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  488. const float edge_right = bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Right);
  489. const float traversable_track_length = track_dimensions.x - bar_dimensions.x - edge_left - edge_right;
  490. const Vector2f offset = {
  491. track->GetRelativeOffset().x + edge_left + traversable_track_length * bar_position,
  492. bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top),
  493. };
  494. bar->SetOffset(offset.Round(), parent);
  495. }
  496. }
  497. void WidgetSlider::ResizeProgress()
  498. {
  499. Box progress_box = progress->GetBox();
  500. Vector2f new_size = progress_box.GetSize();
  501. if (orientation == VERTICAL)
  502. {
  503. new_size.y = bar->GetOffsetTop();
  504. }
  505. else
  506. {
  507. new_size.x = bar->GetOffsetLeft();
  508. }
  509. progress_box.SetContent(new_size);
  510. progress->SetBox(progress_box);
  511. }
  512. float WidgetSlider::SetValueInternal(float new_value, bool force_submit_change_event)
  513. {
  514. if (min_value < max_value)
  515. {
  516. value = Math::Clamp(new_value, min_value, max_value);
  517. }
  518. else if (min_value > max_value)
  519. {
  520. value = Math::Clamp(new_value, max_value, min_value);
  521. }
  522. else
  523. {
  524. value = min_value;
  525. return 0;
  526. }
  527. const bool value_changed = (value != GetParent()->GetAttribute("value", 0.0f));
  528. if (force_submit_change_event || value_changed)
  529. {
  530. Dictionary parameters;
  531. parameters["value"] = value;
  532. GetParent()->DispatchEvent(EventId::Change, parameters);
  533. }
  534. if (value_changed)
  535. GetParent()->SetAttribute("value", value);
  536. return (value - min_value) / (max_value - min_value);
  537. }
  538. } // namespace Rml