Introduce with_transformation to Renderer trait

This commit is contained in:
Héctor Ramón Jiménez 2023-10-24 05:34:03 +02:00
parent a6e91d13d5
commit f4d6648601
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
31 changed files with 161 additions and 118 deletions

View file

@ -26,7 +26,6 @@ iced_futures.workspace = true
bitflags.workspace = true
bytemuck.workspace = true
cosmic-text.workspace = true
glam.workspace = true
half.workspace = true
log.workspace = true
once_cell.workspace = true

View file

@ -19,7 +19,6 @@
mod antialiasing;
mod error;
mod primitive;
mod transformation;
mod viewport;
pub mod backend;
@ -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;

View file

@ -4,11 +4,11 @@ use crate::core::image;
use crate::core::svg;
use crate::core::text;
use crate::core::{
Background, Border, Color, Font, Pixels, Point, Rectangle, Shadow, Vector,
Background, Border, Color, Font, Pixels, Point, Rectangle, Shadow,
Transformation, Vector,
};
use crate::text::editor;
use crate::text::paragraph;
use crate::Transformation;
use std::sync::Arc;

View file

@ -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;
@ -73,20 +73,20 @@ impl<B: Backend> Renderer<B> {
}
/// 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));
}
}
@ -99,16 +99,16 @@ impl<B: Backend> iced_core::Renderer for Renderer<B> {
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(

View file

@ -1,124 +0,0 @@
use crate::core::{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)))
}
/// The scale factor of the [`Transformation`].
pub fn scale_factor(&self) -> f32 {
self.0.x_axis.x
}
/// The translation on the X axis.
pub fn translation_x(&self) -> f32 {
self.0.w_axis.x
}
/// The translation on the Y axis.
pub fn translation_y(&self) -> f32 {
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
}
}

View file

@ -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)]