Skip to content

Commit

Permalink
Independent x/y scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Nov 6, 2024
1 parent 2acfbd3 commit 3d7b716
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32>`. This change allows independent scaling of the x
and y axis.

### Added

Expand Down
12 changes: 6 additions & 6 deletions src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -500,7 +500,7 @@ mod text {
origin: TextOrigin<Px>,
translation: Point<Unit>,
rotation: Option<Angle>,
scale: Option<f32>,
scale: Option<Point<f32>>,
opacity: Option<f32>,
) where
Unit: ScreenUnit,
Expand Down Expand Up @@ -549,7 +549,7 @@ mod text {
fn render_one_glyph<Unit>(
translation: Point<Unit>,
rotation: Option<Angle>,
scale: Option<f32>,
scale: Option<Point<f32>>,
opacity: Option<f32>,
blit: TextureBlit<Px>,
cached: &CachedGlyphHandle,
Expand Down Expand Up @@ -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
});
Expand Down
34 changes: 29 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2391,7 +2391,7 @@ pub struct Drawable<T, Unit> {
/// Rotate the source before rendering.
pub rotation: Option<Angle>,
/// Scale the source before rendering.
pub scale: Option<f32>,
pub scale: Option<Point<f32>>,
/// An opacity multiplier to apply to this drawable.
pub opacity: Option<f32>,
}
Expand Down Expand Up @@ -2434,7 +2434,7 @@ pub trait DrawableExt<Source, Unit> {
/// Rotates `self` by `angle`.
fn rotate_by(self, angle: Angle) -> Drawable<Source, Unit>;
/// Scales `self` by `factor`.
fn scale(self, factor: f32) -> Drawable<Source, Unit>;
fn scale(self, factor: impl ScaleFactor) -> Drawable<Source, Unit>;
/// Renders this drawable with `opacity`, ranged from 0.- to 1.0.
fn opacity(self, opacity: f32) -> Drawable<Source, Unit>;
}
Expand All @@ -2450,8 +2450,8 @@ impl<T, Unit> DrawableExt<T, Unit> for Drawable<T, Unit> {
self
}

fn scale(mut self, factor: f32) -> Drawable<T, Unit> {
self.scale = Some(factor);
fn scale(mut self, factor: impl ScaleFactor) -> Drawable<T, Unit> {
self.scale = Some(factor.into_scaling_vector());
self
}

Expand All @@ -2461,6 +2461,30 @@ impl<T, Unit> DrawableExt<T, Unit> for Drawable<T, Unit> {
}
}

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

impl ScaleFactor for f32 {
fn into_scaling_vector(self) -> Point<f32> {
Point::squared(self)
}
}

impl ScaleFactor for (f32, f32) {
fn into_scaling_vector(self) -> Point<f32> {
Point::new(self.0, self.1)
}
}

impl ScaleFactor for Point<f32> {
fn into_scaling_vector(self) -> Point<f32> {
self
}
}

impl<T, Unit> DrawableExt<T, Unit> for T
where
Drawable<T, Unit>: From<T>,
Expand All @@ -2474,7 +2498,7 @@ where
Drawable::from(self).rotate_by(angle)
}

fn scale(self, factor: f32) -> Drawable<T, Unit> {
fn scale(self, factor: impl ScaleFactor) -> Drawable<T, Unit> {
Drawable::from(self).scale(factor)
}

Expand Down
4 changes: 2 additions & 2 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32>,
pub rotation: f32,
pub opacity: f32,
pub translation: Point<i32>,
Expand Down Expand Up @@ -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
});
Expand Down
5 changes: 3 additions & 2 deletions src/shader.wgsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
struct PushConstants {
flags: i32,
scale: f32,
scale_x: f32,
scale_y: f32,
rotation: f32,
opacity: f32,
translation_x: i32,
Expand Down Expand Up @@ -90,7 +91,7 @@ fn vertex(input: VertexInput) -> VertexOutput {
position = position * mat2x2<f32>(angle_cos, -angle_sin, angle_sin, angle_cos);
}
if (flags & flag_scale) != u32(0) {
position = position * pc.scale;
position = position * vec2<f32>(pc.scale_x, pc.scale_y);
}
if (flags & flag_translate) != u32(0) {
position = position + vec2<f32>(
Expand Down

0 comments on commit 3d7b716

Please sign in to comment.