Skip to content

Commit b765682

Browse files
committed
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>
1 parent db0d769 commit b765682

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

crates/bevy_render/src/camera/projection.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,12 @@ pub enum ScalingMode {
185185
None,
186186
/// Match the window size. 1 world unit = 1 pixel.
187187
WindowSize,
188-
/// Use minimal possible viewport size while keeping the aspect ratio.
188+
/// Keeping the aspect ratio while the axes can't be smaller than given minimum.
189189
/// Arguments are in world units.
190-
Auto { min_width: f32, min_height: f32 },
190+
AutoMin { min_width: f32, min_height: f32 },
191+
/// Keeping the aspect ratio while the axes can't be bigger than given maximum.
192+
/// Arguments are in world units.
193+
AutoMax { max_width: f32, max_height: f32 },
191194
/// Keep vertical axis constant; resize horizontal with aspect ratio.
192195
/// The argument is the desired height of the viewport in world units.
193196
FixedVertical(f32),
@@ -227,16 +230,30 @@ impl CameraProjection for OrthographicProjection {
227230
fn update(&mut self, width: f32, height: f32) {
228231
let (viewport_width, viewport_height) = match self.scaling_mode {
229232
ScalingMode::WindowSize => (width, height),
230-
ScalingMode::Auto {
233+
ScalingMode::AutoMin {
231234
min_width,
232235
min_height,
233236
} => {
237+
// Compare Pixels of current width and minimal height and Pixels of minimal width with current height.
238+
// 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.
234239
if width * min_height > min_width * height {
235240
(width * min_height / height, min_height)
236241
} else {
237242
(min_width, height * min_width / width)
238243
}
239244
}
245+
ScalingMode::AutoMax {
246+
max_width,
247+
max_height,
248+
} => {
249+
// Compare Pixels of current width and maximal height and Pixels of maximal width with current height.
250+
// 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.
251+
if width * max_height < max_width * height {
252+
(width * max_height / height, max_height)
253+
} else {
254+
(max_width, height * max_width / width)
255+
}
256+
}
240257
ScalingMode::FixedVertical(viewport_height) => {
241258
(width * viewport_height / height, viewport_height)
242259
}

0 commit comments

Comments
 (0)