Skip to content

Commit

Permalink
Introduce with_transformation to Renderer trait
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Oct 24, 2023
1 parent 8c7ab6d commit 49682cf
Show file tree
Hide file tree
Showing 31 changed files with 139 additions and 104 deletions.
3 changes: 2 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ keywords.workspace = true

[dependencies]
bitflags.workspace = true
glam.workspace = true
log.workspace = true
num-traits.workspace = true
thiserror.workspace = true
twox-hash.workspace = true
num-traits.workspace = true

palette.workspace = true
palette.optional = true
Expand Down
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ mod point;
mod rectangle;
mod shell;
mod size;
mod transformation;
mod vector;

pub use alignment::Alignment;
Expand All @@ -73,5 +74,6 @@ pub use renderer::Renderer;
pub use shell::Shell;
pub use size::Size;
pub use text::Text;
pub use transformation::Transformation;
pub use vector::Vector;
pub use widget::Widget;
1 change: 1 addition & 0 deletions core/src/mouse/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pub enum Interaction {
ResizingHorizontally,
ResizingVertically,
NotAllowed,
ZoomIn,
}
20 changes: 17 additions & 3 deletions core/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ mod null;
#[cfg(debug_assertions)]
pub use null::Null;

use crate::{Background, BorderRadius, Color, Rectangle, Vector};
use crate::{
Background, BorderRadius, Color, Rectangle, Transformation, Vector,
};

/// A component that can be used by widgets to draw themselves on a screen.
pub trait Renderer: Sized {
Expand All @@ -17,12 +19,24 @@ pub trait Renderer: Sized {
/// The layer will clip its contents to the provided `bounds`.
fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self));

/// Applies a `translation` to the primitives recorded in the given closure.
/// Applies a [`Transformation`] to the primitives recorded in the given closure.
fn with_transformation(
&mut self,
transformation: Transformation,
f: impl FnOnce(&mut Self),
);

/// Applies a translation to the primitives recorded in the given closure.
fn with_translation(
&mut self,
translation: Vector,
f: impl FnOnce(&mut Self),
);
) {
self.with_transformation(
Transformation::translate(translation.x, translation.y),
f,
);
}

/// Fills a [`Quad`] with the provided [`Background`].
fn fill_quad(&mut self, quad: Quad, background: impl Into<Background>);
Expand Down
8 changes: 5 additions & 3 deletions core/src/renderer/null.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::alignment;
use crate::renderer::{self, Renderer};
use crate::text::{self, Text};
use crate::{Background, Color, Font, Pixels, Point, Rectangle, Size, Vector};
use crate::{
Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation,
};

use std::borrow::Cow;

