sense specific buttons

This commit is contained in:
2025-09-15 22:22:52 -04:00
parent 21aa2b3501
commit b48acccb8d
8 changed files with 167 additions and 107 deletions

View File

@@ -1,31 +1,40 @@
use ui::layout::{CursorState, Vec2};
use winit::event::WindowEvent;
use winit::event::{MouseButton, WindowEvent};
use crate::testing::Client;
#[derive(Default)]
pub struct Input {
mouse_pos: Vec2,
mouse_pressed: bool,
mouse_exists: bool,
cursor: CursorState,
}
impl Input {
pub fn event(&mut self, event: &WindowEvent) {
match event {
WindowEvent::CursorMoved { position, .. } => {
self.mouse_pos = Vec2::new(position.x as f32, position.y as f32);
self.mouse_exists = true;
self.cursor.pos = Vec2::new(position.x as f32, position.y as f32);
self.cursor.exists = true;
}
WindowEvent::MouseInput { state, button, .. } => match button {
winit::event::MouseButton::Left => {
self.mouse_pressed = state.is_pressed();
WindowEvent::MouseInput { state, button, .. } => {
let buttons = &mut self.cursor.buttons;
let pressed = state.is_pressed();
match button {
MouseButton::Left => buttons.left.update(pressed),
MouseButton::Right => buttons.right.update(pressed),
MouseButton::Middle => buttons.middle.update(pressed),
_ => (),
}
_ => (),
},
}
WindowEvent::CursorLeft { .. } => {
self.cursor.exists = false;
}
_ => (),
}
}
pub fn end_frame(&mut self) {
self.cursor.buttons.end_frame();
}
}
impl Client {
@@ -34,11 +43,7 @@ impl Client {
(size.width, size.height).into()
}
pub fn cursor_state(&self) -> CursorState {
CursorState {
pos: self.input.mouse_pos,
exists: self.input.mouse_exists,
pressed: self.input.mouse_pressed,
}
pub fn cursor_state(&self) -> &CursorState {
&self.input.cursor
}
}