Skip to main content

cotis_wgpu/
input.rs

1//! Input state collected from winit and exposed through Cotis interactivity traits.
2//!
3//! [`InputSnapshot`] is updated by the renderer's event loop on each
4//! [`cotis::renders::CotisRenderer::draw_frame`] / `pump_events` call.
5//! Cotis interactivity traits on [`crate::renderer::CotisWgpuRenderer`] (see [`crate::cotis_traits`])
6//! read from this snapshot.
7
8use indexmap::IndexSet;
9
10use cotis_utils::interactivity::keyboard::KeyboardKey;
11use cotis_utils::interactivity::mouse::MouseButton;
12use cotis_utils::math::Vector2;
13
14/// Snapshot of keyboard and mouse state for the current frame.
15#[derive(Debug, Clone, Default)]
16pub struct InputSnapshot {
17    /// Cursor position in physical window pixels.
18    pub mouse_position: Vector2,
19    /// Mouse buttons currently held down.
20    pub mouse_buttons: IndexSet<MouseButton>,
21    /// Keyboard keys currently held down.
22    pub pressed_keys: IndexSet<KeyboardKey>,
23    /// Text input characters received this frame (IME preedit).
24    pub pressed_chars: Vec<char>,
25    /// Scroll wheel delta accumulated this frame (lines or pixels, depending on winit event).
26    pub scroll_delta: Vector2,
27}
28
29impl InputSnapshot {
30    /// Returns and clears the accumulated text input characters for this frame.
31    ///
32    /// Call once per frame to consume IME/preedit input without duplication.
33    pub fn take_pressed_chars(&mut self) -> Vec<char> {
34        std::mem::take(&mut self.pressed_chars)
35    }
36
37    /// Returns and clears the accumulated scroll delta for this frame.
38    ///
39    /// Call once per frame to consume wheel movement without duplication.
40    pub fn take_scroll_delta(&mut self) -> Vector2 {
41        std::mem::take(&mut self.scroll_delta)
42    }
43}