Skip to main content

blit/
input.rs

1use alloc::string::String;
2
3use crate::Point;
4
5#[derive(Clone, Debug, Default, PartialEq)]
6pub enum Input<T> {
7    #[default]
8    None,
9    MouseDown {
10        position: Point<T>,
11        button: MouseButton,
12        modifiers: Modifiers,
13    },
14    MouseUp {
15        position: Point<T>,
16        button: MouseButton,
17        modifiers: Modifiers,
18        leave: bool,
19    },
20    MouseMove {
21        position: Point<T>,
22        modifiers: Modifiers,
23    },
24    MouseLeave,
25    Touch {
26        id: u64,
27        position: Point<T>,
28        phase: TouchPhase,
29    },
30    /// cancels all mouse and touch sequences
31    Cancel,
32    Scroll {
33        position: Point<T>,
34        delta: Point<f32>,
35        modifiers: Modifiers,
36        continuous: bool,
37        phase: ScrollPhase,
38    },
39    Text(String),
40    Key(KeyInput),
41}
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub enum TouchPhase {
45    Down,
46    Move,
47    Up,
48    /// aborts this contact and any gesture that depends on it
49    Cancel,
50}
51
52#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
53pub enum ScrollPhase {
54    Started,
55    #[default]
56    Moved,
57    Ended,
58    Cancelled,
59}
60
61#[derive(Clone, Copy, Debug, PartialEq, Eq)]
62pub struct KeyInput {
63    pub key: Key,
64    pub modifiers: Modifiers,
65    pub pressed: bool,
66    pub repeat: bool,
67}
68
69impl KeyInput {
70    pub const fn new(key: Key) -> Self {
71        Self {
72            key,
73            modifiers: Modifiers::NONE,
74            pressed: true,
75            repeat: false,
76        }
77    }
78}
79
80#[derive(Clone, Copy, Debug, PartialEq, Eq)]
81pub enum Key {
82    Character(char),
83    Backspace,
84    Delete,
85    ArrowLeft,
86    ArrowRight,
87    ArrowUp,
88    ArrowDown,
89    Enter,
90    Tab,
91    Escape,
92    Home,
93    End,
94    PageUp,
95    PageDown,
96    Insert,
97    Function(u8),
98}
99
100#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
101pub struct Modifiers(u8);
102
103impl Modifiers {
104    pub const ALT: Self = Self(1 << 2);
105    pub const CONTROL: Self = Self(1 << 1);
106    pub const NONE: Self = Self(0);
107    pub const SHIFT: Self = Self(1 << 0);
108    pub const SUPER: Self = Self(1 << 3);
109
110    pub const fn new(shift: bool, control: bool, alt: bool, super_key: bool) -> Self {
111        let mut bits = 0;
112        if shift {
113            bits |= Self::SHIFT.0;
114        }
115        if control {
116            bits |= Self::CONTROL.0;
117        }
118        if alt {
119            bits |= Self::ALT.0;
120        }
121        if super_key {
122            bits |= Self::SUPER.0;
123        }
124        Self(bits)
125    }
126
127    pub const fn contains(self, modifiers: Self) -> bool {
128        self.0 & modifiers.0 == modifiers.0
129    }
130
131    pub const fn shift(self) -> bool {
132        self.contains(Self::SHIFT)
133    }
134
135    pub const fn control(self) -> bool {
136        self.contains(Self::CONTROL)
137    }
138
139    pub const fn alt(self) -> bool {
140        self.contains(Self::ALT)
141    }
142
143    pub const fn super_key(self) -> bool {
144        self.contains(Self::SUPER)
145    }
146}
147
148#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
149pub enum MouseButton {
150    #[default]
151    Primary,
152    Secondary,
153    Middle,
154    Back,
155    Forward,
156    Other(u16),
157}