QmlMaterial 0.1.0
Loading...
Searching...
No Matches
scroll_motion.hpp
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5
6namespace qml_material
7{
8
9class ScrollMotion {
10public:
11 enum class Mode
12 {
13 Idle,
14 Fling,
15 Target,
16 Wheel
17 };
18 struct Sample {
19 double position;
20 double velocity;
21 bool finished;
22 };
23
24 Mode mode() const { return m_mode; }
25 bool active() const { return m_mode != Mode::Idle; }
26 double target() const { return m_target; }
27 unsigned long long revision() const { return m_revision; }
28
29 void stop() {
30 ++m_revision;
31 m_mode = Mode::Idle;
32 }
33
34 void fling(double position, double velocity, double deceleration, double now) {
35 ++m_revision;
36 m_mode = Mode::Fling;
37 m_start = position;
38 m_velocity = velocity;
39 m_rate = std::max(1.0, deceleration);
40 m_time = now;
41 m_target = position + velocity * std::abs(velocity) / (2 * m_rate);
42 }
43
44 void smooth(double position, double velocity, double target, double now) {
45 ++m_revision;
46 m_mode = Mode::Target;
47 m_start = position;
48 m_velocity = velocity;
49 m_target = target;
50 m_time = now;
51 m_rate = 18;
52 }
53
54 void wheel(double position, double target, double now) {
55 const auto previous = m_mode == Mode::Wheel ? sample(now) : Sample { position, 0, true };
56 if (m_mode == Mode::Wheel && target == m_target) return;
57 ++m_revision;
58 m_mode = Mode::Wheel;
59 m_start = previous.position;
60 m_velocity = previous.velocity;
61 m_target = target;
62 m_time = now;
63 const double distance = target - m_start;
64 // Chromium's inverse-distance duration; bound the initial slope to keep the curve
65 // monotonic.
66 m_duration = std::clamp((840.0 - std::abs(distance)) / 3600.0, 0.1, 0.2);
67 if (distance * m_velocity > 0)
68 m_duration = std::min(m_duration, distance / (0.42 * m_velocity));
69 if (std::abs(distance) < 0.01) m_duration = 0;
70 m_slope =
71 m_duration > 0 ? std::clamp(m_velocity * m_duration / distance, -1000.0, 1000.0) : 0;
72 }
73
74 void translate(double delta) {
75 ++m_revision;
76 m_start += delta;
77 m_target += delta;
78 }
79
80 Sample sample(double now) const {
81 const double elapsed = std::max(0.0, now - m_time);
82 if (m_mode == Mode::Idle) return { m_start, 0, true };
83 if (m_mode == Mode::Fling) {
84 const double duration = std::abs(m_velocity) / m_rate;
85 if (elapsed >= duration) return { m_target, 0, true };
86 const double acceleration = -std::copysign(m_rate, m_velocity);
87 return { m_start + m_velocity * elapsed + acceleration * elapsed * elapsed / 2,
88 m_velocity + acceleration * elapsed,
89 false };
90 }
91 if (m_mode == Mode::Wheel) {
92 if (elapsed >= m_duration) return { m_target, 0, true };
93 if (elapsed == 0) return { m_start, m_velocity, false };
94 const double progress = elapsed / m_duration;
95 // Solve the monotonic time axis of cubic-bezier(0.42, slope * 0.42, 0.58, 1).
96 double low = 0, high = 1;
97 for (int i = 0; i < 40; ++i) {
98 const double t = (low + high) / 2;
99 if (bezier(t, 0.42, 0.58) < progress)
100 low = t;
101 else
102 high = t;
103 }
104 const double t = (low + high) / 2;
105 const double distance = m_target - m_start;
106 return { m_start + distance * bezier(t, m_slope * 0.42, 1),
107 distance / m_duration * derivative(t, m_slope * 0.42, 1) /
108 derivative(t, 0.42, 0.58),
109 false };
110 }
111 const double distance = m_start - m_target;
112 const double b = m_velocity + m_rate * distance;
113 const double decay = std::exp(-m_rate * elapsed);
114 const double offset = (distance + b * elapsed) * decay;
115 const double velocity = (b - m_rate * (distance + b * elapsed)) * decay;
116 if (std::abs(offset) < 0.01 && std::abs(velocity) < 0.1) return { m_target, 0, true };
117 return { m_target + offset, velocity, false };
118 }
119
120private:
121 static double bezier(double t, double a, double b) {
122 const double u = 1 - t;
123 return 3 * u * u * t * a + 3 * u * t * t * b + t * t * t;
124 }
125 static double derivative(double t, double a, double b) {
126 const double u = 1 - t;
127 return 3 * u * u * a + 6 * u * t * (b - a) + 3 * t * t * (1 - b);
128 }
129 unsigned long long m_revision { 0 };
130 Mode m_mode { Mode::Idle };
131 double m_start { 0 };
132 double m_velocity { 0 };
133 double m_target { 0 };
134 double m_time { 0 };
135 double m_rate { 1 };
136 double m_duration { 0 };
137 double m_slope { 0 };
138};
139
140} // namespace qml_material