2019-04-26 04:29:35 +00:00
|
|
|
use conrod_core::{event::Input, input::Button};
|
|
|
|
use vek::*;
|
|
|
|
|
2020-07-13 04:51:39 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2019-04-26 04:29:35 +00:00
|
|
|
pub struct Event(pub Input);
|
|
|
|
impl Event {
|
2019-07-28 15:47:14 +00:00
|
|
|
pub fn try_from(
|
2020-02-08 02:09:56 +00:00
|
|
|
event: &winit::event::Event<()>,
|
2020-06-27 17:44:43 +00:00
|
|
|
window: &winit::window::Window,
|
2019-07-28 15:47:14 +00:00
|
|
|
) -> Option<Self> {
|
2019-04-26 04:29:35 +00:00
|
|
|
use conrod_winit::*;
|
2020-02-01 20:39:39 +00:00
|
|
|
// A wrapper around the winit window that allows us to implement the trait
|
|
|
|
// necessary for enabling the winit <-> conrod conversion functions.
|
2019-10-26 14:50:01 +00:00
|
|
|
struct WindowRef<'a>(&'a winit::window::Window);
|
2019-04-26 04:29:35 +00:00
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
// Implement the `WinitWindow` trait for `WindowRef` to allow for generating
|
|
|
|
// compatible conversion functions.
|
2019-04-26 04:29:35 +00:00
|
|
|
impl<'a> conrod_winit::WinitWindow for WindowRef<'a> {
|
|
|
|
fn get_inner_size(&self) -> Option<(u32, u32)> {
|
2020-02-08 02:09:56 +00:00
|
|
|
Some(
|
2021-07-11 18:41:52 +00:00
|
|
|
winit::window::Window::inner_size(self.0)
|
2020-02-08 02:09:56 +00:00
|
|
|
.to_logical::<u32>(self.hidpi_factor())
|
|
|
|
.into(),
|
|
|
|
)
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2021-07-11 18:41:52 +00:00
|
|
|
fn hidpi_factor(&self) -> f64 { winit::window::Window::scale_factor(self.0) }
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
2020-06-27 17:44:43 +00:00
|
|
|
convert_event!(event, &WindowRef(window)).map(Self)
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-04-26 04:29:35 +00:00
|
|
|
pub fn is_keyboard_or_mouse(&self) -> bool {
|
2020-12-10 11:50:48 +00:00
|
|
|
matches!(
|
|
|
|
self.0,
|
2019-04-26 04:29:35 +00:00
|
|
|
Input::Press(_)
|
2020-12-10 11:50:48 +00:00
|
|
|
| Input::Release(_)
|
|
|
|
| Input::Motion(_)
|
|
|
|
| Input::Touch(_)
|
|
|
|
| Input::Text(_)
|
|
|
|
)
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-04-26 04:29:35 +00:00
|
|
|
pub fn is_keyboard(&self) -> bool {
|
2020-12-10 11:50:48 +00:00
|
|
|
matches!(
|
|
|
|
self.0,
|
2019-04-26 04:29:35 +00:00
|
|
|
Input::Press(Button::Keyboard(_))
|
2020-12-10 11:50:48 +00:00
|
|
|
| Input::Release(Button::Keyboard(_))
|
|
|
|
| Input::Text(_)
|
|
|
|
)
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
|
|
|
pub fn new_resize(dims: Vec2<f64>) -> Self { Self(Input::Resize(dims.x, dims.y)) }
|
2019-04-26 04:29:35 +00:00
|
|
|
}
|