Expand All @@ -23,9 +25,9 @@ impl Renderer for Null {

fn with_layer(&mut self, _bounds: Rectangle, _f: impl FnOnce(&mut Self)) {}

fn with_translation(
fn with_transformation(
&mut self,
_translation: Vector,
_transformation: Transformation,
_f: impl FnOnce(&mut Self),
) {
}
Expand Down
13 changes: 6 additions & 7 deletions graphics/src/transformation.rs → core/src/transformation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::{Point, Rectangle, Size, Vector};
use crate::{Point, Rectangle, Size, Vector};

use glam::{Mat4, Vec3, Vec4};
use std::ops::Mul;
Expand All @@ -8,6 +8,7 @@ use std::ops::Mul;
pub struct Transformation(Mat4);

impl Transformation {
/// A transformation that does nothing.
pub const IDENTITY: Self = Self(Mat4::IDENTITY);

/// Creates an orthographic projection.
Expand All @@ -30,16 +31,14 @@ impl Transformation {
Transformation(Mat4::from_scale(Vec3::new(scaling, scaling, 1.0)))
}

/// Returns the scale factor of the [`Transformation`].
pub fn scale_factor(&self) -> f32 {
self.0.x_axis.x
}

pub fn translation_x(&self) -> f32 {
self.0.w_axis.x
}

pub fn translation_y(&self) -> f32 {
self.0.w_axis.y
/// Returns the translation of the [`Transformation`].
pub fn translation(&self) -> Vector {
Vector::new(self.0.w_axis.x, self.0.w_axis.y)
}
}

Expand Down
1 change: 0 additions & 1 deletion graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ iced_core.workspace = true

bitflags.workspace = true
bytemuck.workspace = true
glam.workspace = true
half.workspace = true
log.workspace = true
raw-window-handle.workspace = true
Expand Down
2 changes: 0 additions & 2 deletions graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
mod antialiasing;
mod error;
mod primitive;
mod transformation;
mod viewport;

pub mod backend;
Expand All @@ -46,7 +45,6 @@ pub use gradient::Gradient;
pub use mesh::Mesh;
pub use primitive::Primitive;
pub use renderer::Renderer;
pub use transformation::Transformation;
pub use viewport::Viewport;

pub use iced_core as core;
5 changes: 3 additions & 2 deletions graphics/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use crate::core::alignment;
use crate::core::image;
use crate::core::svg;
use crate::core::text;
use crate::core::{Background, Color, Font, Pixels, Point, Rectangle, Vector};
use crate::core::{
Background, Color, Font, Pixels, Point, Rectangle, Transformation, Vector,
};
use crate::text::paragraph;
use crate::Transformation;

use std::sync::Arc;

Expand Down
18 changes: 9 additions & 9 deletions graphics/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::renderer;
use crate::core::svg;
use crate::core::text::Text;
use crate::core::{
Background, Color, Font, Pixels, Point, Rectangle, Size, Vector,
Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation,
};
use crate::text;
use crate::Primitive;
Expand Down Expand Up @@ -76,20 +76,20 @@ impl<B: Backend, T> Renderer<B, T> {
}

/// Starts recording a translation.
pub fn start_translation(&mut self) -> Vec<Primitive<B::Primitive>> {
pub fn start_transformation(&mut self) -> Vec<Primitive<B::Primitive>> {
std::mem::take(&mut self.primitives)
}

/// Ends the recording of a translation.
pub fn end_translation(
pub fn end_transformation(
&mut self,
primitives: Vec<Primitive<B::Primitive>>,
translation: Vector,
transformation: Transformation,
) {
let layer = std::mem::replace(&mut self.primitives, primitives);

self.primitives
.push(Primitive::group(layer).translate(translation));
.push(Primitive::group(layer).transform(transformation));
}
}

Expand All @@ -104,16 +104,16 @@ impl<B: Backend, T> iced_core::Renderer for Renderer<B, T> {
self.end_layer(current, bounds);
}

fn with_translation(
fn with_transformation(
&mut self,
translation: Vector,
transformation: Transformation,
f: impl FnOnce(&mut Self),
) {
let current = self.start_translation();
let current = self.start_transformation();

f(self);

self.end_translation(current, translation);
self.end_transformation(current, transformation);
}

fn fill_quad(
Expand Down
4 changes: 1 addition & 3 deletions graphics/src/viewport.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::Transformation;

use iced_core::Size;
use crate::core::{Size, Transformation};

/// A viewing region for displaying computer graphics.
#[derive(Debug, Clone)]
Expand Down
3 changes: 1 addition & 2 deletions renderer/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ mod cache;

pub use cache::Cache;

use crate::core::{Point, Rectangle, Size, Vector};
use crate::core::{Point, Rectangle, Size, Transformation, Vector};
use crate::graphics::geometry::{Fill, Path, Stroke, Text};
use crate::graphics::Transformation;
use crate::Renderer;

macro_rules! delegate {
Expand Down
14 changes: 7 additions & 7 deletions renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use geometry::Geometry;
use crate::core::renderer;
use crate::core::text::{self, Text};
use crate::core::{
Background, Color, Font, Pixels, Point, Rectangle, Size, Vector,
Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation,
};
use crate::graphics::text::Paragraph;
use crate::graphics::Mesh;
Expand Down Expand Up @@ -97,35 +97,35 @@ impl<T> core::Renderer for Renderer<T> {
}
}

fn with_translation(
fn with_transformation(
&mut self,
translation: Vector,
transformation: Transformation,
f: impl FnOnce(&mut Self),
) {
match self {
Self::TinySkia(renderer) => {
let primitives = renderer.start_translation();
let primitives = renderer.start_transformation();

f(self);

match self {
Self::TinySkia(renderer) => {
renderer.end_translation(primitives, translation);
renderer.end_transformation(primitives, transformation);
}
#[cfg(feature = "wgpu")]
_ => unreachable!(),
}
}
#[cfg(feature = "wgpu")]
Self::Wgpu(renderer) => {
let primitives = renderer.start_translation();
let primitives = renderer.start_transformation();

f(self);

match self {
#[cfg(feature = "wgpu")]
Self::Wgpu(renderer) => {
renderer.end_translation(primitives, translation);
renderer.end_transformation(primitives, transformation);
}
_ => unreachable!(),
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ pub use crate::core::alignment;
pub use crate::core::gradient;
pub use crate::core::{
color, Alignment, Background, BorderRadius, Color, ContentFit, Degrees,
Gradient, Length, Padding, Pixels, Point, Radians, Rectangle, Size, Vector,
Gradient, Length, Padding, Pixels, Point, Radians, Rectangle, Size,
Transformation, Vector,
};
pub use crate::runtime::Command;

Expand Down
15 changes: 9 additions & 6 deletions tiny_skia/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::core::{Background, Color, Gradient, Rectangle};
use crate::core::{Background, Color, Gradient, Rectangle, Transformation};
use crate::graphics::backend;
use crate::graphics::text;
use crate::graphics::{Damage, Transformation, Viewport};
use crate::graphics::{Damage, Viewport};
use crate::primitive::{self, Primitive};

use std::borrow::Cow;
Expand Down Expand Up @@ -374,11 +374,12 @@ impl Backend {

self.text_pipeline.draw_paragraph(
paragraph,
*position * transformation,
*position,
*color,
scale_factor * transformation.scale_factor(),
scale_factor,
pixels,
clip_mask,
transformation,
);
}
Primitive::Text {
Expand Down Expand Up @@ -623,13 +624,15 @@ fn into_color(color: Color) -> tiny_skia::Color {
}

fn into_transform(transformation: Transformation) -> tiny_skia::Transform {
let translation = transformation.translation();

tiny_skia::Transform {
sx: transformation.scale_factor(),
kx: 0.0,
ky: 0.0,
sy: transformation.scale_factor(),
tx: transformation.translation_x(),
ty: transformation.translation_y(),
tx: translation.x,
ty: translation.y,
}
}

Expand Down
4 changes: 2 additions & 2 deletions tiny_skia/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::core::{Point, Rectangle, Size, Vector};
use crate::core::{Point, Rectangle, Size, Transformation, Vector};
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::stroke::{self, Stroke};
use crate::graphics::geometry::{Path, Style, Text};
use crate::graphics::{Gradient, Transformation};
use crate::graphics::Gradient;
use crate::primitive::{self, Primitive};

pub struct Frame {
Expand Down
Loading

0 comments on commit 49682cf

Please sign in to comment.