From 27205a93d2923a63e91a8e111159052fd1c99417 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 29 Feb 2024 01:15:29 +0100 Subject: [PATCH] input upd (wip) --- hui-winit/src/lib.rs | 12 ++-- hui/Cargo.toml | 2 + hui/src/event.rs | 64 +--------------------- hui/src/input.rs | 127 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 135 insertions(+), 70 deletions(-) diff --git a/hui-winit/src/lib.rs b/hui-winit/src/lib.rs index 4ec6b8d..8037781 100644 --- a/hui-winit/src/lib.rs +++ b/hui-winit/src/lib.rs @@ -12,15 +12,15 @@ pub fn handle_winit_event(ui: &mut UiInstance, event: &Event) { WindowEvent::MouseInput { state, button, .. } => { ui.push_event(UiEvent::MouseButton { button: match button { - winit::event::MouseButton::Left => hui::event::MouseButton::Primary, - winit::event::MouseButton::Right => hui::event::MouseButton::Secondary, - winit::event::MouseButton::Middle => hui::event::MouseButton::Middle, - winit::event::MouseButton::Other(id) => hui::event::MouseButton::Other(*id as u8), + winit::event::MouseButton::Left => hui::input::MouseButton::Primary, + winit::event::MouseButton::Right => hui::input::MouseButton::Secondary, + winit::event::MouseButton::Middle => hui::input::MouseButton::Middle, + winit::event::MouseButton::Other(id) => hui::input::MouseButton::Other(*id as u8), _ => return, }, state: match state { - winit::event::ElementState::Pressed => hui::event::ButtonState::Pressed, - winit::event::ElementState::Released => hui::event::ButtonState::Released, + winit::event::ElementState::Pressed => hui::input::ButtonState::Pressed, + winit::event::ElementState::Released => hui::input::ButtonState::Released, }, }) }, diff --git a/hui/Cargo.toml b/hui/Cargo.toml index a5ba448..350f699 100644 --- a/hui/Cargo.toml +++ b/hui/Cargo.toml @@ -25,6 +25,8 @@ log = "0.4" nz = "0.3" document-features = "0.2" derive_setters = "0.1" +#smallvec = "1.13" +#tinyset = "0.4" [features] default = ["builtin_elements", "builtin_font", "pixel_perfect_text"] diff --git a/hui/src/event.rs b/hui/src/event.rs index 0b010c5..1fb3fb3 100644 --- a/hui/src/event.rs +++ b/hui/src/event.rs @@ -1,69 +1,7 @@ //! input, window events and event handling use glam::Vec2; - -/// Represents a mouse button. -/// -/// Value of the `Other` variant is currently not standardized\ -/// and may change depending on the platform or the backend used -#[derive(Clone, Copy, Debug, Default)] -pub enum MouseButton { - ///Primary mouse button (usually left) - #[default] - Primary, - ///Secondary mouse button (usually right) - Secondary, - ///Middle mouse button (usually the wheel button) - Middle, - ///Other mouse button (e.g. extra buttons on a gaming mouse) - /// - ///Value is not standardized and may change depending on the platform or the backend used - Other(u8), -} - -/// Represents the state of a button, such as a mouse button or a keyboard key.\ -/// Can be either `Pressed` (0) or `Released` (1). -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] -pub enum ButtonState { - #[default] - Released = 0, - Pressed = 1, -} - -impl ButtonState { - pub fn is_pressed(self) -> bool { - self == ButtonState::Pressed - } - pub fn is_released(self) -> bool { - self == ButtonState::Released - } -} - -/// Represents a keyboard or other hardware key (for example volume buttons) -/// -/// Values of the `KeyCode` variant are not standardized and may change depending on the platform or the backend used. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum KeyboardKey { - //Keyboard buttons: - A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, - Num0, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, - Np0, Np1, Np2, Np3, Np4, Np5, Np6, Np7, Np8, Np9, - NpDivide, NpMultiply, NpSubtract, NpAdd, NpEnter, NpDecimal, - F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, - Up, Down, Left, Right, - Space, Enter, Escape, Backspace, Tab, CapsLock, - LControl, RControl, LShift, RShift, LAlt, RAlt, LSuper, RSuper, - Grave, Minus, Equals, LeftBracket, RightBracket, Backslash, Semicolon, Apostrophe, Comma, Period, Slash, - Insert, Delete, Home, End, PageUp, PageDown, PrintScreen, ScrollLock, Pause, Menu, NumLock, - //Multimedia keys and android-specific (e.g. volume keys): - Mute, VolumeUp, VolumeDown, MediaPlay, MediaStop, MediaNext, MediaPrevious, - //Keycode: - /// Represents a key code. - /// - /// This enum variant holds an unsigned 32-bit integer representing a key code. - /// The value of the key code is not standardized and may change depending on the platform or the backend used. - KeyCode(u32), -} +use crate::input::{MouseButton, ButtonState, KeyboardKey}; #[derive(Clone, Copy, Debug)] pub enum UiEvent { diff --git a/hui/src/input.rs b/hui/src/input.rs index 0551d5b..43399a3 100644 --- a/hui/src/input.rs +++ b/hui/src/input.rs @@ -1,5 +1,130 @@ //! keyboard, mouse, and touch input handling -pub(crate) struct UiInputState { +use std::hash::{Hash, Hasher}; +use glam::Vec2; +use hashbrown::HashMap; +use nohash_hasher::BuildNoHashHasher; +/// Represents a mouse button. +/// +/// Value of the `Other` variant is currently not standardized\ +/// and may change depending on the platform or the backend used +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum MouseButton { + ///Primary mouse button (usually left) + #[default] + Primary, + ///Secondary mouse button (usually right) + Secondary, + ///Middle mouse button (usually the wheel button) + Middle, + ///Other mouse button (e.g. extra buttons on a gaming mouse) + /// + ///Value is not standardized and may change depending on the platform or the backend used + Other(u8), +} + +// Manual hash impl only uses one hash call +impl Hash for MouseButton { + fn hash(&self, state: &mut H) { + match self { + MouseButton::Primary => 0u16.hash(state), + MouseButton::Secondary => 1u16.hash(state), + MouseButton::Middle => 2u16.hash(state), + MouseButton::Other(id) => ((*id as u16) << 8).hash(state), + } + } +} + +/// Represents the state of a button, such as a mouse button or a keyboard key.\ +/// Can be either `Pressed` (0) or `Released` (1). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] +pub enum ButtonState { + #[default] + Released = 0, + Pressed = 1, +} + +impl ButtonState { + pub fn is_pressed(self) -> bool { + self == ButtonState::Pressed + } + pub fn is_released(self) -> bool { + self == ButtonState::Released + } +} + +/// Represents a keyboard or other hardware key (for example volume buttons) +/// +/// Values of the `KeyCode` variant are not standardized and may change depending on the platform or the backend used. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum KeyboardKey { + //Keyboard buttons: + A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, + Num0, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9, + Np0, Np1, Np2, Np3, Np4, Np5, Np6, Np7, Np8, Np9, + NpDivide, NpMultiply, NpSubtract, NpAdd, NpEnter, NpDecimal, + F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, + Up, Down, Left, Right, + Space, Enter, Escape, Backspace, Tab, CapsLock, + LControl, RControl, LShift, RShift, LAlt, RAlt, LSuper, RSuper, + Grave, Minus, Equals, LeftBracket, RightBracket, Backslash, Semicolon, Apostrophe, Comma, Period, Slash, + Insert, Delete, Home, End, PageUp, PageDown, PrintScreen, ScrollLock, Pause, Menu, NumLock, + //Multimedia keys and android-specific (e.g. volume keys): + Mute, VolumeUp, VolumeDown, MediaPlay, MediaStop, MediaNext, MediaPrevious, + //Keycode: + /// Represents a key code. + /// + /// This enum variant holds an unsigned 32-bit integer representing a key code. + /// The value of the key code is not standardized and may change depending on the platform or the backend used. + KeyCode(u32), +} + + +/// Information about a mouse buttons or a touchscreen input that's continuously held down +pub(crate) struct ActiveMouseButton { + /// The button that initiated the input + pub button: MouseButton, + /// Position at which the input was initiated + pub start_position: Vec2, +} + +pub(crate) struct MousePointer { + pub current_position: Vec2, + pub active_buttons: HashMap>, +} + +pub(crate) struct TouchFinger { + /// Unique identifier of the pointer (finger) + pub id: u32, + pub current_position: Vec2, + pub start_position: Vec2, +} + +/// Represents a pointer (mouse or touch) +pub(crate) enum Pointer { + MousePointer(MousePointer), + TouchFinger(TouchFinger), +} + +impl ActiveMouseButton { + /// Check if the pointer (mouse or touch) was just pressed\ + /// (i.e. it was not pressed in the previous frame, but is pressed now) + /// + /// You should avoid using this, as it's not very intuitive for touch input (use `just_pressed` instead, if possible) + pub fn just_pressed(&self) -> bool { + todo!() + } + + /// Check if the pointer (mouse or touch) was just released\ + /// (i.e. it was pressed in the previous frame, but is not pressed now) + /// + /// This is the preferred "on click" event for elements like buttons + pub fn just_released(&self) -> bool { + todo!() + } +} + +pub(crate) struct UiInputState { + pub pointers: Vec, }