Skip to content

Commit

Permalink
Merge pull request #2070 from ripytide/master
Browse files Browse the repository at this point in the history
Added a Frame::scale_nonuniform method
  • Loading branch information
hecrj authored Sep 7, 2023
2 parents d82c8b5 + 09965b6 commit b5e7fb2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
8 changes: 4 additions & 4 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ mod grid {
frame.translate(center);
frame.scale(self.scaling);
frame.translate(self.translation);
frame.scale(Cell::SIZE as f32);
frame.scale(Cell::SIZE);

let region = self.visible_region(frame.size());

Expand All @@ -576,7 +576,7 @@ mod grid {
frame.translate(center);
frame.scale(self.scaling);
frame.translate(self.translation);
frame.scale(Cell::SIZE as f32);
frame.scale(Cell::SIZE);

frame.fill_rectangle(
Point::new(cell.j as f32, cell.i as f32),
Expand Down Expand Up @@ -630,7 +630,7 @@ mod grid {
frame.translate(center);
frame.scale(self.scaling);
frame.translate(self.translation);
frame.scale(Cell::SIZE as f32);
frame.scale(Cell::SIZE);

let region = self.visible_region(frame.size());
let rows = region.rows();
Expand Down Expand Up @@ -834,7 +834,7 @@ mod grid {
}

impl Cell {
const SIZE: usize = 20;
const SIZE: u16 = 20;

fn at(position: Point) -> Cell {
let i = (position.y / Cell::SIZE as f32).ceil() as isize;
Expand Down
10 changes: 8 additions & 2 deletions renderer/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,18 @@ impl Frame {
delegate!(self, frame, frame.rotate(angle));
}

/// Applies a scaling to the current transform of the [`Frame`].
/// Applies a uniform scaling to the current transform of the [`Frame`].
#[inline]
pub fn scale(&mut self, scale: f32) {
pub fn scale(&mut self, scale: impl Into<f32>) {
delegate!(self, frame, frame.scale(scale));
}

/// Applies a non-uniform scaling to the current transform of the [`Frame`].
#[inline]
pub fn scale_nonuniform(&mut self, scale: impl Into<Vector>) {
delegate!(self, frame, frame.scale_nonuniform(scale));
}

pub fn into_geometry(self) -> Geometry {
match self {
Self::TinySkia(frame) => Geometry::TinySkia(frame.into_primitive()),
Expand Down
12 changes: 10 additions & 2 deletions tiny_skia/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,16 @@ impl Frame {
.pre_concat(tiny_skia::Transform::from_rotate(angle.to_degrees()));
}

pub fn scale(&mut self, scale: f32) {
self.transform = self.transform.pre_scale(scale, scale);
pub fn scale(&mut self, scale: impl Into<f32>) {
let scale = scale.into();

self.scale_nonuniform(Vector { x: scale, y: scale });
}

pub fn scale_nonuniform(&mut self, scale: impl Into<Vector>) {
let scale = scale.into();

self.transform = self.transform.pre_scale(scale.x, scale.y);
}

pub fn into_primitive(self) -> Primitive {
Expand Down
16 changes: 13 additions & 3 deletions wgpu/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,21 @@ impl Frame {
self.transforms.current.is_identity = false;
}

/// Applies a scaling to the current transform of the [`Frame`].
/// Applies a uniform scaling to the current transform of the [`Frame`].
#[inline]
pub fn scale(&mut self, scale: f32) {
pub fn scale(&mut self, scale: impl Into<f32>) {
let scale = scale.into();

self.scale_nonuniform(Vector { x: scale, y: scale });
}

/// Applies a non-uniform scaling to the current transform of the [`Frame`].
#[inline]
pub fn scale_nonuniform(&mut self, scale: impl Into<Vector>) {
let scale = scale.into();

self.transforms.current.raw =
self.transforms.current.raw.pre_scale(scale, scale);
self.transforms.current.raw.pre_scale(scale.x, scale.y);
self.transforms.current.is_identity = false;
}

Expand Down
2 changes: 1 addition & 1 deletion widget/src/qr_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<'a, Message, Theme> Widget<Message, Renderer<Theme>> for QRCode<'a> {
let geometry =
self.state.cache.draw(renderer, bounds.size(), |frame| {
// Scale units to cell size
frame.scale(f32::from(self.cell_size));
frame.scale(self.cell_size);

// Draw background
frame.fill_rectangle(
Expand Down

0 comments on commit b5e7fb2

Please sign in to comment.