use winit::application::ApplicationHandler;
use winit::event::{DeviceEvent, DeviceId, StartCause, WindowEvent};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::keyboard::KeyCode;
use winit::window::{Window, WindowId};
use winit_input_helper::WinitInputHelper;
struct App {
window: Option<Window>,
input: WinitInputHelper,
}
impl ApplicationHandler for App {
fn window_event(&mut self, _: &ActiveEventLoop, _: WindowId, event: WindowEvent) {
if self.input.process_window_event(&event) {
}
}
fn device_event(&mut self, _: &ActiveEventLoop, _: DeviceId, event: DeviceEvent) {
self.input.process_device_event(&event);
}
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
self.input.end_step();
if self.input.key_released(KeyCode::KeyQ)
|| self.input.close_requested()
|| self.input.destroyed()
{
println!("The application was requsted to close or the 'Q' key was pressed, quiting the application");
event_loop.exit();
return;
}
if self.input.key_pressed(KeyCode::KeyW) {
println!("The 'W' key (US layout) was pressed on the keyboard");
}
}
fn new_events(&mut self, _: &ActiveEventLoop, _: StartCause) {
self.input.step();
}
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if self.window.is_none() {
self.window = Some(
event_loop
.create_window(Window::default_attributes())
.unwrap(),
);
}
}
}
fn main() {
let event_loop = EventLoop::new().unwrap();
event_loop.set_control_flow(ControlFlow::Poll);
event_loop
.run_app(&mut App {
input: WinitInputHelper::new(),
window: None,
})
.unwrap();
}