mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 14:40:19 +00:00
ddfafab971
# Objective Fix https://github.com/bevyengine/bevy/issues/4530 - Make it easier to open/close/modify windows by setting them up as `Entity`s with a `Window` component. - Make multiple windows very simple to set up. (just add a `Window` component to an entity and it should open) ## Solution - Move all properties of window descriptor to ~components~ a component. - Replace `WindowId` with `Entity`. - ~Use change detection for components to update backend rather than events/commands. (The `CursorMoved`/`WindowResized`/... events are kept for user convenience.~ Check each field individually to see what we need to update, events are still kept for user convenience. --- ## Changelog - `WindowDescriptor` renamed to `Window`. - Width/height consolidated into a `WindowResolution` component. - Requesting maximization/minimization is done on the [`Window::state`] field. - `WindowId` is now `Entity`. ## Migration Guide - Replace `WindowDescriptor` with `Window`. - Change `width` and `height` fields in a `WindowResolution`, either by doing ```rust WindowResolution::new(width, height) // Explicitly // or using From<_> for tuples for convenience (1920., 1080.).into() ``` - Replace any `WindowCommand` code to just modify the `Window`'s fields directly and creating/closing windows is now by spawning/despawning an entity with a `Window` component like so: ```rust let window = commands.spawn(Window { ... }).id(); // open window commands.entity(window).despawn(); // close window ``` ## Unresolved - ~How do we tell when a window is minimized by a user?~ ~Currently using the `Resize(0, 0)` as an indicator of minimization.~ No longer attempting to tell given how finnicky this was across platforms, now the user can only request that a window be maximized/minimized. ## Future work - Move `exit_on_close` functionality out from windowing and into app(?) - https://github.com/bevyengine/bevy/issues/5621 - https://github.com/bevyengine/bevy/issues/7099 - https://github.com/bevyengine/bevy/issues/7098 Co-authored-by: Carter Anderson <mcanders1@gmail.com>
95 lines
2.8 KiB
Rust
95 lines
2.8 KiB
Rust
///! This example illustrates how to resize windows, and how to respond to a window being resized.
|
|
use bevy::{prelude::*, window::WindowResized};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(ResolutionSettings {
|
|
large: Vec2::new(1920.0, 1080.0),
|
|
medium: Vec2::new(800.0, 600.0),
|
|
small: Vec2::new(640.0, 360.0),
|
|
})
|
|
.add_plugins(DefaultPlugins)
|
|
.add_startup_system(setup_camera)
|
|
.add_startup_system(setup_ui)
|
|
.add_system(on_resize_system)
|
|
.add_system(toggle_resolution)
|
|
.run();
|
|
}
|
|
|
|
/// Marker component for the text that displays the current reslution.
|
|
#[derive(Component)]
|
|
struct ResolutionText;
|
|
|
|
/// Stores the various window-resolutions we can select between.
|
|
#[derive(Resource)]
|
|
struct ResolutionSettings {
|
|
large: Vec2,
|
|
medium: Vec2,
|
|
small: Vec2,
|
|
}
|
|
|
|
// Spawns the camera that draws UI
|
|
fn setup_camera(mut cmd: Commands) {
|
|
cmd.spawn(Camera2dBundle::default());
|
|
}
|
|
|
|
// Spawns the UI
|
|
fn setup_ui(mut cmd: Commands, asset_server: Res<AssetServer>) {
|
|
// Node that fills entire background
|
|
cmd.spawn(NodeBundle {
|
|
style: Style {
|
|
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|root| {
|
|
// Text where we display current resolution
|
|
root.spawn((
|
|
TextBundle::from_section(
|
|
"Resolution",
|
|
TextStyle {
|
|
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
|
font_size: 50.0,
|
|
color: Color::BLACK,
|
|
},
|
|
),
|
|
ResolutionText,
|
|
));
|
|
});
|
|
}
|
|
|
|
/// This system shows how to request the window to a new resolution
|
|
fn toggle_resolution(
|
|
keys: Res<Input<KeyCode>>,
|
|
mut windows: Query<&mut Window>,
|
|
resolution: Res<ResolutionSettings>,
|
|
) {
|
|
let mut window = windows.single_mut();
|
|
|
|
if keys.just_pressed(KeyCode::Key1) {
|
|
let res = resolution.small;
|
|
window.resolution.set(res.x, res.y);
|
|
}
|
|
if keys.just_pressed(KeyCode::Key2) {
|
|
let res = resolution.medium;
|
|
window.resolution.set(res.x, res.y);
|
|
}
|
|
if keys.just_pressed(KeyCode::Key3) {
|
|
let res = resolution.large;
|
|
window.resolution.set(res.x, res.y);
|
|
}
|
|
}
|
|
|
|
/// This system shows how to respond to a window being resized.
|
|
/// Whenever the window is resized, the text will update with the new resolution.
|
|
fn on_resize_system(
|
|
mut q: Query<&mut Text, With<ResolutionText>>,
|
|
mut resize_reader: EventReader<WindowResized>,
|
|
) {
|
|
let mut text = q.single_mut();
|
|
for e in resize_reader.iter() {
|
|
// When resolution is being changed
|
|
text.sections[0].value = format!("{:.1} x {:.1}", e.width, e.height);
|
|
}
|
|
}
|