2020-04-25 01:55:15 +00:00
|
|
|
use crate::keyboard::{ElementState, KeyboardInput, VirtualKeyCode};
|
|
|
|
use bevy_app::{AppExit, Events, GetEventReader};
|
2020-04-19 19:13:04 +00:00
|
|
|
use legion::prelude::*;
|
|
|
|
|
|
|
|
pub fn exit_on_esc_system(resources: &mut Resources) -> Box<dyn Schedulable> {
|
|
|
|
let mut keyboard_input_event_reader = resources.get_event_reader::<KeyboardInput>();
|
|
|
|
SystemBuilder::new("exit_on_esc")
|
|
|
|
.read_resource::<Events<KeyboardInput>>()
|
|
|
|
.write_resource::<Events<AppExit>>()
|
2020-04-25 01:55:15 +00:00
|
|
|
.build(
|
|
|
|
move |_, _, (ref keyboard_input_events, ref mut app_exit_events), _| {
|
2020-04-28 17:59:42 +00:00
|
|
|
for event in keyboard_input_event_reader.iter(keyboard_input_events) {
|
2020-04-25 01:55:15 +00:00
|
|
|
if let Some(virtual_key_code) = event.virtual_key_code {
|
|
|
|
if event.state == ElementState::Pressed
|
|
|
|
&& virtual_key_code == VirtualKeyCode::Escape
|
|
|
|
{
|
|
|
|
app_exit_events.send(AppExit);
|
|
|
|
}
|
2020-04-19 19:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-25 01:55:15 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|