WidgetSlider.cpp 18 KB

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