Skip to content

Commit

Permalink
Add size and physical_size to window (#12238)
Browse files Browse the repository at this point in the history
This is an implementation within `bevy_window::window` that fixes
#12229.

# Objective

Fixes #12229, allow users to retrieve the window's size and physical
size as Vectors without having to manually construct them using
`height()` and `width()` or `physical_height()` and `physical_width()`

## Solution

As suggested in #12229, created two public functions within `window`:
`size() -> Vec` and `physical_size() -> UVec` that return the needed
Vectors ready-to-go.

### Discussion

My first FOSS PRQ ever, so bear with me a bit. I'm new to this.

- I replaced instances of ```Vec2::new(window.width(),
window.height());``` or `UVec2::new(window.physical_width(),
window.physical_height());` within bevy examples be replaced with their
`size()`/`physical_size()` counterparts?
- Discussion within #12229 still holds: should these also be added to
WindowResolution?
  • Loading branch information
hi-names-nat authored Mar 1, 2024
1 parent f9cc91d commit cc32610
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 deletions.
5 changes: 1 addition & 4 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,7 @@ impl NormalizedRenderTarget {
.into_iter()
.find(|(entity, _)| *entity == window_ref.entity())
.map(|(_, window)| RenderTargetInfo {
physical_size: UVec2::new(
window.resolution.physical_width(),
window.resolution.physical_height(),
),
physical_size: window.physical_size(),
scale_factor: window.resolution.scale_factor(),
}),
NormalizedRenderTarget::Image(image_handle) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ pub fn resolve_outlines_system(
) {
let viewport_size = primary_window
.get_single()
.map(|window| Vec2::new(window.resolution.width(), window.resolution.height()))
.map(|window| window.size())
.unwrap_or(Vec2::ZERO)
/ ui_scale.0;

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/render/ui_material_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ pub fn extract_ui_material_nodes<M: UiMaterial>(
) {
let ui_logical_viewport_size = windows
.get_single()
.map(|window| Vec2::new(window.resolution.width(), window.resolution.height()))
.map(|window| window.size())
.unwrap_or(Vec2::ZERO)
// The logical window resolution returned by `Window` only takes into account the window scale factor and not `UiScale`,
// so we have to divide by `UiScale` to get the size of the UI viewport.
Expand Down
30 changes: 29 additions & 1 deletion crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy_ecs::{
entity::{Entity, EntityMapper, MapEntities},
prelude::{Component, ReflectComponent},
};
use bevy_math::{DVec2, IVec2, Vec2};
use bevy_math::{DVec2, IVec2, UVec2, Vec2};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};

#[cfg(feature = "serialize")]
Expand Down Expand Up @@ -313,6 +313,14 @@ impl Window {
self.resolution.height()
}

/// The window's client size in logical pixels
///
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
#[inline]
pub fn size(&self) -> Vec2 {
self.resolution.size()
}

/// The window's client area width in physical pixels.
///
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
Expand All @@ -329,6 +337,14 @@ impl Window {
self.resolution.physical_height()
}

/// The window's client size in physical pixels
///
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
#[inline]
pub fn physical_size(&self) -> bevy_math::UVec2 {
self.resolution.physical_size()
}

/// The window's scale factor.
///
/// Ratio of physical size to logical size, see [`WindowResolution`].
Expand Down Expand Up @@ -664,6 +680,12 @@ impl WindowResolution {
self.physical_height() as f32 / self.scale_factor()
}

/// The window's client size in logical pixels
#[inline]
pub fn size(&self) -> Vec2 {
Vec2::new(self.width(), self.height())
}

/// The window's client area width in physical pixels.
#[inline]
pub fn physical_width(&self) -> u32 {
Expand All @@ -676,6 +698,12 @@ impl WindowResolution {
self.physical_height
}

/// The window's client size in physical pixels
#[inline]
pub fn physical_size(&self) -> UVec2 {
UVec2::new(self.physical_width, self.physical_height)
}

/// The ratio of physical pixels to logical pixels.
///
/// `physical_pixels = logical_pixels * scale_factor`
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/split_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fn set_camera_viewports(
// A resize_event is sent when the window is first created, allowing us to reuse this system for initial setup.
for resize_event in resize_events.read() {
let window = windows.get(resize_event.window).unwrap();
let size = UVec2::new(window.physical_width(), window.physical_height()) / 2;
let size = window.physical_size() / 2;

for (camera_position, mut camera) in &mut query {
camera.viewport = Some(Viewport {
Expand Down
2 changes: 1 addition & 1 deletion examples/games/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn collisions(
mut query: Query<(&mut Velocity, &mut Transform), With<Contributor>>,
) {
let window = windows.single();
let window_size = Vec2::new(window.width(), window.height());
let window_size = window.size();

let collision_area = Aabb2d::new(Vec2::ZERO, (window_size - SPRITE_SIZE) / 2.);

Expand Down
8 changes: 2 additions & 6 deletions examples/stress_tests/bevymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,7 @@ fn spawn_birds(
let bird_x = (primary_window_resolution.width() / -2.) + HALF_BIRD_SIZE;
let bird_y = (primary_window_resolution.height() / 2.) - HALF_BIRD_SIZE;

let half_extents = 0.5
* Vec2::new(
primary_window_resolution.width(),
primary_window_resolution.height(),
);
let half_extents = 0.5 * primary_window_resolution.size();

let color = counter.color;
let current_count = counter.count;
Expand Down Expand Up @@ -509,7 +505,7 @@ fn handle_collision(half_extents: Vec2, translation: &Vec3, velocity: &mut Vec3)
fn collision_system(windows: Query<&Window>, mut bird_query: Query<(&mut Bird, &Transform)>) {
let window = windows.single();

let half_extents = 0.5 * Vec2::new(window.width(), window.height());
let half_extents = 0.5 * window.size();

for (mut bird, transform) in &mut bird_query {
handle_collision(half_extents, &transform.translation, &mut bird.velocity);
Expand Down

0 comments on commit cc32610

Please sign in to comment.