REAL SENSORS

This commit is contained in:
2025-08-15 21:42:35 -04:00
parent 9f1802f497
commit a7dfacb83e
10 changed files with 257 additions and 123 deletions

View File

@@ -1,4 +1,4 @@
use gui::{SenseCtx, SenseTrigger, Vec2};
use gui::{Sense, SenseCtx, SenseTrigger, Vec2};
use winit::event::WindowEvent;
use crate::testing::Client;
@@ -9,28 +9,29 @@ pub struct Input {
mouse_pos: Vec2,
mouse_pressed: bool,
mouse_just_pressed: bool,
mouse_just_released: bool,
mouse_in: 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)
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_pos = Vec2::new(position.x as f32, position.y as f32);
self.mouse_in = true;
}
WindowEvent::MouseInput { state, button, .. } => match button {
winit::event::MouseButton::Left => {
if state.is_pressed() {
if !self.mouse_pressed {
self.mouse_just_pressed = true;
} else {
self.mouse_just_pressed = false;
}
self.mouse_just_pressed = !self.mouse_pressed;
self.mouse_pressed = true;
} else {
self.mouse_just_released = self.mouse_pressed;
self.mouse_pressed = false;
}
}
@@ -41,12 +42,14 @@ impl Input {
}
fn active(&mut self, trigger: &SenseTrigger) -> bool {
let region = trigger.shape.to_screen(self.size);
if !region.contains(self.mouse_pos) {
return false;
if !self.mouse_in || !region.contains(self.mouse_pos) {
return trigger.sense == Sense::NoHover;
}
match trigger.sense {
gui::Sense::Click => self.mouse_just_pressed,
gui::Sense::Hover => true,
Sense::Press => self.mouse_just_pressed,
Sense::Held => self.mouse_pressed,
Sense::Hover => true,
Sense::NoHover => false,
}
}
}