From f436f20eb86b2324126a54d4164b4cedf2134a45 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 03:47:36 +0100 Subject: Draft `Canvas` types and `clock` example --- examples/clock/Cargo.toml | 13 ++++ examples/clock/src/main.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 examples/clock/Cargo.toml create mode 100644 examples/clock/src/main.rs (limited to 'examples') diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml new file mode 100644 index 00000000..941e2bd0 --- /dev/null +++ b/examples/clock/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "clock" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[features] +canvas = [] + +[dependencies] +iced = { path = "../..", features = ["canvas", "async-std"] } +chrono = "0.4" diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs new file mode 100644 index 00000000..958846f4 --- /dev/null +++ b/examples/clock/src/main.rs @@ -0,0 +1,145 @@ +use iced::{ + canvas, executor, Application, Canvas, Color, Command, Element, Length, + Point, Settings, +}; + +use std::sync::Arc; + +pub fn main() { + Clock::run(Settings::default()) +} + +struct Clock { + local_time: Arc, + clock: canvas::layer::Cached, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + Tick(chrono::DateTime), +} + +impl Application for Clock { + type Executor = executor::Default; + type Message = Message; + + fn new() -> (Self, Command) { + let now: LocalTime = chrono::Local::now().into(); + + ( + Clock { + local_time: Arc::new(now), + clock: canvas::layer::Cached::new(), + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Clock - Iced") + } + + fn update(&mut self, message: Message) -> Command { + match message { + Message::Tick(local_time) => {} + } + + Command::none() + } + + fn view(&mut self) -> Element { + Canvas::new() + .width(Length::Fill) + .height(Length::Fill) + .push(self.clock.with(&self.local_time)) + .into() + } +} + +#[derive(Debug)] +struct LocalTime { + hour: u32, + minute: u32, + second: u32, +} + +impl From> for LocalTime { + fn from(date_time: chrono::DateTime) -> LocalTime { + use chrono::Timelike; + + LocalTime { + hour: date_time.hour(), + minute: date_time.minute(), + second: date_time.second(), + } + } +} + +impl canvas::layer::Drawable for LocalTime { + fn draw(&self, frame: &mut canvas::Frame) { + let center = frame.center(); + let radius = frame.width().min(frame.height()) as f32 / 2.0; + + let mut path = canvas::Path::new(); + + path.arc(canvas::path::Arc { + center, + radius, + start_angle: 0.0, + end_angle: 360.0 * 2.0 * std::f32::consts::PI, + }); + + frame.fill( + path, + canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)), + ); + + fn draw_handle( + n: u32, + total: u32, + length: f32, + path: &mut canvas::Path, + ) { + let turns = n as f32 / total as f32; + let t = 2.0 * std::f32::consts::PI * (turns - 0.25); + + let x = length * t.cos(); + let y = length * t.sin(); + + path.line_to(Point::new(x, y)); + } + + let mut path = canvas::Path::new(); + + path.move_to(center); + draw_handle(self.hour, 12, 0.6 * radius, &mut path); + + path.move_to(center); + draw_handle(self.minute, 60, 0.9 * radius, &mut path); + + frame.stroke( + path, + canvas::Stroke { + width: 4.0, + color: Color::WHITE, + line_cap: canvas::LineCap::Round, + ..canvas::Stroke::default() + }, + ); + + let mut path = canvas::Path::new(); + + path.move_to(center); + draw_handle(self.second, 60, 0.9 * radius, &mut path); + + frame.stroke( + path, + canvas::Stroke { + width: 2.0, + color: Color::WHITE, + line_cap: canvas::LineCap::Round, + ..canvas::Stroke::default() + }, + ); + } +} -- cgit From 4777f5d787e262aa9093a3099ae62be2b8c0c82f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 04:00:13 +0100 Subject: Remove unnecessary `Arc` from `clock` example --- examples/clock/src/main.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 958846f4..dc2c06cf 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -3,14 +3,12 @@ use iced::{ Point, Settings, }; -use std::sync::Arc; - pub fn main() { Clock::run(Settings::default()) } struct Clock { - local_time: Arc, + now: LocalTime, clock: canvas::layer::Cached, } @@ -28,7 +26,7 @@ impl Application for Clock { ( Clock { - local_time: Arc::new(now), + now, clock: canvas::layer::Cached::new(), }, Command::none(), @@ -41,7 +39,15 @@ impl Application for Clock { fn update(&mut self, message: Message) -> Command { match message { - Message::Tick(local_time) => {} + Message::Tick(local_time) => { + let now = local_time.into(); + + if now != self.now { + self.now = now; + + self.clock.clear(); + } + } } Command::none() @@ -51,12 +57,12 @@ impl Application for Clock { Canvas::new() .width(Length::Fill) .height(Length::Fill) - .push(self.clock.with(&self.local_time)) + .push(self.clock.with(&self.now)) .into() } } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] struct LocalTime { hour: u32, minute: u32, -- cgit From 74dd79e97f83d3e9e13d87444740edeb353f9be8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 06:41:24 +0100 Subject: Rename current `Path` to `path::Builder` --- examples/clock/src/main.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index dc2c06cf..76752d20 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -86,17 +86,17 @@ impl canvas::layer::Drawable for LocalTime { let center = frame.center(); let radius = frame.width().min(frame.height()) as f32 / 2.0; - let mut path = canvas::Path::new(); - - path.arc(canvas::path::Arc { - center, - radius, - start_angle: 0.0, - end_angle: 360.0 * 2.0 * std::f32::consts::PI, + let path = canvas::Path::new(|path| { + path.arc(canvas::path::Arc { + center, + radius, + start_angle: 0.0, + end_angle: 360.0 * 2.0 * std::f32::consts::PI, + }) }); frame.fill( - path, + &path, canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)), ); @@ -104,7 +104,7 @@ impl canvas::layer::Drawable for LocalTime { n: u32, total: u32, length: f32, - path: &mut canvas::Path, + path: &mut canvas::path::Builder, ) { let turns = n as f32 / total as f32; let t = 2.0 * std::f32::consts::PI * (turns - 0.25); @@ -115,16 +115,16 @@ impl canvas::layer::Drawable for LocalTime { path.line_to(Point::new(x, y)); } - let mut path = canvas::Path::new(); - - path.move_to(center); - draw_handle(self.hour, 12, 0.6 * radius, &mut path); + let path = canvas::Path::new(|path| { + path.move_to(center); + draw_handle(self.hour, 12, 0.6 * radius, path); - path.move_to(center); - draw_handle(self.minute, 60, 0.9 * radius, &mut path); + path.move_to(center); + draw_handle(self.minute, 60, 0.9 * radius, path) + }); frame.stroke( - path, + &path, canvas::Stroke { width: 4.0, color: Color::WHITE, @@ -133,13 +133,13 @@ impl canvas::layer::Drawable for LocalTime { }, ); - let mut path = canvas::Path::new(); - - path.move_to(center); - draw_handle(self.second, 60, 0.9 * radius, &mut path); + let path = canvas::Path::new(|path| { + path.move_to(center); + draw_handle(self.second, 60, 0.9 * radius, path) + }); frame.stroke( - path, + &path, canvas::Stroke { width: 2.0, color: Color::WHITE, -- cgit From 578ea4abb8a2dd0d53d7087322796bf9ad541b56 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 08:49:42 +0100 Subject: Finish `clock` example --- examples/clock/Cargo.toml | 4 ++- examples/clock/src/main.rs | 65 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml index 941e2bd0..308cbfbb 100644 --- a/examples/clock/Cargo.toml +++ b/examples/clock/Cargo.toml @@ -9,5 +9,7 @@ publish = false canvas = [] [dependencies] -iced = { path = "../..", features = ["canvas", "async-std"] } +iced = { path = "../..", features = ["canvas", "async-std", "debug"] } +iced_native = { path = "../../native" } chrono = "0.4" +async-std = { version = "1.0", features = ["unstable"] } diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 76752d20..1b8d1ee6 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - canvas, executor, Application, Canvas, Color, Command, Element, Length, - Point, Settings, + canvas, executor, Application, Canvas, Color, Command, Container, Element, + Length, Point, Settings, Subscription, Vector, }; pub fn main() { @@ -53,11 +53,21 @@ impl Application for Clock { Command::none() } + fn subscription(&self) -> Subscription { + time::every(std::time::Duration::from_millis(500)).map(Message::Tick) + } + fn view(&mut self) -> Element { - Canvas::new() + let canvas = Canvas::new() + .width(Length::Units(400)) + .height(Length::Units(400)) + .push(self.clock.with(&self.now)); + + Container::new(canvas) .width(Length::Fill) .height(Length::Fill) - .push(self.clock.with(&self.now)) + .center_x() + .center_y() .into() } } @@ -85,6 +95,7 @@ impl canvas::layer::Drawable for LocalTime { fn draw(&self, frame: &mut canvas::Frame) { let center = frame.center(); let radius = frame.width().min(frame.height()) as f32 / 2.0; + let offset = Vector::new(center.x, center.y); let path = canvas::Path::new(|path| { path.arc(canvas::path::Arc { @@ -104,6 +115,7 @@ impl canvas::layer::Drawable for LocalTime { n: u32, total: u32, length: f32, + offset: Vector, path: &mut canvas::path::Builder, ) { let turns = n as f32 / total as f32; @@ -112,15 +124,15 @@ impl canvas::layer::Drawable for LocalTime { let x = length * t.cos(); let y = length * t.sin(); - path.line_to(Point::new(x, y)); + path.line_to(Point::new(x, y) + offset); } let path = canvas::Path::new(|path| { path.move_to(center); - draw_handle(self.hour, 12, 0.6 * radius, path); + draw_handle(self.hour, 12, 0.5 * radius, offset, path); path.move_to(center); - draw_handle(self.minute, 60, 0.9 * radius, path) + draw_handle(self.minute, 60, 0.8 * radius, offset, path) }); frame.stroke( @@ -135,7 +147,7 @@ impl canvas::layer::Drawable for LocalTime { let path = canvas::Path::new(|path| { path.move_to(center); - draw_handle(self.second, 60, 0.9 * radius, path) + draw_handle(self.second, 60, 0.8 * radius, offset, path) }); frame.stroke( @@ -149,3 +161,40 @@ impl canvas::layer::Drawable for LocalTime { ); } } + +mod time { + use iced::futures; + + pub fn every( + duration: std::time::Duration, + ) -> iced::Subscription> { + iced::Subscription::from_recipe(Every(duration)) + } + + struct Every(std::time::Duration); + + impl iced_native::subscription::Recipe for Every + where + H: std::hash::Hasher, + { + type Output = chrono::DateTime; + + fn hash(&self, state: &mut H) { + use std::hash::Hash; + + std::any::TypeId::of::().hash(state); + self.0.hash(state); + } + + fn stream( + self: Box, + _input: futures::stream::BoxStream<'static, I>, + ) -> futures::stream::BoxStream<'static, Self::Output> { + use futures::stream::StreamExt; + + async_std::stream::interval(self.0) + .map(|_| chrono::Local::now()) + .boxed() + } + } +} -- cgit From 96b36d0f9e4beba2ac9cbe9c873adf1b89d31f99 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 09:00:44 +0100 Subject: Increase line width in `clock` example --- examples/clock/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 1b8d1ee6..e70ce91a 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -54,7 +54,7 @@ impl Application for Clock { } fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(500)).map(Message::Tick) + time::every(std::time::Duration::from_millis(1000)).map(Message::Tick) } fn view(&mut self) -> Element { @@ -138,7 +138,7 @@ impl canvas::layer::Drawable for LocalTime { frame.stroke( &path, canvas::Stroke { - width: 4.0, + width: 6.0, color: Color::WHITE, line_cap: canvas::LineCap::Round, ..canvas::Stroke::default() @@ -153,7 +153,7 @@ impl canvas::layer::Drawable for LocalTime { frame.stroke( &path, canvas::Stroke { - width: 2.0, + width: 3.0, color: Color::WHITE, line_cap: canvas::LineCap::Round, ..canvas::Stroke::default() -- cgit From 1beeaf9db5e4d9bcf9c1fce89463ad47c708b07d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 09:12:15 +0100 Subject: Fix examples using `Mesh2D` --- examples/bezier_tool/src/main.rs | 52 +++++++---------- examples/geometry/src/main.rs | 123 ++++++++++++++++++++------------------- 2 files changed, 84 insertions(+), 91 deletions(-) (limited to 'examples') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 043d265c..fbb6fa24 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -102,7 +102,7 @@ mod bezier { // Draw rectangle border with lyon. basic_shapes::stroke_rectangle( &lyon::math::Rect::new( - lyon::math::Point::new(bounds.x + 0.5, bounds.y + 0.5), + lyon::math::Point::new(0.5, 0.5), lyon::math::Size::new( bounds.width - 1.0, bounds.height - 1.0, @@ -121,48 +121,35 @@ mod bezier { for curve in self.curves { path_builder.move_to(lyon::math::Point::new( - curve.from.x + bounds.x, - curve.from.y + bounds.y, + curve.from.x, + curve.from.y, )); path_builder.quadratic_bezier_to( - lyon::math::Point::new( - curve.control.x + bounds.x, - curve.control.y + bounds.y, - ), - lyon::math::Point::new( - curve.to.x + bounds.x, - curve.to.y + bounds.y, - ), + lyon::math::Point::new(curve.control.x, curve.control.y), + lyon::math::Point::new(curve.to.x, curve.to.y), ); } match self.state.pending { None => {} Some(Pending::One { from }) => { - path_builder.move_to(lyon::math::Point::new( - from.x + bounds.x, - from.y + bounds.y, - )); + path_builder + .move_to(lyon::math::Point::new(from.x, from.y)); path_builder.line_to(lyon::math::Point::new( - cursor_position.x, - cursor_position.y, + cursor_position.x - bounds.x, + cursor_position.y - bounds.y, )); } Some(Pending::Two { from, to }) => { - path_builder.move_to(lyon::math::Point::new( - from.x + bounds.x, - from.y + bounds.y, - )); + path_builder + .move_to(lyon::math::Point::new(from.x, from.y)); path_builder.quadratic_bezier_to( lyon::math::Point::new( - cursor_position.x, - cursor_position.y, - ), - lyon::math::Point::new( - to.x + bounds.x, - to.y + bounds.y, + cursor_position.x - bounds.x, + cursor_position.y - bounds.y, ), + lyon::math::Point::new(to.x, to.y), ); } } @@ -186,10 +173,13 @@ mod bezier { ) .unwrap(); - let mesh = Primitive::Mesh2D(Arc::new(Mesh2D { - vertices: buffer.vertices, - indices: buffer.indices, - })); + let mesh = Primitive::Mesh2D { + origin: Point::new(bounds.x, bounds.y), + buffers: Arc::new(Mesh2D { + vertices: buffer.vertices, + indices: buffer.indices, + }), + }; ( Primitive::Clip { diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index 9d5fd611..795c6a71 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -69,72 +69,75 @@ mod rainbow { let posn_center = { if b.contains(cursor_position) { - [cursor_position.x, cursor_position.y] + [cursor_position.x - b.x, cursor_position.y - b.y] } else { - [b.x + (b.width / 2.0), b.y + (b.height / 2.0)] + [b.width / 2.0, b.height / 2.0] } }; - let posn_tl = [b.x, b.y]; - let posn_t = [b.x + (b.width / 2.0), b.y]; - let posn_tr = [b.x + b.width, b.y]; - let posn_r = [b.x + b.width, b.y + (b.height / 2.0)]; - let posn_br = [b.x + b.width, b.y + b.height]; - let posn_b = [b.x + (b.width / 2.0), b.y + b.height]; - let posn_bl = [b.x, b.y + b.height]; - let posn_l = [b.x, b.y + (b.height / 2.0)]; + let posn_tl = [0.0, 0.0]; + let posn_t = [b.width / 2.0, 0.0]; + let posn_tr = [b.width, 0.0]; + let posn_r = [b.width, b.height / 2.0]; + let posn_br = [b.width, b.height]; + let posn_b = [(b.width / 2.0), b.height]; + let posn_bl = [0.0, b.height]; + let posn_l = [0.0, b.height / 2.0]; ( - Primitive::Mesh2D(std::sync::Arc::new(Mesh2D { - vertices: vec![ - Vertex2D { - position: posn_center, - color: [1.0, 1.0, 1.0, 1.0], - }, - Vertex2D { - position: posn_tl, - color: color_r, - }, - Vertex2D { - position: posn_t, - color: color_o, - }, - Vertex2D { - position: posn_tr, - color: color_y, - }, - Vertex2D { - position: posn_r, - color: color_g, - }, - Vertex2D { - position: posn_br, - color: color_gb, - }, - Vertex2D { - position: posn_b, - color: color_b, - }, - Vertex2D { - position: posn_bl, - color: color_i, - }, - Vertex2D { - position: posn_l, - color: color_v, - }, - ], - indices: vec![ - 0, 1, 2, // TL - 0, 2, 3, // T - 0, 3, 4, // TR - 0, 4, 5, // R - 0, 5, 6, // BR - 0, 6, 7, // B - 0, 7, 8, // BL - 0, 8, 1, // L - ], - })), + Primitive::Mesh2D { + origin: Point::new(b.x, b.y), + buffers: std::sync::Arc::new(Mesh2D { + vertices: vec![ + Vertex2D { + position: posn_center, + color: [1.0, 1.0, 1.0, 1.0], + }, + Vertex2D { + position: posn_tl, + color: color_r, + }, + Vertex2D { + position: posn_t, + color: color_o, + }, + Vertex2D { + position: posn_tr, + color: color_y, + }, + Vertex2D { + position: posn_r, + color: color_g, + }, + Vertex2D { + position: posn_br, + color: color_gb, + }, + Vertex2D { + position: posn_b, + color: color_b, + }, + Vertex2D { + position: posn_bl, + color: color_i, + }, + Vertex2D { + position: posn_l, + color: color_v, + }, + ], + indices: vec![ + 0, 1, 2, // TL + 0, 2, 3, // T + 0, 3, 4, // TR + 0, 4, 5, // R + 0, 5, 6, // BR + 0, 6, 7, // B + 0, 7, 8, // BL + 0, 8, 1, // L + ], + }), + }, MouseCursor::OutOfBounds, ) } -- cgit From 4a24392c9c078c56ea26b49c455c9ef183fa79a6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 09:21:13 +0100 Subject: Simplify `Clock::new` in `clock` example --- examples/clock/src/main.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index e70ce91a..e30158f7 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -22,11 +22,9 @@ impl Application for Clock { type Message = Message; fn new() -> (Self, Command) { - let now: LocalTime = chrono::Local::now().into(); - ( Clock { - now, + now: chrono::Local::now().into(), clock: canvas::layer::Cached::new(), }, Command::none(), -- cgit From 265c08661ca7943473c2ef305246a75bfc7847ff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 22:08:49 +0100 Subject: Fix circle `end_angle` in `clock` example --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index e30158f7..25849d1a 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -100,7 +100,7 @@ impl canvas::layer::Drawable for LocalTime { center, radius, start_angle: 0.0, - end_angle: 360.0 * 2.0 * std::f32::consts::PI, + end_angle: 2.0 * std::f32::consts::PI, }) }); -- cgit From 9dc9305d93b9c03d5ea309ecce148857f9bb0745 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 22:10:00 +0100 Subject: Remove redundant conversion in `clock` example --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 25849d1a..8259e382 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -92,7 +92,7 @@ impl From> for LocalTime { impl canvas::layer::Drawable for LocalTime { fn draw(&self, frame: &mut canvas::Frame) { let center = frame.center(); - let radius = frame.width().min(frame.height()) as f32 / 2.0; + let radius = frame.width().min(frame.height()) / 2.0; let offset = Vector::new(center.x, center.y); let path = canvas::Path::new(|path| { -- cgit From e7c400a0aaa01320e80aeed726e7ba702af9380b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 22:13:59 +0100 Subject: Improve naming in `clock` example --- examples/clock/src/main.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 8259e382..160adffd 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -42,7 +42,6 @@ impl Application for Clock { if now != self.now { self.now = now; - self.clock.clear(); } } @@ -95,7 +94,7 @@ impl canvas::layer::Drawable for LocalTime { let radius = frame.width().min(frame.height()) / 2.0; let offset = Vector::new(center.x, center.y); - let path = canvas::Path::new(|path| { + let clock = canvas::Path::new(|path| { path.arc(canvas::path::Arc { center, radius, @@ -105,11 +104,11 @@ impl canvas::layer::Drawable for LocalTime { }); frame.fill( - &path, + &clock, canvas::Fill::Color(Color::from_rgb8(0x12, 0x93, 0xD8)), ); - fn draw_handle( + fn draw_hand( n: u32, total: u32, length: f32, @@ -125,16 +124,16 @@ impl canvas::layer::Drawable for LocalTime { path.line_to(Point::new(x, y) + offset); } - let path = canvas::Path::new(|path| { + let hour_and_minute_hands = canvas::Path::new(|path| { path.move_to(center); - draw_handle(self.hour, 12, 0.5 * radius, offset, path); + draw_hand(self.hour, 12, 0.5 * radius, offset, path); path.move_to(center); - draw_handle(self.minute, 60, 0.8 * radius, offset, path) + draw_hand(self.minute, 60, 0.8 * radius, offset, path) }); frame.stroke( - &path, + &hour_and_minute_hands, canvas::Stroke { width: 6.0, color: Color::WHITE, @@ -143,13 +142,13 @@ impl canvas::layer::Drawable for LocalTime { }, ); - let path = canvas::Path::new(|path| { + let second_hand = canvas::Path::new(|path| { path.move_to(center); - draw_handle(self.second, 60, 0.8 * radius, offset, path) + draw_hand(self.second, 60, 0.8 * radius, offset, path) }); frame.stroke( - &path, + &second_hand, canvas::Stroke { width: 3.0, color: Color::WHITE, -- cgit From 979edeb213c2ca0dd394a9a6c68a30dfab371263 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 12 Feb 2020 22:38:36 +0100 Subject: Fix `clock` example eventually skipping a second --- examples/clock/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 160adffd..25a213cd 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -51,7 +51,7 @@ impl Application for Clock { } fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(1000)).map(Message::Tick) + time::every(std::time::Duration::from_millis(500)).map(Message::Tick) } fn view(&mut self) -> Element { -- cgit From 76df374624e5d82dcb2670789a6c4ff228dda9e9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Feb 2020 02:23:41 +0100 Subject: Implement additional methods in `path::Builder` --- examples/clock/src/main.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 25a213cd..0a70709f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -94,14 +94,7 @@ impl canvas::layer::Drawable for LocalTime { let radius = frame.width().min(frame.height()) / 2.0; let offset = Vector::new(center.x, center.y); - let clock = canvas::Path::new(|path| { - path.arc(canvas::path::Arc { - center, - radius, - start_angle: 0.0, - end_angle: 2.0 * std::f32::consts::PI, - }) - }); + let clock = canvas::Path::new(|path| path.circle(center, radius)); frame.fill( &clock, -- cgit From ad3a0a184f877cbca3db4006e802231e201138ba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Feb 2020 05:33:58 +0100 Subject: Add `solar_system` example --- examples/solar_system/Cargo.toml | 15 +++ examples/solar_system/src/main.rs | 244 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 259 insertions(+) create mode 100644 examples/solar_system/Cargo.toml create mode 100644 examples/solar_system/src/main.rs (limited to 'examples') diff --git a/examples/solar_system/Cargo.toml b/examples/solar_system/Cargo.toml new file mode 100644 index 00000000..c88cda50 --- /dev/null +++ b/examples/solar_system/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "solar_system" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[features] +canvas = [] + +[dependencies] +iced = { path = "../..", features = ["canvas", "async-std", "debug"] } +iced_native = { path = "../../native" } +async-std = { version = "1.0", features = ["unstable"] } +rand = "0.7" diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs new file mode 100644 index 00000000..d05acf84 --- /dev/null +++ b/examples/solar_system/src/main.rs @@ -0,0 +1,244 @@ +//! An animated solar system. +//! +//! This example showcases how to use a `Canvas` widget with transforms to draw +//! using different coordinate systems. +//! +//! Inspired by the example found in the MDN docs[1]. +//! +//! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system +use iced::{ + canvas, executor, Application, Canvas, Color, Command, Container, Element, + Length, Point, Settings, Size, Subscription, Vector, +}; + +use std::time::Instant; + +pub fn main() { + SolarSystem::run(Settings::default()) +} + +struct SolarSystem { + state: State, + solar_system: canvas::layer::Cached, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + Tick(Instant), +} + +impl Application for SolarSystem { + type Executor = executor::Default; + type Message = Message; + + fn new() -> (Self, Command) { + ( + SolarSystem { + state: State::new(), + solar_system: canvas::layer::Cached::new(), + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Solar system - Iced") + } + + fn update(&mut self, message: Message) -> Command { + match message { + Message::Tick(instant) => { + self.state.update(instant); + self.solar_system.clear(); + } + } + + Command::none() + } + + fn subscription(&self) -> Subscription { + time::every(std::time::Duration::from_millis(10)) + .map(|instant| Message::Tick(instant)) + } + + fn view(&mut self) -> Element { + let canvas = Canvas::new() + .width(Length::Fill) + .height(Length::Fill) + .push(self.solar_system.with(&self.state)); + + Container::new(canvas) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} + +#[derive(Debug)] +struct State { + start: Instant, + current: Instant, + stars: Vec<(Point, f32)>, +} + +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) = Settings::default().window.size; + + State { + start: now, + current: now, + stars: { + use rand::Rng; + + let mut rng = rand::thread_rng(); + + (0..100) + .map(|_| { + ( + Point::new( + rng.gen_range(0.0, width as f32), + rng.gen_range(0.0, height as f32), + ), + rng.gen_range(0.5, 1.0), + ) + }) + .collect() + }, + } + } + + pub fn update(&mut self, now: Instant) { + self.current = now; + } +} + +impl canvas::layer::Drawable for State { + fn draw(&self, frame: &mut canvas::Frame) { + use canvas::{Fill, Path, Stroke}; + use std::f32::consts::PI; + + let center = frame.center(); + + let space = Path::new(|path| { + 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)); + } + }); + + let sun = Path::new(|path| path.circle(center, Self::SUN_RADIUS)); + let orbit = Path::new(|path| path.circle(center, Self::ORBIT_RADIUS)); + + frame.fill(&space, Fill::Color(Color::BLACK)); + frame.fill(&stars, Fill::Color(Color::WHITE)); + frame.fill(&sun, Fill::Color(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.current - self.start; + let elapsed_seconds = elapsed.as_secs() as f32; + let elapsed_millis = elapsed.subsec_millis() as f32; + + frame.with_save(|frame| { + frame.translate(Vector::new(center.x, center.y)); + frame.rotate( + (2.0 * PI / 60.0) * elapsed_seconds + + (2.0 * PI / 60_000.0) * elapsed_millis, + ); + frame.translate(Vector::new(Self::ORBIT_RADIUS, 0.0)); + + let earth = Path::new(|path| { + path.circle(Point::ORIGIN, Self::EARTH_RADIUS) + }); + + let shadow = Path::new(|path| { + path.rectangle( + Point::new(0.0, -Self::EARTH_RADIUS), + Size::new( + Self::EARTH_RADIUS * 4.0, + Self::EARTH_RADIUS * 2.0, + ), + ) + }); + + frame.fill(&earth, Fill::Color(Color::from_rgb8(0x6B, 0x93, 0xD6))); + + frame.with_save(|frame| { + frame.rotate( + ((2.0 * PI) / 6.0) * elapsed_seconds + + ((2.0 * PI) / 6_000.0) * elapsed_millis, + ); + frame.translate(Vector::new(0.0, Self::MOON_DISTANCE)); + + let moon = Path::new(|path| { + path.circle(Point::ORIGIN, Self::MOON_RADIUS) + }); + + frame.fill(&moon, Fill::Color(Color::WHITE)); + }); + + frame.fill( + &shadow, + Fill::Color(Color { + a: 0.7, + ..Color::BLACK + }), + ); + }); + } +} + +mod time { + use iced::futures; + use std::time::Instant; + + pub fn every(duration: std::time::Duration) -> iced::Subscription { + iced::Subscription::from_recipe(Every(duration)) + } + + struct Every(std::time::Duration); + + impl iced_native::subscription::Recipe for Every + where + H: std::hash::Hasher, + { + type Output = Instant; + + fn hash(&self, state: &mut H) { + use std::hash::Hash; + + std::any::TypeId::of::().hash(state); + self.0.hash(state); + } + + fn stream( + self: Box, + _input: futures::stream::BoxStream<'static, I>, + ) -> futures::stream::BoxStream<'static, Self::Output> { + use futures::stream::StreamExt; + + async_std::stream::interval(self.0) + .map(|_| Instant::now()) + .boxed() + } + } +} -- cgit From f5c80a6d75d5022b175d3562f0965598b6398bd7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 14 Feb 2020 05:42:19 +0100 Subject: Upgrade `Mesh2D` indices from `u16` to `u32` --- examples/bezier_tool/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index fbb6fa24..efdb3924 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -94,7 +94,7 @@ mod bezier { layout: Layout<'_>, cursor_position: Point, ) -> (Primitive, MouseCursor) { - let mut buffer: VertexBuffers = VertexBuffers::new(); + let mut buffer: VertexBuffers = VertexBuffers::new(); let mut path_builder = lyon::path::Path::builder(); let bounds = layout.bounds(); -- cgit From dadae122533ae0916bebd04d6efab3de145263d4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 15 Feb 2020 10:08:27 +0100 Subject: Implement MSAA for `triangle` pipeline in `iced_wgpu` --- examples/bezier_tool/src/main.rs | 5 ++++- examples/clock/src/main.rs | 5 ++++- examples/solar_system/src/main.rs | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index efdb3924..023eb0f7 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -286,7 +286,10 @@ use iced::{ }; pub fn main() { - Example::run(Settings::default()) + Example::run(Settings { + antialiasing: true, + ..Settings::default() + }); } #[derive(Default)] diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 0a70709f..f7fb6f2d 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -4,7 +4,10 @@ use iced::{ }; pub fn main() { - Clock::run(Settings::default()) + Clock::run(Settings { + antialiasing: true, + ..Settings::default() + }) } struct Clock { diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index d05acf84..9e7dba2f 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -14,7 +14,10 @@ use iced::{ use std::time::Instant; pub fn main() { - SolarSystem::run(Settings::default()) + SolarSystem::run(Settings { + antialiasing: true, + ..Settings::default() + }) } struct SolarSystem { -- cgit From 570f769744aabce2d9d9618feadb47e4b92f50ca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 15 Feb 2020 10:50:07 +0100 Subject: Rename `Settings::antialiasing` to `use_antialiasing` --- examples/bezier_tool/src/main.rs | 2 +- examples/clock/src/main.rs | 2 +- examples/solar_system/src/main.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 023eb0f7..01f8f847 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -287,7 +287,7 @@ use iced::{ pub fn main() { Example::run(Settings { - antialiasing: true, + use_antialiasing: true, ..Settings::default() }); } diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index f7fb6f2d..d0995e0c 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -5,7 +5,7 @@ use iced::{ pub fn main() { Clock::run(Settings { - antialiasing: true, + use_antialiasing: true, ..Settings::default() }) } diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 9e7dba2f..3cabedbb 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -15,7 +15,7 @@ use std::time::Instant; pub fn main() { SolarSystem::run(Settings { - antialiasing: true, + use_antialiasing: true, ..Settings::default() }) } -- cgit From 9c067562fa765cfc49d09cd9b12fbba96d5619fa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 18 Feb 2020 08:48:54 +0100 Subject: Write documentation for new `canvas` module --- examples/clock/src/main.rs | 6 +++--- examples/solar_system/src/main.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index d0995e0c..559f0192 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -12,7 +12,7 @@ pub fn main() { struct Clock { now: LocalTime, - clock: canvas::layer::Cached, + clock: canvas::layer::Cache, } #[derive(Debug, Clone, Copy)] @@ -28,7 +28,7 @@ impl Application for Clock { ( Clock { now: chrono::Local::now().into(), - clock: canvas::layer::Cached::new(), + clock: canvas::layer::Cache::new(), }, Command::none(), ) @@ -91,7 +91,7 @@ impl From> for LocalTime { } } -impl canvas::layer::Drawable for LocalTime { +impl canvas::Drawable for LocalTime { fn draw(&self, frame: &mut canvas::Frame) { let center = frame.center(); let radius = frame.width().min(frame.height()) / 2.0; diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 3cabedbb..eee51dc2 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -22,7 +22,7 @@ pub fn main() { struct SolarSystem { state: State, - solar_system: canvas::layer::Cached, + solar_system: canvas::layer::Cache, } #[derive(Debug, Clone, Copy)] @@ -38,7 +38,7 @@ impl Application for SolarSystem { ( SolarSystem { state: State::new(), - solar_system: canvas::layer::Cached::new(), + solar_system: canvas::layer::Cache::new(), }, Command::none(), ) @@ -125,7 +125,7 @@ impl State { } } -impl canvas::layer::Drawable for State { +impl canvas::Drawable for State { fn draw(&self, frame: &mut canvas::Frame) { use canvas::{Fill, Path, Stroke}; use std::f32::consts::PI; -- cgit From 6f7247ca13181bcdfe1a3065215c1b3204723b84 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 18 Feb 2020 09:54:24 +0100 Subject: Rename `Settings::use_antialiasing` to `antialiasing` --- examples/bezier_tool/src/main.rs | 2 +- examples/clock/src/main.rs | 2 +- examples/solar_system/src/main.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index 01f8f847..023eb0f7 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -287,7 +287,7 @@ use iced::{ pub fn main() { Example::run(Settings { - use_antialiasing: true, + antialiasing: true, ..Settings::default() }); } diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 559f0192..d8266f06 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -5,7 +5,7 @@ use iced::{ pub fn main() { Clock::run(Settings { - use_antialiasing: true, + antialiasing: true, ..Settings::default() }) } diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index eee51dc2..4c239806 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -15,7 +15,7 @@ use std::time::Instant; pub fn main() { SolarSystem::run(Settings { - use_antialiasing: true, + antialiasing: true, ..Settings::default() }) } -- cgit