From 3c8fdfd655b5be1b5c385041bcd851cf4ebbf9e7 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Tue, 19 Dec 2023 21:55:39 +0000 Subject: [PATCH] Mul for ScalingMode --- crates/bevy_render/src/camera/projection.rs | 57 ++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 637ca423cf544..0505b1f31f4d8 100644 --- a/crates/bevy_render/src/camera/projection.rs +++ b/crates/bevy_render/src/camera/projection.rs @@ -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 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 for ScalingMode { + fn mul_assign(&mut self, rhs: f32) { + *self = *self * rhs; + } +} + +impl Div 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 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. ///