WidgetScroll.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 "WidgetScroll.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/Context.h"
  31. #include "../../Include/RmlUi/Core/Element.h"
  32. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  33. #include "../../Include/RmlUi/Core/Event.h"
  34. #include "../../Include/RmlUi/Core/Factory.h"
  35. #include "../../Include/RmlUi/Core/Property.h"
  36. #include "Clock.h"
  37. #include "Layout/LayoutDetails.h"
  38. #include <algorithm>
  39. namespace Rml {
  40. static constexpr float DEFAULT_REPEAT_DELAY = 0.5f;
  41. static constexpr float DEFAULT_REPEAT_PERIOD = 0.1f;
  42. static constexpr float SCROLL_LINE_LENGTH = 30.f; // [dp]
  43. static constexpr float SCROLL_PAGE_FACTOR = 0.8f;
  44. WidgetScroll::WidgetScroll(Element* _parent)
  45. {
  46. parent = _parent;
  47. orientation = UNKNOWN;
  48. track = nullptr;
  49. bar = nullptr;
  50. arrows[0] = nullptr;
  51. arrows[1] = nullptr;
  52. bar_position = 0;
  53. bar_drag_anchor = 0;
  54. arrow_timers[0] = -1;
  55. arrow_timers[1] = -1;
  56. last_update_time = 0;
  57. track_length = 0;
  58. bar_length = 0;
  59. }
  60. WidgetScroll::~WidgetScroll()
  61. {
  62. if (bar != nullptr)
  63. {
  64. bar->RemoveEventListener(EventId::Drag, this);
  65. bar->RemoveEventListener(EventId::Dragstart, this);
  66. }
  67. if (track != nullptr)
  68. track->RemoveEventListener(EventId::Click, this);
  69. for (int i = 0; i < 2; i++)
  70. {
  71. if (arrows[i] != nullptr)
  72. {
  73. arrows[i]->RemoveEventListener(EventId::Mousedown, this);
  74. arrows[i]->RemoveEventListener(EventId::Mouseup, this);
  75. arrows[i]->RemoveEventListener(EventId::Mouseout, this);
  76. }
  77. }
  78. }
  79. bool WidgetScroll::Initialise(Orientation _orientation)
  80. {
  81. // Check that we haven't already been successfully initialised.
  82. if (orientation != UNKNOWN)
  83. {
  84. RMLUI_ERROR;
  85. return false;
  86. }
  87. // Check that a valid orientation has been passed in.
  88. if (_orientation != HORIZONTAL && _orientation != VERTICAL)
  89. {
  90. RMLUI_ERROR;
  91. return false;
  92. }
  93. orientation = _orientation;
  94. // Create all of our child elements as standard elements, and abort if we can't create them.
  95. ElementPtr track_element = Factory::InstanceElement(parent, "*", "slidertrack", XMLAttributes());
  96. ElementPtr bar_element = Factory::InstanceElement(parent, "*", "sliderbar", XMLAttributes());
  97. ElementPtr arrow0_element = Factory::InstanceElement(parent, "*", "sliderarrowdec", XMLAttributes());
  98. ElementPtr arrow1_element = Factory::InstanceElement(parent, "*", "sliderarrowinc", XMLAttributes());
  99. if (!track_element || !bar_element || !arrow0_element || !arrow1_element)
  100. {
  101. return false;
  102. }
  103. // Add them as non-DOM elements.
  104. track = parent->AppendChild(std::move(track_element), false);
  105. bar = parent->AppendChild(std::move(bar_element), false);
  106. arrows[0] = parent->AppendChild(std::move(arrow0_element), false);
  107. arrows[1] = parent->AppendChild(std::move(arrow1_element), false);
  108. bar->SetProperty(PropertyId::Drag, Property(Style::Drag::Drag));
  109. // Attach the listeners as appropriate.
  110. bar->AddEventListener(EventId::Drag, this);
  111. bar->AddEventListener(EventId::Dragstart, this);
  112. track->AddEventListener(EventId::Click, this);
  113. for (int i = 0; i < 2; i++)
  114. {
  115. arrows[i]->AddEventListener(EventId::Mousedown, this);
  116. arrows[i]->AddEventListener(EventId::Mouseup, this);
  117. arrows[i]->AddEventListener(EventId::Mouseout, this);
  118. }
  119. return true;
  120. }
  121. void WidgetScroll::Update()
  122. {
  123. if (!std::any_of(std::begin(arrow_timers), std::end(arrow_timers), [](float timer) { return timer > 0; }))
  124. return;
  125. const double current_time = Clock::GetElapsedTime();
  126. const float delta_time = float(current_time - last_update_time);
  127. last_update_time = current_time;
  128. for (int i = 0; i < 2; i++)
  129. {
  130. if (arrow_timers[i] > 0)
  131. {
  132. arrow_timers[i] -= delta_time;
  133. while (arrow_timers[i] <= 0)
  134. {
  135. arrow_timers[i] += DEFAULT_REPEAT_PERIOD;
  136. if (i == 0)
  137. ScrollLineUp();
  138. else
  139. ScrollLineDown();
  140. }
  141. if (Context* ctx = parent->GetContext())
  142. ctx->RequestNextUpdate(arrow_timers[i]);
  143. }
  144. }
  145. }
  146. void WidgetScroll::SetBarPosition(float _bar_position)
  147. {
  148. bar_position = Math::Clamp(_bar_position, 0.0f, 1.0f);
  149. PositionBar();
  150. }
  151. float WidgetScroll::GetBarPosition() const
  152. {
  153. return bar_position;
  154. }
  155. WidgetScroll::Orientation WidgetScroll::GetOrientation() const
  156. {
  157. return orientation;
  158. }
  159. void WidgetScroll::GetDimensions(Vector2f& dimensions) const
  160. {
  161. switch (orientation)
  162. {
  163. case VERTICAL:
  164. dimensions.x = 16;
  165. dimensions.y = 256;
  166. break;
  167. case HORIZONTAL:
  168. dimensions.x = 256;
  169. dimensions.y = 16;
  170. break;
  171. case UNKNOWN: break;
  172. }
  173. }
  174. void WidgetScroll::FormatElements(const Vector2f containing_block, float slider_length)
  175. {
  176. int length_axis = orientation == VERTICAL ? 1 : 0;
  177. // Build the box for the containing slider element.
  178. Box parent_box;
  179. LayoutDetails::BuildBox(parent_box, containing_block, parent);
  180. slider_length -= orientation == VERTICAL
  181. ? (parent_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Top) + parent_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Bottom))
  182. : (parent_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Left) + parent_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Right));
  183. // Set the length of the slider.
  184. Vector2f content = parent_box.GetSize();
  185. content[length_axis] = slider_length;
  186. parent_box.SetContent(content);
  187. // And set it on the slider element!
  188. parent->SetBox(parent_box);
  189. // Generate the initial dimensions for the track. It'll need to be cut down to fit the arrows.
  190. Box track_box;
  191. LayoutDetails::BuildBox(track_box, parent_box.GetSize(), track);
  192. content = track_box.GetSize();
  193. content[length_axis] = slider_length -= orientation == VERTICAL
  194. ? (track_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Top) + track_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Bottom))
  195. : (track_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Left) + track_box.GetCumulativeEdge(BoxArea::Content, BoxEdge::Right));
  196. // If no height has been explicitly specified for the track, it'll be initialised to -1 as per normal block
  197. // elements. We'll fix that up here.
  198. if (orientation == HORIZONTAL && content.y < 0)
  199. content.y = parent_box.GetSize().y;
  200. // Now we size the arrows.
  201. for (int i = 0; i < 2; i++)
  202. {
  203. Box arrow_box;
  204. LayoutDetails::BuildBox(arrow_box, parent_box.GetSize(), arrows[i]);
  205. // Clamp the size to (0, 0).
  206. Vector2f arrow_size = arrow_box.GetSize();
  207. if (arrow_size.x < 0 || arrow_size.y < 0)
  208. arrow_box.SetContent(Vector2f(0, 0));
  209. arrows[i]->SetBox(arrow_box);
  210. // Shrink the track length by the arrow size.
  211. content[length_axis] -= arrow_box.GetSize(BoxArea::Margin)[length_axis];
  212. }
  213. // Now the track has been sized, we can fix everything into position.
  214. track_box.SetContent(content);
  215. track->SetBox(track_box);
  216. if (orientation == VERTICAL)
  217. {
  218. Vector2f offset(arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left), arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top));
  219. arrows[0]->SetOffset(offset, parent);
  220. offset.x = track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  221. offset.y += arrows[0]->GetBox().GetSize(BoxArea::Border).y + arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Bottom) +
  222. track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  223. track->SetOffset(offset, parent);
  224. offset.x = arrows[1]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  225. offset.y += track->GetBox().GetSize(BoxArea::Border).y + track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Bottom) +
  226. arrows[1]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  227. arrows[1]->SetOffset(offset, parent);
  228. }
  229. else
  230. {
  231. Vector2f offset(arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left), arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top));
  232. arrows[0]->SetOffset(offset, parent);
  233. offset.x += arrows[0]->GetBox().GetSize(BoxArea::Border).x + arrows[0]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Right) +
  234. track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  235. offset.y = track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  236. track->SetOffset(offset, parent);
  237. offset.x += track->GetBox().GetSize(BoxArea::Border).x + track->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Right) +
  238. arrows[1]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  239. offset.y = arrows[1]->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  240. arrows[1]->SetOffset(offset, parent);
  241. }
  242. FormatBar();
  243. }
  244. void WidgetScroll::FormatBar()
  245. {
  246. Box bar_box;
  247. LayoutDetails::BuildBox(bar_box, parent->GetBox().GetSize(), bar);
  248. const auto& computed = bar->GetComputedValues();
  249. const Style::Width width = computed.width();
  250. const Style::Height height = computed.height();
  251. Vector2f bar_box_content = bar_box.GetSize();
  252. if (orientation == HORIZONTAL)
  253. {
  254. if (height.type == height.Auto)
  255. bar_box_content.y = parent->GetBox().GetSize().y;
  256. }
  257. float relative_bar_length;
  258. if (track_length <= 0)
  259. relative_bar_length = 1;
  260. else if (bar_length <= 0)
  261. relative_bar_length = 0;
  262. else
  263. relative_bar_length = bar_length / track_length;
  264. Vector2f track_size = track->GetBox().GetSize();
  265. if (orientation == VERTICAL)
  266. {
  267. float track_length = track_size.y - bar_box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin, BoxArea::Padding);
  268. if (height.type == height.Auto)
  269. {
  270. bar_box_content.y = track_length * relative_bar_length;
  271. // Check for 'min-height' restrictions.
  272. float min_track_length = ResolveValue(computed.min_height(), track_length);
  273. bar_box_content.y = Math::Max(min_track_length, bar_box_content.y);
  274. // Check for 'max-height' restrictions.
  275. float max_track_length = ResolveValue(computed.max_height(), track_length);
  276. bar_box_content.y = Math::Min(max_track_length, bar_box_content.y);
  277. }
  278. // Make sure we haven't gone further than we're allowed to (min-height may have made us too big).
  279. bar_box_content.y = Math::Min(bar_box_content.y, track_length);
  280. }
  281. else
  282. {
  283. float track_length = track_size.x - bar_box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin, BoxArea::Padding);
  284. if (width.type == width.Auto)
  285. {
  286. bar_box_content.x = track_length * relative_bar_length;
  287. // Check for 'min-width' restrictions.
  288. float min_track_length = ResolveValue(computed.min_width(), track_length);
  289. bar_box_content.x = Math::Max(min_track_length, bar_box_content.x);
  290. // Check for 'max-width' restrictions.
  291. float max_track_length = ResolveValue(computed.max_width(), track_length);
  292. bar_box_content.x = Math::Min(max_track_length, bar_box_content.x);
  293. }
  294. // Make sure we haven't gone further than we're allowed to (min-width may have made us too big).
  295. bar_box_content.x = Math::Min(bar_box_content.x, track_length);
  296. }
  297. // Set the new dimensions on the bar to re-decorate it.
  298. bar_box.SetContent(bar_box_content.Round());
  299. bar->SetBox(bar_box);
  300. // Now that it's been resized, re-position it.
  301. PositionBar();
  302. }
  303. void WidgetScroll::ProcessEvent(Event& event)
  304. {
  305. if (event.GetTargetElement() == bar)
  306. {
  307. if (event == EventId::Drag)
  308. {
  309. float new_bar_position = 0.f;
  310. if (orientation == HORIZONTAL)
  311. {
  312. float traversable_track_length = track->GetBox().GetSize().x - bar->GetBox().GetSize().x;
  313. if (traversable_track_length > 0)
  314. {
  315. float traversable_track_origin = track->GetAbsoluteOffset().x + bar_drag_anchor;
  316. new_bar_position = (event.GetParameter("mouse_x", 0.f) - traversable_track_origin) / traversable_track_length;
  317. }
  318. }
  319. else
  320. {
  321. float traversable_track_length = track->GetBox().GetSize().y - bar->GetBox().GetSize().y;
  322. if (traversable_track_length > 0)
  323. {
  324. float traversable_track_origin = track->GetAbsoluteOffset().y + bar_drag_anchor;
  325. new_bar_position = (event.GetParameter("mouse_y", 0.f) - traversable_track_origin) / traversable_track_length;
  326. }
  327. }
  328. SetBarPosition(new_bar_position);
  329. Scroll(0.f, ScrollBehavior::Instant);
  330. }
  331. else if (event == EventId::Dragstart)
  332. {
  333. if (orientation == HORIZONTAL)
  334. bar_drag_anchor = event.GetParameter("mouse_x", 0.f) - bar->GetAbsoluteOffset().x;
  335. else
  336. bar_drag_anchor = event.GetParameter("mouse_y", 0.f) - bar->GetAbsoluteOffset().y;
  337. }
  338. }
  339. else if (event.GetTargetElement() == track)
  340. {
  341. if (event == EventId::Click)
  342. {
  343. float click_position = 0.f;
  344. if (orientation == HORIZONTAL)
  345. {
  346. float mouse_position = event.GetParameter("mouse_x", 0.f);
  347. click_position = (mouse_position - track->GetAbsoluteOffset().x) / track->GetBox().GetSize().x;
  348. }
  349. else
  350. {
  351. float mouse_position = event.GetParameter<float>("mouse_y", 0);
  352. click_position = (mouse_position - track->GetAbsoluteOffset().y) / track->GetBox().GetSize().y;
  353. }
  354. if (click_position <= bar_position)
  355. ScrollPageUp();
  356. else
  357. ScrollPageDown();
  358. }
  359. }
  360. if (event == EventId::Mousedown)
  361. {
  362. if (event.GetTargetElement() == arrows[0])
  363. {
  364. arrow_timers[0] = DEFAULT_REPEAT_DELAY;
  365. last_update_time = Clock::GetElapsedTime();
  366. ScrollLineUp();
  367. }
  368. else if (event.GetTargetElement() == arrows[1])
  369. {
  370. arrow_timers[1] = DEFAULT_REPEAT_DELAY;
  371. last_update_time = Clock::GetElapsedTime();
  372. ScrollLineDown();
  373. }
  374. }
  375. else if (event == EventId::Mouseup || event == EventId::Mouseout)
  376. {
  377. if (event.GetTargetElement() == arrows[0])
  378. arrow_timers[0] = -1;
  379. else if (event.GetTargetElement() == arrows[1])
  380. arrow_timers[1] = -1;
  381. }
  382. }
  383. void WidgetScroll::PositionBar()
  384. {
  385. const Vector2f track_dimensions = track->GetBox().GetSize();
  386. const Vector2f bar_dimensions = bar->GetBox().GetSize(BoxArea::Border);
  387. if (orientation == VERTICAL)
  388. {
  389. const float traversable_track_length = track_dimensions.y - bar_dimensions.y;
  390. const Vector2f offset = {
  391. bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left),
  392. track->GetRelativeOffset().y + traversable_track_length * bar_position,
  393. };
  394. bar->SetOffset(offset.Round(), parent);
  395. }
  396. else
  397. {
  398. const float traversable_track_length = track_dimensions.x - bar_dimensions.x;
  399. const Vector2f offset = {
  400. track->GetRelativeOffset().x + traversable_track_length * bar_position,
  401. bar->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top),
  402. };
  403. bar->SetOffset(offset.Round(), parent);
  404. }
  405. }
  406. void WidgetScroll::SetTrackLength(float _track_length)
  407. {
  408. track_length = _track_length;
  409. }
  410. void WidgetScroll::SetBarLength(float _bar_length)
  411. {
  412. bar_length = _bar_length;
  413. }
  414. void WidgetScroll::ScrollLineDown()
  415. {
  416. Scroll(SCROLL_LINE_LENGTH * ElementUtilities::GetDensityIndependentPixelRatio(parent), ScrollBehavior::Auto);
  417. }
  418. void WidgetScroll::ScrollLineUp()
  419. {
  420. Scroll(-SCROLL_LINE_LENGTH * ElementUtilities::GetDensityIndependentPixelRatio(parent), ScrollBehavior::Auto);
  421. }
  422. void WidgetScroll::ScrollPageDown()
  423. {
  424. Scroll(SCROLL_PAGE_FACTOR * bar_length, ScrollBehavior::Auto);
  425. }
  426. void WidgetScroll::ScrollPageUp()
  427. {
  428. Scroll(-SCROLL_PAGE_FACTOR * bar_length, ScrollBehavior::Auto);
  429. }
  430. void WidgetScroll::Scroll(float distance, ScrollBehavior behavior)
  431. {
  432. float traversable_track_length = (track_length - bar_length);
  433. float new_bar_position = bar_position;
  434. if (traversable_track_length > 0.f)
  435. new_bar_position = Math::Clamp((bar_position * traversable_track_length + distance) / traversable_track_length, 0.f, 1.f);
  436. // 'parent' is the scrollbar element, its parent again is the actual element we want to scroll
  437. Element* element_scroll = parent->GetParentNode();
  438. if (!element_scroll)
  439. {
  440. RMLUI_ERROR;
  441. return;
  442. }
  443. Vector2f scroll_offset = {element_scroll->GetScrollLeft(), element_scroll->GetScrollTop()};
  444. if (orientation == HORIZONTAL)
  445. scroll_offset.x = new_bar_position * (element_scroll->GetScrollWidth() - element_scroll->GetClientWidth());
  446. else
  447. scroll_offset.y = new_bar_position * (element_scroll->GetScrollHeight() - element_scroll->GetClientHeight());
  448. element_scroll->ScrollTo(scroll_offset, behavior);
  449. }
  450. } // namespace Rml