ScummVM API documentation
FluidScroll.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Based on the implementation by fluid-scroll repository
24  * at https://github.com/ktiays/fluid-scroll
25  */
26 
27 #ifndef GUI_ANIMATION_FLUID_SCROLL_H
28 #define GUI_ANIMATION_FLUID_SCROLL_H
29 
30 #include "common/scummsys.h"
31 
32 namespace GUI {
33 
35 public:
36  FluidScroller();
37  ~FluidScroller() {}
38 
45  void setBounds(float maxScroll, int viewportHeight, float stepSize);
46 
47  void reset();
48 
49  // Reset the animation state (fling/spring-back), keeping current position
50  void stopAnimation();
51 
57  void feedDrag(uint32 time, int deltaY);
58 
59  // Start a fling using the recorded velocity
60  void startFling();
61 
62  // Start a fling with a specific initial velocity
63  void startFling(float velocity);
64 
65  // Record a wheel tick and start/update a fling
66  void feedWheel(uint32 time, float deltaY);
67 
73  void handleMouseWheel(int direction, float multiplier = 1.0f);
74 
75  // Check if there is an active animation (fling or spring-back)
76  bool isAnimating() const { return _mode != kModeNone; }
77 
84  bool update(uint32 time, float &outVisualPos);
85 
86  float setPosition(float pos, bool checkBound = false);
87 
88  // Get the current visual scroll position
89  float getVisualPosition() const;
90 
91  // Trigger an elastic spring-back if the current position is out of bounds
92  void checkBoundaries();
93 
94 private:
95  enum Mode {
96  kModeNone,
97  kModeFling,
98  kModeSpringBack
99  };
100 
101  // Velocity tracking
102  struct VelocityTracker {
103  struct Point {
104  uint32 time;
105  float position;
106  };
107  static const int kHistorySize = 20;
108  Point samples[kHistorySize];
109  int index;
110  int count;
111 
112  VelocityTracker();
113  void reset();
114  void addPoint(uint32 time, float position);
115  float calculateVelocity() const;
116  };
117 
118  VelocityTracker _velocityTracker;
119  uint32 _lastWheelTime;
120 
121  Mode _mode;
122  uint32 _startTime;
123 
124  // Scroll status
125  float _scrollPosRaw; // Physical position (can go out of bounds)
126  float _animationOffset; // Anchor position used as the starting point for animation offsets
127  float _maxScroll;
128  float _stepSize;
129  int _viewportHeight;
130 
131  // Fling parameter
132  float _initialVelocity;
133 
134  // Spring parameters
135  float _lambda; // Spring stiffness factor
136  float _stretchDistance; // Initial distance beyond the edge when spring-back begins
137  float _impactVelocity; // Velocity when hitting the boundary
138 
139 
140  float getVelocityAt(float timeInSeconds) const;
141 
142  // Transition from movement to spring-back animation when hitting an edge
143  void absorb(float velocity, float distance);
144 
145  // Returns the visual offset to apply when scrolled past an edge
146  static float calculateRubberBandOffset(float offset, float range);
147 
148  static const float kRubberBandStretchFraction; // Maximum stretch limit as a fraction of viewport height
149  static const float kDecelerationRate; // Rate at which fling velocity slows down
150  static const float kVelocityThreshold; // Minimum velocity to keep animation running
151  static const float kValueThreshold; // Minimum value difference to keep spring active
152  static const float kDefaultSpringResponse; // Natural response time of the spring
153  static const float kRubberBandCoefficient; // Coefficient for rubber-band stiffness
154 };
155 
156 } // End of namespace GUI
157 
158 #endif
bool update(uint32 time, float &outVisualPos)
Definition: FluidScroll.h:34
Definition: printman.h:30
Definition: FluidScroll.h:103
void feedDrag(uint32 time, int deltaY)
void setBounds(float maxScroll, int viewportHeight, float stepSize)
void handleMouseWheel(int direction, float multiplier=1.0f)