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
#![feature(map_first_last)] #![feature(half_open_range_patterns)] /*! * This library serves as an event interpretation library. * To use, you will need to take the raw events you recieve * on your platform and adapt it to a compatible input * interface. You are expected to call get_pan() once on every frame. * It expects an estimation of the next frametime as well as how * long until the current frame will be rendered. This allows overshoot * calculation to take place. */ extern crate num; use std::f64; mod circular_backqueue; mod interpolate; mod ranged_map; use std::ops; use interpolate::Interpolator; type Timestamp = u64; //#[macro_use] //extern crate smart_default; // configs //// Determines how much "smoothing" happens, at a direct cost to responsiveness to an action //// A large number means more past events will be counted in the current velocity, //// which avoids skipping over or being "hitched" by anomalies, but also means //// that changes to velocity after the initial touch are responded to slower //const MAX_EVENTS_CONSIDERED: u32 = 5; //// Determines whether the prior config (MAX_EVENTS_CONSIDERED) is used. //// If false, no smoothing occurs and the velocity is simply the most recent event //// Equivalent to setting MAX_EVENTS_CONSIDERED to 1, but allows a performance shortcut //const ENABLE_VELOCITY_SMOOTHING: bool = true; //const FLING_FRICTION_FACTOR: f64 = 0.998; //const PAN_ACCELERATION_FACTOR_TOUCHPAD: f64 = 1.34; //// Used to specify over what window (in number of frames) the ratio of input events to frames //// should be derived. The ratio is then used to interpolate/extrapolate input events const SAMPLE_OVER_X_FRAMES: usize = 10; //// The granularity through which displacement is integrated from the velocity function (sampled //// with velocity_at(f64)) //const INTEGRATION_DX: f64 = 0.07; //// Degree of polynomial used to interpolate velocity //const VELOCITY_POLYNOMIAL_DEGREE: usize = 4; //type Millis = f64; /// Represents a single scrollview and tracks all state related to it. //#[derive(Default)] pub struct Scrollview { content_height: u64, content_width: u64, viewport_height: u64, viewport_width: u64, current_source: Source, //frametime: Millis, //time_to_pageflip: Millis, //current_timestamp: u64, //interpolation_ratio: f64, input_per_frame_log: circular_backqueue::ForgetfulLogQueue<u32>, x: Interpolator, y: Interpolator, } /// Describes a vector in terms of its 2 2d axis magnitudes, /// used often to describe transforms and offsets #[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> { pub fn scale(&self, scalar: f64) -> AxisVector<f64> { AxisVector { x: self.x * scalar, y: self.y * scalar, ..self.clone() } } } // TODO: consider naming, doing pythagorean add on + may make more sense, with alternative op to // simply add elems 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, } /// Pass along with any events to indicate what kind of device the event came from #[derive(Copy)] #[derive(Clone)] pub enum Source { /// Device type is unknown, assume nothing (very suboptimal to actually use this, should only /// be used when device type can not be feasibly deduced, and even then may not be the best /// choice) Undefined, /// Device is a touchscreen, hint to avoid acceleration, but perform tracking prediction Touchscreen, /// Device is a touchpad, hint to accelerate input, and perform tracking prediction Touchpad, /// Device is a mousewheel that reports deltas of around 15 degrees (coarse) and requires /// smoothing and mid-delta animation Mousewheel, /// Device is a mousewheel that reports deltas of less than 15 degrees (usually much less, /// indicating that very little/no smoothing needs to be applied) PreciseMousewheel, /// Do no manual smoothing or acceleration, assume driver already does this or input method /// would be strange to use with either Passthrough, /// Same as passthrough, but input fling events should trigger a kinetic fling animation KineticPassthrough, /// The device type last used Previous, } impl Default for Source { fn default() -> Self { Source::Undefined } } // pub interface impl Scrollview { /// Gives the current best estimate for the position of the content relative to /// the viewport in device pixels pub fn sample(&mut self, timestamp: Timestamp) -> AxisVector<f64> { AxisVector { x: self.x.sample(timestamp), y: self.y.sample(timestamp), ..Default::default() } } /// Create a new scrollview with default settings /// /// Warning: these settings are unlikely to be /// particularly useful, so set_geometry(), set_avg_frametime(), and any /// other relevant initialization functions still need to be used pub fn new() -> Scrollview { Scrollview { input_per_frame_log: circular_backqueue::ForgetfulLogQueue::new(SAMPLE_OVER_X_FRAMES), content_height: 0, content_width: 0, viewport_height: 0, viewport_width: 0, current_source: Source::Undefined, //frametime: 0.0, //time_to_pageflip: 0.0, //current_timestamp: 0, //interpolation_ratio: 0.0, x: Interpolator::new(false, (0.0, 0.0), 0.0), y: Interpolator::new(false, (0.0, 0.0), 0.0), } } /// Deletes/deinitializes the current scrollview /// /// Primarily intended for ffi use, Scrollview implements Drop /// where deinitialization is required, so this is only useful /// for ffi use /// /// NOTE: likely will be removed, not sure why I put this in here to begin with pub fn del(_: Scrollview) {} /// Set the geometry for the given scrollview /// /// Can be used both on scrollview initialization and on scrollview resize 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; self.x.set_geometry(0.0, (content_width - viewport_width) as f64); self.x.set_geometry(0.0, (content_height - viewport_height) as f64); } /// True if scrollview should continue to be polled /// even in absence of events (fling or other /// animation in progress) pub fn animating(&self) -> bool { //self.current_velocity.decay_active() self.x.animating() || self.y.animating() } /// Enqueue a pan event for the referenced scrollview pub fn push_pan(&mut self, axis: Axis, amount: f64, timestamp: Option<u64>) { match axis { Axis::Horizontal => self.x.signal_pan(timestamp.unwrap(), amount), Axis::Vertical => self.y.signal_pan(timestamp.unwrap(), amount), } } /// Enqueue a fling (finger lift at any velocity) for the referenced scrollview pub fn push_fling(&mut self, timestamp: Option<u64>) { //self.current_velocity.decay_start(); self.x.signal_fling(timestamp.unwrap()); self.y.signal_fling(timestamp.unwrap()); } /// Enqueue a scroll interrupt (finger down at any time, gesture start) for the referenced /// scrollview pub fn push_interrupt(&mut self, timestamp: Option<u64>) { //self.pan_log_x.clear(); //self.pan_log_y.clear(); //self.current_velocity = AxisVector { x: 0.0, y: 0.0, ..self.current_velocity }; self.x.signal_interrupt(timestamp.unwrap()); self.y.signal_interrupt(timestamp.unwrap()); } /// Set what device type is going to be providing any events that follow until the next source /// is declared pub fn set_source(&mut self, source: Source) { self.current_source = source; } }