2022-06-10 20:43:33 +10:00
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
2020-09-10 11:58:30 +10:00
|
|
|
use simple_logger::SimpleLogger;
|
2019-06-22 01:33:15 +10:00
|
|
|
use winit::{
|
2023-05-29 04:02:59 +10:00
|
|
|
event::{DeviceEvent, ElementState, Event, KeyEvent, WindowEvent},
|
2022-04-10 11:32:02 +10:00
|
|
|
event_loop::EventLoop,
|
2023-05-29 04:02:59 +10:00
|
|
|
keyboard::{Key, ModifiersState},
|
2022-06-13 16:43:14 +10:00
|
|
|
window::{CursorGrabMode, WindowBuilder},
|
2019-06-22 01:33:15 +10:00
|
|
|
};
|
2019-02-06 02:30:33 +11:00
|
|
|
|
2023-06-20 04:46:38 +10:00
|
|
|
#[path = "util/fill.rs"]
|
|
|
|
mod fill;
|
|
|
|
|
2018-06-19 02:32:18 +10:00
|
|
|
fn main() {
|
2020-09-10 11:58:30 +10:00
|
|
|
SimpleLogger::new().init().unwrap();
|
2019-02-06 02:30:33 +11:00
|
|
|
let event_loop = EventLoop::new();
|
2018-06-19 02:32:18 +10:00
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
let window = WindowBuilder::new()
|
2018-06-19 02:32:18 +10:00
|
|
|
.with_title("Super Cursor Grab'n'Hide Simulator 9000")
|
2019-02-06 02:30:33 +11:00
|
|
|
.build(&event_loop)
|
2018-06-19 02:32:18 +10:00
|
|
|
.unwrap();
|
|
|
|
|
2019-12-31 06:11:11 +11:00
|
|
|
let mut modifiers = ModifiersState::default();
|
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2022-04-10 11:32:02 +10:00
|
|
|
control_flow.set_wait();
|
2020-01-05 18:12:03 +11:00
|
|
|
|
2019-06-27 16:58:21 +10:00
|
|
|
match event {
|
|
|
|
Event::WindowEvent { event, .. } => match event {
|
2022-04-10 11:32:02 +10:00
|
|
|
WindowEvent::CloseRequested => control_flow.set_exit(),
|
2019-02-06 02:30:33 +11:00
|
|
|
WindowEvent::KeyboardInput {
|
2023-05-29 04:02:59 +10:00
|
|
|
event:
|
|
|
|
KeyEvent {
|
|
|
|
logical_key: key,
|
2019-06-22 01:33:15 +10:00
|
|
|
state: ElementState::Released,
|
|
|
|
..
|
|
|
|
},
|
2018-06-19 02:32:18 +10:00
|
|
|
..
|
|
|
|
} => {
|
2022-06-13 16:43:14 +10:00
|
|
|
let result = match key {
|
2023-05-29 04:02:59 +10:00
|
|
|
Key::Escape => {
|
2022-06-13 16:43:14 +10:00
|
|
|
control_flow.set_exit();
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-29 04:02:59 +10:00
|
|
|
Key::Character(ch) => match ch.to_lowercase().as_str() {
|
|
|
|
"g" => window.set_cursor_grab(CursorGrabMode::Confined),
|
|
|
|
"l" => window.set_cursor_grab(CursorGrabMode::Locked),
|
|
|
|
"a" => window.set_cursor_grab(CursorGrabMode::None),
|
|
|
|
"h" => {
|
|
|
|
window.set_cursor_visible(modifiers.shift_key());
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Ok(()),
|
|
|
|
},
|
2022-06-13 16:43:14 +10:00
|
|
|
_ => Ok(()),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(err) = result {
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("error: {err}");
|
2018-06-19 02:32:18 +10:00
|
|
|
}
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2023-05-29 04:02:59 +10:00
|
|
|
WindowEvent::ModifiersChanged(new) => modifiers = new.state(),
|
2018-06-19 02:32:18 +10:00
|
|
|
_ => (),
|
2019-06-27 16:58:21 +10:00
|
|
|
},
|
|
|
|
Event::DeviceEvent { event, .. } => match event {
|
2023-01-27 15:18:58 +11:00
|
|
|
DeviceEvent::MouseMotion { delta } => println!("mouse moved: {delta:?}"),
|
2019-06-27 16:58:21 +10:00
|
|
|
DeviceEvent::Button { button, state } => match state {
|
2023-01-27 15:18:58 +11:00
|
|
|
ElementState::Pressed => println!("mouse button {button} pressed"),
|
|
|
|
ElementState::Released => println!("mouse button {button} released"),
|
2019-06-27 16:58:21 +10:00
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
},
|
2023-06-20 04:46:38 +10:00
|
|
|
Event::RedrawRequested(_) => fill::fill_window(&window),
|
2019-06-27 16:58:21 +10:00
|
|
|
_ => (),
|
2018-06-19 02:32:18 +10:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|