Draft Canvas types and clock example
This commit is contained in:
parent
8daf798e57
commit
f436f20eb8
9 changed files with 494 additions and 2 deletions
|
|
@ -1,5 +1,158 @@
|
|||
//! Draw freely in 2D.
|
||||
use crate::{Defaults, Primitive, Renderer};
|
||||
|
||||
use iced_native::{
|
||||
layout, Color, Element, Hasher, Layout, Length, MouseCursor, Point, Size,
|
||||
Widget,
|
||||
};
|
||||
use std::hash::Hash;
|
||||
|
||||
pub mod layer;
|
||||
pub mod path;
|
||||
|
||||
mod data;
|
||||
mod frame;
|
||||
|
||||
pub use data::Data;
|
||||
pub use frame::Frame;
|
||||
pub use layer::Layer;
|
||||
pub use path::Path;
|
||||
|
||||
/// A 2D drawable region.
|
||||
#[derive(Debug)]
|
||||
pub struct Canvas;
|
||||
pub struct Canvas<'a> {
|
||||
width: Length,
|
||||
height: Length,
|
||||
layers: Vec<Box<dyn Layer + 'a>>,
|
||||
}
|
||||
|
||||
impl<'a> Canvas<'a> {
|
||||
const DEFAULT_SIZE: u16 = 100;
|
||||
|
||||
pub fn new() -> Self {
|
||||
Canvas {
|
||||
width: Length::Units(Self::DEFAULT_SIZE),
|
||||
height: Length::Units(Self::DEFAULT_SIZE),
|
||||
layers: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn width(mut self, width: Length) -> Self {
|
||||
self.width = width;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn height(mut self, height: Length) -> Self {
|
||||
self.height = height;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn push(mut self, layer: impl Layer + 'a) -> Self {
|
||||
self.layers.push(Box::new(layer));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> Widget<Message, Renderer> for Canvas<'a> {
|
||||
fn width(&self) -> Length {
|
||||
self.width
|
||||
}
|
||||
|
||||
fn height(&self) -> Length {
|
||||
self.height
|
||||
}
|
||||
|
||||
fn layout(
|
||||
&self,
|
||||
_renderer: &Renderer,
|
||||
limits: &layout::Limits,
|
||||
) -> layout::Node {
|
||||
let limits = limits.width(self.width).height(self.height);
|
||||
|
||||
let size = limits.resolve(Size::ZERO);
|
||||
|
||||
layout::Node::new(size)
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
_renderer: &mut Renderer,
|
||||
_defaults: &Defaults,
|
||||
_layout: Layout<'_>,
|
||||
_cursor_position: Point,
|
||||
) -> (Primitive, MouseCursor) {
|
||||
(Primitive::None, MouseCursor::Idle)
|
||||
}
|
||||
|
||||
fn hash_layout(&self, state: &mut Hasher) {
|
||||
std::any::TypeId::of::<Canvas<'static>>().hash(state);
|
||||
|
||||
self.width.hash(state);
|
||||
self.height.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message> From<Canvas<'a>> for Element<'a, Message, Renderer>
|
||||
where
|
||||
Message: 'static,
|
||||
{
|
||||
fn from(canvas: Canvas<'a>) -> Element<'a, Message, Renderer> {
|
||||
Element::new(canvas)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Stroke {
|
||||
pub color: Color,
|
||||
pub width: f32,
|
||||
pub line_cap: LineCap,
|
||||
pub line_join: LineJoin,
|
||||
}
|
||||
|
||||
impl Default for Stroke {
|
||||
fn default() -> Stroke {
|
||||
Stroke {
|
||||
color: Color::BLACK,
|
||||
width: 1.0,
|
||||
line_cap: LineCap::default(),
|
||||
line_join: LineJoin::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum LineCap {
|
||||
Butt,
|
||||
Square,
|
||||
Round,
|
||||
}
|
||||
|
||||
impl Default for LineCap {
|
||||
fn default() -> LineCap {
|
||||
LineCap::Butt
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum LineJoin {
|
||||
Miter,
|
||||
Round,
|
||||
Bevel,
|
||||
}
|
||||
|
||||
impl Default for LineJoin {
|
||||
fn default() -> LineJoin {
|
||||
LineJoin::Miter
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Fill {
|
||||
Color(Color),
|
||||
}
|
||||
|
||||
impl Default for Fill {
|
||||
fn default() -> Fill {
|
||||
Fill::Color(Color::BLACK)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
20
wgpu/src/widget/canvas/data.rs
Normal file
20
wgpu/src/widget/canvas/data.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||
pub struct Data<T> {
|
||||
raw: T,
|
||||
version: usize,
|
||||
}
|
||||
|
||||
impl<T> Data<T> {
|
||||
pub fn new(data: T) -> Self {
|
||||
Data {
|
||||
raw: data,
|
||||
version: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, f: impl FnOnce(&mut T)) {
|
||||
f(&mut self.raw);
|
||||
|
||||
self.version += 1;
|
||||
}
|
||||
}
|
||||
39
wgpu/src/widget/canvas/frame.rs
Normal file
39
wgpu/src/widget/canvas/frame.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use iced_native::Point;
|
||||
|
||||
use crate::{
|
||||
canvas::{Fill, Path, Stroke},
|
||||
triangle,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Frame {
|
||||
width: u32,
|
||||
height: u32,
|
||||
buffers: lyon::tessellation::VertexBuffers<triangle::Vertex2D, u16>,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
pub(crate) fn new(width: u32, height: u32) -> Frame {
|
||||
Frame {
|
||||
width,
|
||||
height,
|
||||
buffers: lyon::tessellation::VertexBuffers::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn width(&self) -> u32 {
|
||||
self.width
|
||||
}
|
||||
|
||||
pub fn height(&self) -> u32 {
|
||||
self.height
|
||||
}
|
||||
|
||||
pub fn center(&self) -> Point {
|
||||
Point::new(self.width as f32 / 2.0, self.height as f32 / 2.0)
|
||||
}
|
||||
|
||||
pub fn fill(&mut self, path: Path, fill: Fill) {}
|
||||
|
||||
pub fn stroke(&mut self, path: Path, stroke: Stroke) {}
|
||||
}
|
||||
41
wgpu/src/widget/canvas/layer.rs
Normal file
41
wgpu/src/widget/canvas/layer.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
use crate::canvas::Frame;
|
||||
|
||||
pub trait Layer: std::fmt::Debug {}
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Cached<T: Drawable> {
|
||||
input: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> Cached<T>
|
||||
where
|
||||
T: Drawable + std::fmt::Debug,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
Cached { input: PhantomData }
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {}
|
||||
|
||||
pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a {
|
||||
Bind {
|
||||
cache: self,
|
||||
input: input,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Bind<'a, T: Drawable> {
|
||||
cache: &'a Cached<T>,
|
||||
input: &'a T,
|
||||
}
|
||||
|
||||
impl<'a, T> Layer for Bind<'a, T> where T: Drawable + std::fmt::Debug {}
|
||||
|
||||
pub trait Drawable {
|
||||
fn draw(&self, frame: &mut Frame);
|
||||
}
|
||||
78
wgpu/src/widget/canvas/path.rs
Normal file
78
wgpu/src/widget/canvas/path.rs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
use iced_native::{Point, Vector};
|
||||
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct Path {
|
||||
raw: lyon::path::Builder,
|
||||
}
|
||||
|
||||
impl Path {
|
||||
pub fn new() -> Path {
|
||||
Path {
|
||||
raw: lyon::path::Path::builder(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn move_to(&mut self, point: Point) {
|
||||
let _ = self.raw.move_to(lyon::math::Point::new(point.x, point.y));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn line_to(&mut self, point: Point) {
|
||||
let _ = self.raw.line_to(lyon::math::Point::new(point.x, point.y));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn arc(&mut self, arc: Arc) {
|
||||
self.ellipse(arc.into())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn ellipse(&mut self, ellipse: Ellipse) {
|
||||
let arc = lyon::geom::Arc {
|
||||
center: lyon::math::Point::new(ellipse.center.x, ellipse.center.y),
|
||||
radii: lyon::math::Vector::new(ellipse.radii.x, ellipse.radii.y),
|
||||
x_rotation: lyon::math::Angle::radians(ellipse.rotation),
|
||||
start_angle: lyon::math::Angle::radians(ellipse.start_angle),
|
||||
sweep_angle: lyon::math::Angle::radians(ellipse.end_angle),
|
||||
};
|
||||
|
||||
arc.for_each_quadratic_bezier(&mut |curve| {
|
||||
let _ = self.raw.quadratic_bezier_to(curve.ctrl, curve.to);
|
||||
});
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn close(&mut self) {
|
||||
self.raw.close()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Arc {
|
||||
pub center: Point,
|
||||
pub radius: f32,
|
||||
pub start_angle: f32,
|
||||
pub end_angle: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Ellipse {
|
||||
pub center: Point,
|
||||
pub radii: Vector,
|
||||
pub rotation: f32,
|
||||
pub start_angle: f32,
|
||||
pub end_angle: f32,
|
||||
}
|
||||
|
||||
impl From<Arc> for Ellipse {
|
||||
fn from(arc: Arc) -> Ellipse {
|
||||
Ellipse {
|
||||
center: arc.center,
|
||||
radii: Vector::new(arc.radius, arc.radius),
|
||||
rotation: 0.0,
|
||||
start_angle: arc.start_angle,
|
||||
end_angle: arc.end_angle,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue