Skip to main content

trackball/
slide.rs

1use glam::Vec2;
2
3/// Slide induced by displacement on screen.
4///
5/// Implements [`Default`] and can be created with `Slide::default()`.
6///
7/// Both its methods must be invoked on matching events fired by your 3D graphics library of choice.
8#[derive(Debug, Clone, Default)]
9pub struct Slide {
10    /// Caches previous cursor/finger position.
11    pos: Option<Vec2>,
12}
13
14impl Slide {
15    /// Computes slide between previous and current cursor/finger position in screen space.
16    pub fn compute(&mut self, pos: Vec2) -> Option<Vec2> {
17        self.pos.replace(pos).map(|old| old - pos)
18    }
19    /// Discards cached previous cursor/finger position on button/finger release.
20    pub const fn discard(&mut self) {
21        self.pos = None;
22    }
23}