2022-06-10 20:43:33 +10:00
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
2020-02-14 09:20:32 +11:00
|
|
|
use std::{thread, time};
|
|
|
|
|
2020-09-10 11:58:30 +10:00
|
|
|
use simple_logger::SimpleLogger;
|
2020-02-14 09:20:32 +11:00
|
|
|
use winit::{
|
2023-05-29 04:02:59 +10:00
|
|
|
event::{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,
|
2020-02-14 09:20:32 +11:00
|
|
|
window::WindowBuilder,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
enum Mode {
|
|
|
|
Wait,
|
|
|
|
WaitUntil,
|
|
|
|
Poll,
|
|
|
|
}
|
|
|
|
|
|
|
|
const WAIT_TIME: time::Duration = time::Duration::from_millis(100);
|
|
|
|
const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(100);
|
|
|
|
|
|
|
|
fn main() {
|
2020-09-10 11:58:30 +10:00
|
|
|
SimpleLogger::new().init().unwrap();
|
2020-02-14 09:20:32 +11:00
|
|
|
|
|
|
|
println!("Press '1' to switch to Wait mode.");
|
|
|
|
println!("Press '2' to switch to WaitUntil mode.");
|
|
|
|
println!("Press '3' to switch to Poll mode.");
|
|
|
|
println!("Press 'R' to toggle request_redraw() calls.");
|
|
|
|
println!("Press 'Esc' to close the window.");
|
|
|
|
|
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
let window = WindowBuilder::new()
|
|
|
|
.with_title("Press 1, 2, 3 to change control flow mode. Press R to toggle redraw requests.")
|
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut mode = Mode::Wait;
|
|
|
|
let mut request_redraw = false;
|
|
|
|
let mut wait_cancelled = false;
|
|
|
|
let mut close_requested = false;
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2023-05-29 04:02:59 +10:00
|
|
|
use winit::event::StartCause;
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("{event:?}");
|
2020-02-14 09:20:32 +11:00
|
|
|
match event {
|
|
|
|
Event::NewEvents(start_cause) => {
|
2020-04-20 03:55:10 +10:00
|
|
|
wait_cancelled = match start_cause {
|
|
|
|
StartCause::WaitCancelled { .. } => mode == Mode::WaitUntil,
|
|
|
|
_ => false,
|
2020-02-14 09:20:32 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::WindowEvent { event, .. } => match event {
|
|
|
|
WindowEvent::CloseRequested => {
|
|
|
|
close_requested = true;
|
|
|
|
}
|
|
|
|
WindowEvent::KeyboardInput {
|
2023-05-29 04:02:59 +10:00
|
|
|
event:
|
|
|
|
KeyEvent {
|
|
|
|
logical_key: key,
|
2020-02-14 09:20:32 +11:00
|
|
|
state: ElementState::Pressed,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
2023-05-29 04:02:59 +10:00
|
|
|
} => match key.as_ref() {
|
|
|
|
// WARNING: Consider using `key_without_modifers()` if available on your platform.
|
|
|
|
// See the `key_binding` example
|
|
|
|
Key::Character("1") => {
|
2020-02-14 09:20:32 +11:00
|
|
|
mode = Mode::Wait;
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("\nmode: {mode:?}\n");
|
2020-02-14 09:20:32 +11:00
|
|
|
}
|
2023-05-29 04:02:59 +10:00
|
|
|
Key::Character("2") => {
|
2020-02-14 09:20:32 +11:00
|
|
|
mode = Mode::WaitUntil;
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("\nmode: {mode:?}\n");
|
2020-02-14 09:20:32 +11:00
|
|
|
}
|
2023-05-29 04:02:59 +10:00
|
|
|
Key::Character("3") => {
|
2020-02-14 09:20:32 +11:00
|
|
|
mode = Mode::Poll;
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("\nmode: {mode:?}\n");
|
2020-02-14 09:20:32 +11:00
|
|
|
}
|
2023-05-29 04:02:59 +10:00
|
|
|
Key::Character("r") => {
|
2020-02-14 09:20:32 +11:00
|
|
|
request_redraw = !request_redraw;
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("\nrequest_redraw: {request_redraw}\n");
|
2020-02-14 09:20:32 +11:00
|
|
|
}
|
2023-05-29 04:02:59 +10:00
|
|
|
Key::Escape => {
|
2020-02-14 09:20:32 +11:00
|
|
|
close_requested = true;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
Event::MainEventsCleared => {
|
|
|
|
if request_redraw && !wait_cancelled && !close_requested {
|
|
|
|
window.request_redraw();
|
|
|
|
}
|
|
|
|
if close_requested {
|
2022-04-10 11:32:02 +10:00
|
|
|
control_flow.set_exit();
|
2020-02-14 09:20:32 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::RedrawRequested(_window_id) => {}
|
|
|
|
Event::RedrawEventsCleared => {
|
2022-04-10 11:32:02 +10:00
|
|
|
match mode {
|
|
|
|
Mode::Wait => control_flow.set_wait(),
|
2020-03-07 04:48:54 +11:00
|
|
|
Mode::WaitUntil => {
|
2022-04-10 11:32:02 +10:00
|
|
|
if !wait_cancelled {
|
|
|
|
control_flow.set_wait_until(instant::Instant::now() + WAIT_TIME);
|
2020-03-07 04:48:54 +11:00
|
|
|
}
|
|
|
|
}
|
2020-02-14 09:20:32 +11:00
|
|
|
Mode::Poll => {
|
|
|
|
thread::sleep(POLL_SLEEP_TIME);
|
2022-04-10 11:32:02 +10:00
|
|
|
control_flow.set_poll();
|
2020-02-14 09:20:32 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|