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

can change window settings at runtime #644

Merged
merged 10 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ path = "examples/window/multiple_windows.rs"
name = "window_settings"
path = "examples/window/window_settings.rs"

[[example]]
name = "change_window_settings"
path = "examples/window/change_window_settings.rs"

[[example]]
name = "hello_wasm"
path = "examples/wasm/hello_wasm.rs"
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub fn camera_system<T: CameraProjection + Component>(
}
for (entity, mut camera, mut camera_projection) in &mut query.iter() {
if let Some(window) = windows.get(camera.window) {
if changed_window_ids.contains(&window.id) || added_cameras.contains(&entity) {
camera_projection.update(window.width as usize, window.height as usize);
if changed_window_ids.contains(&window.id()) || added_cameras.contains(&entity) {
camera_projection.update(window.width() as usize, window.height() as usize);
camera.projection_matrix = camera_projection.get_projection_matrix();
camera.depth_calculation = camera_projection.depth_calculation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ impl Node for WindowSwapChainNode {
// create window swapchain when window is resized or created
if self
.window_created_event_reader
.find_latest(&window_created_events, |e| e.id == window.id)
.find_latest(&window_created_events, |e| e.id == window.id())
.is_some()
|| self
.window_resized_event_reader
.find_latest(&window_resized_events, |e| e.id == window.id)
.find_latest(&window_resized_events, |e| e.id == window.id())
.is_some()
{
render_resource_context.create_swap_chain(window);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ impl Node for WindowTextureNode {

if self
.window_created_event_reader
.find_latest(&window_created_events, |e| e.id == window.id)
.find_latest(&window_created_events, |e| e.id == window.id())
.is_some()
|| self
.window_resized_event_reader
.find_latest(&window_resized_events, |e| e.id == window.id)
.find_latest(&window_resized_events, |e| e.id == window.id())
.is_some()
{
let render_resource_context = render_context.resources_mut();
if let Some(RenderResourceId::Texture(old_texture)) = output.get(WINDOW_TEXTURE) {
render_resource_context.remove_texture(old_texture);
}

self.descriptor.size.width = window.width;
self.descriptor.size.height = window.height;
self.descriptor.size.width = window.width();
self.descriptor.size.height = window.height();
let texture_resource = render_resource_context.create_texture(self.descriptor);
output.set(WINDOW_TEXTURE, RenderResourceId::Texture(texture_resource));
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl FlexSurface {

pub fn update_window(&mut self, window: &Window) {
let stretch = &mut self.stretch;
let node = self.window_nodes.entry(window.id).or_insert_with(|| {
let node = self.window_nodes.entry(window.id()).or_insert_with(|| {
stretch
.new_node(stretch::style::Style::default(), Vec::new())
.unwrap()
Expand All @@ -106,8 +106,8 @@ impl FlexSurface {
*node,
stretch::style::Style {
size: stretch::geometry::Size {
width: stretch::style::Dimension::Points(window.width as f32),
height: stretch::style::Dimension::Points(window.height as f32),
width: stretch::style::Dimension::Points(window.width() as f32),
height: stretch::style::Dimension::Points(window.height() as f32),
},
..Default::default()
},
Expand Down Expand Up @@ -179,7 +179,7 @@ pub fn flex_node_system(

// update window children (for now assuming all Nodes live in the primary window)
if let Some(primary_window) = windows.get_primary() {
flex_surface.set_window_children(primary_window.id, root_node_query.iter().iter());
flex_surface.set_window_children(primary_window.id(), root_node_query.iter().iter());
}

// update children
Expand Down
13 changes: 8 additions & 5 deletions crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,25 @@ impl RenderResourceContext for WgpuRenderResourceContext {

let swap_chain_descriptor: wgpu::SwapChainDescriptor = window.wgpu_into();
let surface = surfaces
.get(&window.id)
.get(&window.id())
.expect("No surface found for window");
let swap_chain = self
.device
.create_swap_chain(surface, &swap_chain_descriptor);

window_swap_chains.insert(window.id, swap_chain);
window_swap_chains.insert(window.id(), swap_chain);
}

fn next_swap_chain_texture(&self, window: &bevy_window::Window) -> TextureId {
if let Some(texture_id) = self.try_next_swap_chain_texture(window.id) {
if let Some(texture_id) = self.try_next_swap_chain_texture(window.id()) {
texture_id
} else {
self.resources.window_swap_chains.write().remove(&window.id);
self.resources
.window_swap_chains
.write()
.remove(&window.id());
self.create_swap_chain(window);
self.try_next_swap_chain_texture(window.id)
self.try_next_swap_chain_texture(window.id())
.expect("Failed to acquire next swap chain texture!")
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_wgpu/src/wgpu_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ impl WgpuRenderer {
#[cfg(feature = "bevy_winit")]
{
let winit_windows = resources.get::<bevy_winit::WinitWindows>().unwrap();
let winit_window = winit_windows.get_window(window.id).unwrap();
let winit_window = winit_windows.get_window(window.id()).unwrap();
let surface = unsafe { self.instance.create_surface(winit_window.deref()) };
render_resource_context.set_window_surface(window.id, surface);
render_resource_context.set_window_surface(window.id(), surface);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_wgpu/src/wgpu_type_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,9 @@ impl WgpuFrom<&Window> for wgpu::SwapChainDescriptor {
wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format: wgpu::TextureFormat::Bgra8UnormSrgb,
width: window.width,
height: window.height,
present_mode: if window.vsync {
width: window.width(),
height: window.height(),
present_mode: if window.vsync() {
wgpu::PresentMode::Fifo
} else {
wgpu::PresentMode::Immediate
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bevy_utils = { path = "../bevy_utils", version = "0.2.1" }

# other
uuid = { version = "0.8", features = ["v4", "serde"] }
smallvec = { version = "1.4", features = ["serde"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
uuid = { version = "0.8", features = ["wasm-bindgen"] }
Expand Down
106 changes: 98 additions & 8 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use smallvec::SmallVec;
use uuid::Uuid;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -33,16 +34,28 @@ impl Default for WindowId {

#[derive(Debug)]
pub struct Window {
pub id: WindowId,
pub width: u32,
pub height: u32,
pub title: String,
pub vsync: bool,
pub resizable: bool,
pub decorations: bool,
pub mode: WindowMode,
id: WindowId,
width: u32,
height: u32,
title: String,
vsync: bool,
resizable: bool,
decorations: bool,
mode: WindowMode,
#[cfg(target_arch = "wasm32")]
pub canvas: Option<String>,
#[doc(hidden)]
pub command_queue: SmallVec<[WindowCommand; 4]>,
}
mockersf marked this conversation as resolved.
Show resolved Hide resolved
mockersf marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug)]
pub enum WindowCommand {
SetWindowMode { mode: WindowMode },
SetTitle { title: String },
SetResolution { width: u32, height: u32 },
SetVsync { vsync: bool },
SetResizable { resizable: bool },
SetDecorations { decorations: bool },
}

/// Defines the way a window is displayed
Expand Down Expand Up @@ -70,8 +83,85 @@ impl Window {
mode: window_descriptor.mode,
#[cfg(target_arch = "wasm32")]
canvas: window_descriptor.canvas.clone(),
command_queue: SmallVec::new(),
}
}

#[inline]
pub fn id(&self) -> WindowId {
self.id
}

#[inline]
pub fn width(&self) -> u32 {
self.width
}

#[inline]
pub fn height(&self) -> u32 {
self.height
}

pub fn set_resolution(&mut self, width: u32, height: u32) {
self.width = width;
self.height = height;
self.command_queue
.push(WindowCommand::SetResolution { width, height });
}

#[doc(hidden)]
pub fn update_resolution_from_backend(&mut self, width: u32, height: u32) {
self.width = width;
self.height = height;
}

pub fn title(&'_ self) -> &'_ str {
mockersf marked this conversation as resolved.
Show resolved Hide resolved
&self.title
}

pub fn set_title(&mut self, title: String) {
self.title = title.to_string();
self.command_queue.push(WindowCommand::SetTitle { title });
}

pub fn vsync(&self) -> bool {
self.vsync
}

pub fn set_vsync(&mut self, vsync: bool) {
self.vsync = vsync;
self.command_queue.push(WindowCommand::SetVsync { vsync });
}

pub fn resizable(&self) -> bool {
self.resizable
}

pub fn set_resizable(&mut self, resizable: bool) {
self.resizable = resizable;
self.command_queue
.push(WindowCommand::SetResizable { resizable });
}

pub fn decorations(&self) -> bool {
self.decorations
}

pub fn set_decorations(&mut self, decorations: bool) {
self.decorations = decorations;
self.command_queue
.push(WindowCommand::SetDecorations { decorations });
}

pub fn mode(&self) -> WindowMode {
self.mode
}

pub fn set_mode(&mut self, mode: WindowMode) {
self.mode = mode;
self.command_queue
.push(WindowCommand::SetWindowMode { mode });
}
}

#[derive(Debug, Clone)]
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_window/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Windows {

impl Windows {
pub fn add(&mut self, window: Window) {
self.windows.insert(window.id, window);
self.windows.insert(window.id(), window);
}

pub fn get(&self, id: WindowId) -> Option<&Window> {
Expand All @@ -23,7 +23,15 @@ impl Windows {
self.get(WindowId::primary())
}

pub fn get_primary_mut(&mut self) -> Option<&mut Window> {
self.get_mut(WindowId::primary())
}

pub fn iter(&self) -> impl Iterator<Item = &Window> {
self.windows.values()
}

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Window> {
self.windows.values_mut()
}
}
Loading