diff --git a/CHANGELOG.md b/CHANGELOG.md index d320dc316..9f75de2b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Window::winit` now returns an `Arc`-wrapped winit Window. - `WindowBehavior::render` no longer returns a `bool`. Closing the window can be done through the `RunningWindow` parameter. +- `DrawableExt::scale` now accepts a parameter that can be an `f32`, + `(f32, f32)`, or `Point`. This change allows independent scaling of the x + and y axis. ### Added diff --git a/src/drawing.rs b/src/drawing.rs index 753ecaaeb..0f119098a 100644 --- a/src/drawing.rs +++ b/src/drawing.rs @@ -178,13 +178,13 @@ impl<'render, 'gfx> Renderer<'render, 'gfx> { } else { None }; - let scale = shape.scale.map_or(1., |scale| { + let scale = shape.scale.map_or(Point::squared(1.), |scale| { flags |= FLAG_SCALE; scale }); - let rotation = shape.rotation.map_or(0., |scale| { + let rotation = shape.rotation.map_or(0., |rotation| { flags |= FLAG_ROTATE; - scale.into_raidans_f() + rotation.into_raidans_f() }); if !shape.translation.is_zero() { flags |= FLAG_TRANSLATE; @@ -500,7 +500,7 @@ mod text { origin: TextOrigin, translation: Point, rotation: Option, - scale: Option, + scale: Option>, opacity: Option, ) where Unit: ScreenUnit, @@ -549,7 +549,7 @@ mod text { fn render_one_glyph( translation: Point, rotation: Option, - scale: Option, + scale: Option>, opacity: Option, blit: TextureBlit, cached: &CachedGlyphHandle, @@ -584,7 +584,7 @@ mod text { if cached.is_mask { flags |= FLAG_MASKED; } - let scale = scale.map_or(1., |scale| { + let scale = scale.map_or(Point::squared(1.), |scale| { flags |= FLAG_SCALE; scale }); diff --git a/src/lib.rs b/src/lib.rs index 10eddbf4b..ed92336f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2391,7 +2391,7 @@ pub struct Drawable { /// Rotate the source before rendering. pub rotation: Option, /// Scale the source before rendering. - pub scale: Option, + pub scale: Option>, /// An opacity multiplier to apply to this drawable. pub opacity: Option, } @@ -2434,7 +2434,7 @@ pub trait DrawableExt { /// Rotates `self` by `angle`. fn rotate_by(self, angle: Angle) -> Drawable; /// Scales `self` by `factor`. - fn scale(self, factor: f32) -> Drawable; + fn scale(self, factor: impl ScaleFactor) -> Drawable; /// Renders this drawable with `opacity`, ranged from 0.- to 1.0. fn opacity(self, opacity: f32) -> Drawable; } @@ -2450,8 +2450,8 @@ impl DrawableExt for Drawable { self } - fn scale(mut self, factor: f32) -> Drawable { - self.scale = Some(factor); + fn scale(mut self, factor: impl ScaleFactor) -> Drawable { + self.scale = Some(factor.into_scaling_vector()); self } @@ -2461,6 +2461,30 @@ impl DrawableExt for Drawable { } } +/// A type representing an x and y scaling factor. +pub trait ScaleFactor { + /// Returns a `Point` with an x and y scaling factor from `self`. + fn into_scaling_vector(self) -> Point; +} + +impl ScaleFactor for f32 { + fn into_scaling_vector(self) -> Point { + Point::squared(self) + } +} + +impl ScaleFactor for (f32, f32) { + fn into_scaling_vector(self) -> Point { + Point::new(self.0, self.1) + } +} + +impl ScaleFactor for Point { + fn into_scaling_vector(self) -> Point { + self + } +} + impl DrawableExt for T where Drawable: From, @@ -2474,7 +2498,7 @@ where Drawable::from(self).rotate_by(angle) } - fn scale(self, factor: f32) -> Drawable { + fn scale(self, factor: impl ScaleFactor) -> Drawable { Drawable::from(self).scale(factor) } diff --git a/src/pipeline.rs b/src/pipeline.rs index 34921cb0f..7a915de26 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -73,7 +73,7 @@ pub(crate) const FLAG_MASKED: u32 = 1 << 5; #[repr(C)] pub(crate) struct PushConstants { pub flags: u32, - pub scale: f32, + pub scale: Point, pub rotation: f32, pub opacity: f32, pub translation: Point, @@ -146,7 +146,7 @@ where flags |= FLAG_MASKED; } } - let scale = self.scale.map_or(1., |scale| { + let scale = self.scale.map_or(Point::squared(1.), |scale| { flags |= FLAG_SCALE; scale }); diff --git a/src/shader.wgsl b/src/shader.wgsl index 6cc8616b4..27106934a 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -1,6 +1,7 @@ struct PushConstants { flags: i32, - scale: f32, + scale_x: f32, + scale_y: f32, rotation: f32, opacity: f32, translation_x: i32, @@ -90,7 +91,7 @@ fn vertex(input: VertexInput) -> VertexOutput { position = position * mat2x2(angle_cos, -angle_sin, angle_sin, angle_cos); } if (flags & flag_scale) != u32(0) { - position = position * pc.scale; + position = position * vec2(pc.scale_x, pc.scale_y); } if (flags & flag_translate) != u32(0) { position = position + vec2(