WidgetScroll.cpp 18 KB

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