Skip to content

Commit

Permalink
Mul<f32> for ScalingMode
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancheg committed Dec 19, 2023
1 parent dc8fc6c commit 234660f
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion crates/bevy_render/src/camera/projection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::marker::PhantomData;
use std::ops::{Mul, MulAssign};

use bevy_app::{App, Plugin, PostStartup, PostUpdate};
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
Expand Down Expand Up @@ -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.
Expand All @@ -215,6 +216,45 @@ pub enum ScalingMode {
FixedHorizontal(f32),
}

/// Scale the scaling mode. For example, multiplying by 2 makes everything twice as small.
impl Mul<f32> for ScalingMode {
type Output = ScalingMode;

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;
}
}

/// 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.
///
Expand Down

0 comments on commit 234660f

Please sign in to comment.