mirror of
https://github.com/griffi-gh/hUI.git
synced 2024-11-22 07:08:42 -06:00
add some helper fns
This commit is contained in:
parent
027bc2c429
commit
eab5072d1e
|
@ -136,7 +136,7 @@ impl_fits64_for_keyboard_key!(
|
||||||
#[derive(Default, Clone, Copy, Debug)]
|
#[derive(Default, Clone, Copy, Debug)]
|
||||||
pub struct MouseButtonMeta {
|
pub struct MouseButtonMeta {
|
||||||
/// Position at which the input was initiated (last time it was pressed **down**)
|
/// Position at which the input was initiated (last time it was pressed **down**)
|
||||||
pub start_position: Option<Vec2>,
|
pub start_position: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -186,7 +186,7 @@ impl UiInputState {
|
||||||
ButtonState::Pressed => {
|
ButtonState::Pressed => {
|
||||||
let button = self.mouse_pointer.buttons.entry(button)
|
let button = self.mouse_pointer.buttons.entry(button)
|
||||||
.or_insert(MouseButtonMeta::default());
|
.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 => {
|
ButtonState::Released => {
|
||||||
self.mouse_pointer.buttons.remove(&button);
|
self.mouse_pointer.buttons.remove(&button);
|
||||||
|
@ -230,6 +230,22 @@ impl<'a> InputCtx<'a> {
|
||||||
self.0.mouse_pointer.buttons.contains_key(&button).into()
|
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<Vec2> {
|
||||||
|
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<Vec2> {
|
||||||
|
let start = self.mouse_button_start_position(button)?;
|
||||||
|
Some(self.mouse_position() - start)
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if a rect can be considered "hovered"
|
/// Check if a rect can be considered "hovered"
|
||||||
pub fn is_hovered(&self, rect: Rect) -> bool {
|
pub fn is_hovered(&self, rect: Rect) -> bool {
|
||||||
rect.contains_point(self.0.mouse_pointer.current_position)
|
rect.contains_point(self.0.mouse_pointer.current_position)
|
||||||
|
|
Loading…
Reference in a new issue