Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mul<f32> for ScalingMode #11030

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 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::{Div, DivAssign, 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,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.
///
Expand Down