Move close_on_esc to bevy_dev_tools (#12855)

# Objective

- As @james7132 said [on
Discord](https://discord.com/channels/691052431525675048/692572690833473578/1224626740773523536),
the `close_on_esc` system is forcing `bevy_window` to depend on
`bevy_input`.
- `close_on_esc` is not likely to be used in production, so it arguably
does not have a place in `bevy_window`.

## Solution

- As suggested by @afonsolage, move `close_on_esc` into
`bevy_dev_tools`.
  - Add an example to the documentation too.
- Remove `bevy_window`'s dependency on `bevy_input`.
- Add `bevy_reflect`'s `smol_str` feature to `bevy_window` because it
was implicitly depended upon with `bevy_input` before it was removed.
- Remove any usage of `close_on_esc` from the examples.
- `bevy_dev_tools` is not enabled by default. I personally find it
frustrating to run examples with additional features, so I opted to
remove it entirely.
  - This is up for discussion if you have an alternate solution.

---

## Changelog

- Moved `bevy_window::close_on_esc` to `bevy_dev_tools::close_on_esc`.
- Removed usage of `bevy_dev_tools::close_on_esc` from all examples.

## Migration Guide

`bevy_window::close_on_esc` has been moved to
`bevy_dev_tools::close_on_esc`. You will first need to enable
`bevy_dev_tools` as a feature in your `Cargo.toml`:

```toml
[dependencies]
bevy = { version = "0.14", features = ["bevy_dev_tools"] }
```

Finally, modify any imports to use `bevy_dev_tools` instead:

```rust
// Old:
// use bevy:🪟:close_on_esc;

// New:
use bevy::dev_tools::close_on_esc;

App::new()
    .add_systems(Update, close_on_esc)
    // ...
    .run();
```
This commit is contained in:
BD103 2024-04-02 21:29:06 -04:00 committed by GitHub
parent 017ffc5a7b
commit 97131e1909
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 45 additions and 43 deletions

View file

@ -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();
}
}
}

View file

@ -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.
///

View file

@ -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 }

View file

@ -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(

View file

@ -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.
///
@ -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();
}
}
}

View file

@ -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.
///

View file

@ -17,7 +17,6 @@ fn main() {
rotate_to_player_system,
),
)
.add_systems(Update, bevy::window::close_on_esc)
.run();
}

View file

@ -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()
@ -19,7 +19,6 @@ fn main() {
update_parallax_depth_scale,
update_parallax_layers,
switch_method,
close_on_esc,
),
)
.run();

View file

@ -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();

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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();
}