close
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions crates/bevy_dev_tools/src/close_on_esc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use bevy_ecs::prelude::*;
use bevy_input::{keyboard::KeyCode, ButtonInput};
use bevy_window::Window;

/// Close the focused window whenever the escape key (<kbd>Esc</kbd>) is pressed
///
/// This is useful for examples or prototyping.
///
/// # Example
///
/// ```no_run
/// # use bevy_app::prelude::*;
/// # use bevy_dev_tools::close_on_esc;
/// #
/// App::new()
/// .add_systems(Update, close_on_esc);
/// ```
pub fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}

if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}
5 changes: 5 additions & 0 deletions crates/bevy_dev_tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ use bevy_app::prelude::*;

#[cfg(feature = "bevy_ci_testing")]
pub mod ci_testing;

pub mod fps_overlay;

#[cfg(feature = "bevy_ui_debug")]
pub mod ui_debug_overlay;

mod close_on_esc;

pub use crate::close_on_esc::close_on_esc;

/// Enables developer tools in an [`App`]. This plugin is added automatically with `bevy_dev_tools`
/// feature.
///
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev" }
bevy_math = { path = "../bevy_math", version = "0.14.0-dev" }
bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [
"glam",
"smol_str",
] }
bevy_utils = { path = "../bevy_utils", version = "0.14.0-dev" }
# Used for close_on_esc
bevy_input = { path = "../bevy_input", version = "0.14.0-dev" }

# other
serde = { version = "1.0", features = ["derive"], optional = true }
Expand Down
5 changes: 2 additions & 3 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,12 @@ pub struct WindowDestroyed {
/// The event is sent only if the cursor is over one of the application's windows.
/// It is the translated version of [`WindowEvent::CursorMoved`] from the `winit` crate with the addition of `delta`.
///
/// Not to be confused with the [`MouseMotion`] event from `bevy_input`.
/// Not to be confused with the `MouseMotion` event from `bevy_input`.
///
/// Because the range of data is limited by the window area and it may have been transformed by the OS to implement certain effects like acceleration,
/// you should not use it for non-cursor-like behaviour such as 3D camera control. Please see [`MouseMotion`] instead.
/// you should not use it for non-cursor-like behaviour such as 3D camera control. Please see `MouseMotion` instead.
///
/// [`WindowEvent::CursorMoved`]: https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.CursorMoved
/// [`MouseMotion`]: bevy_input::mouse::MouseMotion
#[derive(Event, Debug, Clone, PartialEq, Reflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(
Expand Down
20 changes: 0 additions & 20 deletions crates/bevy_window/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{PrimaryWindow, Window, WindowCloseRequested};

use bevy_app::AppExit;
use bevy_ecs::prelude::*;
use bevy_input::{keyboard::KeyCode, ButtonInput};

/// Exit the application when there are no open windows.
///
Expand Down Expand Up @@ -45,22 +44,3 @@ pub fn close_when_requested(mut commands: Commands, mut closed: EventReader<Wind
commands.entity(event.window).despawn();
}
}

/// Close the focused window whenever the escape key (<kbd>Esc</kbd>) is pressed
///
/// This is useful for examples or prototyping.
pub fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}

if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}
2 changes: 1 addition & 1 deletion crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub struct Window {
///
/// If enabled, the window will receive [`Ime`](crate::Ime) events instead of
/// [`ReceivedCharacter`](crate::ReceivedCharacter) or
/// [`KeyboardInput`](bevy_input::keyboard::KeyboardInput).
/// `KeyboardInput` from `bevy_input`.
///
/// IME should be enabled during text input, but not when you expect to get the exact key pressed.
///
Expand Down
1 change: 0 additions & 1 deletion examples/2d/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ fn main() {
rotate_to_player_system,
),
)
.add_systems(Update, bevy::window::close_on_esc)
.run();
}

Expand Down
3 changes: 1 addition & 2 deletions examples/3d/parallax_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::fmt;

use bevy::{prelude::*, render::render_resource::TextureFormat, window::close_on_esc};
use bevy::{prelude::*, render::render_resource::TextureFormat};

fn main() {
App::new()
Expand All @@ -19,7 +19,6 @@ fn main() {
update_parallax_depth_scale,
update_parallax_layers,
switch_method,
close_on_esc,
),
)
.run();
Expand Down
5 changes: 1 addition & 4 deletions examples/games/alien_cake_addict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ fn main() {
.add_systems(OnEnter(GameState::GameOver), display_score)
.add_systems(
Update,
(
gameover_keyboard.run_if(in_state(GameState::GameOver)),
bevy::window::close_on_esc,
),
gameover_keyboard.run_if(in_state(GameState::GameOver)),
)
.add_systems(OnExit(GameState::GameOver), teardown)
.run();
Expand Down
2 changes: 1 addition & 1 deletion examples/games/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn main() {
// `chain`ing systems together runs them in order
.chain(),
)
.add_systems(Update, (update_scoreboard, bevy::window::close_on_esc))
.add_systems(Update, update_scoreboard)
.run();
}

Expand Down
1 change: 0 additions & 1 deletion examples/window/multiple_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ fn main() {
// By default, a primary window gets spawned by `WindowPlugin`, contained in `DefaultPlugins`
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup_scene)
.add_systems(Update, bevy::window::close_on_esc)
.run();
}

Expand Down
9 changes: 1 addition & 8 deletions tests/window/resizing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ fn main() {
})
.insert_resource(ContractingY)
.add_systems(Startup, (setup_3d, setup_2d))
.add_systems(
Update,
(
change_window_size,
sync_dimensions,
bevy::window::close_on_esc,
),
)
.add_systems(Update, (change_window_size, sync_dimensions))
.run();
}

Expand Down