ScrollController.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "ScrollController.h"
  29. #include "../../Include/RmlUi/Core/Core.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. #include "../../Include/RmlUi/Core/SystemInterface.h"
  32. namespace Rml {
  33. static constexpr float AUTOSCROLL_SPEED_FACTOR = 0.09f;
  34. static constexpr float AUTOSCROLL_DEADZONE = 10.0f; // [dp]
  35. static constexpr float SMOOTHSCROLL_WINDOW_SIZE = 50.f; // The window where smoothing is applied, as a distance from scroll start and end. [dp]
  36. static constexpr float SMOOTHSCROLL_VELOCITY_CONSTANT = 800.f; // The constant velocity, any smoothing is applied on top of this. [dp/s]
  37. static constexpr float SMOOTHSCROLL_VELOCITY_SQUARE_FACTOR = 0.05f;
  38. // Clamp the delta time to some reasonable FPS range, to avoid large steps in case of stuttering or freezing.
  39. static constexpr float DELTA_TIME_CLAMP_LOW = 1.f / 500.f; // [s]
  40. static constexpr float DELTA_TIME_CLAMP_HIGH = 1.f / 15.f; // [s]
  41. // Determines the autoscroll velocity based on the distance from the scroll-start mouse position. [px/s]
  42. static Vector2f CalculateAutoscrollVelocity(Vector2f target_delta, float dp_ratio)
  43. {
  44. target_delta = target_delta / dp_ratio;
  45. target_delta = {
  46. Math::AbsoluteValue(target_delta.x) < AUTOSCROLL_DEADZONE ? 0.f : target_delta.x,
  47. Math::AbsoluteValue(target_delta.y) < AUTOSCROLL_DEADZONE ? 0.f : target_delta.y,
  48. };
  49. // We use a signed square model for the velocity, which seems to work quite well. This is mostly about feeling and tuning.
  50. return AUTOSCROLL_SPEED_FACTOR * target_delta * Math::AbsoluteValue(target_delta);
  51. }
  52. // Determines the smoothscroll velocity based on the distance to the target, and the distance scrolled so far. [px/s]
  53. static Vector2f CalculateSmoothscrollVelocity(Vector2f target_delta, Vector2f scrolled_distance, float dp_ratio)
  54. {
  55. scrolled_distance = Math::AbsoluteValue(scrolled_distance) / dp_ratio;
  56. target_delta = target_delta / dp_ratio;
  57. const Vector2f target_delta_abs = Math::AbsoluteValue(target_delta);
  58. Vector2f target_delta_signum = {
  59. target_delta.x > 0.f ? 1.f : (target_delta.x < 0.f ? -1.f : 0.f),
  60. target_delta.y > 0.f ? 1.f : (target_delta.y < 0.f ? -1.f : 0.f),
  61. };
  62. // The window provides velocity smoothing near the start and end of the scroll.
  63. const Tween tween(Tween::Exponential, Tween::Out);
  64. const Vector2f alpha_in = Math::Min(scrolled_distance / SMOOTHSCROLL_WINDOW_SIZE, Vector2f(1.f));
  65. const Vector2f alpha_out = Math::Min(target_delta_abs / SMOOTHSCROLL_WINDOW_SIZE, Vector2f(1.f));
  66. const Vector2f smooth_window = {
  67. tween(alpha_in.x) * tween(alpha_out.x),
  68. tween(alpha_in.y) * tween(alpha_out.y),
  69. };
  70. const Vector2f velocity_constant = Vector2f(SMOOTHSCROLL_VELOCITY_CONSTANT);
  71. const Vector2f velocity_square = SMOOTHSCROLL_VELOCITY_SQUARE_FACTOR * target_delta_abs * target_delta_abs;
  72. // Short scrolls are dominated by the smoothed constant velocity, while the square term is added for quick longer scrolls.
  73. return dp_ratio * target_delta_signum * (smooth_window * velocity_constant + velocity_square);
  74. }
  75. void ScrollController::ActivateAutoscroll(Element* in_target, Vector2i start_position)
  76. {
  77. Reset();
  78. if (!in_target)
  79. return;
  80. target = in_target;
  81. mode = Mode::Autoscroll;
  82. autoscroll_start_position = start_position;
  83. UpdateTime();
  84. }
  85. void ScrollController::ActivateSmoothscroll(Element* in_target)
  86. {
  87. Reset();
  88. if (!in_target)
  89. return;
  90. target = in_target;
  91. mode = Mode::Smoothscroll;
  92. UpdateTime();
  93. }
  94. void ScrollController::Update(Vector2i mouse_position, float dp_ratio)
  95. {
  96. if (mode == Mode::Autoscroll)
  97. UpdateAutoscroll(mouse_position, dp_ratio);
  98. else if (mode == Mode::Smoothscroll)
  99. UpdateSmoothscroll(dp_ratio);
  100. }
  101. void ScrollController::UpdateAutoscroll(Vector2i mouse_position, float dp_ratio)
  102. {
  103. RMLUI_ASSERT(mode == Mode::Autoscroll && target);
  104. const float dt = UpdateTime();
  105. const Vector2f scroll_delta = Vector2f(mouse_position - autoscroll_start_position);
  106. const Vector2f scroll_velocity = CalculateAutoscrollVelocity(scroll_delta, dp_ratio);
  107. autoscroll_accumulated_length += scroll_velocity * dt;
  108. // Only submit the integer part of the scroll length, accumulate and store fractional parts to enable sub-pixel-per-frame scrolling speeds.
  109. Vector2f scroll_length_integral = autoscroll_accumulated_length;
  110. autoscroll_accumulated_length.x = Math::DecomposeFractionalIntegral(autoscroll_accumulated_length.x, &scroll_length_integral.x);
  111. autoscroll_accumulated_length.y = Math::DecomposeFractionalIntegral(autoscroll_accumulated_length.y, &scroll_length_integral.y);
  112. if (scroll_velocity != Vector2f(0.f))
  113. autoscroll_moved = true;
  114. PerformScrollOnTarget(scroll_length_integral);
  115. }
  116. void ScrollController::UpdateSmoothscroll(float dp_ratio)
  117. {
  118. RMLUI_ASSERT(mode == Mode::Smoothscroll && target);
  119. const Vector2f target_delta = Vector2f(smoothscroll_target_distance - smoothscroll_scrolled_distance);
  120. const Vector2f velocity = CalculateSmoothscrollVelocity(target_delta, smoothscroll_scrolled_distance, dp_ratio);
  121. const float dt = UpdateTime();
  122. Vector2f scroll_distance = (velocity * dt).Round();
  123. for (int i = 0; i < 2; i++)
  124. {
  125. // Ensure minimum scroll speed of 1px/frame, and clamp the distance to the target in case of overshooting
  126. // integration. As opposed to autoscroll, we don't care about fractional speeds here since we want to be fast.
  127. if (target_delta[i] > 0.f)
  128. scroll_distance[i] = Math::Min(Math::Max(scroll_distance[i], 1.f), target_delta[i]);
  129. else if (target_delta[i] < 0.f)
  130. scroll_distance[i] = Math::Max(Math::Min(scroll_distance[i], -1.f), target_delta[i]);
  131. else
  132. scroll_distance[i] = 0.f;
  133. }
  134. smoothscroll_scrolled_distance += scroll_distance;
  135. PerformScrollOnTarget(scroll_distance);
  136. if (scroll_distance == target_delta)
  137. Reset();
  138. }
  139. void ScrollController::PerformScrollOnTarget(Vector2f delta_distance)
  140. {
  141. RMLUI_ASSERT(target);
  142. if (delta_distance.x != 0.f)
  143. target->SetScrollLeft(target->GetScrollLeft() + delta_distance.x);
  144. if (delta_distance.y != 0.f)
  145. target->SetScrollTop(target->GetScrollTop() + delta_distance.y);
  146. }
  147. void ScrollController::IncrementSmoothscrollTarget(Vector2f delta_distance)
  148. {
  149. auto OppositeDirection = [](float a, float b) { return (a < 0.f && b > 0.f) || (a > 0.f && b < 0.f); };
  150. Vector2f delta = smoothscroll_target_distance - smoothscroll_scrolled_distance;
  151. // Reset movement state if we start scrolling in the opposite direction.
  152. for (int i = 0; i < 2; i++)
  153. {
  154. if (OppositeDirection(delta_distance[i], delta[i]))
  155. {
  156. smoothscroll_target_distance[i] = 0.f;
  157. smoothscroll_scrolled_distance[i] = 0.f;
  158. }
  159. }
  160. smoothscroll_target_distance += delta_distance;
  161. }
  162. void ScrollController::Reset()
  163. {
  164. *this = ScrollController{};
  165. }
  166. String ScrollController::GetAutoscrollCursor(Vector2i mouse_position, float dp_ratio) const
  167. {
  168. RMLUI_ASSERT(mode == Mode::Autoscroll);
  169. const Vector2f scroll_delta = Vector2f(mouse_position - autoscroll_start_position);
  170. const Vector2f scroll_velocity = CalculateAutoscrollVelocity(scroll_delta, dp_ratio);
  171. if (scroll_velocity == Vector2f(0.f))
  172. return "rmlui-scroll-idle";
  173. String result = "rmlui-scroll";
  174. if (scroll_velocity.y > 0.f)
  175. result += "-up";
  176. else if (scroll_velocity.y < 0.f)
  177. result += "-down";
  178. if (scroll_velocity.x > 0.f)
  179. result += "-right";
  180. else if (scroll_velocity.x < 0.f)
  181. result += "-left";
  182. return result;
  183. }
  184. bool ScrollController::HasAutoscrollMoved() const
  185. {
  186. return mode == Mode::Autoscroll && autoscroll_moved;
  187. }
  188. float ScrollController::UpdateTime()
  189. {
  190. const double previous_tick = previous_update_time;
  191. previous_update_time = GetSystemInterface()->GetElapsedTime();
  192. const float dt = float(previous_update_time - previous_tick);
  193. return Math::Clamp(dt, DELTA_TIME_CLAMP_LOW, DELTA_TIME_CLAMP_HIGH);
  194. }
  195. } // namespace Rml