From fdbc5a14c74df9baabb49fcd79b3b492f7367d21 Mon Sep 17 00:00:00 2001 From: wandalen Date: Tue, 1 Feb 2022 02:32:07 +0200 Subject: [PATCH 01/12] add example window_resize --- examples/window/window_resize.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 examples/window/window_resize.rs diff --git a/examples/window/window_resize.rs b/examples/window/window_resize.rs new file mode 100644 index 0000000000000..b7048a7dcbac8 --- /dev/null +++ b/examples/window/window_resize.rs @@ -0,0 +1,21 @@ +//! This example illustrates how to handle window resize + +use bevy::prelude::*; +use bevy::window::WindowResized; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_system(on_resize_system) + .run(); +} + +/// +/// The system iterates resize events and print them. +/// + +pub fn on_resize_system(mut resize_reader: EventReader) { + for e in resize_reader.iter() { + println!("event {:?}", e); + } +} From 3f3db1655c03445da3fb840a8bdf9a86bb073c20 Mon Sep 17 00:00:00 2001 From: wandalen Date: Tue, 1 Feb 2022 02:45:59 +0200 Subject: [PATCH 02/12] Add example to examples/README.md. Improve description. --- examples/README.md | 1 + examples/window/window_resize.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index e5330e31cb752..ee578d1b0f02c 100644 --- a/examples/README.md +++ b/examples/README.md @@ -257,6 +257,7 @@ Example | File | Description `scale_factor_override` | [`window/scale_factor_override.rs`](./window/scale_factor_override.rs) | Illustrates how to customize the default window settings `transparent_window` | [`window/transparent_window.rs`](./window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration `window_settings` | [`window/window_settings.rs`](./window/window_settings.rs) | Demonstrates customizing default window settings +`window_resize` | [`window/window_resize.rs`](./window/window_resize.rs) | Demonstrates how to handle window resize event # Platform-Specific Examples diff --git a/examples/window/window_resize.rs b/examples/window/window_resize.rs index b7048a7dcbac8..24c365c335bcf 100644 --- a/examples/window/window_resize.rs +++ b/examples/window/window_resize.rs @@ -1,4 +1,4 @@ -//! This example illustrates how to handle window resize +//! This example illustrates how to handle window resize event use bevy::prelude::*; use bevy::window::WindowResized; From 338562081ad9a555304f00d478a3b5752dc6ec0c Mon Sep 17 00:00:00 2001 From: wandalen Date: Tue, 1 Feb 2022 02:47:52 +0200 Subject: [PATCH 03/12] Add example to cargo file. --- Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 9e0b659358b7c..13463061f9124 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -502,6 +502,10 @@ path = "examples/window/transparent_window.rs" name = "window_settings" path = "examples/window/window_settings.rs" +[[example]] +name = "window_resize" +path = "examples/window/window_resize.rs" + # Android [[example]] crate-type = ["cdylib"] From 3ea6ff9970f132d2d75e0b3c4d36f9ff939c4b37 Mon Sep 17 00:00:00 2001 From: wandalen Date: Tue, 1 Feb 2022 02:55:16 +0200 Subject: [PATCH 04/12] Example window_resize: escape on exit --- examples/window/window_resize.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/window/window_resize.rs b/examples/window/window_resize.rs index 24c365c335bcf..e9aaa64a533b8 100644 --- a/examples/window/window_resize.rs +++ b/examples/window/window_resize.rs @@ -2,11 +2,13 @@ use bevy::prelude::*; use bevy::window::WindowResized; +use bevy::input::system::exit_on_esc_system; fn main() { App::new() .add_plugins(DefaultPlugins) .add_system(on_resize_system) + .add_system(exit_on_esc_system) .run(); } From 1b4a8c934fa4413b65635de63827ae5421c904e9 Mon Sep 17 00:00:00 2001 From: wandalen Date: Tue, 1 Feb 2022 02:56:18 +0200 Subject: [PATCH 05/12] Example window_resize: fix formatting --- examples/window/window_resize.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/window/window_resize.rs b/examples/window/window_resize.rs index e9aaa64a533b8..d62886f5dce59 100644 --- a/examples/window/window_resize.rs +++ b/examples/window/window_resize.rs @@ -18,6 +18,6 @@ fn main() { pub fn on_resize_system(mut resize_reader: EventReader) { for e in resize_reader.iter() { - println!("event {:?}", e); + println!("event {:?}", e); } } From 4faf9ca07c109413a3e54803bdb36e9eb49f528e Mon Sep 17 00:00:00 2001 From: wandalen Date: Tue, 1 Feb 2022 03:07:54 +0200 Subject: [PATCH 06/12] Example window_resize: rearrange uses to solve format issue --- examples/window/window_resize.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/window/window_resize.rs b/examples/window/window_resize.rs index d62886f5dce59..2d1ab232108c4 100644 --- a/examples/window/window_resize.rs +++ b/examples/window/window_resize.rs @@ -1,8 +1,8 @@ //! This example illustrates how to handle window resize event +use bevy::input::system::exit_on_esc_system; use bevy::prelude::*; use bevy::window::WindowResized; -use bevy::input::system::exit_on_esc_system; fn main() { App::new() From 2d60a2b75bc82720c652afb397724a39e00645dd Mon Sep 17 00:00:00 2001 From: wandalen Date: Thu, 3 Feb 2022 09:06:03 +0200 Subject: [PATCH 07/12] example window_resize: minor adjustments --- examples/README.md | 2 +- examples/window/window_resize.rs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/README.md b/examples/README.md index ee578d1b0f02c..b61ddfcaf7415 100644 --- a/examples/README.md +++ b/examples/README.md @@ -257,7 +257,7 @@ Example | File | Description `scale_factor_override` | [`window/scale_factor_override.rs`](./window/scale_factor_override.rs) | Illustrates how to customize the default window settings `transparent_window` | [`window/transparent_window.rs`](./window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration `window_settings` | [`window/window_settings.rs`](./window/window_settings.rs) | Demonstrates customizing default window settings -`window_resize` | [`window/window_resize.rs`](./window/window_resize.rs) | Demonstrates how to handle window resize event +`window_resize` | [`window/window_resize.rs`](./window/window_resize.rs) | Demonstrates how to handle window resize events and fit the window # Platform-Specific Examples diff --git a/examples/window/window_resize.rs b/examples/window/window_resize.rs index 2d1ab232108c4..be5b5de385ff7 100644 --- a/examples/window/window_resize.rs +++ b/examples/window/window_resize.rs @@ -1,4 +1,4 @@ -//! This example illustrates how to handle window resize event +//! This example illustrates how to handle window resize events and fit the window use bevy::input::system::exit_on_esc_system; use bevy::prelude::*; @@ -12,9 +12,7 @@ fn main() { .run(); } -/// /// The system iterates resize events and print them. -/// pub fn on_resize_system(mut resize_reader: EventReader) { for e in resize_reader.iter() { From 23b4c9acae3febe679e90814a3bcae67fb4ce9bb Mon Sep 17 00:00:00 2001 From: wandalen Date: Thu, 3 Feb 2022 09:09:03 +0200 Subject: [PATCH 08/12] example window_resize: minor adjustments --- examples/window/window_resize.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/window/window_resize.rs b/examples/window/window_resize.rs index be5b5de385ff7..3ea38fba87710 100644 --- a/examples/window/window_resize.rs +++ b/examples/window/window_resize.rs @@ -12,8 +12,8 @@ fn main() { .run(); } -/// The system iterates resize events and print them. +/// The system iterates resize events and print them. pub fn on_resize_system(mut resize_reader: EventReader) { for e in resize_reader.iter() { println!("event {:?}", e); From 6bcb405a8ef02147540c276262c83d25019c5f25 Mon Sep 17 00:00:00 2001 From: wandalen Date: Thu, 3 Feb 2022 09:49:10 +0200 Subject: [PATCH 09/12] example window_resize: remove extra line --- examples/window/window_resize.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/window/window_resize.rs b/examples/window/window_resize.rs index 3ea38fba87710..d9605a9f06883 100644 --- a/examples/window/window_resize.rs +++ b/examples/window/window_resize.rs @@ -12,7 +12,6 @@ fn main() { .run(); } - /// The system iterates resize events and print them. pub fn on_resize_system(mut resize_reader: EventReader) { for e in resize_reader.iter() { From 5c582291345e2ed00fa639c4567897b99e6b6b77 Mon Sep 17 00:00:00 2001 From: Andreas Weibye <13300393+Weibye@users.noreply.github.com> Date: Sat, 27 Aug 2022 14:54:17 +0200 Subject: [PATCH 10/12] Rename file and fix metadata --- Cargo.toml | 10 ++++++++-- examples/README.md | 2 +- .../window/{window_resize.rs => window_resizing.rs} | 0 3 files changed, 9 insertions(+), 3 deletions(-) rename examples/window/{window_resize.rs => window_resizing.rs} (100%) diff --git a/Cargo.toml b/Cargo.toml index 868f0ad37fbed..7628b50fcbab1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1528,8 +1528,14 @@ path = "tests/window/minimising.rs" hidden = true [[example]] -name = "window_resize" -path = "examples/window/window_resize.rs" +name = "window_resizing" +path = "examples/window/window_resizing.rs" + +[package.metadata.example.window_resizing] +name = "Window Resizing" +description = "Demonstrates resizing and responding to resizing a window" +category = "Window" +wasm = true # Android [[example]] diff --git a/examples/README.md b/examples/README.md index 07965fd74f6e8..c8705bd2e1262 100644 --- a/examples/README.md +++ b/examples/README.md @@ -324,7 +324,7 @@ Example | Description [Scale Factor Override](../examples/window/scale_factor_override.rs) | Illustrates how to customize the default window settings [Transparent Window](../examples/window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration [Window Settings](../examples/window/window_settings.rs) | Demonstrates customizing default window settings -`window_resize` | [`window/window_resize.rs`](./window/window_resize.rs) | Demonstrates how to handle window resize events and fit the window +[Window Resizing](../examples/window/window_resizing.rs) | Demonstrates how to resize a window and how to respond to resize events # Tests diff --git a/examples/window/window_resize.rs b/examples/window/window_resizing.rs similarity index 100% rename from examples/window/window_resize.rs rename to examples/window/window_resizing.rs From 30c385f2dd118b6a3d10fe8a823faf99ffd6b071 Mon Sep 17 00:00:00 2001 From: Andreas Weibye <13300393+Weibye@users.noreply.github.com> Date: Sat, 27 Aug 2022 14:49:28 +0200 Subject: [PATCH 11/12] Flesh out example --- examples/window/window_resizing.rs | 91 +++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 9 deletions(-) diff --git a/examples/window/window_resizing.rs b/examples/window/window_resizing.rs index d9605a9f06883..a5debeda738c6 100644 --- a/examples/window/window_resizing.rs +++ b/examples/window/window_resizing.rs @@ -1,20 +1,93 @@ -//! This example illustrates how to handle window resize events and fit the window - -use bevy::input::system::exit_on_esc_system; -use bevy::prelude::*; -use bevy::window::WindowResized; +///! 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(exit_on_esc_system) + .add_system(toggle_resolution) .run(); } -/// The system iterates resize events and print them. -pub fn on_resize_system(mut resize_reader: EventReader) { +/// 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_bundle(Camera2dBundle::default()); +} + +// Spawns the UI +fn setup_ui(mut cmd: Commands, asset_server: Res) { + // Node that fills entire background + cmd.spawn_bundle(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_bundle(TextBundle::from_section( + "Resolution", + TextStyle { + font: asset_server.load("fonts/FiraMono-Medium.ttf"), + font_size: 50.0, + color: Color::BLACK, + }, + )) + .insert(ResolutionText); + }); +} + +/// This system shows how to request the window to a new resolution +fn toggle_resolution( + keys: Res>, + mut windows: ResMut, + resolution: Res, +) { + let window = windows.get_primary_mut().unwrap(); + + if keys.just_pressed(KeyCode::Key1) { + let res = resolution.small; + window.set_resolution(res.x, res.y); + } + if keys.just_pressed(KeyCode::Key2) { + let res = resolution.medium; + window.set_resolution(res.x, res.y); + } + if keys.just_pressed(KeyCode::Key3) { + let res = resolution.large; + window.set_resolution(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>, + mut resize_reader: EventReader, +) { + let mut text = q.get_single_mut().unwrap(); for e in resize_reader.iter() { - println!("event {:?}", e); + // When resolution is being changed + text.sections[0].value = format!("{:.1} x {:.1}", e.width, e.height); } } From 2aba58d653269f4655d081b80802dac7769faf32 Mon Sep 17 00:00:00 2001 From: Andreas Weibye <13300393+Weibye@users.noreply.github.com> Date: Sat, 27 Aug 2022 15:12:28 +0200 Subject: [PATCH 12/12] Rebuilding example page --- examples/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/README.md b/examples/README.md index c8705bd2e1262..9451c5c1f23cf 100644 --- a/examples/README.md +++ b/examples/README.md @@ -323,8 +323,8 @@ Example | Description [Multiple Windows](../examples/window/multiple_windows.rs) | Demonstrates creating multiple windows, and rendering to them [Scale Factor Override](../examples/window/scale_factor_override.rs) | Illustrates how to customize the default window settings [Transparent Window](../examples/window/transparent_window.rs) | Illustrates making the window transparent and hiding the window decoration +[Window Resizing](../examples/window/window_resizing.rs) | Demonstrates resizing and responding to resizing a window [Window Settings](../examples/window/window_settings.rs) | Demonstrates customizing default window settings -[Window Resizing](../examples/window/window_resizing.rs) | Demonstrates how to resize a window and how to respond to resize events # Tests