Replace Primitive::Translate with Transform

This commit is contained in:
Héctor Ramón Jiménez 2023-10-23 03:13:28 +02:00
parent 759f0e9225
commit 5467c19c80
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
11 changed files with 205 additions and 133 deletions

View file

@ -8,6 +8,7 @@ use crate::graphics::geometry::{
};
use crate::graphics::gradient::{self, Gradient};
use crate::graphics::mesh::{self, Mesh};
use crate::graphics::Transformation;
use crate::primitive::{self, Primitive};
use lyon::geom::euclid;
@ -435,7 +436,7 @@ impl Frame {
pub fn clip(&mut self, frame: Frame, at: Point) {
let size = frame.size();
let primitives = frame.into_primitives();
let translation = Vector::new(at.x, at.y);
let transformation = Transformation::translate(at.x, at.y);
let (text, meshes) = primitives
.into_iter()
@ -443,12 +444,12 @@ impl Frame {
self.primitives.push(Primitive::Group {
primitives: vec![
Primitive::Translate {
translation,
Primitive::Transform {
transformation,
content: Box::new(Primitive::Group { primitives: meshes }),
},
Primitive::Translate {
translation,
Primitive::Transform {
transformation,
content: Box::new(Primitive::Clip {
bounds: Rectangle::with_size(size),
content: Box::new(Primitive::Group {

View file

@ -15,7 +15,7 @@ use crate::core::alignment;
use crate::core::{Color, Font, Pixels, Point, Rectangle, Size, Vector};
use crate::graphics;
use crate::graphics::color;
use crate::graphics::Viewport;
use crate::graphics::{Transformation, Viewport};
use crate::primitive::{self, Primitive};
use crate::quad::{self, Quad};
@ -104,7 +104,7 @@ impl<'a> Layer<'a> {
for primitive in primitives {
Self::process_primitive(
&mut layers,
Vector::new(0.0, 0.0),
Transformation::IDENTITY,
primitive,
0,
);
@ -115,7 +115,7 @@ impl<'a> Layer<'a> {
fn process_primitive(
layers: &mut Vec<Self>,
translation: Vector,
transformation: Transformation,
primitive: &'a Primitive,
current_layer: usize,
) {
@ -130,9 +130,10 @@ impl<'a> Layer<'a> {
layer.text.push(Text::Paragraph {
paragraph: paragraph.clone(),
position: *position + translation,
position: *position * transformation,
color: *color,
clip_bounds: *clip_bounds + translation,
clip_bounds: *clip_bounds * transformation,
scale: transformation.scale_y(),
});
}
Primitive::Editor {
@ -145,9 +146,10 @@ impl<'a> Layer<'a> {
layer.text.push(Text::Editor {
editor: editor.clone(),
position: *position + translation,
position: *position * transformation,
color: *color,
clip_bounds: *clip_bounds + translation,
clip_bounds: *clip_bounds * transformation,
scale: transformation.scale_y(),
});
}
Primitive::Text {
@ -166,7 +168,7 @@ impl<'a> Layer<'a> {
layer.text.push(Text::Cached(text::Cached {
content,
bounds: *bounds + translation,
bounds: *bounds * transformation,
size: *size,
line_height: *line_height,
color: *color,
@ -174,7 +176,7 @@ impl<'a> Layer<'a> {
horizontal_alignment: *horizontal_alignment,
vertical_alignment: *vertical_alignment,
shaping: *shaping,
clip_bounds: *clip_bounds + translation,
clip_bounds: *clip_bounds * transformation,
}));
}
graphics::Primitive::RawText(graphics::text::Raw {
@ -187,9 +189,9 @@ impl<'a> Layer<'a> {
layer.text.push(Text::Raw(graphics::text::Raw {
buffer: buffer.clone(),
position: *position + translation,
position: *position * transformation,
color: *color,
clip_bounds: *clip_bounds + translation,
clip_bounds: *clip_bounds * transformation,
}));
}
Primitive::Quad {
@ -199,12 +201,10 @@ impl<'a> Layer<'a> {
shadow,
} => {
let layer = &mut layers[current_layer];
let bounds = *bounds * transformation;
let quad = Quad {
position: [
bounds.x + translation.x,
bounds.y + translation.y,
],
position: [bounds.x, bounds.y],
size: [bounds.width, bounds.height],
border_color: color::pack(border.color),
border_radius: border.radius.into(),
@ -226,7 +226,7 @@ impl<'a> Layer<'a> {
layer.images.push(Image::Raster {
handle: handle.clone(),
filter_method: *filter_method,
bounds: *bounds + translation,
bounds: *bounds * transformation,
});
}
Primitive::Svg {
@ -239,7 +239,7 @@ impl<'a> Layer<'a> {
layer.images.push(Image::Vector {
handle: handle.clone(),
color: *color,
bounds: *bounds + translation,
bounds: *bounds * transformation,
});
}
Primitive::Group { primitives } => {
@ -247,7 +247,7 @@ impl<'a> Layer<'a> {
for primitive in primitives {
Self::process_primitive(
layers,
translation,
transformation,
primitive,
current_layer,
);
@ -255,7 +255,7 @@ impl<'a> Layer<'a> {
}
Primitive::Clip { bounds, content } => {
let layer = &mut layers[current_layer];
let translated_bounds = *bounds + translation;
let translated_bounds = *bounds * transformation;
// Only draw visible content
if let Some(clip_bounds) =
@ -266,19 +266,19 @@ impl<'a> Layer<'a> {
Self::process_primitive(
layers,
translation,
transformation,
content,
layers.len() - 1,
);
}
}
Primitive::Translate {
translation: new_translation,
Primitive::Transform {
transformation: new_transformation,
content,
} => {
Self::process_primitive(
layers,
translation + *new_translation,
transformation * *new_transformation,
content,
current_layer,
);
@ -286,7 +286,7 @@ impl<'a> Layer<'a> {
Primitive::Cache { content } => {
Self::process_primitive(
layers,
translation,
transformation,
content,
current_layer,
);
@ -296,20 +296,15 @@ impl<'a> Layer<'a> {
graphics::Mesh::Solid { buffers, size } => {
let layer = &mut layers[current_layer];
let bounds = Rectangle::new(
Point::new(translation.x, translation.y),
*size,
);
let bounds =
Rectangle::with_size(*size) * transformation;
// Only draw visible content
if let Some(clip_bounds) =
layer.bounds.intersection(&bounds)
{
layer.meshes.push(Mesh::Solid {
origin: Point::new(
translation.x,
translation.y,
),
transformation,
buffers,
clip_bounds,
});
@ -318,20 +313,15 @@ impl<'a> Layer<'a> {
graphics::Mesh::Gradient { buffers, size } => {
let layer = &mut layers[current_layer];
let bounds = Rectangle::new(
Point::new(translation.x, translation.y),
*size,
);
let bounds =
Rectangle::with_size(*size) * transformation;
// Only draw visible content
if let Some(clip_bounds) =
layer.bounds.intersection(&bounds)
{
layer.meshes.push(Mesh::Gradient {
origin: Point::new(
translation.x,
translation.y,
),
transformation,
buffers,
clip_bounds,
});
@ -340,7 +330,7 @@ impl<'a> Layer<'a> {
},
primitive::Custom::Pipeline(pipeline) => {
let layer = &mut layers[current_layer];
let bounds = pipeline.bounds + translation;
let bounds = pipeline.bounds * transformation;
if let Some(clip_bounds) =
layer.bounds.intersection(&bounds)

View file

@ -1,14 +1,15 @@
//! A collection of triangle primitives.
use crate::core::{Point, Rectangle};
use crate::core::Rectangle;
use crate::graphics::mesh;
use crate::graphics::Transformation;
/// A mesh of triangles.
#[derive(Debug, Clone, Copy)]
pub enum Mesh<'a> {
/// A mesh of triangles with a solid color.
Solid {
/// The origin of the vertices of the [`Mesh`].
origin: Point,
/// The [`Transformation`] for the vertices of the [`Mesh`].
transformation: Transformation,
/// The vertex and index buffers of the [`Mesh`].
buffers: &'a mesh::Indexed<mesh::SolidVertex2D>,
@ -18,8 +19,8 @@ pub enum Mesh<'a> {
},
/// A mesh of triangles with a gradient color.
Gradient {
/// The origin of the vertices of the [`Mesh`].
origin: Point,
/// The [`Transformation`] for the vertices of the [`Mesh`].
transformation: Transformation,
/// The vertex and index buffers of the [`Mesh`].
buffers: &'a mesh::Indexed<mesh::GradientVertex2D>,
@ -31,11 +32,10 @@ pub enum Mesh<'a> {
impl Mesh<'_> {
/// Returns the origin of the [`Mesh`].
pub fn origin(&self) -> Point {
pub fn transformation(&self) -> Transformation {
match self {
Self::Solid { origin, .. } | Self::Gradient { origin, .. } => {
*origin
}
Self::Solid { transformation, .. }
| Self::Gradient { transformation, .. } => *transformation,
}
}

View file

@ -15,6 +15,7 @@ pub enum Text<'a> {
position: Point,
color: Color,
clip_bounds: Rectangle,
scale: f32,
},
/// An editor.
#[allow(missing_docs)]
@ -23,6 +24,7 @@ pub enum Text<'a> {
position: Point,
color: Color,
clip_bounds: Rectangle,
scale: f32,
},
/// Some cached text.
Cached(Cached<'a>),

View file

@ -319,7 +319,7 @@ impl Uniforms {
impl Default for Uniforms {
fn default() -> Self {
Self {
transform: *Transformation::identity().as_ref(),
transform: *Transformation::IDENTITY.as_ref(),
scale: 1.0,
_padding: [0.0; 3],
}

View file

@ -98,12 +98,10 @@ impl Layer {
let mut index_offset = 0;
for mesh in meshes {
let origin = mesh.origin();
let indices = mesh.indices();
let uniforms = Uniforms::new(
transformation * Transformation::translate(origin.x, origin.y),
);
let uniforms =
Uniforms::new(transformation * mesh.transformation());
index_offset +=
self.index_buffer.write(queue, index_offset, indices);