59 lines
1.7 KiB
Rust
59 lines
1.7 KiB
Rust
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)
|
|
}
|
|
}
|