From 0b5028b1ab47707a469176e9bf20cacdd3a19861 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 14:39:30 +0200 Subject: Draft `Program` interactivity for `Canvas` --- examples/solar_system/src/main.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'examples/solar_system/src/main.rs') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index bcd1dc71..eeb0796a 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -66,10 +66,9 @@ impl Application for SolarSystem { } fn view(&mut self) -> Element { - let canvas = Canvas::new() + let canvas = Canvas::new(&mut self.solar_system, &self.state) .width(Length::Fill) - .height(Length::Fill) - .push(self.solar_system.with(&self.state)); + .height(Length::Fill); Container::new(canvas) .width(Length::Fill) -- cgit From bb424e54c5083402225a0fdda6130de575592dca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 18:48:30 +0200 Subject: Add interactivity to `solar_system` example --- examples/solar_system/src/main.rs | 87 +++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 18 deletions(-) (limited to 'examples/solar_system/src/main.rs') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index eeb0796a..8870fe52 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -10,6 +10,7 @@ use iced::{ canvas, executor, window, Application, Canvas, Color, Command, Container, Element, Length, Point, Settings, Size, Subscription, Vector, }; +use iced_native::input::{self, mouse}; use std::time::Instant; @@ -22,7 +23,7 @@ pub fn main() { struct SolarSystem { state: State, - solar_system: canvas::layer::Cache, + now: Instant, } #[derive(Debug, Clone, Copy)] @@ -39,7 +40,7 @@ impl Application for SolarSystem { ( SolarSystem { state: State::new(), - solar_system: Default::default(), + now: Instant::now(), }, Command::none(), ) @@ -52,8 +53,8 @@ impl Application for SolarSystem { fn update(&mut self, message: Message) -> Command { match message { Message::Tick(instant) => { - self.state.update(instant); - self.solar_system.clear(); + self.now = instant; + self.state.clear(); } } @@ -66,7 +67,7 @@ impl Application for SolarSystem { } fn view(&mut self) -> Element { - let canvas = Canvas::new(&mut self.solar_system, &self.state) + let canvas = Canvas::new(&mut self.state, &self.now) .width(Length::Fill) .height(Length::Fill); @@ -81,25 +82,21 @@ impl Application for SolarSystem { #[derive(Debug)] struct State { + cache: canvas::layer::Cache, + cursor_position: Point, 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) = window::Settings::default().size; State { + cache: Default::default(), + cursor_position: Point::ORIGIN, start: now, - current: now, stars: { use rand::Rng; @@ -120,12 +117,66 @@ impl State { } } - pub fn update(&mut self, now: Instant) { - self.current = now; + pub fn clear(&mut self) { + self.cache.clear(); } } -impl canvas::Drawable for State { +impl canvas::Program for State { + type Input = Instant; + + fn update( + &mut self, + event: canvas::Event, + _bounds: Size, + _input: &Instant, + ) { + match event { + canvas::Event::Mouse(mouse_event) => match mouse_event { + mouse::Event::CursorMoved { x, y } => { + self.cursor_position = Point::new(x, y); + } + mouse::Event::Input { + button: mouse::Button::Left, + state: input::ButtonState::Released, + } => { + self.stars.push((self.cursor_position, 2.0)); + } + _ => {} + }, + } + } + + fn layers<'a>( + &'a self, + now: &'a Instant, + ) -> Vec> { + let system = System { + stars: &self.stars, + start: &self.start, + now, + }; + + vec![Box::new(self.cache.with(system))] + } +} + +#[derive(Debug)] +struct System<'a> { + stars: &'a [(Point, f32)], + start: &'a Instant, + now: &'a 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<'a> canvas::Drawable for System<'a> { fn draw(&self, frame: &mut canvas::Frame) { use canvas::{Path, Stroke}; use std::f32::consts::PI; @@ -135,7 +186,7 @@ impl canvas::Drawable for State { let space = Path::rectangle(Point::new(0.0, 0.0), frame.size()); let stars = Path::new(|path| { - for (p, size) in &self.stars { + for (p, size) in self.stars { path.rectangle(*p, Size::new(*size, *size)); } }); @@ -155,7 +206,7 @@ impl canvas::Drawable for State { }, ); - let elapsed = self.current - self.start; + let elapsed = *self.now - *self.start; let elapsed_seconds = elapsed.as_secs() as f32; let elapsed_millis = elapsed.subsec_millis() as f32; -- cgit From 592cc685067c36cbba87e4db14f4ebc71d65b951 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 19 Apr 2020 21:55:23 +0200 Subject: Remove `Layer` trait and simplify `Canvas` --- examples/solar_system/src/main.rs | 138 +++++++++++++++++++++----------------- 1 file changed, 76 insertions(+), 62 deletions(-) (limited to 'examples/solar_system/src/main.rs') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 8870fe52..618f4206 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -23,7 +23,6 @@ pub fn main() { struct SolarSystem { state: State, - now: Instant, } #[derive(Debug, Clone, Copy)] @@ -40,7 +39,6 @@ impl Application for SolarSystem { ( SolarSystem { state: State::new(), - now: Instant::now(), }, Command::none(), ) @@ -53,8 +51,7 @@ impl Application for SolarSystem { fn update(&mut self, message: Message) -> Command { match message { Message::Tick(instant) => { - self.now = instant; - self.state.clear(); + self.state.update(instant); } } @@ -67,7 +64,7 @@ impl Application for SolarSystem { } fn view(&mut self) -> Element { - let canvas = Canvas::new(&mut self.state, &self.now) + let canvas = Canvas::new(&mut self.state) .width(Length::Fill) .height(Length::Fill); @@ -82,9 +79,11 @@ impl Application for SolarSystem { #[derive(Debug)] struct State { - cache: canvas::layer::Cache, + space_cache: canvas::Cache, + system_cache: canvas::Cache, cursor_position: Point, start: Instant, + now: Instant, stars: Vec<(Point, f32)>, } @@ -94,43 +93,52 @@ impl State { let (width, height) = window::Settings::default().size; State { - cache: Default::default(), + space_cache: Default::default(), + system_cache: Default::default(), cursor_position: Point::ORIGIN, start: 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() - }, + now, + stars: Self::generate_stars(width, height), } } - pub fn clear(&mut self) { - self.cache.clear(); + pub fn space(&self) -> Space<'_> { + Space { stars: &self.stars } } -} -impl canvas::Program for State { - type Input = Instant; + 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(); + } + + fn generate_stars(width: u32, height: u32) -> Vec<(Point, f32)> { + 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() + } +} - fn update( - &mut self, - event: canvas::Event, - _bounds: Size, - _input: &Instant, - ) { +impl canvas::State for State { + fn update(&mut self, event: canvas::Event, _bounds: Size) { match event { canvas::Event::Mouse(mouse_event) => match mouse_event { mouse::Event::CursorMoved { x, y } => { @@ -141,34 +149,50 @@ impl canvas::Program for State { state: input::ButtonState::Released, } => { self.stars.push((self.cursor_position, 2.0)); + self.space_cache.clear(); } _ => {} }, } } - fn layers<'a>( - &'a self, - now: &'a Instant, - ) -> Vec> { - let system = System { - stars: &self.stars, - start: &self.start, - now, - }; - - vec![Box::new(self.cache.with(system))] + fn draw(&self, bounds: Size) -> Vec { + vec![ + self.space_cache.draw(bounds, self.space()), + self.system_cache.draw(bounds, self.system()), + ] } } #[derive(Debug)] -struct System<'a> { +struct Space<'a> { stars: &'a [(Point, f32)], - start: &'a Instant, - now: &'a Instant, } -impl System<'_> { +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.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; @@ -176,26 +200,16 @@ impl System<'_> { const MOON_DISTANCE: f32 = 28.0; } -impl<'a> canvas::Drawable for System<'a> { +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 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)); - } - }); - let sun = Path::circle(center, Self::SUN_RADIUS); let orbit = Path::circle(center, Self::ORBIT_RADIUS); - frame.fill(&space, Color::BLACK); - frame.fill(&stars, Color::WHITE); frame.fill(&sun, Color::from_rgb8(0xF9, 0xD7, 0x1C)); frame.stroke( &orbit, @@ -206,7 +220,7 @@ impl<'a> canvas::Drawable for System<'a> { }, ); - let elapsed = *self.now - *self.start; + let elapsed = self.now - self.start; let elapsed_seconds = elapsed.as_secs() as f32; let elapsed_millis = elapsed.subsec_millis() as f32; -- cgit From dc97d6f33e1e8f8c276fd1cf1d4ed12892ce3ec9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 01:10:59 +0200 Subject: Remove interaction from `solar_system` example --- examples/solar_system/src/main.rs | 57 ++++++++++++--------------------------- 1 file changed, 17 insertions(+), 40 deletions(-) (limited to 'examples/solar_system/src/main.rs') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 618f4206..5392f712 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -7,10 +7,9 @@ //! //! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system use iced::{ - canvas, executor, window, Application, Canvas, Color, Command, Container, - Element, Length, Point, Settings, Size, Subscription, Vector, + canvas, executor, window, Application, Canvas, Color, Command, Element, + Length, Point, Settings, Size, Subscription, Vector, }; -use iced_native::input::{self, mouse}; use std::time::Instant; @@ -64,15 +63,9 @@ impl Application for SolarSystem { } fn view(&mut self) -> Element { - let canvas = Canvas::new(&mut self.state) - .width(Length::Fill) - .height(Length::Fill); - - Container::new(canvas) + Canvas::new(&mut self.state) .width(Length::Fill) .height(Length::Fill) - .center_x() - .center_y() .into() } } @@ -127,8 +120,14 @@ impl State { .map(|_| { ( Point::new( - rng.gen_range(0.0, width as f32), - rng.gen_range(0.0, height as f32), + rng.gen_range( + -(width as f32) / 2.0, + width as f32 / 2.0, + ), + rng.gen_range( + -(height as f32) / 2.0, + height as f32 / 2.0, + ), ), rng.gen_range(0.5, 1.0), ) @@ -138,24 +137,6 @@ impl State { } impl canvas::State for State { - fn update(&mut self, event: canvas::Event, _bounds: Size) { - match event { - canvas::Event::Mouse(mouse_event) => match mouse_event { - mouse::Event::CursorMoved { x, y } => { - self.cursor_position = Point::new(x, y); - } - mouse::Event::Input { - button: mouse::Button::Left, - state: input::ButtonState::Released, - } => { - self.stars.push((self.cursor_position, 2.0)); - self.space_cache.clear(); - } - _ => {} - }, - } - } - fn draw(&self, bounds: Size) -> Vec { vec![ self.space_cache.draw(bounds, self.space()), @@ -182,6 +163,8 @@ impl canvas::Drawable for Space<'_> { }); frame.fill(&space, Color::BLACK); + + frame.translate(frame.center() - Point::ORIGIN); frame.fill(&stars, Color::WHITE); } } @@ -221,15 +204,12 @@ impl canvas::Drawable for System { ); let elapsed = self.now - self.start; - let elapsed_seconds = elapsed.as_secs() as f32; - let elapsed_millis = elapsed.subsec_millis() as f32; + 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( - (2.0 * PI / 60.0) * elapsed_seconds - + (2.0 * PI / 60_000.0) * elapsed_millis, - ); + frame.rotate(rotation); frame.translate(Vector::new(Self::ORBIT_RADIUS, 0.0)); let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS); @@ -241,10 +221,7 @@ impl canvas::Drawable for System { frame.fill(&earth, 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.rotate(rotation * 10.0); frame.translate(Vector::new(0.0, Self::MOON_DISTANCE)); let moon = Path::circle(Point::ORIGIN, Self::MOON_RADIUS); -- cgit From e4eb0553de13053c9828fd5454c281e27e598d65 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 03:46:03 +0200 Subject: Allow `canvas::State` to produce messages --- examples/solar_system/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/solar_system/src/main.rs') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 5392f712..9337c7b5 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -136,7 +136,7 @@ impl State { } } -impl canvas::State for State { +impl canvas::State for State { fn draw(&self, bounds: Size) -> Vec { vec![ self.space_cache.draw(bounds, self.space()), -- cgit From 2539042b71d70afd4d8f262783d441e768811ee9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 06:24:12 +0200 Subject: Remove `Drawable` and rename `State` to `Program` --- examples/solar_system/src/main.rs | 158 +++++++++++++++----------------------- 1 file changed, 64 insertions(+), 94 deletions(-) (limited to 'examples/solar_system/src/main.rs') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 9337c7b5..e2f107bd 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -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 canvas::State for State { +impl canvas::Program for State { fn draw(&self, bounds: Size) -> Vec { - vec![ - self.space_cache.draw(bounds, self.space()), - self.system_cache.draw(bounds, self.system()), - ] - } -} + use canvas::{Path, Stroke}; + use std::f32::consts::PI; -#[derive(Debug)] -struct Space<'a> { - stars: &'a [(Point, f32)], -} + let background = self.space_cache.draw(bounds, |frame| { + let space = Path::rectangle(Point::new(0.0, 0.0), frame.size()); -impl canvas::Drawable for Space<'_> { - fn draw(&self, frame: &mut canvas::Frame) { - use canvas::Path; + let stars = Path::new(|path| { + for (p, size) in &self.stars { + path.rectangle(*p, Size::new(*size, *size)); + } + }); - let space = Path::rectangle(Point::new(0.0, 0.0), frame.size()); + frame.fill(&space, Color::BLACK); - let stars = Path::new(|path| { - for (p, size) in self.stars { - path.rectangle(*p, Size::new(*size, *size)); - } + frame.translate(frame.center() - Point::ORIGIN); + frame.fill(&stars, Color::WHITE); }); - frame.fill(&space, Color::BLACK); + let system = self.system_cache.draw(bounds, |frame| { + let center = frame.center(); - frame.translate(frame.center() - Point::ORIGIN); - frame.fill(&stars, Color::WHITE); - } -} + let sun = Path::circle(center, Self::SUN_RADIUS); + let orbit = Path::circle(center, Self::ORBIT_RADIUS); -#[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 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() - }, - ); + 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; + 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)); + 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, + ), + ); - 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.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)); - 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 moon = Path::circle(Point::ORIGIN, Self::MOON_RADIUS); - frame.fill(&moon, Color::WHITE); + frame.fill( + &shadow, + Color { + a: 0.7, + ..Color::BLACK + }, + ); }); - - frame.fill( - &shadow, - Color { - a: 0.7, - ..Color::BLACK - }, - ); }); + + vec![background, system] } } -- cgit From dc51080328caa12d2b1fc02febc72cab70bb9f50 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 04:25:49 +0200 Subject: Introduce `Cursor` type in `canvas` --- examples/solar_system/src/main.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'examples/solar_system/src/main.rs') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index e2f107bd..a25e43df 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -7,8 +7,9 @@ //! //! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system use iced::{ - canvas, executor, window, Application, Canvas, Color, Command, Element, - Length, Point, Settings, Size, Subscription, Vector, + canvas::{self, Cursor, Path, Stroke}, + executor, window, Application, Canvas, Color, Command, Element, Length, + Point, Rectangle, Settings, Size, Subscription, Vector, }; use std::time::Instant; @@ -132,11 +133,14 @@ impl State { } impl canvas::Program for State { - fn draw(&self, bounds: Size) -> Vec { - use canvas::{Path, Stroke}; + fn draw( + &self, + bounds: Rectangle, + _cursor: Cursor, + ) -> Vec { use std::f32::consts::PI; - let background = self.space_cache.draw(bounds, |frame| { + let background = self.space_cache.draw(bounds.size(), |frame| { let space = Path::rectangle(Point::new(0.0, 0.0), frame.size()); let stars = Path::new(|path| { @@ -151,7 +155,7 @@ impl canvas::Program for State { frame.fill(&stars, Color::WHITE); }); - let system = self.system_cache.draw(bounds, |frame| { + let system = self.system_cache.draw(bounds.size(), |frame| { let center = frame.center(); let sun = Path::circle(center, Self::SUN_RADIUS); -- cgit From e2076612cb98d04a8a48add14d0068c2974d5653 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 05:37:44 +0200 Subject: Implement `time::every` in `iced_futures` --- examples/solar_system/src/main.rs | 40 ++------------------------------------- 1 file changed, 2 insertions(+), 38 deletions(-) (limited to 'examples/solar_system/src/main.rs') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index a25e43df..98bd3b21 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -8,8 +8,8 @@ //! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system use iced::{ canvas::{self, Cursor, Path, Stroke}, - executor, window, Application, Canvas, Color, Command, Element, Length, - Point, Rectangle, Settings, Size, Subscription, Vector, + executor, time, window, Application, Canvas, Color, Command, Element, + Length, Point, Rectangle, Settings, Size, Subscription, Vector, }; use std::time::Instant; @@ -212,39 +212,3 @@ impl canvas::Program for State { vec![background, system] } } - -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