1use std::cell::RefCell;
2use std::collections::{HashMap, HashSet};
3use std::rc::Rc;
4
5use glam::Vec2;
6use repame_input::{AimTracker, GamepadState, MouseState, StagedClick, TouchContact};
7use repose_core::input::{Key, KeyEvent, KeyEventType, PhysicalKey, PointerButton};
8use repose_core::runtime::Scheduler;
9
10#[derive(Default)]
11pub struct Staging {
12 pub held: HashSet<PhysicalKey>,
13 pub edges: Vec<PhysicalKey>,
14 pub window_focused: bool,
15 pub capture_armed: bool,
16 pub clicks: Vec<StagedClick>,
17 pub aim: AimTracker,
18 pub mouse_edges: Vec<(PointerButton, bool)>,
19 pub lmb_held: bool,
20 pub rmb_held: bool,
21 pub rmb_down_edge: bool,
22 pub capture_pending_physical: Option<PhysicalKey>,
23 pub capture_pending_key: Option<Key>,
24 pub capture_pending_mouse: Option<bool>,
25 pub touch_active: HashMap<u64, (Vec2, Vec2)>,
26 pub touch_new: HashSet<u64>,
27 pub pads: Vec<GamepadState>,
28 pub pad_live: bool,
29 pub view_width: f32,
30}
31
32impl Staging {
33 pub fn shared() -> Rc<RefCell<Self>> {
34 Rc::new(RefCell::new(Self {
35 window_focused: true,
36 view_width: 1280.0,
37 ..Default::default()
38 }))
39 }
40
41 pub fn handle_key(&mut self, ke: &KeyEvent) {
42 let down = matches!(ke.event_type, KeyEventType::Down);
43 if down && !ke.is_repeat {
44 match &ke.key {
45 Key::Backspace => {
46 self.capture_pending_key = Some(Key::Backspace);
47 return;
48 }
49 _ => {}
50 }
51 if self.capture_armed {
52 if let Some(key) = ke.physical {
53 self.capture_pending_physical = Some(key);
54 } else {
55 self.capture_pending_key = Some(ke.key.clone());
56 }
57 return;
58 }
59 if matches!(ke.key, Key::Escape | Key::Enter) {
60 return;
61 }
62 }
63 match &ke.key {
64 Key::Escape | Key::Enter => {}
65 Key::Character(_) => {
66 if let Some(key) = ke.physical {
67 self.stage_physical(key, down, ke.is_repeat);
68 }
69 }
70 Key::ShiftLeft => self.stage_code(PhysicalKey::ShiftLeft, down, ke.is_repeat),
71 Key::ShiftRight => self.stage_code(PhysicalKey::ShiftRight, down, ke.is_repeat),
72 Key::Space => self.stage_code(PhysicalKey::Space, down, ke.is_repeat),
73 Key::Tab => self.stage_code(PhysicalKey::Tab, down, ke.is_repeat),
74 Key::ArrowUp => self.stage_code(PhysicalKey::ArrowUp, down, ke.is_repeat),
75 Key::ArrowDown => self.stage_code(PhysicalKey::ArrowDown, down, ke.is_repeat),
76 Key::ArrowLeft => self.stage_code(PhysicalKey::ArrowLeft, down, ke.is_repeat),
77 Key::ArrowRight => self.stage_code(PhysicalKey::ArrowRight, down, ke.is_repeat),
78 _ => {}
79 }
80 }
81
82 pub fn stage_physical(&mut self, key: PhysicalKey, down: bool, is_repeat: bool) {
83 if down {
84 if self.held.insert(key) && !is_repeat {
85 self.edges.push(key);
86 }
87 } else {
88 self.held.remove(&key);
89 }
90 }
91
92 fn stage_code(&mut self, code: PhysicalKey, down: bool, is_repeat: bool) {
93 self.stage_physical(code, down, is_repeat);
94 }
95
96 pub fn stage_physical_key(&mut self, key: PhysicalKey, down: bool) {
97 self.stage_physical(key, down, false);
98 }
99
100 pub fn set_window_focused(&mut self, focused: bool) {
101 self.window_focused = focused;
102 if !focused {
103 self.held.clear();
104 self.edges.clear();
105 self.capture_pending_physical = None;
106 self.capture_pending_key = None;
107 self.capture_pending_mouse = None;
108 self.lmb_held = false;
109 self.rmb_held = false;
110 self.clicks.clear();
111 self.mouse_edges.clear();
112 self.rmb_down_edge = false;
113 self.touch_active.clear();
114 self.touch_new.clear();
115 self.pads.clear();
116 self.pad_live = false;
117 }
118 }
119
120 pub fn feed_polled(&mut self, sched: &Scheduler) {
121 if !sched.window_focused {
122 self.set_window_focused(false);
123 return;
124 }
125 self.window_focused = true;
126 self.held.retain(|key| sched.held_keys.contains(key));
129 if !sched.mouse_primary {
130 self.lmb_held = false;
131 }
132 if !sched.mouse_secondary {
133 self.rmb_held = false;
134 }
135 if !sched.mouse_middle {
136 self.mouse_edges
137 .retain(|(button, _)| *button != PointerButton::Tertiary);
138 }
139 }
140
141 pub fn pick_down(&mut self, button: PointerButton) {
142 if self.capture_armed {
143 self.capture_pending_mouse = Some(button == PointerButton::Primary);
144 return;
145 }
146 self.mouse_edges.push((button, true));
147 if button == PointerButton::Primary {
148 self.lmb_held = true;
149 } else {
150 self.rmb_down_edge = true;
151 self.rmb_held = true;
152 }
153 }
154
155 pub fn pick_up(&mut self, button: PointerButton) {
156 self.mouse_edges.push((button, false));
157 if button == PointerButton::Primary {
158 self.lmb_held = false;
159 } else {
160 self.rmb_held = false;
161 }
162 }
163
164 pub fn take_capture_physical(&mut self) -> Option<PhysicalKey> {
165 self.capture_pending_physical.take()
166 }
167
168 pub fn take_capture_key(&mut self) -> Option<Key> {
169 self.capture_pending_key.take()
170 }
171
172 pub fn take_capture_mouse(&mut self) -> Option<bool> {
173 self.capture_pending_mouse.take()
174 }
175
176 pub fn take_rmb_down(&mut self) -> bool {
177 std::mem::replace(&mut self.rmb_down_edge, false)
178 }
179
180 pub fn stage_click(&mut self, world: Vec2, screen: [f32; 2], density: f32) {
181 let d = density.max(1e-6);
182 self.clicks.push(StagedClick {
183 world,
184 dp: [screen[0] / d, screen[1] / d],
185 });
186 }
187
188 pub fn take_clicks(&mut self) -> Vec<StagedClick> {
189 std::mem::take(&mut self.clicks)
190 }
191
192 pub fn take_edges(&mut self) -> Vec<PhysicalKey> {
193 std::mem::take(&mut self.edges)
194 }
195
196 pub fn take_mouse_edges(&mut self) -> Vec<(PointerButton, bool)> {
197 std::mem::take(&mut self.mouse_edges)
198 }
199
200 pub fn take_pads(&mut self) -> Vec<GamepadState> {
201 self.pad_live = !self.pads.is_empty();
202 std::mem::take(&mut self.pads)
203 }
204
205 pub fn stage_gamepad(&mut self, pad: GamepadState) {
206 self.pads.push(pad);
207 self.pad_live = true;
208 }
209
210 pub fn touch_down(&mut self, id: u64, screen: Vec2) {
211 self.touch_active.insert(id, (screen, screen));
212 self.touch_new.insert(id);
213 }
214
215 pub fn touch_move(&mut self, id: u64, screen: Vec2) {
216 if let Some(contact) = self.touch_active.get_mut(&id) {
217 contact.1 = screen;
218 }
219 }
220
221 pub fn touch_up(&mut self, id: u64) {
222 self.touch_active.remove(&id);
223 self.touch_new.remove(&id);
224 }
225
226 pub fn touch_contacts(&mut self, density: f32) -> Vec<TouchContact> {
227 let d = density.max(1e-6);
228 let out: Vec<TouchContact> = self
229 .touch_active
230 .iter()
231 .map(|(id, (start, pos))| TouchContact {
232 start: *start / d,
233 pos: *pos / d,
234 just_pressed: self.touch_new.contains(id),
235 })
236 .collect();
237 self.touch_new.clear();
238 out
239 }
240
241 pub fn cursor_move(&mut self, phys_px: Vec2) {
242 self.aim.cursor_move(phys_px);
243 }
244
245 pub fn stage_hover(&mut self, world: Vec2, screen: [f32; 2]) {
246 self.aim.stage_hover(world, screen);
247 }
248
249 pub fn mouse_state(&self, allow_fire: bool) -> MouseState {
250 MouseState {
251 left_held: self.lmb_held && allow_fire,
252 left_pressed: !self.clicks.is_empty() && allow_fire,
253 right_held: self.rmb_held,
254 right_pressed: self.rmb_down_edge,
255 }
256 }
257}
258
259#[cfg(test)]
260mod tests {
261 use super::*;
262 use repose_core::input::{Key, Modifiers};
263
264 fn key(physical: PhysicalKey) -> KeyEvent {
265 KeyEvent {
266 key: Key::Character('x'),
267 modifiers: Modifiers::default(),
268 is_repeat: false,
269 event_type: KeyEventType::Down,
270 utf16_code_point: 0,
271 physical: Some(physical),
272 }
273 }
274
275 #[test]
276 fn edges_dedupe_while_held() {
277 let mut s = Staging::default();
278 s.handle_key(&key(PhysicalKey::KeyW));
279 s.handle_key(&key(PhysicalKey::KeyW));
280 assert_eq!(s.take_edges().len(), 1);
281 }
282
283 #[test]
284 fn focus_loss_clears_levels() {
285 let mut s = Staging::default();
286 s.handle_key(&key(PhysicalKey::KeyW));
287 s.set_window_focused(false);
288 assert!(s.held.is_empty());
289 }
290
291 #[test]
292 fn capture_stashes_physical_and_skips_edges() {
293 let mut s = Staging::default();
294 s.capture_armed = true;
295 s.handle_key(&key(PhysicalKey::KeyZ));
296 assert_eq!(s.capture_pending_physical, Some(PhysicalKey::KeyZ));
297 assert!(s.take_edges().is_empty());
298 }
299
300 #[test]
301 fn capture_click_stashes_button_not_click_edge() {
302 let mut s = Staging::default();
303 s.capture_armed = true;
304 s.pick_down(PointerButton::Primary);
305 assert_eq!(s.capture_pending_mouse, Some(true));
306 assert!(s.take_clicks().is_empty());
307 assert!(!s.lmb_held);
308 }
309}