Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposes Common Window Resolutions as an Enum. #14158

Closed
wants to merge 17 commits into from
Closed
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
69 changes: 67 additions & 2 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::{Display, Formatter};
use std::num::NonZeroU32;

use bevy_ecs::{
Expand Down Expand Up @@ -708,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,
}
Expand Down Expand Up @@ -869,6 +870,70 @@ impl From<DVec2> 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)]
#[non_exhaustive]
pub enum CommonScreenResolution {
/// 640 x 360
R360p,

/// 1280 x 720
R720p,

/// 1920 x 1080
R1080p,

/// 2560 x 1440
R2k,
Copy link

@GunboatDiplomat GunboatDiplomat Jul 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling QHD resolution 2K is a misnomer. 2K is closer to 1080p (FullHD) than 2.5k (QHD).

}

impl CommonScreenResolution {
/// Iterates through all [`CommonScreenResolution`] variants.
pub fn iter() -> impl Iterator<Item = CommonScreenResolution> {
[
CommonScreenResolution::R360p,
CommonScreenResolution::R720p,
CommonScreenResolution::R1080p,
CommonScreenResolution::R2k,
]
.into_iter()
}
}

impl From<CommonScreenResolution> for UVec2 {
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),
}
}
}

impl From<CommonScreenResolution> for Vec2 {
fn from(resolution: CommonScreenResolution) -> Self {
UVec2::from(resolution).as_vec2()
}
}

impl From<CommonScreenResolution> for WindowResolution {
fn from(resolution: CommonScreenResolution) -> Self {
WindowResolution::from(Vec2::from(resolution))
}
}

impl Display for CommonScreenResolution {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let res = UVec2::from(*self);
write!(f, "{} x {}", res.x, res.y)
}
}

/// Defines if and how the [`Cursor`] is grabbed by a [`Window`].
///
/// ## Platform-specific
Expand Down
5 changes: 3 additions & 2 deletions examples/window/window_resizing.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! This example illustrates how to resize windows, and how to respond to a window being resized.
use bevy::window::CommonScreenResolution;
use bevy::{prelude::*, window::WindowResized};

fn main() {
App::new()
.insert_resource(ResolutionSettings {
large: Vec2::new(1920.0, 1080.0),
large: CommonScreenResolution::R1080p.into(),
medium: Vec2::new(800.0, 600.0),
small: Vec2::new(640.0, 360.0),
small: CommonScreenResolution::R360p.into(),
})
.add_plugins(DefaultPlugins)
.add_systems(Startup, (setup_camera, setup_ui))
Expand Down
28 changes: 27 additions & 1 deletion examples/window/window_settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Illustrates how to change window settings and shows how to affect
//! the mouse pointer in various ways.

use bevy::window::{CommonScreenResolution, WindowResolution};
use bevy::{
core::FrameCount,
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
Expand All @@ -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,
Expand Down Expand Up @@ -45,6 +46,7 @@ fn main() {
toggle_cursor,
toggle_vsync,
toggle_window_controls,
toggle_resolutions,
cycle_cursor_icon,
switch_level,
make_visible,
Expand Down Expand Up @@ -124,6 +126,30 @@ fn toggle_window_controls(input: Res<ButtonInput<KeyCode>>, mut windows: Query<&
}
}

fn toggle_resolutions(input: Res<ButtonInput<KeyCode>>, 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(std::convert::Into::into)
.collect::<Vec<CommonScreenResolution>>();
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()
}
}
}
}
Comment on lines +129 to +151
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be more idiomatic if CurrentResolution was a Resource initialized at CommonScreenResolution::R360p imo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Iterator::cycle(), store that as a resource and call next every time input is pressed


/// This system will then change the title during execution
fn change_title(mut windows: Query<&mut Window>, time: Res<Time>) {
let mut window = windows.single_mut();
Expand Down