hUI/hui-winit/src/lib.rs

39 lines
1.6 KiB
Rust
Raw Permalink Normal View History

2024-08-04 11:06:49 -05:00
// #[cfg(all(feature = "winit_30", feature = "winit_29"))]
// compile_error!("Only one of the winit_30 and winit_29 features can be enabled at a time");
// #[cfg(not(any(feature = "winit_30", feature = "winit_29")))]
// compile_error!("One of the winit_30 and winit_29 features must be enabled");
// #[cfg(feature = "winit_30")] extern crate winit_30 as winit;
// #[cfg(feature = "winit_29")] extern crate winit_29 as winit;
2024-05-04 14:43:03 -05:00
2024-02-19 18:00:57 -06:00
use glam::vec2;
use hui::{event::UiEvent, UiInstance};
2024-05-04 14:43:03 -05:00
use winit::event::{Event, WindowEvent, MouseButton, ElementState};
2024-02-19 18:00:57 -06:00
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-05-04 14:43:03 -05:00
MouseButton::Left => hui::input::MouseButton::Primary,
MouseButton::Right => hui::input::MouseButton::Secondary,
MouseButton::Middle => hui::input::MouseButton::Middle,
MouseButton::Other(id) => hui::input::MouseButton::Other(*id as u8),
2024-02-19 18:00:57 -06:00
_ => return,
},
state: match state {
2024-05-04 14:43:03 -05:00
ElementState::Pressed => hui::input::ButtonState::Pressed,
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
_ => (),
}
}
}