Skip to main content

glory_render/
plot_pointer.rs

1//! Finite queued pointer observations for explicitly declared plot viewports.
2use crate::{Error, Rect};
3
4/// Logical CSS-pixel admission bound, independent of physical device scale.
5pub const MAX_PLOT_POINTER_COORDINATE: f64 = 10_000_000.;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum PlotPointerDevice {
9    Mouse,
10    Pen,
11    Touch,
12}
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum PlotPointerPhase {
16    Move,
17    Down,
18    Up,
19    Leave,
20    Cancel,
21}
22
23#[derive(Clone, Copy, Debug, PartialEq)]
24pub struct PlotPointer {
25    pub pointer_id: i32,
26    pub device: PlotPointerDevice,
27    pub phase: PlotPointerPhase,
28    /// Fractional local border-box coordinates. A captured terminal point can
29    /// lie outside the viewport; no clamping, rounding or DPR multiplication.
30    pub x: f64,
31    pub y: f64,
32    /// The same observation's fractional border-box extent, with x=y=0.
33    /// Consumers map this extent to their accepted logical drawing viewport.
34    pub viewport: Rect,
35}
36
37impl PlotPointer {
38    pub fn validate(self) -> Result<(), Error> {
39        let terminal = matches!(
40            self.phase,
41            PlotPointerPhase::Leave | PlotPointerPhase::Cancel
42        );
43        let bounded = |value: f64| value.is_finite() && value.abs() <= MAX_PLOT_POINTER_COORDINATE;
44        if !bounded(self.x)
45            || !bounded(self.y)
46            || self.viewport.x != 0.
47            || self.viewport.y != 0.
48            || !bounded(self.viewport.width)
49            || !bounded(self.viewport.height)
50            || self.viewport.width < 0.
51            || self.viewport.height < 0.
52            || (!terminal && (self.viewport.width == 0. || self.viewport.height == 0.))
53        {
54            return Err(Error::InvalidEvent("invalid local plot pointer geometry"));
55        }
56        Ok(())
57    }
58}