Remove Drawable
and rename State
to Program
This commit is contained in:
parent
7f1e7aea07
commit
2539042b71
@ -135,8 +135,8 @@ impl From<Rectangle<f32>> for Rectangle<u32> {
|
||||
Rectangle {
|
||||
x: rectangle.x as u32,
|
||||
y: rectangle.y as u32,
|
||||
width: rectangle.width.ceil() as u32,
|
||||
height: rectangle.height.ceil() as u32,
|
||||
width: (rectangle.width + 0.5).round() as u32,
|
||||
height: (rectangle.height + 0.5).round() as u32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -69,10 +69,8 @@ impl Sandbox for Example {
|
||||
|
||||
mod bezier {
|
||||
use iced::{
|
||||
canvas::{
|
||||
self, Canvas, Drawable, Event, Frame, Geometry, Path, Stroke,
|
||||
},
|
||||
mouse, ButtonState, Element, Length, Point, Size,
|
||||
canvas::{self, Canvas, Event, Frame, Geometry, Path, Stroke},
|
||||
mouse, ButtonState, Element, Length, Point, Rectangle, Size,
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
@ -106,8 +104,10 @@ mod bezier {
|
||||
curves: &'a [Curve],
|
||||
}
|
||||
|
||||
impl<'a> canvas::State<Curve> for Bezier<'a> {
|
||||
fn update(&mut self, event: Event, _bounds: Size) -> Option<Curve> {
|
||||
impl<'a> canvas::Program<Curve> for Bezier<'a> {
|
||||
fn update(&mut self, event: Event, bounds: Size) -> Option<Curve> {
|
||||
let bounds = Rectangle::new(Point::ORIGIN, bounds);
|
||||
|
||||
match event {
|
||||
Event::Mouse(mouse_event) => match mouse_event {
|
||||
mouse::Event::CursorMoved { x, y } => {
|
||||
@ -118,46 +118,55 @@ mod bezier {
|
||||
mouse::Event::Input {
|
||||
button: mouse::Button::Left,
|
||||
state: ButtonState::Pressed,
|
||||
} => match self.state.pending {
|
||||
None => {
|
||||
self.state.pending = Some(Pending::One {
|
||||
from: self.state.cursor_position,
|
||||
});
|
||||
None
|
||||
}
|
||||
Some(Pending::One { from }) => {
|
||||
self.state.pending = Some(Pending::Two {
|
||||
from,
|
||||
to: self.state.cursor_position,
|
||||
});
|
||||
} if bounds.contains(self.state.cursor_position) => {
|
||||
match self.state.pending {
|
||||
None => {
|
||||
self.state.pending = Some(Pending::One {
|
||||
from: self.state.cursor_position,
|
||||
});
|
||||
None
|
||||
}
|
||||
Some(Pending::One { from }) => {
|
||||
self.state.pending = Some(Pending::Two {
|
||||
from,
|
||||
to: self.state.cursor_position,
|
||||
});
|
||||
|
||||
None
|
||||
}
|
||||
Some(Pending::Two { from, to }) => {
|
||||
self.state.pending = None;
|
||||
None
|
||||
}
|
||||
Some(Pending::Two { from, to }) => {
|
||||
self.state.pending = None;
|
||||
|
||||
Some(Curve {
|
||||
from,
|
||||
to,
|
||||
control: self.state.cursor_position,
|
||||
})
|
||||
Some(Curve {
|
||||
from,
|
||||
to,
|
||||
control: self.state.cursor_position,
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn draw(&self, bounds: Size) -> Vec<Geometry> {
|
||||
let curves = self.state.cache.draw(bounds, &self.curves);
|
||||
let content = self.state.cache.draw(bounds, |frame: &mut Frame| {
|
||||
Curve::draw_all(self.curves, frame);
|
||||
|
||||
frame.stroke(
|
||||
&Path::rectangle(Point::ORIGIN, frame.size()),
|
||||
Stroke::default(),
|
||||
);
|
||||
});
|
||||
|
||||
if let Some(pending) = &self.state.pending {
|
||||
let pending_curve =
|
||||
pending.draw(bounds, self.state.cursor_position);
|
||||
|
||||
vec![curves, pending_curve]
|
||||
vec![content, pending_curve]
|
||||
} else {
|
||||
vec![curves]
|
||||
vec![content]
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -169,14 +178,16 @@ mod bezier {
|
||||
control: Point,
|
||||
}
|
||||
|
||||
impl Drawable for Curve {
|
||||
fn draw(&self, frame: &mut Frame) {
|
||||
let curve = Path::new(|p| {
|
||||
p.move_to(self.from);
|
||||
p.quadratic_curve_to(self.control, self.to);
|
||||
impl Curve {
|
||||
fn draw_all(curves: &[Curve], frame: &mut Frame) {
|
||||
let curves = Path::new(|p| {
|
||||
for curve in curves {
|
||||
p.move_to(curve.from);
|
||||
p.quadratic_curve_to(curve.control, curve.to);
|
||||
}
|
||||
});
|
||||
|
||||
frame.stroke(&curve, Stroke::default().with_width(2.0));
|
||||
frame.stroke(&curves, Stroke::default().with_width(2.0));
|
||||
}
|
||||
}
|
||||
|
||||
@ -202,7 +213,7 @@ mod bezier {
|
||||
control: cursor_position,
|
||||
};
|
||||
|
||||
curve.draw(&mut frame);
|
||||
Curve::draw_all(&[curve], &mut frame);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -5,9 +5,6 @@ authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[features]
|
||||
canvas = []
|
||||
|
||||
[dependencies]
|
||||
iced = { path = "../..", features = ["canvas", "async-std", "debug"] }
|
||||
iced_native = { path = "../../native" }
|
||||
|
@ -1,6 +1,7 @@
|
||||
use iced::{
|
||||
canvas, executor, Application, Canvas, Color, Command, Container, Element,
|
||||
Length, Point, Settings, Subscription, Vector,
|
||||
canvas::{self, Cache, Canvas, Geometry, LineCap, Path, Stroke},
|
||||
executor, Application, Color, Command, Container, Element, Length, Point,
|
||||
Settings, Size, Subscription, Vector,
|
||||
};
|
||||
|
||||
pub fn main() {
|
||||
@ -11,8 +12,8 @@ pub fn main() {
|
||||
}
|
||||
|
||||
struct Clock {
|
||||
now: LocalTime,
|
||||
clock: canvas::Cache,
|
||||
now: chrono::DateTime<chrono::Local>,
|
||||
clock: Cache,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@ -28,7 +29,7 @@ impl Application for Clock {
|
||||
fn new(_flags: ()) -> (Self, Command<Message>) {
|
||||
(
|
||||
Clock {
|
||||
now: chrono::Local::now().into(),
|
||||
now: chrono::Local::now(),
|
||||
clock: Default::default(),
|
||||
},
|
||||
Command::none(),
|
||||
@ -59,7 +60,7 @@ impl Application for Clock {
|
||||
}
|
||||
|
||||
fn view(&mut self) -> Element<Message> {
|
||||
let canvas = Canvas::new(self.clock.with(&self.now))
|
||||
let canvas = Canvas::new(self)
|
||||
.width(Length::Units(400))
|
||||
.height(Length::Units(400));
|
||||
|
||||
@ -73,69 +74,54 @@ impl Application for Clock {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct LocalTime {
|
||||
hour: u32,
|
||||
minute: u32,
|
||||
second: u32,
|
||||
}
|
||||
|
||||
impl From<chrono::DateTime<chrono::Local>> for LocalTime {
|
||||
fn from(date_time: chrono::DateTime<chrono::Local>) -> LocalTime {
|
||||
impl canvas::Program<Message> for Clock {
|
||||
fn draw(&self, bounds: Size) -> Vec<Geometry> {
|
||||
use chrono::Timelike;
|
||||
|
||||
LocalTime {
|
||||
hour: date_time.hour(),
|
||||
minute: date_time.minute(),
|
||||
second: date_time.second(),
|
||||
}
|
||||
}
|
||||
}
|
||||
let clock = self.clock.draw(bounds, |frame| {
|
||||
let center = frame.center();
|
||||
let radius = frame.width().min(frame.height()) / 2.0;
|
||||
|
||||
impl canvas::Drawable for LocalTime {
|
||||
fn draw(&self, frame: &mut canvas::Frame) {
|
||||
use canvas::Path;
|
||||
let background = Path::circle(center, radius);
|
||||
frame.fill(&background, Color::from_rgb8(0x12, 0x93, 0xD8));
|
||||
|
||||
let center = frame.center();
|
||||
let radius = frame.width().min(frame.height()) / 2.0;
|
||||
let short_hand =
|
||||
Path::line(Point::ORIGIN, Point::new(0.0, -0.5 * radius));
|
||||
|
||||
let clock = Path::circle(center, radius);
|
||||
frame.fill(&clock, Color::from_rgb8(0x12, 0x93, 0xD8));
|
||||
let long_hand =
|
||||
Path::line(Point::ORIGIN, Point::new(0.0, -0.8 * radius));
|
||||
|
||||
let short_hand =
|
||||
Path::line(Point::ORIGIN, Point::new(0.0, -0.5 * radius));
|
||||
let thin_stroke = Stroke {
|
||||
width: radius / 100.0,
|
||||
color: Color::WHITE,
|
||||
line_cap: LineCap::Round,
|
||||
..Stroke::default()
|
||||
};
|
||||
|
||||
let long_hand =
|
||||
Path::line(Point::ORIGIN, Point::new(0.0, -0.8 * radius));
|
||||
let wide_stroke = Stroke {
|
||||
width: thin_stroke.width * 3.0,
|
||||
..thin_stroke
|
||||
};
|
||||
|
||||
let thin_stroke = canvas::Stroke {
|
||||
width: radius / 100.0,
|
||||
color: Color::WHITE,
|
||||
line_cap: canvas::LineCap::Round,
|
||||
..canvas::Stroke::default()
|
||||
};
|
||||
frame.translate(Vector::new(center.x, center.y));
|
||||
|
||||
let wide_stroke = canvas::Stroke {
|
||||
width: thin_stroke.width * 3.0,
|
||||
..thin_stroke
|
||||
};
|
||||
frame.with_save(|frame| {
|
||||
frame.rotate(hand_rotation(self.now.hour(), 12));
|
||||
frame.stroke(&short_hand, wide_stroke);
|
||||
});
|
||||
|
||||
frame.translate(Vector::new(center.x, center.y));
|
||||
frame.with_save(|frame| {
|
||||
frame.rotate(hand_rotation(self.now.minute(), 60));
|
||||
frame.stroke(&long_hand, wide_stroke);
|
||||
});
|
||||
|
||||
frame.with_save(|frame| {
|
||||
frame.rotate(hand_rotation(self.hour, 12));
|
||||
frame.stroke(&short_hand, wide_stroke);
|
||||
frame.with_save(|frame| {
|
||||
frame.rotate(hand_rotation(self.now.second(), 60));
|
||||
frame.stroke(&long_hand, thin_stroke);
|
||||
})
|
||||
});
|
||||
|
||||
frame.with_save(|frame| {
|
||||
frame.rotate(hand_rotation(self.minute, 60));
|
||||
frame.stroke(&long_hand, wide_stroke);
|
||||
});
|
||||
|
||||
frame.with_save(|frame| {
|
||||
frame.rotate(hand_rotation(self.second, 60));
|
||||
frame.stroke(&long_hand, thin_stroke);
|
||||
});
|
||||
vec![clock]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,9 +5,6 @@ authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[features]
|
||||
canvas = []
|
||||
|
||||
[dependencies]
|
||||
iced = { path = "../..", features = ["canvas", "async-std", "debug"] }
|
||||
iced_native = { path = "../../native" }
|
||||
|
@ -81,6 +81,12 @@ struct State {
|
||||
}
|
||||
|
||||
impl State {
|
||||
const SUN_RADIUS: f32 = 70.0;
|
||||
const ORBIT_RADIUS: f32 = 150.0;
|
||||
const EARTH_RADIUS: f32 = 12.0;
|
||||
const MOON_RADIUS: f32 = 4.0;
|
||||
const MOON_DISTANCE: f32 = 28.0;
|
||||
|
||||
pub fn new() -> State {
|
||||
let now = Instant::now();
|
||||
let (width, height) = window::Settings::default().size;
|
||||
@ -95,17 +101,6 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn space(&self) -> Space<'_> {
|
||||
Space { stars: &self.stars }
|
||||
}
|
||||
|
||||
pub fn system(&self) -> System {
|
||||
System {
|
||||
start: self.start,
|
||||
now: self.now,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, now: Instant) {
|
||||
self.now = now;
|
||||
self.system_cache.clear();
|
||||
@ -136,106 +131,81 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message> canvas::State<Message> for State {
|
||||
impl<Message> canvas::Program<Message> for State {
|
||||
fn draw(&self, bounds: Size) -> Vec<canvas::Geometry> {
|
||||
vec![
|
||||
self.space_cache.draw(bounds, self.space()),
|
||||
self.system_cache.draw(bounds, self.system()),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Space<'a> {
|
||||
stars: &'a [(Point, f32)],
|
||||
}
|
||||
|
||||
impl canvas::Drawable for Space<'_> {
|
||||
fn draw(&self, frame: &mut canvas::Frame) {
|
||||
use canvas::Path;
|
||||
|
||||
let space = Path::rectangle(Point::new(0.0, 0.0), frame.size());
|
||||
|
||||
let stars = Path::new(|path| {
|
||||
for (p, size) in self.stars {
|
||||
path.rectangle(*p, Size::new(*size, *size));
|
||||
}
|
||||
});
|
||||
|
||||
frame.fill(&space, Color::BLACK);
|
||||
|
||||
frame.translate(frame.center() - Point::ORIGIN);
|
||||
frame.fill(&stars, Color::WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct System {
|
||||
start: Instant,
|
||||
now: Instant,
|
||||
}
|
||||
|
||||
impl System {
|
||||
const SUN_RADIUS: f32 = 70.0;
|
||||
const ORBIT_RADIUS: f32 = 150.0;
|
||||
const EARTH_RADIUS: f32 = 12.0;
|
||||
const MOON_RADIUS: f32 = 4.0;
|
||||
const MOON_DISTANCE: f32 = 28.0;
|
||||
}
|
||||
|
||||
impl canvas::Drawable for System {
|
||||
fn draw(&self, frame: &mut canvas::Frame) {
|
||||
use canvas::{Path, Stroke};
|
||||
use std::f32::consts::PI;
|
||||
|
||||
let center = frame.center();
|
||||
let background = self.space_cache.draw(bounds, |frame| {
|
||||
let space = Path::rectangle(Point::new(0.0, 0.0), frame.size());
|
||||
|
||||
let sun = Path::circle(center, Self::SUN_RADIUS);
|
||||
let orbit = Path::circle(center, Self::ORBIT_RADIUS);
|
||||
|
||||
frame.fill(&sun, Color::from_rgb8(0xF9, 0xD7, 0x1C));
|
||||
frame.stroke(
|
||||
&orbit,
|
||||
Stroke {
|
||||
width: 1.0,
|
||||
color: Color::from_rgba8(0, 153, 255, 0.1),
|
||||
..Stroke::default()
|
||||
},
|
||||
);
|
||||
|
||||
let elapsed = self.now - self.start;
|
||||
let rotation = (2.0 * PI / 60.0) * elapsed.as_secs() as f32
|
||||
+ (2.0 * PI / 60_000.0) * elapsed.subsec_millis() as f32;
|
||||
|
||||
frame.with_save(|frame| {
|
||||
frame.translate(Vector::new(center.x, center.y));
|
||||
frame.rotate(rotation);
|
||||
frame.translate(Vector::new(Self::ORBIT_RADIUS, 0.0));
|
||||
|
||||
let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS);
|
||||
let shadow = Path::rectangle(
|
||||
Point::new(0.0, -Self::EARTH_RADIUS),
|
||||
Size::new(Self::EARTH_RADIUS * 4.0, Self::EARTH_RADIUS * 2.0),
|
||||
);
|
||||
|
||||
frame.fill(&earth, Color::from_rgb8(0x6B, 0x93, 0xD6));
|
||||
|
||||
frame.with_save(|frame| {
|
||||
frame.rotate(rotation * 10.0);
|
||||
frame.translate(Vector::new(0.0, Self::MOON_DISTANCE));
|
||||
|
||||
let moon = Path::circle(Point::ORIGIN, Self::MOON_RADIUS);
|
||||
frame.fill(&moon, Color::WHITE);
|
||||
let stars = Path::new(|path| {
|
||||
for (p, size) in &self.stars {
|
||||
path.rectangle(*p, Size::new(*size, *size));
|
||||
}
|
||||
});
|
||||
|
||||
frame.fill(
|
||||
&shadow,
|
||||
Color {
|
||||
a: 0.7,
|
||||
..Color::BLACK
|
||||
frame.fill(&space, Color::BLACK);
|
||||
|
||||
frame.translate(frame.center() - Point::ORIGIN);
|
||||
frame.fill(&stars, Color::WHITE);
|
||||
});
|
||||
|
||||
let system = self.system_cache.draw(bounds, |frame| {
|
||||
let center = frame.center();
|
||||
|
||||
let sun = Path::circle(center, Self::SUN_RADIUS);
|
||||
let orbit = Path::circle(center, Self::ORBIT_RADIUS);
|
||||
|
||||
frame.fill(&sun, Color::from_rgb8(0xF9, 0xD7, 0x1C));
|
||||
frame.stroke(
|
||||
&orbit,
|
||||
Stroke {
|
||||
width: 1.0,
|
||||
color: Color::from_rgba8(0, 153, 255, 0.1),
|
||||
..Stroke::default()
|
||||
},
|
||||
);
|
||||
|
||||
let elapsed = self.now - self.start;
|
||||
let rotation = (2.0 * PI / 60.0) * elapsed.as_secs() as f32
|
||||
+ (2.0 * PI / 60_000.0) * elapsed.subsec_millis() as f32;
|
||||
|
||||
frame.with_save(|frame| {
|
||||
frame.translate(Vector::new(center.x, center.y));
|
||||
frame.rotate(rotation);
|
||||
frame.translate(Vector::new(Self::ORBIT_RADIUS, 0.0));
|
||||
|
||||
let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS);
|
||||
let shadow = Path::rectangle(
|
||||
Point::new(0.0, -Self::EARTH_RADIUS),
|
||||
Size::new(
|
||||
Self::EARTH_RADIUS * 4.0,
|
||||
Self::EARTH_RADIUS * 2.0,
|
||||
),
|
||||
);
|
||||
|
||||
frame.fill(&earth, Color::from_rgb8(0x6B, 0x93, 0xD6));
|
||||
|
||||
frame.with_save(|frame| {
|
||||
frame.rotate(rotation * 10.0);
|
||||
frame.translate(Vector::new(0.0, Self::MOON_DISTANCE));
|
||||
|
||||
let moon = Path::circle(Point::ORIGIN, Self::MOON_RADIUS);
|
||||
frame.fill(&moon, Color::WHITE);
|
||||
});
|
||||
|
||||
frame.fill(
|
||||
&shadow,
|
||||
Color {
|
||||
a: 0.7,
|
||||
..Color::BLACK
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
vec![background, system]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -207,7 +207,7 @@ use iced_web as runtime;
|
||||
|
||||
pub use runtime::{
|
||||
futures, Align, Background, Color, Command, Font, HorizontalAlignment,
|
||||
Length, Point, Size, Subscription, Vector, VerticalAlignment,
|
||||
Length, Point, Rectangle, Size, Subscription, Vector, VerticalAlignment,
|
||||
};
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
|
@ -18,34 +18,27 @@ use std::marker::PhantomData;
|
||||
pub mod path;
|
||||
|
||||
mod cache;
|
||||
mod drawable;
|
||||
mod event;
|
||||
mod fill;
|
||||
mod frame;
|
||||
mod geometry;
|
||||
mod state;
|
||||
mod program;
|
||||
mod stroke;
|
||||
mod text;
|
||||
|
||||
pub use cache::Cache;
|
||||
pub use drawable::Drawable;
|
||||
pub use event::Event;
|
||||
pub use fill::Fill;
|
||||
pub use frame::Frame;
|
||||
pub use geometry::Geometry;
|
||||
pub use path::Path;
|
||||
pub use state::State;
|
||||
pub use program::Program;
|
||||
pub use stroke::{LineCap, LineJoin, Stroke};
|
||||
pub use text::Text;
|
||||
|
||||
/// A widget capable of drawing 2D graphics.
|
||||
///
|
||||
/// A [`Canvas`] may contain multiple layers. A [`Layer`] is drawn using the
|
||||
/// painter's algorithm. In other words, layers will be drawn on top of each
|
||||
/// other in the same order they are pushed into the [`Canvas`].
|
||||
///
|
||||
/// [`Canvas`]: struct.Canvas.html
|
||||
/// [`Layer`]: layer/trait.Layer.html
|
||||
///
|
||||
/// # Examples
|
||||
/// The repository has a couple of [examples] showcasing how to use a
|
||||
@ -66,10 +59,10 @@ pub use text::Text;
|
||||
/// ```no_run
|
||||
/// # mod iced {
|
||||
/// # pub use iced_wgpu::canvas;
|
||||
/// # pub use iced_native::Color;
|
||||
/// # pub use iced_native::{Color, Size};
|
||||
/// # }
|
||||
/// use iced::canvas::{self, Cache, Canvas, Drawable, Fill, Frame, Path};
|
||||
/// use iced::Color;
|
||||
/// use iced::canvas::{self, Cache, Canvas, Fill, Frame, Geometry, Path, Program};
|
||||
/// use iced::{Color, Size};
|
||||
///
|
||||
/// // First, we define the data we need for drawing
|
||||
/// #[derive(Debug)]
|
||||
@ -77,42 +70,45 @@ pub use text::Text;
|
||||
/// radius: f32,
|
||||
/// }
|
||||
///
|
||||
/// // Then, we implement the `Drawable` trait
|
||||
/// impl Drawable for Circle {
|
||||
/// fn draw(&self, frame: &mut Frame) {
|
||||
/// // Then, we implement the `Program` trait
|
||||
/// impl Program<()> for Circle {
|
||||
/// fn draw(&self, bounds: Size) -> Vec<Geometry>{
|
||||
/// // We prepare a new `Frame`
|
||||
/// let mut frame = Frame::new(bounds);
|
||||
///
|
||||
/// // We create a `Path` representing a simple circle
|
||||
/// let circle = Path::circle(frame.center(), self.radius);
|
||||
///
|
||||
/// // And fill it with some color
|
||||
/// frame.fill(&circle, Fill::Color(Color::BLACK));
|
||||
///
|
||||
/// // Finally, we produce the geometry
|
||||
/// vec![frame.into_geometry()]
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// // We can use a `Cache` to avoid unnecessary re-tessellation
|
||||
/// let cache = Cache::new();
|
||||
///
|
||||
/// // Finally, we simply use our `Cache` to create the `Canvas`!
|
||||
/// let canvas: Canvas<_, ()> = Canvas::new(cache.with(Circle { radius: 50.0 }));
|
||||
/// let canvas = Canvas::new(Circle { radius: 50.0 });
|
||||
/// ```
|
||||
#[derive(Debug)]
|
||||
pub struct Canvas<S: State<Message>, Message> {
|
||||
pub struct Canvas<Message, P: Program<Message>> {
|
||||
width: Length,
|
||||
height: Length,
|
||||
state: S,
|
||||
program: P,
|
||||
phantom: PhantomData<Message>,
|
||||
}
|
||||
|
||||
impl<Message, S: State<Message>> Canvas<S, Message> {
|
||||
impl<Message, P: Program<Message>> Canvas<Message, P> {
|
||||
const DEFAULT_SIZE: u16 = 100;
|
||||
|
||||
/// Creates a new [`Canvas`] with no layers.
|
||||
/// Creates a new [`Canvas`].
|
||||
///
|
||||
/// [`Canvas`]: struct.Canvas.html
|
||||
pub fn new(state: S) -> Self {
|
||||
pub fn new(program: P) -> Self {
|
||||
Canvas {
|
||||
width: Length::Units(Self::DEFAULT_SIZE),
|
||||
height: Length::Units(Self::DEFAULT_SIZE),
|
||||
state,
|
||||
program,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
@ -134,8 +130,8 @@ impl<Message, S: State<Message>> Canvas<S, Message> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message, S: State<Message>> Widget<Message, Renderer>
|
||||
for Canvas<S, Message>
|
||||
impl<Message, P: Program<Message>> Widget<Message, Renderer>
|
||||
for Canvas<Message, P>
|
||||
{
|
||||
fn width(&self) -> Length {
|
||||
self.width
|
||||
@ -184,7 +180,7 @@ impl<Message, S: State<Message>> Widget<Message, Renderer>
|
||||
|
||||
if let Some(canvas_event) = canvas_event {
|
||||
if let Some(message) =
|
||||
self.state.update(canvas_event, bounds.size())
|
||||
self.program.update(canvas_event, bounds.size())
|
||||
{
|
||||
messages.push(message);
|
||||
}
|
||||
@ -207,7 +203,7 @@ impl<Message, S: State<Message>> Widget<Message, Renderer>
|
||||
translation,
|
||||
content: Box::new(Primitive::Group {
|
||||
primitives: self
|
||||
.state
|
||||
.program
|
||||
.draw(size)
|
||||
.into_iter()
|
||||
.map(Geometry::into_primitive)
|
||||
@ -227,12 +223,12 @@ impl<Message, S: State<Message>> Widget<Message, Renderer>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, S: State<Message> + 'a> From<Canvas<S, Message>>
|
||||
impl<'a, Message, P: Program<Message> + 'a> From<Canvas<Message, P>>
|
||||
for Element<'a, Message, Renderer>
|
||||
where
|
||||
Message: 'static,
|
||||
{
|
||||
fn from(canvas: Canvas<S, Message>) -> Element<'a, Message, Renderer> {
|
||||
fn from(canvas: Canvas<Message, P>) -> Element<'a, Message, Renderer> {
|
||||
Element::new(canvas)
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
canvas::{Drawable, Frame, Geometry},
|
||||
canvas::{Frame, Geometry},
|
||||
Primitive,
|
||||
};
|
||||
|
||||
@ -48,10 +48,11 @@ impl Cache {
|
||||
*self.state.borrow_mut() = State::Empty;
|
||||
}
|
||||
|
||||
pub fn draw<T>(&self, new_bounds: Size, input: T) -> Geometry
|
||||
where
|
||||
T: Drawable + std::fmt::Debug,
|
||||
{
|
||||
pub fn draw(
|
||||
&self,
|
||||
new_bounds: Size,
|
||||
draw_fn: impl Fn(&mut Frame),
|
||||
) -> Geometry {
|
||||
use std::ops::Deref;
|
||||
|
||||
if let State::Filled { bounds, primitive } = self.state.borrow().deref()
|
||||
@ -64,7 +65,7 @@ impl Cache {
|
||||
}
|
||||
|
||||
let mut frame = Frame::new(new_bounds);
|
||||
input.draw(&mut frame);
|
||||
draw_fn(&mut frame);
|
||||
|
||||
let primitive = {
|
||||
let geometry = frame.into_geometry();
|
||||
@ -79,30 +80,6 @@ impl Cache {
|
||||
|
||||
Geometry::from_primitive(Primitive::Cached { cache: primitive })
|
||||
}
|
||||
|
||||
pub fn with<'a, T, Message>(
|
||||
&'a self,
|
||||
input: T,
|
||||
) -> impl crate::canvas::State<Message> + 'a
|
||||
where
|
||||
T: Drawable + std::fmt::Debug + 'a,
|
||||
{
|
||||
Bind { cache: self, input }
|
||||
}
|
||||
}
|
||||
|
||||
struct Bind<'a, T> {
|
||||
cache: &'a Cache,
|
||||
input: T,
|
||||
}
|
||||
|
||||
impl<'a, T, Message> crate::canvas::State<Message> for Bind<'a, T>
|
||||
where
|
||||
T: Drawable + std::fmt::Debug + 'a,
|
||||
{
|
||||
fn draw(&self, bounds: Size) -> Vec<Geometry> {
|
||||
vec![self.cache.draw(bounds, &self.input)]
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for State {
|
||||
|
@ -1,36 +0,0 @@
|
||||
use crate::canvas::Frame;
|
||||
|
||||
/// A type that can be drawn on a [`Frame`].
|
||||
///
|
||||
/// [`Frame`]: struct.Frame.html
|
||||
pub trait Drawable {
|
||||
/// Draws the [`Drawable`] on the given [`Frame`].
|
||||
///
|
||||
/// [`Drawable`]: trait.Drawable.html
|
||||
/// [`Frame`]: struct.Frame.html
|
||||
fn draw(&self, frame: &mut Frame);
|
||||
}
|
||||
|
||||
impl<'a> Drawable for dyn Fn(&mut Frame) + 'a {
|
||||
fn draw(&self, frame: &mut Frame) {
|
||||
self(frame)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drawable for &T
|
||||
where
|
||||
T: Drawable,
|
||||
{
|
||||
fn draw(&self, frame: &mut Frame) {
|
||||
T::draw(self, frame)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drawable for &[T]
|
||||
where
|
||||
T: Drawable,
|
||||
{
|
||||
fn draw(&self, frame: &mut Frame) {
|
||||
self.iter().for_each(|drawable| drawable.draw(frame));
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
use crate::canvas::{Event, Geometry, Size};
|
||||
|
||||
pub trait State<Message> {
|
||||
pub trait Program<Message> {
|
||||
fn update(&mut self, _event: Event, _bounds: Size) -> Option<Message> {
|
||||
None
|
||||
}
|
||||
@ -8,9 +8,9 @@ pub trait State<Message> {
|
||||
fn draw(&self, bounds: Size) -> Vec<Geometry>;
|
||||
}
|
||||
|
||||
impl<T, Message> State<Message> for &mut T
|
||||
impl<T, Message> Program<Message> for &mut T
|
||||
where
|
||||
T: State<Message>,
|
||||
T: Program<Message>,
|
||||
{
|
||||
fn update(&mut self, event: Event, bounds: Size) -> Option<Message> {
|
||||
T::update(self, event, bounds)
|
Loading…
x
Reference in New Issue
Block a user