Move mesh::Style to triangle and reuse it in fill and stroke
This commit is contained in:
parent
e0bb707f1e
commit
84d1b79fef
10 changed files with 72 additions and 98 deletions
|
|
@ -133,7 +133,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool {
|
||||||
if solid {
|
if solid {
|
||||||
frame.fill_rectangle(top_left, size, random_color());
|
frame.fill_rectangle(top_left, size, random_color());
|
||||||
} else {
|
} else {
|
||||||
frame.fill_rectangle(top_left, size, &gradient(top_left, size));
|
frame.fill_rectangle(top_left, size, gradient(top_left, size));
|
||||||
};
|
};
|
||||||
|
|
||||||
solid
|
solid
|
||||||
|
|
|
||||||
|
|
@ -213,7 +213,7 @@ impl<Message> canvas::Program<Message> for State {
|
||||||
.build()
|
.build()
|
||||||
.expect("Build Earth fill gradient");
|
.expect("Build Earth fill gradient");
|
||||||
|
|
||||||
frame.fill(&earth, &earth_fill);
|
frame.fill(&earth, earth_fill);
|
||||||
|
|
||||||
frame.with_save(|frame| {
|
frame.with_save(|frame| {
|
||||||
frame.rotate(rotation * 10.0);
|
frame.rotate(rotation * 10.0);
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,11 @@ mod gradient;
|
||||||
mod solid;
|
mod solid;
|
||||||
|
|
||||||
use crate::{program, Transformation};
|
use crate::{program, Transformation};
|
||||||
|
|
||||||
|
use iced_graphics::layer::mesh::{self, Mesh};
|
||||||
|
use iced_graphics::triangle;
|
||||||
|
|
||||||
use glow::HasContext;
|
use glow::HasContext;
|
||||||
use iced_graphics::layer::{mesh, Mesh};
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
pub use iced_graphics::triangle::{Mesh2D, Vertex2D};
|
pub use iced_graphics::triangle::{Mesh2D, Vertex2D};
|
||||||
|
|
@ -136,10 +139,10 @@ impl Pipeline {
|
||||||
);
|
);
|
||||||
|
|
||||||
match mesh.style {
|
match mesh.style {
|
||||||
mesh::Style::Solid(color) => {
|
triangle::Style::Solid(color) => {
|
||||||
self.programs.solid.use_program(gl, color, &transform);
|
self.programs.solid.use_program(gl, color, &transform);
|
||||||
}
|
}
|
||||||
mesh::Style::Gradient(gradient) => {
|
triangle::Style::Gradient(gradient) => {
|
||||||
self.programs
|
self.programs
|
||||||
.gradient
|
.gradient
|
||||||
.use_program(gl, gradient, &transform);
|
.use_program(gl, gradient, &transform);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
//! A collection of triangle primitives.
|
//! A collection of triangle primitives.
|
||||||
use crate::gradient::Gradient;
|
use crate::triangle;
|
||||||
use crate::{triangle, Color, Point, Rectangle};
|
use crate::{Point, Rectangle};
|
||||||
|
|
||||||
/// A mesh of triangles.
|
/// A mesh of triangles.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
|
@ -15,22 +15,7 @@ pub struct Mesh<'a> {
|
||||||
pub clip_bounds: Rectangle<f32>,
|
pub clip_bounds: Rectangle<f32>,
|
||||||
|
|
||||||
/// The shader of the [`Mesh`].
|
/// The shader of the [`Mesh`].
|
||||||
pub style: &'a Style,
|
pub style: &'a triangle::Style,
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
/// Supported shaders for primitives.
|
|
||||||
pub enum Style {
|
|
||||||
/// Fill a primitive with a solid color.
|
|
||||||
Solid(Color),
|
|
||||||
/// Fill a primitive with an interpolated color.
|
|
||||||
Gradient(Gradient),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Gradient> for Style {
|
|
||||||
fn from(gradient: Gradient) -> Self {
|
|
||||||
Self::Gradient(gradient)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of total vertices & total indices of all [`Mesh`]es.
|
/// Returns the number of total vertices & total indices of all [`Mesh`]es.
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ use iced_native::image;
|
||||||
use iced_native::svg;
|
use iced_native::svg;
|
||||||
use iced_native::{Background, Color, Font, Rectangle, Size, Vector};
|
use iced_native::{Background, Color, Font, Rectangle, Size, Vector};
|
||||||
|
|
||||||
|
use crate::alignment;
|
||||||
use crate::triangle;
|
use crate::triangle;
|
||||||
use crate::{alignment, layer::mesh};
|
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
|
@ -90,7 +90,7 @@ pub enum Primitive {
|
||||||
size: Size,
|
size: Size,
|
||||||
|
|
||||||
/// The shader of the mesh
|
/// The shader of the mesh
|
||||||
style: mesh::Style,
|
style: triangle::Style,
|
||||||
},
|
},
|
||||||
/// A cached primitive.
|
/// A cached primitive.
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
//! Draw geometry using meshes of triangles.
|
//! Draw geometry using meshes of triangles.
|
||||||
|
use crate::{Color, Gradient};
|
||||||
|
|
||||||
use bytemuck::{Pod, Zeroable};
|
use bytemuck::{Pod, Zeroable};
|
||||||
|
|
||||||
/// A set of [`Vertex2D`] and indices representing a list of triangles.
|
/// A set of [`Vertex2D`] and indices representing a list of triangles.
|
||||||
|
|
@ -19,3 +21,24 @@ pub struct Vertex2D {
|
||||||
/// The vertex position in 2D space.
|
/// The vertex position in 2D space.
|
||||||
pub position: [f32; 2],
|
pub position: [f32; 2],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
/// Supported shaders for triangle primitives.
|
||||||
|
pub enum Style {
|
||||||
|
/// Fill a primitive with a solid color.
|
||||||
|
Solid(Color),
|
||||||
|
/// Fill a primitive with an interpolated color.
|
||||||
|
Gradient(Gradient),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Color> for Style {
|
||||||
|
fn from(color: Color) -> Self {
|
||||||
|
Self::Solid(color)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Gradient> for Style {
|
||||||
|
fn from(gradient: Gradient) -> Self {
|
||||||
|
Self::Gradient(gradient)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,15 @@
|
||||||
//! Fill [crate::widget::canvas::Geometry] with a certain style.
|
//! Fill [crate::widget::canvas::Geometry] with a certain style.
|
||||||
|
use crate::{Color, Gradient};
|
||||||
|
|
||||||
use crate::gradient::Gradient;
|
pub use crate::triangle::Style;
|
||||||
use crate::layer::mesh;
|
|
||||||
use crate::widget::canvas::frame::Transform;
|
|
||||||
use iced_native::Color;
|
|
||||||
|
|
||||||
/// The style used to fill geometry.
|
/// The style used to fill geometry.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Fill<'a> {
|
pub struct Fill {
|
||||||
/// The color or gradient of the fill.
|
/// The color or gradient of the fill.
|
||||||
///
|
///
|
||||||
/// By default, it is set to [`FillStyle::Solid`] `BLACK`.
|
/// By default, it is set to [`FillStyle::Solid`] `BLACK`.
|
||||||
pub style: Style<'a>,
|
pub style: Style,
|
||||||
|
|
||||||
/// The fill rule defines how to determine what is inside and what is
|
/// The fill rule defines how to determine what is inside and what is
|
||||||
/// outside of a shape.
|
/// outside of a shape.
|
||||||
|
|
@ -24,17 +22,17 @@ pub struct Fill<'a> {
|
||||||
pub rule: FillRule,
|
pub rule: FillRule,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Default for Fill<'a> {
|
impl Default for Fill {
|
||||||
fn default() -> Fill<'a> {
|
fn default() -> Self {
|
||||||
Fill {
|
Self {
|
||||||
style: Style::Solid(Color::BLACK),
|
style: Style::Solid(Color::BLACK),
|
||||||
rule: FillRule::NonZero,
|
rule: FillRule::NonZero,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<Color> for Fill<'a> {
|
impl From<Color> for Fill {
|
||||||
fn from(color: Color) -> Fill<'a> {
|
fn from(color: Color) -> Fill {
|
||||||
Fill {
|
Fill {
|
||||||
style: Style::Solid(color),
|
style: Style::Solid(color),
|
||||||
..Fill::default()
|
..Fill::default()
|
||||||
|
|
@ -42,8 +40,8 @@ impl<'a> From<Color> for Fill<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Gradient> for Fill<'a> {
|
impl From<Gradient> for Fill {
|
||||||
fn from(gradient: &'a Gradient) -> Self {
|
fn from(gradient: Gradient) -> Self {
|
||||||
Fill {
|
Fill {
|
||||||
style: Style::Gradient(gradient),
|
style: Style::Gradient(gradient),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|
@ -51,27 +49,6 @@ impl<'a> From<&'a Gradient> for Fill<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The style of a [`Fill`].
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum Style<'a> {
|
|
||||||
/// A solid color
|
|
||||||
Solid(Color),
|
|
||||||
/// A color gradient
|
|
||||||
Gradient(&'a Gradient),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Style<'a> {
|
|
||||||
/// Converts a fill's [Style] to a [mesh::Style] for use in the renderer's shader.
|
|
||||||
pub(crate) fn as_mesh_style(&self, transform: &Transform) -> mesh::Style {
|
|
||||||
match self {
|
|
||||||
Style::Solid(color) => mesh::Style::Solid(*color),
|
|
||||||
Style::Gradient(gradient) => mesh::Style::Gradient(
|
|
||||||
transform.transform_gradient((*gradient).clone()),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The fill rule defines how to determine what is inside and what is outside of
|
/// The fill rule defines how to determine what is inside and what is outside of
|
||||||
/// a shape.
|
/// a shape.
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ use std::borrow::Cow;
|
||||||
use iced_native::{Point, Rectangle, Size, Vector};
|
use iced_native::{Point, Rectangle, Size, Vector};
|
||||||
|
|
||||||
use crate::gradient::Gradient;
|
use crate::gradient::Gradient;
|
||||||
use crate::layer::mesh;
|
|
||||||
use crate::triangle;
|
use crate::triangle;
|
||||||
use crate::triangle::Vertex2D;
|
use crate::triangle::Vertex2D;
|
||||||
use crate::widget::canvas::{path, Fill, Geometry, Path, Stroke, Text};
|
use crate::widget::canvas::{path, Fill, Geometry, Path, Stroke, Text};
|
||||||
|
|
@ -26,7 +25,7 @@ pub struct Frame {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BufferStack {
|
struct BufferStack {
|
||||||
stack: Vec<(tessellation::VertexBuffers<Vertex2D, u32>, mesh::Style)>,
|
stack: Vec<(tessellation::VertexBuffers<Vertex2D, u32>, triangle::Style)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BufferStack {
|
impl BufferStack {
|
||||||
|
|
@ -36,7 +35,7 @@ impl BufferStack {
|
||||||
|
|
||||||
fn get(
|
fn get(
|
||||||
&mut self,
|
&mut self,
|
||||||
mesh_style: mesh::Style,
|
mesh_style: triangle::Style,
|
||||||
) -> tessellation::BuffersBuilder<'_, Vertex2D, u32, Vertex2DBuilder> {
|
) -> tessellation::BuffersBuilder<'_, Vertex2D, u32, Vertex2DBuilder> {
|
||||||
match self.stack.last_mut() {
|
match self.stack.last_mut() {
|
||||||
Some((_, current_style)) if current_style == &mesh_style => {}
|
Some((_, current_style)) if current_style == &mesh_style => {}
|
||||||
|
|
@ -74,6 +73,15 @@ impl Transform {
|
||||||
point.y = transformed.y;
|
point.y = transformed.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn transform_style(&self, style: triangle::Style) -> triangle::Style {
|
||||||
|
match style {
|
||||||
|
triangle::Style::Solid(color) => triangle::Style::Solid(color),
|
||||||
|
triangle::Style::Gradient(gradient) => {
|
||||||
|
triangle::Style::Gradient(self.transform_gradient(gradient))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn transform_gradient(
|
pub(crate) fn transform_gradient(
|
||||||
&self,
|
&self,
|
||||||
mut gradient: Gradient,
|
mut gradient: Gradient,
|
||||||
|
|
@ -135,12 +143,12 @@ impl Frame {
|
||||||
|
|
||||||
/// Draws the given [`Path`] on the [`Frame`] by filling it with the
|
/// Draws the given [`Path`] on the [`Frame`] by filling it with the
|
||||||
/// provided style.
|
/// provided style.
|
||||||
pub fn fill<'a>(&mut self, path: &Path, fill: impl Into<Fill<'a>>) {
|
pub fn fill(&mut self, path: &Path, fill: impl Into<Fill>) {
|
||||||
let Fill { style, rule } = fill.into();
|
let Fill { style, rule } = fill.into();
|
||||||
|
|
||||||
let mut buffer = self
|
let mut buffer = self
|
||||||
.buffers
|
.buffers
|
||||||
.get(style.as_mesh_style(&self.transforms.current));
|
.get(self.transforms.current.transform_style(style));
|
||||||
|
|
||||||
let options =
|
let options =
|
||||||
tessellation::FillOptions::default().with_fill_rule(rule.into());
|
tessellation::FillOptions::default().with_fill_rule(rule.into());
|
||||||
|
|
@ -165,17 +173,17 @@ impl Frame {
|
||||||
|
|
||||||
/// Draws an axis-aligned rectangle given its top-left corner coordinate and
|
/// Draws an axis-aligned rectangle given its top-left corner coordinate and
|
||||||
/// its `Size` on the [`Frame`] by filling it with the provided style.
|
/// its `Size` on the [`Frame`] by filling it with the provided style.
|
||||||
pub fn fill_rectangle<'a>(
|
pub fn fill_rectangle(
|
||||||
&mut self,
|
&mut self,
|
||||||
top_left: Point,
|
top_left: Point,
|
||||||
size: Size,
|
size: Size,
|
||||||
fill: impl Into<Fill<'a>>,
|
fill: impl Into<Fill>,
|
||||||
) {
|
) {
|
||||||
let Fill { style, rule } = fill.into();
|
let Fill { style, rule } = fill.into();
|
||||||
|
|
||||||
let mut buffer = self
|
let mut buffer = self
|
||||||
.buffers
|
.buffers
|
||||||
.get(style.as_mesh_style(&self.transforms.current));
|
.get(self.transforms.current.transform_style(style));
|
||||||
|
|
||||||
let top_left =
|
let top_left =
|
||||||
self.transforms.current.raw.transform_point(
|
self.transforms.current.raw.transform_point(
|
||||||
|
|
@ -206,7 +214,7 @@ impl Frame {
|
||||||
|
|
||||||
let mut buffer = self
|
let mut buffer = self
|
||||||
.buffers
|
.buffers
|
||||||
.get(stroke.style.as_mesh_style(&self.transforms.current));
|
.get(self.transforms.current.transform_style(stroke.style));
|
||||||
|
|
||||||
let mut options = tessellation::StrokeOptions::default();
|
let mut options = tessellation::StrokeOptions::default();
|
||||||
options.line_width = stroke.width;
|
options.line_width = stroke.width;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
//! Create lines from a [crate::widget::canvas::Path] and assigns them various attributes/styles.
|
//! Create lines from a [crate::widget::canvas::Path] and assigns them various attributes/styles.
|
||||||
|
pub use crate::triangle::Style;
|
||||||
|
|
||||||
use crate::gradient::Gradient;
|
|
||||||
use crate::layer::mesh;
|
|
||||||
use crate::widget::canvas::frame::Transform;
|
|
||||||
use iced_native::Color;
|
use iced_native::Color;
|
||||||
|
|
||||||
/// The style of a stroke.
|
/// The style of a stroke.
|
||||||
|
|
@ -11,7 +9,7 @@ pub struct Stroke<'a> {
|
||||||
/// The color or gradient of the stroke.
|
/// The color or gradient of the stroke.
|
||||||
///
|
///
|
||||||
/// By default, it is set to [`StrokeStyle::Solid`] `BLACK`.
|
/// By default, it is set to [`StrokeStyle::Solid`] `BLACK`.
|
||||||
pub style: Style<'a>,
|
pub style: Style,
|
||||||
/// The distance between the two edges of the stroke.
|
/// The distance between the two edges of the stroke.
|
||||||
pub width: f32,
|
pub width: f32,
|
||||||
/// The shape to be used at the end of open subpaths when they are stroked.
|
/// The shape to be used at the end of open subpaths when they are stroked.
|
||||||
|
|
@ -60,27 +58,6 @@ impl<'a> Default for Stroke<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The style of a [`Stroke`].
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub enum Style<'a> {
|
|
||||||
/// A solid color
|
|
||||||
Solid(Color),
|
|
||||||
/// A color gradient
|
|
||||||
Gradient(&'a Gradient),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Style<'a> {
|
|
||||||
/// Converts a fill's [Style] to a [mesh::Style] for use in the renderer's shader.
|
|
||||||
pub(crate) fn as_mesh_style(&self, transform: &Transform) -> mesh::Style {
|
|
||||||
match self {
|
|
||||||
Style::Solid(color) => mesh::Style::Solid(*color),
|
|
||||||
Style::Gradient(gradient) => mesh::Style::Gradient(
|
|
||||||
transform.transform_gradient((*gradient).clone()),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The shape used at the end of open subpaths when they are stroked.
|
/// The shape used at the end of open subpaths when they are stroked.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum LineCap {
|
pub enum LineCap {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@ use crate::{settings, Transformation};
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use std::fmt::Formatter;
|
use std::fmt::Formatter;
|
||||||
|
|
||||||
use iced_graphics::layer::{mesh, Mesh};
|
use iced_graphics::layer::mesh::{self, Mesh};
|
||||||
|
use iced_graphics::triangle;
|
||||||
use iced_graphics::Size;
|
use iced_graphics::Size;
|
||||||
|
|
||||||
use crate::buffer::r#static::Buffer;
|
use crate::buffer::r#static::Buffer;
|
||||||
|
|
@ -141,10 +142,10 @@ impl Pipeline {
|
||||||
|
|
||||||
//push uniform data to CPU buffers
|
//push uniform data to CPU buffers
|
||||||
match mesh.style {
|
match mesh.style {
|
||||||
mesh::Style::Solid(color) => {
|
triangle::Style::Solid(color) => {
|
||||||
self.pipelines.solid.push(transform, color);
|
self.pipelines.solid.push(transform, color);
|
||||||
}
|
}
|
||||||
mesh::Style::Gradient(gradient) => {
|
triangle::Style::Gradient(gradient) => {
|
||||||
self.pipelines.gradient.push(transform, gradient);
|
self.pipelines.gradient.push(transform, gradient);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -199,7 +200,7 @@ impl Pipeline {
|
||||||
);
|
);
|
||||||
|
|
||||||
match mesh.style {
|
match mesh.style {
|
||||||
mesh::Style::Solid(_) => {
|
triangle::Style::Solid(_) => {
|
||||||
if !last_is_solid.unwrap_or(false) {
|
if !last_is_solid.unwrap_or(false) {
|
||||||
self.pipelines
|
self.pipelines
|
||||||
.solid
|
.solid
|
||||||
|
|
@ -215,7 +216,7 @@ impl Pipeline {
|
||||||
|
|
||||||
num_solids += 1;
|
num_solids += 1;
|
||||||
}
|
}
|
||||||
mesh::Style::Gradient(_) => {
|
triangle::Style::Gradient(_) => {
|
||||||
if last_is_solid.unwrap_or(true) {
|
if last_is_solid.unwrap_or(true) {
|
||||||
self.pipelines
|
self.pipelines
|
||||||
.gradient
|
.gradient
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue