use crate::coordinate_space::Pixels;
use crate::geometry::{Coordinates, PointerOrientation};
use enumset::{EnumSet, EnumSetType};
use euclid::Size2D;
use typed_builder::TypedBuilder;
#[derive(TypedBuilder, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PointerProperties {
coordinates: Coordinates,
#[builder(setter(into))]
held_buttons: PointerButtonSet,
id: PointerId,
#[builder(default=Size2D::new(1., 1.))]
dimensions: Size2D<f64, Pixels>,
#[builder(default = 0.5)]
pressure: f64,
#[builder(default = 0.)]
tangential_pressure: f64,
#[builder(default)]
orientation: PointerOrientation,
pointer_type: PointerType,
is_primary: bool,
}
impl PointerProperties {
pub fn coordinates(&self) -> &Coordinates {
&self.coordinates
}
pub fn held_buttons(&self) -> PointerButtonSet {
self.held_buttons
}
pub fn id(&self) -> PointerId {
self.id
}
pub fn dimensions(&self) -> Size2D<f64, Pixels> {
self.dimensions
}
pub fn pressure(&self) -> f64 {
self.pressure
}
pub fn tangential_pressure(&self) -> f64 {
self.tangential_pressure
}
pub fn orientation(&self) -> &PointerOrientation {
&self.orientation
}
pub fn pointer_type(&self) -> PointerType {
self.pointer_type
}
pub fn is_primary(&self) -> bool {
self.is_primary
}
}
#[cfg(test)]
mod pointer_state {
use super::*;
use crate::{ClientPoint, ElementPoint, PagePoint, ScreenPoint};
use assert2::assert;
#[test]
fn minimal_builder() {
let c = Coordinates::new(
ScreenPoint::new(1., 1.),
PagePoint::new(2., 2.),
ClientPoint::new(3., 3.),
ElementPoint::new(4., 4.),
);
let state: PointerProperties = PointerProperties::builder()
.coordinates(c.clone())
.held_buttons(PointerButtonSet::empty())
.id(PointerId(0))
.pointer_type(PointerType::Mouse)
.is_primary(true)
.build();
assert!(state.coordinates() == &c);
assert!(state.held_buttons() == PointerButtonSet::empty());
assert!(state.id() == PointerId(0));
assert!(state.dimensions() == Size2D::new(1., 1.));
assert!(state.pressure() == 0.5);
assert!(state.tangential_pressure() == 0.);
assert!(state.orientation() == &PointerOrientation::default());
assert!(state.pointer_type() == PointerType::Mouse);
assert!(state.is_primary() == true);
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PointerType {
Mouse,
Pen,
Touch,
Unknown,
}
impl Default for PointerType {
fn default() -> Self {
PointerType::Mouse
}
}
#[derive(EnumSetType, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum PointerButton {
Primary,
Secondary,
Auxiliary,
Fourth,
Fifth,
Eraser,
Unknown,
}
pub type PointerButtonSet = EnumSet<PointerButton>;
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PointerId(pub i64);