Mul<f32> for ScalingMode (#11030)

Complement to https://github.com/bevyengine/bevy/pull/11022: if
`OrthographicProjection.scale` is removed, this can be used instead.

CC @doonv @Davier
This commit is contained in:
Stepan Koltsov 2024-01-08 22:24:32 +00:00 committed by GitHub
parent 38ef170b86
commit 8d9a0a883f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,5 @@
use std::marker::PhantomData;
use std::ops::{Div, DivAssign, Mul, MulAssign};
use bevy_app::{App, Plugin, PostStartup, PostUpdate};
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
@ -192,7 +193,7 @@ impl Default for PerspectiveProjection {
}
}
#[derive(Debug, Clone, Reflect, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Reflect, Serialize, Deserialize)]
#[reflect(Serialize, Deserialize)]
pub enum ScalingMode {
/// Manually specify the projection's size, ignoring window resizing. The image will stretch.
@ -215,6 +216,60 @@ pub enum ScalingMode {
FixedHorizontal(f32),
}
impl Mul<f32> for ScalingMode {
type Output = ScalingMode;
/// Scale the `ScalingMode`. For example, multiplying by 2 makes the viewport twice as large.
fn mul(self, rhs: f32) -> ScalingMode {
match self {
ScalingMode::Fixed { width, height } => ScalingMode::Fixed {
width: width * rhs,
height: height * rhs,
},
ScalingMode::WindowSize(pixels_per_world_unit) => {
ScalingMode::WindowSize(pixels_per_world_unit / rhs)
}
ScalingMode::AutoMin {
min_width,
min_height,
} => ScalingMode::AutoMin {
min_width: min_width * rhs,
min_height: min_height * rhs,
},
ScalingMode::AutoMax {
max_width,
max_height,
} => ScalingMode::AutoMax {
max_width: max_width * rhs,
max_height: max_height * rhs,
},
ScalingMode::FixedVertical(size) => ScalingMode::FixedVertical(size * rhs),
ScalingMode::FixedHorizontal(size) => ScalingMode::FixedHorizontal(size * rhs),
}
}
}
impl MulAssign<f32> for ScalingMode {
fn mul_assign(&mut self, rhs: f32) {
*self = *self * rhs;
}
}
impl Div<f32> for ScalingMode {
type Output = ScalingMode;
/// Scale the `ScalingMode`. For example, dividing by 2 makes the viewport half as large.
fn div(self, rhs: f32) -> ScalingMode {
self * (1.0 / rhs)
}
}
impl DivAssign<f32> for ScalingMode {
fn div_assign(&mut self, rhs: f32) {
*self = *self / rhs;
}
}
/// Project a 3D space onto a 2D surface using parallel lines, i.e., unlike [`PerspectiveProjection`],
/// the size of objects remains the same regardless of their distance to the camera.
///