WidgetSlider.cpp 18 KB

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