hUI/hui-winit/src/lib.rs

32 lines
1.2 KiB
Rust
Raw Normal View History

2024-02-19 18:00:57 -06:00
use glam::vec2;
use hui::{event::UiEvent, UiInstance};
use winit::event::{Event, WindowEvent};
2024-02-20 13:24:36 -06:00
//TODO: check window id
2024-02-19 18:00:57 -06:00
pub fn handle_winit_event<T>(ui: &mut UiInstance, event: &Event<T>) {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CursorMoved { position, .. } => {
ui.push_event(UiEvent::MouseMove(vec2(position.x as f32, position.y as f32)));
},
WindowEvent::MouseInput { state, button, .. } => {
ui.push_event(UiEvent::MouseButton {
button: match button {
2024-02-28 18:15:29 -06:00
winit::event::MouseButton::Left => hui::input::MouseButton::Primary,
winit::event::MouseButton::Right => hui::input::MouseButton::Secondary,
winit::event::MouseButton::Middle => hui::input::MouseButton::Middle,
winit::event::MouseButton::Other(id) => hui::input::MouseButton::Other(*id as u8),
2024-02-19 18:00:57 -06:00
_ => return,
},
state: match state {
2024-02-28 18:15:29 -06:00
winit::event::ElementState::Pressed => hui::input::ButtonState::Pressed,
winit::event::ElementState::Released => hui::input::ButtonState::Released,
2024-02-19 18:00:57 -06:00
},
})
},
2024-02-20 13:24:36 -06:00
//TODO: translate keyboard input
2024-02-19 18:00:57 -06:00
_ => (),
}
}
}