ScrollController.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include "../../Include/RmlUi/Core/Header.h"
  3. #include "../../Include/RmlUi/Core/ScrollTypes.h"
  4. #include "../../Include/RmlUi/Core/Types.h"
  5. namespace Rml {
  6. /**
  7. Implements scrolling behavior that occurs over time.
  8. Scrolling modes are activated externally, targeting a given element. The actual scrolling takes place during update calls.
  9. */
  10. class ScrollController {
  11. public:
  12. enum class Mode {
  13. None,
  14. Smoothscroll, // Smooth scrolling to target distance.
  15. Autoscroll, // Scrolling with middle mouse button.
  16. Inertia, // Applying scrolling inertia when using swipe gesture
  17. };
  18. void ActivateAutoscroll(Element* target, Vector2i start_position);
  19. void ActivateSmoothscroll(Element* target, Vector2f delta_distance, ScrollBehavior scroll_behavior);
  20. void ActivateInertia(Element* target, Vector2f velocity);
  21. void InstantScrollOnTarget(Element* target, Vector2f delta_distance);
  22. bool Update(Vector2i mouse_position, float dp_ratio);
  23. void IncrementSmoothscrollTarget(Vector2f delta_distance);
  24. // Resets any active mode and its state.
  25. void Reset();
  26. // Sets the scroll behavior for mouse wheel processing and scrollbar interaction.
  27. void SetDefaultScrollBehavior(ScrollBehavior scroll_behavior, float speed_factor);
  28. // Returns the autoscroll cursor based on the active scroll velocity.
  29. String GetAutoscrollCursor(Vector2i mouse_position, float dp_ratio) const;
  30. // Returns true if autoscroll mode is active and the cursor has been moved outside the idle scroll area.
  31. bool HasAutoscrollMoved() const;
  32. Mode GetMode() const { return mode; }
  33. Element* GetTarget() const { return target; }
  34. private:
  35. // Updates time to now, and returns the delta time since the previous time update.
  36. float UpdateTime();
  37. void UpdateAutoscroll(float dt, Vector2i mouse_position, float dp_ratio);
  38. void UpdateSmoothscroll(float dt, float dp_ratio);
  39. void UpdateInertia(float dt);
  40. bool HasSmoothscrollReachedTarget() const;
  41. void PerformScrollOnTarget(Vector2f delta_distance);
  42. Mode mode = Mode::None;
  43. Element* target = nullptr;
  44. double previous_update_time = 0;
  45. Vector2i autoscroll_start_position;
  46. Vector2f autoscroll_accumulated_length;
  47. bool autoscroll_moved = false;
  48. bool smoothscroll_prefer_instant = false;
  49. float smoothscroll_speed_factor = 1.f;
  50. Vector2f smoothscroll_target_distance;
  51. Vector2f smoothscroll_scrolled_distance;
  52. Vector2f smoothscroll_accumulated_fractional_distance;
  53. Vector2f inertia_scroll_velocity;
  54. };
  55. } // namespace Rml