From 234660ff31298e994af805a88aba7cb81f794c30 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 | 42 ++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/bevy_render/src/camera/projection.rs b/crates/bevy_render/src/camera/projection.rs index 637ca423cf5449..906c0d40e79d80 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::{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,45 @@ pub enum ScalingMode { FixedHorizontal(f32), } +/// Scale the scaling mode. For example, multiplying by 2 makes everything twice as small. +impl Mul 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 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. ///