WidgetScroll.cpp 15 KB

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