Add transform stack to `canvas::Frame`

This commit is contained in:
Héctor Ramón Jiménez 2020-02-14 04:59:31 +01:00
parent 76df374624
commit 558abf648b
6 changed files with 122 additions and 12 deletions

View File

@ -44,11 +44,18 @@ impl Color {
///
/// [`Color`]: struct.Color.html
pub fn from_rgb8(r: u8, g: u8, b: u8) -> Color {
Color::from_rgba8(r, g, b, 1.0)
}
/// Creates a [`Color`] from its RGB8 components and an alpha value.
///
/// [`Color`]: struct.Color.html
pub fn from_rgba8(r: u8, g: u8, b: u8, a: f32) -> Color {
Color {
r: f32::from(r) / 255.0,
g: f32::from(g) / 255.0,
b: f32::from(b) / 255.0,
a: 1.0,
a,
}
}

View File

@ -11,10 +11,15 @@ pub struct Point {
}
impl Point {
/// The origin (i.e. a [`Point`] with both X=0 and Y=0).
///
/// [`Point`]: struct.Point.html
pub const ORIGIN: Point = Point::new(0.0, 0.0);
/// Creates a new [`Point`] with the given coordinates.
///
/// [`Point`]: struct.Point.html
pub fn new(x: f32, y: f32) -> Self {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}

View File

@ -204,5 +204,5 @@ use iced_web as common;
pub use common::{
futures, Align, Background, Color, Command, Font, HorizontalAlignment,
Length, Point, Space, Subscription, Vector, VerticalAlignment,
Length, Point, Size, Space, Subscription, Vector, VerticalAlignment,
};

View File

@ -73,8 +73,8 @@ pub use dodrio;
pub use element::Element;
pub use hasher::Hasher;
pub use iced_core::{
Align, Background, Color, Font, HorizontalAlignment, Length, Point, Vector,
VerticalAlignment,
Align, Background, Color, Font, HorizontalAlignment, Length, Point, Size,
Vector, VerticalAlignment,
};
pub use iced_futures::{executor, futures, Command};
pub use subscription::Subscription;

View File

@ -1,4 +1,4 @@
use iced_native::Point;
use iced_native::{Point, Size, Vector};
use crate::{
canvas::{Fill, Path, Stroke},
@ -10,6 +10,20 @@ pub struct Frame {
width: f32,
height: f32,
buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u16>,
transforms: Transforms,
}
#[derive(Debug)]
struct Transforms {
previous: Vec<Transform>,
current: Transform,
}
#[derive(Debug, Clone, Copy)]
struct Transform {
raw: lyon::math::Transform,
is_identity: bool,
}
impl Frame {
@ -18,17 +32,32 @@ impl Frame {
width,
height,
buffers: lyon::tessellation::VertexBuffers::new(),
transforms: Transforms {
previous: Vec::new(),
current: Transform {
raw: lyon::math::Transform::identity(),
is_identity: true,
},
},
}
}
#[inline]
pub fn width(&self) -> f32 {
self.width
}
#[inline]
pub fn height(&self) -> f32 {
self.height
}
#[inline]
pub fn size(&self) -> Size {
Size::new(self.width, self.height)
}
#[inline]
pub fn center(&self) -> Point {
Point::new(self.width / 2.0, self.height / 2.0)
}
@ -47,9 +76,23 @@ impl Frame {
let mut tessellator = FillTessellator::new();
let _ = tessellator
.tessellate_path(path.raw(), &FillOptions::default(), &mut buffers)
.expect("Tessellate path");
let result = if self.transforms.current.is_identity {
tessellator.tessellate_path(
path.raw(),
&FillOptions::default(),
&mut buffers,
)
} else {
let path = path.transformed(&self.transforms.current.raw);
tessellator.tessellate_path(
path.raw(),
&FillOptions::default(),
&mut buffers,
)
};
let _ = result.expect("Tessellate path");
}
pub fn stroke(&mut self, path: &Path, stroke: Stroke) {
@ -70,9 +113,54 @@ impl Frame {
options.end_cap = stroke.line_cap.into();
options.line_join = stroke.line_join.into();
let _ = tessellator
.tessellate_path(path.raw(), &options, &mut buffers)
.expect("Stroke path");
let result = if self.transforms.current.is_identity {
tessellator.tessellate_path(path.raw(), &options, &mut buffers)
} else {
let path = path.transformed(&self.transforms.current.raw);
tessellator.tessellate_path(path.raw(), &options, &mut buffers)
};
let _ = result.expect("Stroke path");
}
#[inline]
pub fn with_save(&mut self, f: impl FnOnce(&mut Frame)) {
self.transforms.previous.push(self.transforms.current);
f(self);
self.transforms.current = self.transforms.previous.pop().unwrap();
}
#[inline]
pub fn translate(&mut self, translation: Vector) {
self.transforms.current.raw = self
.transforms
.current
.raw
.pre_translate(lyon::math::Vector::new(
translation.x,
translation.y,
));
self.transforms.current.is_identity = false;
}
#[inline]
pub fn rotate(&mut self, angle: f32) {
self.transforms.current.raw = self
.transforms
.current
.raw
.pre_rotate(lyon::math::Angle::radians(-angle));
self.transforms.current.is_identity = false;
}
#[inline]
pub fn scale(&mut self, scale: f32) {
self.transforms.current.raw =
self.transforms.current.raw.pre_scale(scale, scale);
self.transforms.current.is_identity = false;
}
pub fn into_mesh(self) -> triangle::Mesh2D {

View File

@ -21,6 +21,16 @@ impl Path {
pub(crate) fn raw(&self) -> &lyon::path::Path {
&self.raw
}
#[inline]
pub(crate) fn transformed(
&self,
transform: &lyon::math::Transform,
) -> Path {
Path {
raw: self.raw.transformed(transform),
}
}
}
#[allow(missing_debug_implementations)]