Skip to content

Commit

Permalink
make sure to quit if close is requested
Browse files Browse the repository at this point in the history
also unified Trans-comments
  • Loading branch information
fkarg committed Oct 15, 2019
1 parent 6674178 commit 762f68c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
8 changes: 5 additions & 3 deletions examples/states_ui/credits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ impl SimpleState for CreditsScreen {
) -> SimpleTrans {
match &event {
StateEvent::Window(event) => {
if is_close_requested(&event)
|| is_key_down(&event, VirtualKeyCode::Escape)
if is_close_requested(&event) {
log::info!("[Trans::Quit] Quitting Application!");
Trans::Quit
} else if is_key_down(&event, VirtualKeyCode::Escape)
|| is_mouse_button_down(&event, MouseButton::Left)
{
log::info!("Switching back to MainMenu!");
log::info!("[Trans::Switch] Switching to MainMenu!");
Trans::Switch(Box::new(MainMenu::default()))
} else {
Trans::None
Expand Down
7 changes: 5 additions & 2 deletions examples/states_ui/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ impl SimpleState for Game {
) -> SimpleTrans {
match &event {
StateEvent::Window(event) => {
if is_close_requested(&event) || is_key_down(&event, VirtualKeyCode::Escape) {
log::info!("Pausing Game!");
if is_close_requested(&event) {
log::info!("[Trans::Quit] Quitting Application!");
Trans::Quit
} else if is_key_down(&event, VirtualKeyCode::Escape) {
log::info!("[Trans::Push] Pausing Game!");
Trans::Push(Box::new(PauseMenuState::default()))
} else {
Trans::None
Expand Down
11 changes: 7 additions & 4 deletions examples/states_ui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ impl SimpleState for MainMenu {
) -> SimpleTrans {
match event {
StateEvent::Window(event) => {
if is_close_requested(&event) || is_key_down(&event, VirtualKeyCode::Escape) {
log::info!("Switching back to WelcomeScreen!");
if is_close_requested(&event) {
log::info!("[Trans::Quit] Quitting Application!");
Trans::Quit
} else if is_key_down(&event, VirtualKeyCode::Escape) {
log::info!("[Trans::Switch] Switching back to WelcomeScreen!");
Trans::Switch(Box::new(WelcomeScreen::default()))
} else {
Trans::None
Expand All @@ -73,11 +76,11 @@ impl SimpleState for MainMenu {
target,
}) => {
if Some(target) == self.button_credits {
log::info!("Switching to CreditsScreen!");
log::info!("[Trans::Switch] Switching to CreditsScreen!");
return Trans::Switch(Box::new(CreditsScreen::default()));
}
if Some(target) == self.button_start {
log::info!("Switching to Game!");
log::info!("[Trans::Switch] Switching to Game!");
return Trans::Switch(Box::new(Game::default()));
}
if Some(target) == self.button_load || Some(target) == self.button_options {
Expand Down
11 changes: 8 additions & 3 deletions examples/states_ui/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ impl<'a> SimpleState for PauseMenuState {
fn handle_event(&mut self, data: StateData<GameData>, event: StateEvent) -> SimpleTrans {
match event {
StateEvent::Window(event) => {
if is_close_requested(&event) || is_key_down(&event, VirtualKeyCode::Escape) {
if is_close_requested(&event) {
log::info!("[Trans::Quit] Quitting Application!");
Trans::Quit
} else if is_key_down(&event, VirtualKeyCode::Escape) {
log::info!("[Trans::Pop] Closing Pause Menu!");
Trans::Pop
} else {
Trans::None
Expand All @@ -69,14 +73,15 @@ impl<'a> SimpleState for PauseMenuState {
.world
.write_resource::<EventChannel<TransEvent<GameData, StateEvent>>>();

log::info!("Exiting Game, Switching to MainMenu!");

// this allows us to first 'Pop' this state, and then exchange whatever was
// below that with a new MainMenu state.
state_transition_event_channel.single_write(Box::new(|| Trans::Pop));
state_transition_event_channel
.single_write(Box::new(|| Trans::Switch(Box::new(MainMenu::default()))));

log::info!("[Trans::Pop] Closing Pause Menu!");
log::info!("[Trans::Switch] Switching to MainMenu!");

Trans::None // we could also not add the pop to the channel and Pop here
// but like this the execution order is guaranteed (in the next versions)
} else if Some(target) == self.exit_button {
Expand Down
4 changes: 2 additions & 2 deletions examples/states_ui/welcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ impl SimpleState for WelcomeScreen {
match &event {
StateEvent::Window(event) => {
if is_close_requested(&event) || is_key_down(&event, VirtualKeyCode::Escape) {
log::info!("Quitting the application!");
log::info!("[Trans::Quit] Quitting Application!");
Trans::Quit
} else if is_mouse_button_down(&event, MouseButton::Left) {
log::info!("Switching to MainMenu!");
log::info!("[Trans::Switch] Switching to MainMenu!");
Trans::Switch(Box::new(crate::menu::MainMenu::default()))
} else {
Trans::None
Expand Down

0 comments on commit 762f68c

Please sign in to comment.