1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
extern crate num;
use std::f64;
mod circular_backqueue;
use std::ops;
const MAX_EVENTS_CONSIDERED: u32 = 5;
const ENABLE_VELOCITY_SMOOTHING: bool = true;
const FLING_FRICTION_FACTOR: f64 = 0.998;
const f: f64 = PAN_ACCELERATION_FACTOR;
const PAN_ACCELERATION_FACTOR: f64 = 1.34;
const SAMPLE_OVER_X_FRAMES: usize = 10;
type Millis = f64;
#[derive(Default)]
pub struct Scrollview {
content_height: u64,
content_width: u64,
viewport_height: u64,
viewport_width: u64,
current_velocity: AxisVector<f64>,
current_position: AxisVector<f64>,
frametime: Millis,
time_to_pageflip: Millis,
current_timestamp: u64,
interpolation_ratio: f64,
input_per_frame_log: circular_backqueue::ForgetfulLogQueue<u32>,
pan_log_x: circular_backqueue::ForgetfulLogQueue<(u64, f64)>,
pan_log_y: circular_backqueue::ForgetfulLogQueue<(u64, f64)>,
}
#[derive(Copy)]
#[derive(Clone)]
#[derive(Default)]
pub struct AxisVector<T> where T: num::Num, T: PartialOrd, T: Copy {
pub x: T,
pub y: T,
x_threshold: T,
y_threshold: T,
decaying: bool,
}
impl<T> AxisVector<T> where T: num::Num, T: PartialOrd, T: Copy {
fn difference(self, other: AxisVector<T>) -> AxisVector<T> {
AxisVector {
x: self.x - other.x,
y: self.y - other.y,
..self
}
}
fn replace(&mut self, axis: Axis, magnitude: T) {
match axis {
Axis::Horizontal => self.x = magnitude,
Axis::Vertical => self.y = magnitude,
}
}
fn get_at(&self, axis: Axis) -> T {
match axis {
Axis::Horizontal => self.x,
Axis::Vertical => self.y
}
}
fn update(&mut self, axis: Axis, magnitude: T) {
match axis {
Axis::Horizontal => self.x = magnitude + self.x,
Axis::Vertical => self.y = magnitude + self.y,
}
}
}
impl AxisVector<f64> {
fn decay_active(&self) -> bool {
self.decaying && self.x > self.x_threshold && self.y > self.y_threshold
}
fn decay_start(&mut self) {
self.decaying = true;
}
fn step_frame(&mut self) {
if self.decay_active() {
self.x = Scrollview::fling_decay(self.x);
self.y = Scrollview::fling_decay(self.y);
}
if self.x < self.x_threshold && self.y < self.y_threshold {
self.decaying = false;
}
}
}
impl<T> ops::Add<AxisVector<T>> for AxisVector<T> where T: num::Num, T: PartialOrd, T: Copy {
type Output = AxisVector<T>;
fn add(self, rhs: AxisVector<T>) -> AxisVector<T> {
AxisVector {
x: self.x + rhs.x,
y: self.y + rhs.y,
..self
}
}
}
#[derive(Copy)]
#[derive(Clone)]
pub enum Axis {
Horizontal,
Vertical,
}
impl Scrollview {
pub fn new() -> Scrollview {
Scrollview {
input_per_frame_log: circular_backqueue::ForgetfulLogQueue::new(SAMPLE_OVER_X_FRAMES),
..Default::default()
}
}
pub fn del(_: Scrollview) {}
pub fn set_geometry(
&mut self,
content_height: u64,
content_width: u64,
viewport_height: u64,
viewport_width: u64,
) {
self.content_height = content_height;
self.content_width = content_width;
self.viewport_height = viewport_height;
self.viewport_width = viewport_width;
}
pub fn animating(&self) -> bool {
self.current_velocity.decay_active()
}
pub fn step_frame(&mut self, timestamp: Option<u64>) {
self.interpolation_ratio = self.input_per_frame_log.all().iter().sum::<u32>() as f64 / self.input_per_frame_log.size() as f64;
self.current_timestamp = timestamp.unwrap_or(1);
self.current_velocity.step_frame();
self.update_velocity();
self.current_position.x += Self::accelerate(self.current_velocity.x) * self.interpolation_ratio * self.frametime;
self.current_position.y += Self::accelerate(self.current_velocity.y) * self.interpolation_ratio * self.frametime;
self.input_per_frame_log.push(0);
}
pub fn set_avg_frametime(&mut self, milliseconds: f64) {
self.frametime = milliseconds;
}
pub fn set_next_frame_predict(&mut self, milliseconds: f64) {
self.time_to_pageflip = milliseconds;
}
pub fn get_position_absolute(&self) -> AxisVector<f64> {
self.current_position + self.get_overshoot()
}
}
impl Scrollview {
pub fn push_pan(&mut self, timestamp: Option<u64>, axis: Axis, amount: f64) {
match axis {
Axis::Horizontal => self.pan_log_x.push((timestamp.unwrap_or(self.current_timestamp), amount)),
Axis::Vertical => self.pan_log_y.push((timestamp.unwrap_or(self.current_timestamp), amount)),
}
}
pub fn push_fling(&mut self) {
self.current_velocity.decay_start();
}
pub fn push_interrupt(&mut self) {
self.pan_log_x.clear();
self.pan_log_y.clear();
self.current_velocity = AxisVector { x: 0.0, y: 0.0, ..self.current_velocity };
}
fn get_overshoot(&self) -> AxisVector<f64> {
let time_to_target = (self.frametime / 2.0) + self.time_to_pageflip;
AxisVector {
x: self.current_velocity.x * time_to_target,
y: self.current_velocity.y * time_to_target,
decaying: false,
..Default::default()
}
}
fn update_velocity(&mut self) {
if ENABLE_VELOCITY_SMOOTHING == false {
self.current_velocity = AxisVector {
x: self.pan_log_x.get_or_avg(0).1,
y: self.pan_log_y.get_or_avg(0).1,
..self.current_velocity
}
} else {
let mut sum_x = 0.0;
let mut sum_y = 0.0;
let mut weight_x = 0.0;
let mut weight_y = 0.0;
let axes = vec![(&self.pan_log_x, &mut sum_x, &mut weight_x), (&self.pan_log_y, &mut sum_y, &mut weight_y)];
for (log, sum, weight) in axes {
for i in 0..(MAX_EVENTS_CONSIDERED - 1) {
match log.get(i as usize) {
None => (),
Some((timestamp, magnitude)) => {
let staleness = self.current_timestamp - timestamp;
let staleness_mult_factor = 1.0 / (staleness as f64);
*weight += staleness_mult_factor;
*sum += magnitude * staleness_mult_factor;
}
}
}
}
let avg_x = sum_x / weight_x;
let avg_y = sum_y / weight_y;
self.current_velocity = AxisVector {
x: avg_x,
y: avg_y,
..self.current_velocity
}
}
}
fn accelerate(from: f64) -> f64 {
from.powf(PAN_ACCELERATION_FACTOR)
}
fn fling_decay(from: f64) -> f64 {
from.powf(FLING_FRICTION_FACTOR)
}
}