From e08c87847174b8285b5b05f59e66a2f4e7ed61c6 Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 10:40:15 -0500 Subject: [PATCH 01/17] Exposes Common Window Resolutions as an Enum. --- crates/bevy_window/src/window.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 8bc0cf902904e..c796f784e4b7d 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -714,6 +714,7 @@ impl Default for WindowResolution { scale_factor: 1.0, } } + } impl WindowResolution { @@ -869,6 +870,37 @@ impl From for WindowResolution { } } +/// Creates and defines a Resolution Enum to expose common resolutions. +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +pub enum Resolution{ + R360p, + R720p, + R1080p, + R2k, +} + +impl Resolution { + pub fn iter() -> impl Iterator { + [ + Resolution::R360p, + Resolution::R720p, + Resolution::R1080p, + Resolution::R2k, + ].into_iter() + } +} + +impl From for UVec2 { + fn from(resolution: Resolution) -> Self { + match resolution { + Resolution::R360p => Self::new(640, 360), + Resolution::R720p => Self::new(1280, 720), + Resolution::R1080p => Self::new(1920, 1080), + Resolution::R2k => Self::new(2560, 1440), + } + } +} + /// Defines if and how the [`Cursor`] is grabbed by a [`Window`]. /// /// ## Platform-specific From 38f080f27e752f489120f7900311cac99e711f3a Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 11:00:23 -0500 Subject: [PATCH 02/17] Added documentation to the variants and to iter() --- crates/bevy_window/src/window.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index c796f784e4b7d..e3fe1849e8bfb 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -873,13 +873,21 @@ impl From for WindowResolution { /// Creates and defines a Resolution Enum to expose common resolutions. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum Resolution{ + /// 640 x 360 R360p, + + /// 1280 x 720 R720p, + + /// 1920 x 1080 R1080p, + + /// 2560 x 1440 R2k, } impl Resolution { + /// Gets all the available resolutions as an iterator. pub fn iter() -> impl Iterator { [ Resolution::R360p, From 76ced3f4d60ef724c1de220c610c1d32c082c735 Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 11:02:39 -0500 Subject: [PATCH 03/17] Update documentation to fit with suggestions from review. Runs `cargo fmt` --- crates/bevy_window/src/window.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index e3fe1849e8bfb..80e58a03ab07c 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -714,7 +714,6 @@ impl Default for WindowResolution { scale_factor: 1.0, } } - } impl WindowResolution { @@ -870,9 +869,9 @@ impl From for WindowResolution { } } -/// Creates and defines a Resolution Enum to expose common resolutions. +// Common screen resolutions. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -pub enum Resolution{ +pub enum Resolution { /// 640 x 360 R360p, @@ -887,14 +886,15 @@ pub enum Resolution{ } impl Resolution { - /// Gets all the available resolutions as an iterator. + // Iterates through all [`Resolution`] variants. pub fn iter() -> impl Iterator { [ Resolution::R360p, Resolution::R720p, Resolution::R1080p, Resolution::R2k, - ].into_iter() + ] + .into_iter() } } From c8555a5a370ce85d0fc53ddd5fdcbfc76d2d84b6 Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 11:03:16 -0500 Subject: [PATCH 04/17] Fix typo. --- crates/bevy_window/src/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 80e58a03ab07c..0bdc8638ada49 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -869,7 +869,7 @@ impl From for WindowResolution { } } -// Common screen resolutions. +/// Common screen resolutions. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum Resolution { /// 640 x 360 From 8842456098b7e28b6f0aaacc9a382bbf573a68e8 Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 11:08:16 -0500 Subject: [PATCH 05/17] Fixes other typo. --- crates/bevy_window/src/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 0bdc8638ada49..1c55350e55083 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -886,7 +886,7 @@ pub enum Resolution { } impl Resolution { - // Iterates through all [`Resolution`] variants. + /// Iterates through all [`Resolution`] variants. pub fn iter() -> impl Iterator { [ Resolution::R360p, From bad94ba9ae8fdeee182ffb0595eb108a436dde19 Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:28:52 -0500 Subject: [PATCH 06/17] Rename Resolution to CommonScreenResolution. --- crates/bevy_window/src/window.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 1c55350e55083..cdb54b74ebde1 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -871,7 +871,7 @@ impl From for WindowResolution { /// Common screen resolutions. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] -pub enum Resolution { +pub enum CommonScreenResolution { /// 640 x 360 R360p, @@ -885,26 +885,26 @@ pub enum Resolution { R2k, } -impl Resolution { - /// Iterates through all [`Resolution`] variants. - pub fn iter() -> impl Iterator { +impl CommonScreenResolution { + /// Iterates through all [`CommonScreenResolution`] variants. + pub fn iter() -> impl Iterator { [ - Resolution::R360p, - Resolution::R720p, - Resolution::R1080p, - Resolution::R2k, + CommonScreenResolution::R360p, + CommonScreenResolution::R720p, + CommonScreenResolution::R1080p, + CommonScreenResolution::R2k, ] .into_iter() } } -impl From for UVec2 { - fn from(resolution: Resolution) -> Self { +impl From for UVec2 { + fn from(resolution: CommonScreenResolution) -> Self { match resolution { - Resolution::R360p => Self::new(640, 360), - Resolution::R720p => Self::new(1280, 720), - Resolution::R1080p => Self::new(1920, 1080), - Resolution::R2k => Self::new(2560, 1440), + CommonScreenResolution::R360p => Self::new(640, 360), + CommonScreenResolution::R720p => Self::new(1280, 720), + CommonScreenResolution::R1080p => Self::new(1920, 1080), + CommonScreenResolution::R2k => Self::new(2560, 1440), } } } From f8b8dfb59f1dea00bd3c4f4fe2dc0348096afdea Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:50:56 -0500 Subject: [PATCH 07/17] Update documentation --- crates/bevy_window/src/window.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index cdb54b74ebde1..06b879fd4bdc7 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -870,6 +870,10 @@ impl From for WindowResolution { } /// Common screen resolutions. +/// +/// These resolutions are common resolutions that are more than likely going to be used, using the +/// common name. This keeps developers from having to remember each resolution they wish to use, along +/// with makes things simpler for implementing menus. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum CommonScreenResolution { /// 640 x 360 From 884ed08e7c13c4cedde0b026e42628bca0d96acc Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:51:15 -0500 Subject: [PATCH 08/17] Add in From for WindowResolution. --- crates/bevy_window/src/window.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 06b879fd4bdc7..34181cccc0cc3 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -913,6 +913,17 @@ impl From for UVec2 { } } +impl From for WindowResolution { + fn from(resolution: CommonScreenResolution) -> Self { + match resolution { + CommonScreenResolution::R360p => Self::new(640., 360.), + CommonScreenResolution::R720p => Self::new(1280., 720.), + CommonScreenResolution::R1080p => Self::new(1920., 1080.), + CommonScreenResolution::R2k => Self::new(2560., 1440.), + } + } +} + /// Defines if and how the [`Cursor`] is grabbed by a [`Window`]. /// /// ## Platform-specific From 8deccf634c112fdbba4113e5d4f6d048ece46ac5 Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 13:11:13 -0500 Subject: [PATCH 09/17] Implement display for CommonScreenResolution. --- crates/bevy_window/src/window.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 34181cccc0cc3..dbd47516502f9 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -1,3 +1,4 @@ +use std::fmt::{Display, Formatter}; use std::num::NonZeroU32; use bevy_ecs::{ @@ -924,6 +925,12 @@ impl From for WindowResolution { } } +impl Display for CommonScreenResolution { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{} x {}", UVec2::from(self).x, UVec2::from(self).y) + } +} + /// Defines if and how the [`Cursor`] is grabbed by a [`Window`]. /// /// ## Platform-specific From a450128196a27543aecfda81a9e9cbe7ddc53e65 Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 13:16:42 -0500 Subject: [PATCH 10/17] Fix Display for CommonScreenResolution. --- crates/bevy_window/src/window.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index dbd47516502f9..c73ebc46166cd 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -709,8 +709,8 @@ pub struct WindowResolution { impl Default for WindowResolution { fn default() -> Self { WindowResolution { - physical_width: 1280, - physical_height: 720, + physical_width: UVec2::from(CommonScreenResolution::R720p).x, + physical_height: UVec2::from(CommonScreenResolution::R720p).y, scale_factor_override: None, scale_factor: 1.0, } @@ -927,7 +927,12 @@ impl From for WindowResolution { impl Display for CommonScreenResolution { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{} x {}", UVec2::from(self).x, UVec2::from(self).y) + match self { + CommonScreenResolution::R360p => write!(f, "{} x {}", 640, 360), + CommonScreenResolution::R720p => write!(f, "{} x {}", 1280, 720), + CommonScreenResolution::R1080p => write!(f, "{} x {}", 1920, 1080), + CommonScreenResolution::R2k => write!(f, "{} x {}", 2560, 1440), + } } } From a6893e90b24acdcf9ad12441648cba637afcbea0 Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 13:19:14 -0500 Subject: [PATCH 11/17] Fix formatting. --- crates/bevy_window/src/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index c73ebc46166cd..122d1ec252d9d 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -892,7 +892,7 @@ pub enum CommonScreenResolution { impl CommonScreenResolution { /// Iterates through all [`CommonScreenResolution`] variants. - pub fn iter() -> impl Iterator { + pub fn iter() -> impl Iterator { [ CommonScreenResolution::R360p, CommonScreenResolution::R720p, From c77a6408f209cc304711ba1012e5a291db4422c5 Mon Sep 17 00:00:00 2001 From: Sapein <5852983+Sapein@users.noreply.github.com> Date: Fri, 5 Jul 2024 14:14:12 -0500 Subject: [PATCH 12/17] Add in CommonScreenResolutions to window_settings.rs --- examples/window/window_settings.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/examples/window/window_settings.rs b/examples/window/window_settings.rs index 2e509e2fba7eb..06e685ad1b42d 100644 --- a/examples/window/window_settings.rs +++ b/examples/window/window_settings.rs @@ -7,6 +7,7 @@ use bevy::{ prelude::*, window::{CursorGrabMode, PresentMode, WindowLevel, WindowTheme}, }; +use bevy::window::{CommonScreenResolution, WindowResolution}; fn main() { App::new() @@ -15,7 +16,7 @@ fn main() { primary_window: Some(Window { title: "I am a window!".into(), name: Some("bevy.app".into()), - resolution: (500., 300.).into(), + resolution: CommonScreenResolution::R360p.into(), present_mode: PresentMode::AutoVsync, // Tells wasm to resize the window according to the available canvas fit_canvas_to_parent: true, @@ -45,6 +46,7 @@ fn main() { toggle_cursor, toggle_vsync, toggle_window_controls, + toggle_resolutions, cycle_cursor_icon, switch_level, make_visible, @@ -124,6 +126,25 @@ fn toggle_window_controls(input: Res>, mut windows: Query<& } } +fn toggle_resolutions(input: Res>, mut windows: Query<&mut Window>) { + if input.just_pressed(KeyCode::KeyR) { + let mut window = windows.single_mut(); + let current_resolution = CommonScreenResolution::iter().find(|&r| WindowResolution::from(r) == window.resolution); + window.resolution = if current_resolution.is_none() { + CommonScreenResolution::R360p.into() + } else { + let resolutions = CommonScreenResolution::iter().map(|r| r.into()).collect::>(); + let current_resolution_index = CommonScreenResolution::iter().position(|r| r == current_resolution.unwrap()).unwrap(); + + if current_resolution_index + 1 >= resolutions.len() { + resolutions[0].into() + } else { + resolutions[current_resolution_index + 1].into() + } + } + } +} + /// This system will then change the title during execution fn change_title(mut windows: Query<&mut Window>, time: Res