2020-06-15 19:47:35 +00:00
|
|
|
use crate::keyboard::{ElementState, KeyCode, KeyboardInput};
|
2020-07-17 01:47:51 +00:00
|
|
|
use bevy_app::{
|
|
|
|
prelude::{EventReader, Events},
|
|
|
|
AppExit,
|
|
|
|
};
|
2020-07-10 04:18:35 +00:00
|
|
|
use bevy_ecs::{Local, Res, ResMut};
|
2020-04-19 19:13:04 +00:00
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// Local "exit on escape" system state
|
2020-07-10 04:18:35 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ExitOnEscapeState {
|
|
|
|
reader: EventReader<KeyboardInput>,
|
|
|
|
}
|
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// Sends the AppExit event whenever the "esc" key is pressed.
|
2020-07-10 04:18:35 +00:00
|
|
|
pub fn exit_on_esc_system(
|
|
|
|
mut state: Local<ExitOnEscapeState>,
|
|
|
|
keyboard_input_events: Res<Events<KeyboardInput>>,
|
|
|
|
mut app_exit_events: ResMut<Events<AppExit>>,
|
|
|
|
) {
|
|
|
|
for event in state.reader.iter(&keyboard_input_events) {
|
|
|
|
if let Some(key_code) = event.key_code {
|
|
|
|
if event.state == ElementState::Pressed && key_code == KeyCode::Escape {
|
|
|
|
app_exit_events.send(AppExit);
|
2020-04-30 23:29:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-10 04:18:35 +00:00
|
|
|
}
|
2020-04-25 01:55:15 +00:00
|
|
|
}
|