WidgetSlider.cpp 19 KB

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