From eab5072d1eca1e69c0c07021b880d9adfa878bc6 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Mon, 11 Mar 2024 18:48:46 +0100 Subject: [PATCH] add some helper fns --- hui/src/input.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/hui/src/input.rs b/hui/src/input.rs index 8b5da27..0d92285 100644 --- a/hui/src/input.rs +++ b/hui/src/input.rs @@ -136,7 +136,7 @@ impl_fits64_for_keyboard_key!( #[derive(Default, Clone, Copy, Debug)] pub struct MouseButtonMeta { /// Position at which the input was initiated (last time it was pressed **down**) - pub start_position: Option, + pub start_position: Vec2, } #[derive(Default)] @@ -186,7 +186,7 @@ impl UiInputState { ButtonState::Pressed => { let button = self.mouse_pointer.buttons.entry(button) .or_insert(MouseButtonMeta::default()); - button.start_position = state.is_pressed().then_some(self.mouse_pointer.current_position); + button.start_position = self.mouse_pointer.current_position; }, ButtonState::Released => { self.mouse_pointer.buttons.remove(&button); @@ -230,6 +230,22 @@ impl<'a> InputCtx<'a> { self.0.mouse_pointer.buttons.contains_key(&button).into() } + /// Get the start position of a mouse button\ + /// (Position at the last time it was pressed **down**) + /// + /// Returns `None` if the button is not currently down + pub fn mouse_button_start_position(&self, button: MouseButton) -> Option { + self.0.mouse_pointer.buttons.get(&button).map(|meta| meta.start_position) + } + + /// Get the relative movement of the mouse pointer since the button was pressed down + /// + /// This function is similar to [`InputCtx::mouse_button_start_position`], but returns the relative movement instead of the absolute position + pub fn mouse_button_relative_movement(&self, button: MouseButton) -> Option { + let start = self.mouse_button_start_position(button)?; + Some(self.mouse_position() - start) + } + /// Check if a rect can be considered "hovered" pub fn is_hovered(&self, rect: Rect) -> bool { rect.contains_point(self.0.mouse_pointer.current_position)