From e21119d2fa1ddc36864972c12a0f23f604d620ab Mon Sep 17 00:00:00 2001 From: Lixou Date: Mon, 14 Nov 2022 22:34:28 +0000 Subject: [PATCH] Add AutoMax next to ScalingMode::AutoMin (#6496) # Objective `ScalingMode::Auto` for cameras only targets min_height and min_width, or as the docs say it `Use minimal possible viewport size while keeping the aspect ratio.` But there is no ScalingMode that targets max_height and Max_width or `Use maximal possible viewport size while keeping the aspect ratio.` ## Solution Added `ScalingMode::AutoMax` that does the exact opposite of `ScalingMode::Auto` --- ## Changelog Renamed `ScalingMode::Auto` to `ScalingMode::AutoMin`. ## Migration Guide just rename `ScalingMode::Auto` to `ScalingMode::AutoMin` if you are using it. Co-authored-by: Lixou <82600264+DasLixou@users.noreply.github.com> --- crates/bevy_render/src/camera/projection.rs | 23 ++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 87953a4134a15..1e30953ee109b 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -185,9 +185,12 @@ pub enum ScalingMode { None, /// Match the window size. 1 world unit = 1 pixel. WindowSize, - /// Use minimal possible viewport size while keeping the aspect ratio. + /// Keeping the aspect ratio while the axes can't be smaller than given minimum. /// Arguments are in world units. - Auto { min_width: f32, min_height: f32 }, + AutoMin { min_width: f32, min_height: f32 }, + /// Keeping the aspect ratio while the axes can't be bigger than given maximum. + /// Arguments are in world units. + AutoMax { max_width: f32, max_height: f32 }, /// Keep vertical axis constant; resize horizontal with aspect ratio. /// The argument is the desired height of the viewport in world units. FixedVertical(f32), @@ -227,16 +230,30 @@ impl CameraProjection for OrthographicProjection { fn update(&mut self, width: f32, height: f32) { let (viewport_width, viewport_height) = match self.scaling_mode { ScalingMode::WindowSize => (width, height), - ScalingMode::Auto { + ScalingMode::AutoMin { min_width, min_height, } => { + // Compare Pixels of current width and minimal height and Pixels of minimal width with current height. + // Then use bigger (min_height when true) as what it refers to (height when true) and calculate rest so it can't get under minimum. if width * min_height > min_width * height { (width * min_height / height, min_height) } else { (min_width, height * min_width / width) } } + ScalingMode::AutoMax { + max_width, + max_height, + } => { + // Compare Pixels of current width and maximal height and Pixels of maximal width with current height. + // Then use smaller (max_height when true) as what it refers to (height when true) and calculate rest so it can't get over maximum. + if width * max_height < max_width * height { + (width * max_height / height, max_height) + } else { + (max_width, height * max_width / width) + } + } ScalingMode::FixedVertical(viewport_height) => { (width * viewport_height / height, viewport_height) }