Skip to content

Commit

Permalink
update Window's width & height methods to return f32 (#1033)
Browse files Browse the repository at this point in the history
update `Window`'s `width` & `height` methods to return `f32`
  • Loading branch information
blunted2night authored Dec 13, 2020
1 parent e511cdb commit d2e4327
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 87 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn camera_system<T: CameraProjection + Component>(
for (entity, mut camera, mut camera_projection) in queries.q0_mut().iter_mut() {
if let Some(window) = windows.get(camera.window) {
if changed_window_ids.contains(&window.id()) || added_cameras.contains(&entity) {
camera_projection.update(window.logical_width(), window.logical_height());
camera_projection.update(window.width(), window.height());
camera.projection_matrix = camera_projection.get_projection_matrix();
camera.depth_calculation = camera_projection.depth_calculation();
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ impl FlexSurface {
*node,
stretch::style::Style {
size: stretch::geometry::Size {
width: stretch::style::Dimension::Points(window.logical_width()),
height: stretch::style::Dimension::Points(window.logical_height()),
width: stretch::style::Dimension::Points(window.width()),
height: stretch::style::Dimension::Points(window.height()),
},
..Default::default()
},
Expand Down
100 changes: 71 additions & 29 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,29 @@ impl Default for WindowId {
}
}

/// An operating system window that can present content and receive user input.
///
/// ## Window Sizes
///
/// There are three sizes associated with a window. The physical size which is
/// the height and width in physical pixels on the monitor. The logical size
/// which is the physical size scaled by an operating system provided factor to
/// account for monitors with differing pixel densities or user preference. And
/// the requested size, measured in logical pixels, which is the value submitted
/// to the API when creating the window, or requesting that it be resized.
///
/// The actual size, in logical pixels, of the window may not match the
/// requested size due to operating system limits on the window size, or the
/// quantization of the logical size when converting the physical size to the
/// logical size through the scaling factor.
#[derive(Debug)]
pub struct Window {
id: WindowId,
requested_width: f32,
requested_height: f32,
physical_width: u32,
physical_height: u32,
scale_factor: f64,
title: String,
vsync: bool,
resizable: bool,
Expand All @@ -48,7 +66,6 @@ pub struct Window {
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,
command_queue: Vec<WindowCommand>,
scale_factor: f64,
}

#[derive(Debug)]
Expand All @@ -61,8 +78,7 @@ pub enum WindowCommand {
title: String,
},
SetResolution {
physical_width: u32,
physical_height: u32,
resolution: (f32, f32),
},
SetVsync {
vsync: bool,
Expand Down Expand Up @@ -100,11 +116,20 @@ pub enum WindowMode {
}

impl Window {
pub fn new(id: WindowId, window_descriptor: &WindowDescriptor) -> Self {
pub fn new(
id: WindowId,
window_descriptor: &WindowDescriptor,
physical_width: u32,
physical_height: u32,
scale_factor: f64,
) -> Self {
Window {
id,
physical_height: window_descriptor.height,
physical_width: window_descriptor.width,
requested_width: window_descriptor.width,
requested_height: window_descriptor.height,
physical_width,
physical_height,
scale_factor,
title: window_descriptor.title.clone(),
vsync: window_descriptor.vsync,
resizable: window_descriptor.resizable,
Expand All @@ -116,7 +141,6 @@ impl Window {
#[cfg(target_arch = "wasm32")]
canvas: window_descriptor.canvas.clone(),
command_queue: Vec::new(),
scale_factor: 1.0,
}
}

Expand All @@ -125,31 +149,45 @@ impl Window {
self.id
}

/// The current logical width of the window's client area.
#[inline]
pub fn width(&self) -> u32 {
self.logical_width() as u32
pub fn width(&self) -> f32 {
(self.physical_width as f64 / self.scale_factor) as f32
}

/// The current logical height of the window's client area.
#[inline]
pub fn height(&self) -> u32 {
self.logical_height() as u32
pub fn height(&self) -> f32 {
(self.physical_height as f64 / self.scale_factor) as f32
}

/// The requested window client area width in logical pixels from window
/// creation or the last call to [set_resolution](Window::set_resolution).
///
/// This may differ from the actual width depending on OS size limits and
/// the scaling factor for high DPI monitors.
#[inline]
pub fn logical_width(&self) -> f32 {
(self.physical_width as f64 / self.scale_factor) as f32
pub fn requested_width(&self) -> f32 {
self.requested_width
}

/// The requested window client area height in logical pixels from window
/// creation or the last call to [set_resolution](Window::set_resolution).
///
/// This may differ from the actual width depending on OS size limits and
/// the scaling factor for high DPI monitors.
#[inline]
pub fn logical_height(&self) -> f32 {
(self.physical_height as f64 / self.scale_factor) as f32
pub fn requested_height(&self) -> f32 {
self.requested_height
}

/// The window's client area width in physical pixels.
#[inline]
pub fn physical_width(&self) -> u32 {
self.physical_width
}

/// The window's client area height in physical pixels.
#[inline]
pub fn physical_height(&self) -> u32 {
self.physical_height
Expand All @@ -161,28 +199,32 @@ impl Window {
.push(WindowCommand::SetMaximized { maximized });
}

pub fn set_resolution(&mut self, width: u32, height: u32) {
self.physical_width = (width as f64 * self.scale_factor) as u32;
self.physical_height = (height as f64 * self.scale_factor) as u32;
/// Request the OS to resize the window such the the client area matches the
/// specified width and height.
pub fn set_resolution(&mut self, width: f32, height: f32) {
self.requested_width = width;
self.requested_height = height;
self.command_queue.push(WindowCommand::SetResolution {
physical_width: self.physical_width,
physical_height: self.physical_height,
resolution: (self.requested_width, self.requested_height),
});
}

#[allow(missing_docs)]
#[inline]
pub fn update_physical_size_from_backend(&mut self, width: u32, height: u32) {
self.physical_width = width;
self.physical_height = height;
pub fn update_scale_factor_from_backend(&mut self, scale_factor: f64) {
self.scale_factor = scale_factor;
}

#[allow(missing_docs)]
#[inline]
pub fn update_scale_factor_from_backend(&mut self, scale_factor: f64) {
self.scale_factor = scale_factor;
pub fn update_actual_size_from_backend(&mut self, physical_width: u32, physical_height: u32) {
self.physical_width = physical_width;
self.physical_height = physical_height;
}

/// The ratio of physical pixels to logical pixels
///
/// `physical_pixels = logical_pixels * scale_factor`
#[inline]
pub fn scale_factor(&self) -> f64 {
self.scale_factor
Expand Down Expand Up @@ -291,8 +333,8 @@ impl Window {

#[derive(Debug, Clone)]
pub struct WindowDescriptor {
pub width: u32,
pub height: u32,
pub width: f32,
pub height: f32,
pub title: String,
pub vsync: bool,
pub resizable: bool,
Expand All @@ -308,8 +350,8 @@ impl Default for WindowDescriptor {
fn default() -> Self {
WindowDescriptor {
title: "bevy".to_string(),
width: 1280,
height: 720,
width: 1280.,
height: 720.,
vsync: true,
resizable: true,
decorations: true,
Expand Down
38 changes: 21 additions & 17 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use bevy_ecs::{Resources, World};
use bevy_math::Vec2;
use bevy_utils::tracing::{error, trace};
use bevy_window::{
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window,
WindowCloseRequested, WindowCreated, WindowFocused, WindowResized, Windows,
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, WindowCloseRequested,
WindowCreated, WindowFocused, WindowResized, Windows,
};
use winit::{
event::{self, DeviceEvent, Event, WindowEvent},
Expand Down Expand Up @@ -68,13 +68,12 @@ fn change_window(_: &mut World, resources: &mut Resources) {
window.set_title(&title);
}
bevy_window::WindowCommand::SetResolution {
physical_width,
physical_height,
resolution: (logical_width, logical_height),
} => {
let window = winit_windows.get_window(id).unwrap();
window.set_inner_size(winit::dpi::PhysicalSize::new(
physical_width,
physical_height,
window.set_inner_size(winit::dpi::LogicalSize::new(
logical_width,
logical_height,
));
}
bevy_window::WindowCommand::SetVsync { .. } => (),
Expand Down Expand Up @@ -194,14 +193,13 @@ pub fn winit_runner(mut app: App) {
let mut windows = app.resources.get_mut::<Windows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
let window = windows.get_mut(window_id).unwrap();
window.update_physical_size_from_backend(size.width, size.height);

window.update_actual_size_from_backend(size.width, size.height);
let mut resize_events =
app.resources.get_mut::<Events<WindowResized>>().unwrap();
resize_events.send(WindowResized {
id: window_id,
height: window.logical_height(),
width: window.logical_width(),
width: window.width(),
height: window.height(),
});
}
WindowEvent::CloseRequested => {
Expand Down Expand Up @@ -299,7 +297,7 @@ pub fn winit_runner(mut app: App) {

// FIXME?: On Android window start is top while on PC/Linux/OSX on bottom
if cfg!(target_os = "android") {
let window_height = windows.get_primary().unwrap().logical_height();
let window_height = windows.get_primary().unwrap().height();
location.y = window_height - location.y;
}
touch_input_events.send(converters::convert_touch_input(touch, location));
Expand All @@ -326,11 +324,13 @@ pub fn winit_runner(mut app: App) {
let mut windows = app.resources.get_mut::<Windows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
let window = windows.get_mut(window_id).unwrap();
window.update_physical_size_from_backend(
window.update_actual_size_from_backend(
new_inner_size.width,
new_inner_size.height,
);
window.update_scale_factor_from_backend(scale_factor);
// should we send a resize event to indicate the change in
// logical size?
}
WindowEvent::Focused(focused) => {
let mut focused_events =
Expand Down Expand Up @@ -382,10 +382,14 @@ fn handle_create_window_events(
let create_window_events = resources.get::<Events<CreateWindow>>().unwrap();
let mut window_created_events = resources.get_mut::<Events<WindowCreated>>().unwrap();
for create_window_event in create_window_event_reader.iter(&create_window_events) {
let mut window = Window::new(create_window_event.id, &create_window_event.descriptor);
winit_windows.create_window(event_loop, &mut window);
let window_id = window.id();
let window = winit_windows.create_window(
event_loop,
create_window_event.id,
&create_window_event.descriptor,
);
windows.add(window);
window_created_events.send(WindowCreated { id: window_id });
window_created_events.send(WindowCreated {
id: create_window_event.id,
});
}
}
Loading

0 comments on commit d2e4327

Please sign in to comment.