Skip to content

Commit

Permalink
Merge pull request #2120 from iced-rs/transform-primitive
Browse files Browse the repository at this point in the history
`Transform` primitive
  • Loading branch information
hecrj authored Feb 2, 2024
2 parents 759f0e9 + b3adf31 commit aea1725
Show file tree
Hide file tree
Showing 36 changed files with 623 additions and 293 deletions.
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ keywords.workspace = true

[dependencies]
bitflags.workspace = true
glam.workspace = true
log.workspace = true
num-traits.workspace = true
smol_str.workspace = 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 @@ -49,6 +49,7 @@ mod rectangle;
mod shadow;
mod shell;
mod size;
mod transformation;
mod vector;

pub use alignment::Alignment;
Expand All @@ -75,6 +76,7 @@ pub use shadow::Shadow;
pub use shell::Shell;
pub use size::Size;
pub use text::Text;
pub use transformation::Transformation;
pub use vector::Vector;
pub use widget::Widget;

Expand Down
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,
}
8 changes: 8 additions & 0 deletions core/src/pixels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ impl From<Pixels> for f32 {
pixels.0
}
}

impl std::ops::Mul<f32> for Pixels {
type Output = Pixels;

fn mul(self, rhs: f32) -> Self {
Pixels(self.0 * rhs)
}
}
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, Border, Color, Rectangle, Shadow, Size, Vector};
use crate::{
Background, Border, Color, Rectangle, Shadow, Size, Transformation, Vector,
};

/// A component that can be used by widgets to draw themselves on a screen.
pub trait Renderer: Sized {
Expand All @@ -14,12 +16,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 @@ -21,9 +23,9 @@ impl Null {
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
119 changes: 119 additions & 0 deletions core/src/transformation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use crate::{Point, Rectangle, Size, Vector};

use glam::{Mat4, Vec3, Vec4};
use std::ops::Mul;

/// A 2D transformation matrix.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Transformation(Mat4);

impl Transformation {
/// A [`Transformation`] that preserves whatever is transformed.
pub const IDENTITY: Self = Self(Mat4::IDENTITY);

/// Creates an orthographic projection.
#[rustfmt::skip]
pub fn orthographic(width: u32, height: u32) -> Transformation {
Transformation(Mat4::orthographic_rh_gl(
0.0, width as f32,
height as f32, 0.0,
-1.0, 1.0
))
}

/// Creates a translate transformation.
pub fn translate(x: f32, y: f32) -> Transformation {
Transformation(Mat4::from_translation(Vec3::new(x, y, 0.0)))
}

/// Creates a uniform scaling transformation.
pub fn scale(scaling: f32) -> 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
}

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

impl Mul for Transformation {
type Output = Self;

fn mul(self, rhs: Self) -> Self {
Transformation(self.0 * rhs.0)
}
}

impl Mul<Transformation> for Point {
type Output = Self;

fn mul(self, transformation: Transformation) -> Self {
let point = transformation
.0
.mul_vec4(Vec4::new(self.x, self.y, 1.0, 1.0));

Point::new(point.x, point.y)
}
}

impl Mul<Transformation> for Vector {
type Output = Self;

fn mul(self, transformation: Transformation) -> Self {
let new_vector = transformation
.0
.mul_vec4(Vec4::new(self.x, self.y, 1.0, 0.0));

Vector::new(new_vector.x, new_vector.y)
}
}

impl Mul<Transformation> for Size {
type Output = Self;

fn mul(self, transformation: Transformation) -> Self {
let new_size = transformation.0.mul_vec4(Vec4::new(
self.width,
self.height,
1.0,
0.0,
));

Size::new(new_size.x, new_size.y)
}
}

impl Mul<Transformation> for Rectangle {
type Output = Self;

fn mul(self, transformation: Transformation) -> Self {
let position = self.position();
let size = self.size();

Self::new(position * transformation, size * transformation)
}
}

impl AsRef<[f32; 16]> for Transformation {
fn as_ref(&self) -> &[f32; 16] {
self.0.as_ref()
}
}

impl From<Transformation> for [f32; 16] {
fn from(t: Transformation) -> [f32; 16] {
*t.as_ref()
}
}

impl From<Transformation> for Mat4 {
fn from(transformation: Transformation) -> Self {
transformation.0
}
}
2 changes: 1 addition & 1 deletion examples/bezier_tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ publish = false

[dependencies]
iced.workspace = true
iced.features = ["canvas"]
iced.features = ["canvas", "debug"]
10 changes: 10 additions & 0 deletions examples/loupe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "loupe"
version = "0.1.0"
authors = ["Héctor Ramón Jiménez <[email protected]>"]
edition = "2021"
publish = false

[dependencies]
iced.workspace = true
iced.features = ["advanced", "debug"]
Loading

0 comments on commit aea1725

Please sign in to comment.