1use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
7#[serde(rename_all = "snake_case")]
8pub enum Button {
9 Primary,
10 Secondary,
11 Middle,
12 Extra1,
13 Extra2,
14}
15
16impl Button {
17 pub fn egui(self) -> egui::PointerButton {
18 match self {
19 Button::Primary => egui::PointerButton::Primary,
20 Button::Secondary => egui::PointerButton::Secondary,
21 Button::Middle => egui::PointerButton::Middle,
22 Button::Extra1 => egui::PointerButton::Extra1,
23 Button::Extra2 => egui::PointerButton::Extra2,
24 }
25 }
26 fn index(self) -> usize {
27 self as usize
28 }
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
32#[serde(rename_all = "snake_case")]
33pub enum WheelUnit {
34 Point,
35 Line,
36 Page,
37}
38
39#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
40#[serde(deny_unknown_fields)]
41pub struct Modifiers {
42 #[serde(default)]
43 pub ctrl: bool,
44 #[serde(default)]
45 pub shift: bool,
46 #[serde(default)]
47 pub alt: bool,
48 #[serde(default)]
50 pub command: bool,
51}
52
53impl Modifiers {
54 pub fn egui(self) -> egui::Modifiers {
56 self.egui_for(false)
57 }
58
59 pub fn egui_for(self, mac: bool) -> egui::Modifiers {
64 if mac {
65 egui::Modifiers {
66 alt: self.alt,
67 ctrl: self.ctrl,
68 shift: self.shift,
69 mac_cmd: self.command,
70 command: self.command,
71 }
72 } else {
73 egui::Modifiers {
74 alt: self.alt,
75 ctrl: self.ctrl || self.command,
76 shift: self.shift,
77 mac_cmd: self.command,
78 command: self.ctrl || self.command,
79 }
80 }
81 }
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
86#[serde(rename_all = "snake_case")]
87pub enum Platform {
88 Mac,
89 Windows,
90 Linux,
91}
92
93impl Platform {
94 pub fn os(self) -> egui::os::OperatingSystem {
95 match self {
96 Self::Mac => egui::os::OperatingSystem::Mac,
97 Self::Windows => egui::os::OperatingSystem::Windows,
98 Self::Linux => egui::os::OperatingSystem::Nix,
99 }
100 }
101}
102
103#[derive(Debug, Clone, Serialize, schemars::JsonSchema)]
105pub struct PointerState {
106 pub pos: Option<[f32; 2]>,
108 pub buttons: Vec<Button>,
109 pub modifiers: Modifiers,
110}
111
112pub const MULTI_CLICK_WINDOW: f64 = 0.3;
118
119pub const CLICK_DIST: f32 = 6.0;
122pub const CLICK_DURATION: f64 = 0.8;
124
125#[derive(Debug, Clone, Default)]
126pub struct Pointer {
127 pub pos: Option<egui::Pos2>,
128 pub buttons: [bool; 5],
129 pub modifiers: Modifiers,
130 pub platform: Option<Platform>,
134 pub modifiers_applied: bool,
137 pub now: f64,
141 pub reset_clicks: bool,
144 released: Option<f64>,
147 press: Option<(egui::Pos2, f64)>,
150 moved_far: bool,
151}
152
153const ALL_BUTTONS: [Button; 5] = [Button::Primary, Button::Secondary, Button::Middle, Button::Extra1, Button::Extra2];
154
155impl Pointer {
156 pub fn state(&self) -> PointerState {
157 PointerState {
158 pos: self.pos.map(|p| [p.x, p.y]),
159 buttons: ALL_BUTTONS.iter().copied().filter(|b| self.buttons[b.index()]).collect(),
160 modifiers: self.modifiers,
161 }
162 }
163
164 pub fn moved(&mut self, x: f32, y: f32, out: &mut Vec<egui::Event>) {
165 let pos = egui::pos2(x, y);
166 self.pos = Some(pos);
167 if let Some((origin, _)) = self.press {
168 self.moved_far |= origin.distance(pos) > CLICK_DIST;
169 }
170 out.push(egui::Event::PointerMoved(pos));
171 }
172
173 pub fn button(&mut self, button: Button, pressed: bool, out: &mut Vec<egui::Event>) -> Result<(), String> {
174 let pos = self.pos.ok_or("the pointer has no position yet: move it first")?;
175 self.buttons[button.index()] = pressed;
176 let now = self.now;
177 if pressed {
178 self.press = Some((pos, now));
179 self.moved_far = false;
180 } else if let Some((_, down)) = self.press.take() {
181 if !self.moved_far && now - down <= CLICK_DURATION {
182 self.released = Some(now);
183 }
184 }
185 out.push(egui::Event::PointerButton { pos, button: button.egui(), pressed, modifiers: self.egui_modifiers() });
186 Ok(())
187 }
188
189 pub fn click_gap(&mut self) -> bool {
209 let recent = self.released.is_some_and(|t| self.now - t < 2.0 * MULTI_CLICK_WINDOW);
210 if !recent || self.buttons.iter().any(|&b| b) {
211 return false;
212 }
213 self.released = None;
214 self.reset_clicks = true;
215 true
216 }
217
218 pub fn gone(&mut self, out: &mut Vec<egui::Event>) {
219 self.pos = None;
220 self.buttons = [false; 5];
221 out.push(egui::Event::PointerGone);
222 }
223
224 pub fn wheel(&mut self, dx: f32, dy: f32, unit: WheelUnit, out: &mut Vec<egui::Event>) {
225 let unit = match unit {
226 WheelUnit::Point => egui::MouseWheelUnit::Point,
227 WheelUnit::Line => egui::MouseWheelUnit::Line,
228 WheelUnit::Page => egui::MouseWheelUnit::Page,
229 };
230 out.push(egui::Event::MouseWheel {
231 unit,
232 delta: egui::vec2(dx, dy),
233 phase: egui::TouchPhase::Move,
234 modifiers: self.egui_modifiers(),
235 });
236 }
237
238 pub fn egui_modifiers(&self) -> egui::Modifiers {
240 self.modifiers.egui_for(self.platform == Some(Platform::Mac))
241 }
242
243 pub fn set_modifiers(&mut self, m: Modifiers) {
244 self.modifiers = m;
245 }
246
247 pub fn key(&mut self, name: &str, pressed: bool, repeat: bool, out: &mut Vec<egui::Event>) -> Result<(), String> {
248 let key = egui::Key::from_name(name).ok_or_else(|| format!("unknown key `{name}` (egui key names: A..Z, 0..9, Enter, Escape, Tab, Space, Backspace, Delete, ArrowLeft/Right/Up/Down, Home, End, PageUp/Down, F1..F12, Minus, Plus, …)"))?;
249 out.push(egui::Event::Key { key, physical_key: None, pressed, repeat, modifiers: self.egui_modifiers() });
250 Ok(())
251 }
252
253 pub fn text(&mut self, text: &str, out: &mut Vec<egui::Event>) {
254 if !text.is_empty() {
255 out.push(egui::Event::Text(text.to_string()));
256 }
257 }
258}