ScrollController.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "ScrollController.h"
  2. #include "../../Include/RmlUi/Core/ComputedValues.h"
  3. #include "../../Include/RmlUi/Core/Core.h"
  4. #include "../../Include/RmlUi/Core/Element.h"
  5. #include "../../Include/RmlUi/Core/SystemInterface.h"
  6. namespace Rml {
  7. static constexpr float AUTOSCROLL_SPEED_FACTOR = 0.09f;
  8. static constexpr float AUTOSCROLL_DEADZONE = 10.0f; // [dp]
  9. static constexpr float SMOOTHSCROLL_WINDOW_SIZE = 100.f; // The window where smoothing is applied, as a distance from scroll start and end. [dp]
  10. static constexpr float SMOOTHSCROLL_MAX_VELOCITY = 10'000.f; // [dp/s]
  11. static constexpr float SMOOTHSCROLL_VELOCITY_CONSTANT = 800.f; // [dp/s]
  12. static constexpr float SMOOTHSCROLL_VELOCITY_SQUARE_FACTOR = 0.05f;
  13. static constexpr float SMOOTHSCROLL_FIRST_FRAME_DELTA_TIME_MIN = 1.f / 100.f; // To make the scroll feel a bit more responsive. [s]
  14. // Factor to multiply friction by before applying to velocity.
  15. static constexpr float INERTIA_FRICTION_FACTOR = 5.0f;
  16. static constexpr float INERTIA_VELOCITY_CUTOFF = 30.0f;
  17. // Saturate the delta time to some reasonable FPS value, to avoid large steps in case of stuttering or freezing.
  18. static constexpr float DELTA_TIME_MAX = 1.f / 15.f; // [s]
  19. // Determines the autoscroll velocity based on the distance from the scroll-start mouse position. [px/s]
  20. static Vector2f CalculateAutoscrollVelocity(Vector2f target_delta, float dp_ratio)
  21. {
  22. target_delta = target_delta / dp_ratio;
  23. target_delta = {
  24. Math::Absolute(target_delta.x) < AUTOSCROLL_DEADZONE ? 0.f : target_delta.x,
  25. Math::Absolute(target_delta.y) < AUTOSCROLL_DEADZONE ? 0.f : target_delta.y,
  26. };
  27. // We use a signed square model for the velocity, which seems to work quite well. This is mostly about feeling and tuning.
  28. return AUTOSCROLL_SPEED_FACTOR * target_delta * Math::Absolute(target_delta);
  29. }
  30. // Determines the smoothscroll velocity based on the distance to the target, and the distance scrolled so far. [px/s]
  31. static Vector2f CalculateSmoothscrollVelocity(Vector2f target_delta, Vector2f scrolled_distance, float dp_ratio)
  32. {
  33. scrolled_distance = Math::Absolute(scrolled_distance) / dp_ratio;
  34. target_delta = target_delta / dp_ratio;
  35. const Vector2f target_delta_abs = Math::Absolute(target_delta);
  36. Vector2f target_delta_signum = {
  37. target_delta.x > 0.f ? 1.f : (target_delta.x < 0.f ? -1.f : 0.f),
  38. target_delta.y > 0.f ? 1.f : (target_delta.y < 0.f ? -1.f : 0.f),
  39. };
  40. // The window provides velocity smoothing near the start and end of the scroll.
  41. const Tween tween(Tween::Exponential, Tween::Out);
  42. const Vector2f alpha_in = Math::Min(scrolled_distance / SMOOTHSCROLL_WINDOW_SIZE, Vector2f(1.f));
  43. const Vector2f alpha_out = Math::Min(target_delta_abs / SMOOTHSCROLL_WINDOW_SIZE, Vector2f(1.f));
  44. const Vector2f smooth_window = {
  45. 0.35f + 0.65f * tween(alpha_in.x) * tween(alpha_out.x),
  46. 0.35f + 0.65f * tween(alpha_in.y) * tween(alpha_out.y),
  47. };
  48. const Vector2f velocity_constant = Vector2f(SMOOTHSCROLL_VELOCITY_CONSTANT);
  49. const Vector2f velocity_square = SMOOTHSCROLL_VELOCITY_SQUARE_FACTOR * target_delta_abs * target_delta_abs;
  50. // Short scrolls are dominated by the smoothed constant velocity, while the square term is added for quick longer scrolls.
  51. return dp_ratio * target_delta_signum * smooth_window * Math::Min(velocity_constant + velocity_square, Vector2f(SMOOTHSCROLL_MAX_VELOCITY));
  52. }
  53. void ScrollController::ActivateAutoscroll(Element* in_target, Vector2i start_position)
  54. {
  55. Reset();
  56. if (!in_target)
  57. return;
  58. target = in_target;
  59. mode = Mode::Autoscroll;
  60. autoscroll_start_position = start_position;
  61. UpdateTime();
  62. }
  63. void ScrollController::ActivateSmoothscroll(Element* in_target, Vector2f delta_distance, ScrollBehavior scroll_behavior)
  64. {
  65. Reset();
  66. if (!in_target)
  67. return;
  68. target = in_target;
  69. // Do instant scroll if preferred.
  70. if (smoothscroll_prefer_instant && scroll_behavior != ScrollBehavior::Smooth)
  71. {
  72. PerformScrollOnTarget(delta_distance);
  73. target = nullptr;
  74. return;
  75. }
  76. mode = Mode::Smoothscroll;
  77. UpdateTime();
  78. IncrementSmoothscrollTarget(delta_distance);
  79. // If the target is scrolled to its edge already, simply cancel the smoothscroll operation.
  80. if (HasSmoothscrollReachedTarget())
  81. Reset();
  82. }
  83. void ScrollController::ActivateInertia(Element* in_target, Vector2f velocity)
  84. {
  85. Reset();
  86. if (!in_target || (velocity.x == 0 && velocity.y == 0))
  87. return;
  88. target = in_target;
  89. inertia_scroll_velocity = velocity;
  90. mode = Mode::Inertia;
  91. UpdateTime();
  92. }
  93. void ScrollController::InstantScrollOnTarget(Element* in_target, Vector2f delta_distance)
  94. {
  95. if (!in_target)
  96. return;
  97. // instant scroll element without changing the current target
  98. Element* safe_target = target;
  99. target = in_target;
  100. PerformScrollOnTarget(delta_distance);
  101. target = safe_target;
  102. }
  103. bool ScrollController::Update(Vector2i mouse_position, float dp_ratio)
  104. {
  105. const float dt = (mode == Mode::None ? 0.f : UpdateTime());
  106. switch (mode)
  107. {
  108. case Mode::Smoothscroll: UpdateSmoothscroll(dt, dp_ratio); break;
  109. case Mode::Autoscroll: UpdateAutoscroll(dt, mouse_position, dp_ratio); break;
  110. case Mode::Inertia: UpdateInertia(dt); break;
  111. case Mode::None: break;
  112. }
  113. return mode != Mode::None;
  114. }
  115. void ScrollController::UpdateAutoscroll(float dt, Vector2i mouse_position, float dp_ratio)
  116. {
  117. RMLUI_ASSERT(mode == Mode::Autoscroll && target);
  118. const Vector2f scroll_delta = Vector2f(mouse_position - autoscroll_start_position);
  119. const Vector2f scroll_velocity = CalculateAutoscrollVelocity(scroll_delta, dp_ratio);
  120. autoscroll_accumulated_length += scroll_velocity * dt;
  121. // Only submit the integer part of the scroll length, accumulate and store fractional parts to enable sub-pixel-per-frame scrolling speeds.
  122. Vector2f scroll_length_integral;
  123. autoscroll_accumulated_length.x = Math::DecomposeFractionalIntegral(autoscroll_accumulated_length.x, &scroll_length_integral.x);
  124. autoscroll_accumulated_length.y = Math::DecomposeFractionalIntegral(autoscroll_accumulated_length.y, &scroll_length_integral.y);
  125. if (scroll_velocity != Vector2f(0.f))
  126. autoscroll_moved = true;
  127. PerformScrollOnTarget(scroll_length_integral);
  128. }
  129. void ScrollController::UpdateSmoothscroll(float dt, float dp_ratio)
  130. {
  131. RMLUI_ASSERT(mode == Mode::Smoothscroll && target);
  132. const Vector2f target_delta = Vector2f(smoothscroll_target_distance - smoothscroll_scrolled_distance);
  133. const Vector2f velocity = CalculateSmoothscrollVelocity(target_delta, smoothscroll_scrolled_distance, dp_ratio);
  134. if (smoothscroll_scrolled_distance == Vector2f{0})
  135. dt = Math::Max(dt, SMOOTHSCROLL_FIRST_FRAME_DELTA_TIME_MIN);
  136. Vector2f scroll_distance_fractional = smoothscroll_speed_factor * velocity * dt + smoothscroll_accumulated_fractional_distance;
  137. Vector2f scroll_distance_integral;
  138. smoothscroll_accumulated_fractional_distance.x = Math::DecomposeFractionalIntegral(scroll_distance_fractional.x, &scroll_distance_integral.x);
  139. smoothscroll_accumulated_fractional_distance.y = Math::DecomposeFractionalIntegral(scroll_distance_fractional.y, &scroll_distance_integral.y);
  140. for (int i = 0; i < 2; i++)
  141. {
  142. // Clamp the distance to the target in case of overshooting integration.
  143. if (target_delta[i] > 0.f)
  144. scroll_distance_integral[i] = Math::Min(scroll_distance_integral[i], target_delta[i]);
  145. else if (target_delta[i] < 0.f)
  146. scroll_distance_integral[i] = Math::Max(scroll_distance_integral[i], target_delta[i]);
  147. else
  148. scroll_distance_integral[i] = 0.f;
  149. }
  150. #if 0
  151. // Useful debugging output for velocity model tuning.
  152. Log::Message(Log::LT_INFO, "Scroll y0 %8.2f y1 %8.2f dt %1.4f v %8.2f dy %8.2f frac %1.2f", smoothscroll_scrolled_distance.y,
  153. target_delta.y, dt, velocity.y, scroll_distance_integral.y, smoothscroll_accumulated_fractional_distance.y);
  154. #endif
  155. smoothscroll_scrolled_distance += scroll_distance_integral;
  156. PerformScrollOnTarget(scroll_distance_integral);
  157. if (HasSmoothscrollReachedTarget())
  158. Reset();
  159. }
  160. void ScrollController::UpdateInertia(float dt)
  161. {
  162. RMLUI_ASSERT(mode == Mode::Inertia && target);
  163. if (inertia_scroll_velocity.x == 0.0f && inertia_scroll_velocity.y == 0.0f)
  164. {
  165. Reset();
  166. return;
  167. }
  168. // Apply and dampen inertia.
  169. Vector2f scroll_delta = inertia_scroll_velocity * dt;
  170. PerformScrollOnTarget(scroll_delta);
  171. float dampening = 1.0f - INERTIA_FRICTION_FACTOR * dt;
  172. inertia_scroll_velocity *= dampening;
  173. if (Math::Absolute(inertia_scroll_velocity.x) < INERTIA_VELOCITY_CUTOFF)
  174. inertia_scroll_velocity.x = 0.0f;
  175. if (Math::Absolute(inertia_scroll_velocity.y) < INERTIA_VELOCITY_CUTOFF)
  176. inertia_scroll_velocity.y = 0.0f;
  177. }
  178. bool ScrollController::HasSmoothscrollReachedTarget() const
  179. {
  180. constexpr float epsilon = 0.1f;
  181. return (smoothscroll_target_distance - smoothscroll_scrolled_distance).SquaredMagnitude() < epsilon;
  182. }
  183. void ScrollController::PerformScrollOnTarget(Vector2f delta_distance)
  184. {
  185. RMLUI_ASSERT(target);
  186. auto overflow_is_scrollable = [](Style::Overflow overflow) { return overflow == Style::Overflow::Auto || overflow == Style::Overflow::Scroll; };
  187. auto& computed_values = target->GetComputedValues();
  188. if (delta_distance.x != 0.f && overflow_is_scrollable(computed_values.overflow_x()))
  189. target->SetScrollLeft(target->GetScrollLeft() + delta_distance.x);
  190. if (delta_distance.y != 0.f && overflow_is_scrollable(computed_values.overflow_y()))
  191. target->SetScrollTop(target->GetScrollTop() + delta_distance.y);
  192. }
  193. void ScrollController::IncrementSmoothscrollTarget(Vector2f delta_distance)
  194. {
  195. auto OppositeDirection = [](float a, float b) { return (a < 0.f && b > 0.f) || (a > 0.f && b < 0.f); };
  196. Vector2f delta = smoothscroll_target_distance - smoothscroll_scrolled_distance;
  197. // Reset movement state if we start scrolling in the opposite direction.
  198. for (int i = 0; i < 2; i++)
  199. {
  200. if (OppositeDirection(delta_distance[i], delta[i]))
  201. {
  202. smoothscroll_target_distance[i] = 0.f;
  203. smoothscroll_scrolled_distance[i] = 0.f;
  204. smoothscroll_accumulated_fractional_distance[i] = 0.f;
  205. }
  206. }
  207. // Clamp the delta distance to the scrollable area.
  208. const Vector2f scroll_offset = {target->GetScrollLeft(), target->GetScrollTop()};
  209. const Vector2f max_offset = {target->GetScrollWidth() - target->GetClientWidth(), target->GetScrollHeight() - target->GetClientHeight()};
  210. const Vector2f target_offset = scroll_offset + smoothscroll_target_distance - smoothscroll_scrolled_distance;
  211. const Vector2f clamped_delta = Math::Clamp(delta_distance + target_offset, Vector2f(0.f), max_offset) - target_offset;
  212. smoothscroll_target_distance += clamped_delta;
  213. }
  214. void ScrollController::Reset()
  215. {
  216. mode = Mode::None;
  217. target = nullptr;
  218. autoscroll_start_position = Vector2i{};
  219. autoscroll_accumulated_length = Vector2f{};
  220. autoscroll_moved = false;
  221. smoothscroll_target_distance = Vector2f{};
  222. smoothscroll_scrolled_distance = Vector2f{};
  223. smoothscroll_accumulated_fractional_distance = Vector2f{};
  224. // Keep smoothscroll configuration parameters.
  225. inertia_scroll_velocity = Vector2f{};
  226. }
  227. void ScrollController::SetDefaultScrollBehavior(ScrollBehavior scroll_behavior, float speed_factor)
  228. {
  229. smoothscroll_prefer_instant = (scroll_behavior == ScrollBehavior::Instant);
  230. smoothscroll_speed_factor = speed_factor;
  231. }
  232. String ScrollController::GetAutoscrollCursor(Vector2i mouse_position, float dp_ratio) const
  233. {
  234. RMLUI_ASSERT(mode == Mode::Autoscroll);
  235. const Vector2f scroll_delta = Vector2f(mouse_position - autoscroll_start_position);
  236. const Vector2f scroll_velocity = CalculateAutoscrollVelocity(scroll_delta, dp_ratio);
  237. if (scroll_velocity == Vector2f(0.f))
  238. return "rmlui-scroll-idle";
  239. String result = "rmlui-scroll";
  240. if (scroll_velocity.y > 0.f)
  241. result += "-up";
  242. else if (scroll_velocity.y < 0.f)
  243. result += "-down";
  244. if (scroll_velocity.x > 0.f)
  245. result += "-right";
  246. else if (scroll_velocity.x < 0.f)
  247. result += "-left";
  248. return result;
  249. }
  250. bool ScrollController::HasAutoscrollMoved() const
  251. {
  252. return mode == Mode::Autoscroll && autoscroll_moved;
  253. }
  254. float ScrollController::UpdateTime()
  255. {
  256. const double previous_tick = previous_update_time;
  257. previous_update_time = GetSystemInterface()->GetElapsedTime();
  258. const float dt = float(previous_update_time - previous_tick);
  259. return Math::Min(dt, DELTA_TIME_MAX);
  260. }
  261. } // namespace Rml