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