actually sane sensor handling

This commit is contained in:
2025-08-16 00:56:00 -04:00
parent f4aef3a983
commit 11188f2951
9 changed files with 306 additions and 117 deletions

View File

@@ -1,61 +1,44 @@
use gui::{Sense, SenseCtx, SenseTrigger, Vec2};
use gui::{CursorState, Vec2};
use winit::event::WindowEvent;
use crate::testing::Client;
#[derive(Default)]
pub struct Input {
size: Vec2,
mouse_pos: Vec2,
mouse_pressed: bool,
mouse_just_pressed: bool,
mouse_just_released: bool,
mouse_in: bool,
mouse_exists: bool,
}
impl Input {
pub fn event(&mut self, event: &WindowEvent) {
self.mouse_just_pressed = false;
self.mouse_just_released = false;
match event {
WindowEvent::Resized(size) => {
self.size = Vec2::new(size.width as f32, size.height as f32);
}
WindowEvent::CursorMoved { position, .. } => {
self.mouse_pos = Vec2::new(position.x as f32, position.y as f32);
self.mouse_in = true;
self.mouse_exists = true;
}
WindowEvent::MouseInput { state, button, .. } => match button {
winit::event::MouseButton::Left => {
if state.is_pressed() {
self.mouse_just_pressed = !self.mouse_pressed;
self.mouse_pressed = true;
} else {
self.mouse_just_released = self.mouse_pressed;
self.mouse_pressed = false;
}
self.mouse_pressed = state.is_pressed();
}
_ => (),
},
_ => (),
}
}
fn active(&mut self, trigger: &SenseTrigger) -> bool {
let region = trigger.shape.to_screen(self.size);
if !self.mouse_in || !region.contains(self.mouse_pos) {
return trigger.sense == Sense::NoHover;
}
match trigger.sense {
Sense::Press => self.mouse_just_pressed,
Sense::Held => self.mouse_pressed,
Sense::Hover => true,
Sense::NoHover => false,
}
}
}
impl SenseCtx for Client {
fn active(&mut self, trigger: &SenseTrigger) -> bool {
self.input.active(trigger)
impl Client {
pub fn window_size(&self) -> Vec2 {
let size = self.renderer.window().inner_size();
(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,
}
}
}