Files
iris/src/testing/input.rs
2025-09-15 22:22:52 -04:00

50 lines
1.4 KiB
Rust

use ui::layout::{CursorState, Vec2};
use winit::event::{MouseButton, WindowEvent};
use crate::testing::Client;
#[derive(Default)]
pub struct Input {
cursor: CursorState,
}
impl Input {
pub fn event(&mut self, event: &WindowEvent) {
match event {
WindowEvent::CursorMoved { position, .. } => {
self.cursor.pos = Vec2::new(position.x as f32, position.y as f32);
self.cursor.exists = true;
}
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 {
pub fn window_size(&self) -> Vec2 {
let size = self.renderer.window().inner_size();
(size.width, size.height).into()
}
pub fn cursor_state(&self) -> &CursorState {
&self.input.cursor
}
}