From 62956974e282df749af7e267fbe39d981024e0a4 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Thu, 14 Apr 2022 19:52:49 +0100 Subject: [PATCH] No longer test 0x0 windows as they are unsupported on X11 --- examples/window/expanding_window.rs | 53 +++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/examples/window/expanding_window.rs b/examples/window/expanding_window.rs index 9b9e12481d9542..fb31bdcadf74b5 100644 --- a/examples/window/expanding_window.rs +++ b/examples/window/expanding_window.rs @@ -1,19 +1,30 @@ -use bevy::prelude::*; +use bevy::{input::system::exit_on_esc_system, prelude::*}; -const MAX_WIDTH: f32 = 400.; -const MAX_HEIGHT: f32 = 400.; +const MAX_WIDTH: u16 = 401; +const MAX_HEIGHT: u16 = 401; + +struct Dimensions { + width: u16, + height: u16, +} fn main() { App::new() .insert_resource(WindowDescriptor { - width: MAX_WIDTH, - height: MAX_HEIGHT, + width: MAX_WIDTH.try_into().unwrap(), + height: MAX_HEIGHT.try_into().unwrap(), scale_factor_override: Some(1.), ..Default::default() }) + .insert_resource(Dimensions { + width: MAX_WIDTH, + height: MAX_HEIGHT, + }) .add_plugins(DefaultPlugins) .insert_resource(Phase::ContractingY) .add_system(change_window_size) + .add_system(sync_dimensions) + .add_system(exit_on_esc_system) .add_startup_system(setup) .run(); } @@ -28,7 +39,7 @@ enum Phase { use Phase::*; fn change_window_size( - mut windows: ResMut, + mut windows: ResMut, mut phase: ResMut, mut first_complete: Local, ) { @@ -38,37 +49,49 @@ fn change_window_size( *first_complete = true; return; } - let primary = windows.get_primary_mut().unwrap(); - let height = primary.height(); - let width = primary.width(); + let height = windows.height; + let width = windows.width; match *phase { Phase::ContractingY => { - if height <= 0.5 { + if windows.height <= 1 { *phase = ContractingX; + } else { + windows.height -= 4; } - primary.set_resolution(width, (height - 4.).max(0.0)) } Phase::ContractingX => { - if width <= 0.5 { + if width <= 1 { *phase = ExpandingY; + } else { + windows.width -= 4; } - primary.set_resolution((width - 4.).max(0.0), height) } Phase::ExpandingY => { if height >= MAX_HEIGHT { *phase = ExpandingX; + } else { + windows.height += 4; } - primary.set_resolution(width, height + 4.) } Phase::ExpandingX => { if width >= MAX_WIDTH { *phase = ContractingY; + } else { + windows.width += 4; } - primary.set_resolution(width + 4., height) } } } +fn sync_dimensions(dim: Res, mut windows: ResMut) { + if dim.is_changed() { + windows.get_primary_mut().unwrap().set_resolution( + dim.width.try_into().unwrap(), + dim.height.try_into().unwrap(), + ); + } +} + /// A simple 3d scene, taken from the `3d_scene` example fn setup( mut commands: Commands,