This commit is contained in:
2025-08-15 15:48:00 -04:00
parent c5aa0a02e2
commit 78ea738b8e
8 changed files with 222 additions and 52 deletions

58
src/testing/input.rs Normal file
View File

@@ -0,0 +1,58 @@
use gui::{SenseCtx, SenseTrigger, 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,
}
impl Input {
pub fn event(&mut self, event: &WindowEvent) {
self.mouse_just_pressed = 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)
}
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_pressed = true;
} else {
self.mouse_pressed = false;
}
}
_ => (),
},
_ => (),
}
}
fn active(&mut self, trigger: &SenseTrigger) -> bool {
let region = trigger.shape.to_screen(self.size);
if !region.contains(self.mouse_pos) {
return false;
}
match trigger.sense {
gui::Sense::Click => self.mouse_just_pressed,
gui::Sense::Hover => true,
}
}
}
impl SenseCtx for Client {
fn active(&mut self, trigger: &SenseTrigger) -> bool {
self.input.active(trigger)
}
}