From 40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 10:52:58 -0700 Subject: Adds linear gradient support to 2D meshes in the canvas widget. --- examples/arc/src/main.rs | 14 ++-- examples/clock/src/main.rs | 54 +++++++++------ examples/geometry/src/main.rs | 41 ++++++----- examples/modern_art/Cargo.toml | 10 +++ examples/modern_art/src/main.rs | 141 ++++++++++++++++++++++++++++++++++++++ examples/solar_system/src/main.rs | 13 ++-- 6 files changed, 220 insertions(+), 53 deletions(-) create mode 100644 examples/modern_art/Cargo.toml create mode 100644 examples/modern_art/src/main.rs (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 0c619dc9..6029a69c 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, + self, Cache, Canvas, Cursor, Geometry, Path, Stroke, StrokeStyle, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -52,11 +52,6 @@ impl Application for Arc { Command::none() } - fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(10)) - .map(|_| Message::Tick) - } - fn view(&self) -> Element { Canvas::new(self) .width(Length::Fill) @@ -67,6 +62,11 @@ impl Application for Arc { fn theme(&self) -> Theme { Theme::Dark } + + fn subscription(&self) -> Subscription { + iced::time::every(std::time::Duration::from_millis(10)) + .map(|_| Message::Tick) + } } impl canvas::Program for Arc { @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - color: palette.text, + style: StrokeStyle::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 8818fb54..51f25a3f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,5 +1,7 @@ use iced::executor; -use iced::widget::canvas::{Cache, Cursor, Geometry, LineCap, Path, Stroke}; +use iced::widget::canvas::{ + Cache, Cursor, Geometry, LineCap, Path, Stroke, StrokeStyle, +}; use iced::widget::{canvas, container}; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -24,9 +26,9 @@ enum Message { } impl Application for Clock { + type Executor = executor::Default; type Message = Message; type Theme = Theme; - type Executor = executor::Default; type Flags = (); fn new(_flags: ()) -> (Self, Command) { @@ -59,15 +61,6 @@ impl Application for Clock { Command::none() } - fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(500)).map(|_| { - Message::Tick( - time::OffsetDateTime::now_local() - .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), - ) - }) - } - fn view(&self) -> Element { let canvas = canvas(self as &Self) .width(Length::Fill) @@ -79,6 +72,15 @@ impl Application for Clock { .padding(20) .into() } + + fn subscription(&self) -> Subscription { + iced::time::every(std::time::Duration::from_millis(500)).map(|_| { + Message::Tick( + time::OffsetDateTime::now_local() + .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), + ) + }) + } } impl canvas::Program for Clock { @@ -104,33 +106,41 @@ impl canvas::Program for Clock { let long_hand = Path::line(Point::ORIGIN, Point::new(0.0, -0.8 * radius)); - let thin_stroke = Stroke { - width: radius / 100.0, - color: Color::WHITE, - line_cap: LineCap::Round, - ..Stroke::default() + let width = radius / 100.0; + + let thin_stroke = || -> Stroke { + Stroke { + width, + style: StrokeStyle::Solid(Color::WHITE), + line_cap: LineCap::Round, + ..Stroke::default() + } }; - let wide_stroke = Stroke { - width: thin_stroke.width * 3.0, - ..thin_stroke + let wide_stroke = || -> Stroke { + Stroke { + width: width * 3.0, + style: StrokeStyle::Solid(Color::WHITE), + line_cap: LineCap::Round, + ..Stroke::default() + } }; frame.translate(Vector::new(center.x, center.y)); frame.with_save(|frame| { 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.rotate(hand_rotation(self.now.minute(), 60)); - frame.stroke(&long_hand, wide_stroke); + frame.stroke(&long_hand, wide_stroke()); }); frame.with_save(|frame| { frame.rotate(hand_rotation(self.now.second(), 60)); - frame.stroke(&long_hand, thin_stroke); + frame.stroke(&long_hand, thin_stroke()); }) }); diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index d8b99ab3..a8ce26f8 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -1,6 +1,9 @@ //! This example showcases a simple native custom widget that renders using //! arbitrary low-level geometry. +//! +//TODO need to update this now that vertex data doesn't contain color data mod rainbow { + use iced::Color; // For now, to implement a custom native widget you will need to add // `iced_native` and `iced_wgpu` to your dependencies. // @@ -12,6 +15,7 @@ mod rainbow { // implemented by `iced_wgpu` and other renderers. use iced_graphics::renderer::{self, Renderer}; use iced_graphics::{Backend, Primitive}; + use iced_graphics::shader::Shader; use iced_native::widget::{self, Widget}; use iced_native::{ @@ -63,20 +67,20 @@ mod rainbow { cursor_position: Point, _viewport: &Rectangle, ) { - use iced_graphics::triangle::{Mesh2D, Vertex2D}; + use iced_graphics::triangle::{Mesh2D, Shader, Vertex2D}; use iced_native::Renderer as _; let b = layout.bounds(); // R O Y G B I V - let color_r = [1.0, 0.0, 0.0, 1.0]; - let color_o = [1.0, 0.5, 0.0, 1.0]; - let color_y = [1.0, 1.0, 0.0, 1.0]; - let color_g = [0.0, 1.0, 0.0, 1.0]; - let color_gb = [0.0, 1.0, 0.5, 1.0]; - let color_b = [0.0, 0.2, 1.0, 1.0]; - let color_i = [0.5, 0.0, 1.0, 1.0]; - let color_v = [0.75, 0.0, 0.5, 1.0]; + // let color_r = [1.0, 0.0, 0.0, 1.0]; + // let color_o = [1.0, 0.5, 0.0, 1.0]; + // let color_y = [1.0, 1.0, 0.0, 1.0]; + // let color_g = [0.0, 1.0, 0.0, 1.0]; + // let color_gb = [0.0, 1.0, 0.5, 1.0]; + // let color_b = [0.0, 0.2, 1.0, 1.0]; + // let color_i = [0.5, 0.0, 1.0, 1.0]; + // let color_v = [0.75, 0.0, 0.5, 1.0]; let posn_center = { if b.contains(cursor_position) { @@ -101,39 +105,39 @@ mod rainbow { vertices: vec![ Vertex2D { position: posn_center, - color: [1.0, 1.0, 1.0, 1.0], + // color: [1.0, 1.0, 1.0, 1.0], }, Vertex2D { position: posn_tl, - color: color_r, + // color: color_r, }, Vertex2D { position: posn_t, - color: color_o, + // color: color_o, }, Vertex2D { position: posn_tr, - color: color_y, + // color: color_y, }, Vertex2D { position: posn_r, - color: color_g, + // color: color_g, }, Vertex2D { position: posn_br, - color: color_gb, + // color: color_gb, }, Vertex2D { position: posn_b, - color: color_b, + // color: color_b, }, Vertex2D { position: posn_bl, - color: color_i, + // color: color_i, }, Vertex2D { position: posn_l, - color: color_v, + // color: color_v, }, ], indices: vec![ @@ -147,6 +151,7 @@ mod rainbow { 0, 8, 1, // L ], }, + shader: Shader::Solid(Color::BLACK), }; renderer.with_translation(Vector::new(b.x, b.y), |renderer| { diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml new file mode 100644 index 00000000..4995e9a7 --- /dev/null +++ b/examples/modern_art/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "modern_art" +version = "0.1.0" +authors = ["Bingus "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["canvas", "tokio", "debug"] } +rand = "0.8.5" \ No newline at end of file diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs new file mode 100644 index 00000000..17149cbe --- /dev/null +++ b/examples/modern_art/src/main.rs @@ -0,0 +1,141 @@ +use rand::{Rng, thread_rng}; +use crate::canvas::{Cursor, FillStyle, Geometry, Gradient}; +use iced::widget::canvas::{Cache, Fill, Frame}; +use iced::widget::{canvas, Canvas}; +use iced::Settings; +use iced::{ + executor, Application, Color, Command, Element, Length, Point, Rectangle, + Renderer, Size, Theme, +}; + +fn main() -> iced::Result { + ModernArt::run(Settings { + antialiasing: true, + ..Settings::default() + }) +} + +#[derive(Debug, Clone, Copy)] +enum Message {} + +struct ModernArt { + cache: Cache, +} + +impl Application for ModernArt { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command) { + ( + ModernArt { + cache: Default::default(), + }, + Command::none(), + ) + } + + fn title(&self) -> String { + String::from("Modern Art") + } + + fn update(&mut self, _message: Self::Message) -> Command { + Command::none() + } + + fn view(&self) -> Element<'_, Self::Message, Renderer> { + Canvas::new(self) + .width(Length::Fill) + .height(Length::Fill) + .into() + } +} + +impl canvas::Program for ModernArt { + type State = (); + + fn draw( + &self, + _state: &Self::State, + _theme: &Theme, + bounds: Rectangle, + _cursor: Cursor, + ) -> Vec { + let geometry = self.cache.draw(bounds.size(), |frame| { + let num_squares = thread_rng().gen_range(0..1200); + + let mut i = 0; + while i <= num_squares { + generate_box(frame, bounds.size()); + i += 1; + } + }); + + vec![geometry] + } +} + +fn generate_box(frame: &mut Frame, bounds: Size) -> bool { + let solid = rand::random::(); + + let random_color = || -> Color { + Color::from_rgb( + thread_rng().gen_range(0.0..1.0), + thread_rng().gen_range(0.0..1.0), + thread_rng().gen_range(0.0..1.0), + ) + }; + + let gradient = |top_left: Point, bottom_right: Point| -> Gradient { + let mut builder = Gradient::linear(top_left, bottom_right); + let stops = thread_rng().gen_range(1..64u32); + + let mut i = 0; + while i <= stops { + builder = builder.add_stop( + i as f32 / stops as f32, + random_color() + ); + i += 1; + } + + builder.build().unwrap() + }; + + let top_left = Point::new( + thread_rng().gen_range(0.0..bounds.width), + thread_rng().gen_range(0.0..bounds.height) + ); + + let size = Size::new( + thread_rng().gen_range(50.0..200.0), + thread_rng().gen_range(50.0..200.0), + ); + + if solid { + frame.fill_rectangle( + top_left, + size, + Fill { + style: FillStyle::Solid(random_color()), + .. Default::default() + } + ); + } else { + frame.fill_rectangle( + top_left, + size, + Fill { + style: FillStyle::Gradient(&gradient( + top_left, + Point::new(top_left.x + size.width, top_left.y + size.height) + )), + .. Default::default() + } + ); + }; + + solid +} \ No newline at end of file diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index c59d73a8..fcd20561 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,6 +19,7 @@ use iced::{ }; use std::time::Instant; +use crate::canvas::StrokeStyle; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -37,9 +38,9 @@ enum Message { } impl Application for SolarSystem { + type Executor = executor::Default; type Message = Message; type Theme = Theme; - type Executor = executor::Default; type Flags = (); fn new(_flags: ()) -> (Self, Command) { @@ -65,10 +66,6 @@ impl Application for SolarSystem { Command::none() } - fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(10)).map(Message::Tick) - } - fn view(&self) -> Element { canvas(&self.state) .width(Length::Fill) @@ -86,6 +83,10 @@ impl Application for SolarSystem { text_color: Color::WHITE, }) } + + fn subscription(&self) -> Subscription { + time::every(std::time::Duration::from_millis(10)).map(Message::Tick) + } } #[derive(Debug)] @@ -178,8 +179,8 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { + style: StrokeStyle::Solid(Color::from_rgba8(0, 153, 255, 0.1)), width: 1.0, - color: Color::from_rgba8(0, 153, 255, 0.1), line_dash: canvas::LineDash { offset: 0, segments: &[3.0, 6.0], -- cgit From e25f3d3dea2d2c1ba7d2778948443df2745084be Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 17:13:32 -0700 Subject: Fixed issue where OpenGL would not render both shaders at once under certain circumstances. --- examples/modern_art/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 17149cbe..7b04b581 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -90,7 +90,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let gradient = |top_left: Point, bottom_right: Point| -> Gradient { let mut builder = Gradient::linear(top_left, bottom_right); - let stops = thread_rng().gen_range(1..64u32); + let stops = thread_rng().gen_range(1..10u32); let mut i = 0; while i <= stops { -- cgit From 5d0fffc626928177239336757507b986b081b878 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 30 Sep 2022 10:27:00 -0700 Subject: Fixed some importing issues since you can use a Shader::Gradient outside a Canvas widget, where it was previously only accessible. --- examples/modern_art/Cargo.toml | 1 + examples/modern_art/src/main.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml index 4995e9a7..8242f7e4 100644 --- a/examples/modern_art/Cargo.toml +++ b/examples/modern_art/Cargo.toml @@ -7,4 +7,5 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } +iced_graphics = { path = "../../graphics" } rand = "0.8.5" \ No newline at end of file diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 7b04b581..c7945012 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use rand::{Rng, thread_rng}; -use crate::canvas::{Cursor, FillStyle, Geometry, Gradient}; +use crate::canvas::{Cursor, FillStyle, Geometry}; use iced::widget::canvas::{Cache, Fill, Frame}; use iced::widget::{canvas, Canvas}; use iced::Settings; @@ -7,6 +7,7 @@ use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, Renderer, Size, Theme, }; +use iced_graphics::gradient::Gradient; fn main() -> iced::Result { ModernArt::run(Settings { -- cgit From 6e7b3ced0b1daf368e44e181ecdb4ae529877eb6 Mon Sep 17 00:00:00 2001 From: shan Date: Tue, 4 Oct 2022 18:24:46 -0700 Subject: Reworked wgpu buffers, updated glow side to have proper transform location storage, attempting to fix visibility modifiers, implemented some of the feedback received in initial PR. --- examples/arc/src/main.rs | 4 ++-- examples/clock/src/main.rs | 6 +++--- examples/modern_art/src/main.rs | 7 ++++--- examples/solar_system/src/main.rs | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 6029a69c..bc7e49c6 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, StrokeStyle, + self, Cache, Canvas, Cursor, Geometry, Path, Stroke, Style, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - style: StrokeStyle::Solid(palette.text), + style: Style::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 51f25a3f..06ed44f0 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,6 +1,6 @@ use iced::executor; use iced::widget::canvas::{ - Cache, Cursor, Geometry, LineCap, Path, Stroke, StrokeStyle, + Cache, Cursor, Geometry, LineCap, Path, Stroke, Style, }; use iced::widget::{canvas, container}; use iced::{ @@ -111,7 +111,7 @@ impl canvas::Program for Clock { let thin_stroke = || -> Stroke { Stroke { width, - style: StrokeStyle::Solid(Color::WHITE), + style: Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } @@ -120,7 +120,7 @@ impl canvas::Program for Clock { let wide_stroke = || -> Stroke { Stroke { width: width * 3.0, - style: StrokeStyle::Solid(Color::WHITE), + style: Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index c7945012..238c9a0f 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use rand::{Rng, thread_rng}; -use crate::canvas::{Cursor, FillStyle, Geometry}; +use crate::canvas::{Cursor, Geometry}; use iced::widget::canvas::{Cache, Fill, Frame}; use iced::widget::{canvas, Canvas}; use iced::Settings; @@ -8,6 +8,7 @@ use iced::{ Renderer, Size, Theme, }; use iced_graphics::gradient::Gradient; +use iced_graphics::widget::canvas::Style; fn main() -> iced::Result { ModernArt::run(Settings { @@ -120,7 +121,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: FillStyle::Solid(random_color()), + style: Style::Solid(random_color()), .. Default::default() } ); @@ -129,7 +130,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: FillStyle::Gradient(&gradient( + style: Style::Gradient(&gradient( top_left, Point::new(top_left.x + size.width, top_left.y + size.height) )), diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index fcd20561..8d713ce0 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,7 +19,7 @@ use iced::{ }; use std::time::Instant; -use crate::canvas::StrokeStyle; +use crate::canvas::Style; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -179,7 +179,7 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { - style: StrokeStyle::Solid(Color::from_rgba8(0, 153, 255, 0.1)), + style: Style::Solid(Color::from_rgba8(0, 153, 255, 0.1)), width: 1.0, line_dash: canvas::LineDash { offset: 0, -- cgit From 30432cbade3d9b25c4df62656a7494db3f4ea82a Mon Sep 17 00:00:00 2001 From: shan Date: Wed, 5 Oct 2022 10:49:58 -0700 Subject: Readjusted namespaces, removed Geometry example as it's no longer relevant. --- examples/arc/src/main.rs | 4 +- examples/geometry/Cargo.toml | 11 -- examples/geometry/README.md | 18 ---- examples/geometry/src/main.rs | 222 ---------------------------------------- examples/modern_art/src/main.rs | 9 +- 5 files changed, 6 insertions(+), 258 deletions(-) delete mode 100644 examples/geometry/Cargo.toml delete mode 100644 examples/geometry/README.md delete mode 100644 examples/geometry/src/main.rs (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index bc7e49c6..7b6ea0e1 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -2,7 +2,7 @@ use std::{f32::consts::PI, time::Instant}; use iced::executor; use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Geometry, Path, Stroke, Style, + self, stroke, Cache, Canvas, Cursor, Geometry, Path, Stroke, }; use iced::{ Application, Command, Element, Length, Point, Rectangle, Settings, @@ -114,7 +114,7 @@ impl canvas::Program for Arc { frame.stroke( &path, Stroke { - style: Style::Solid(palette.text), + style: stroke::Style::Solid(palette.text), width: 10.0, ..Stroke::default() }, diff --git a/examples/geometry/Cargo.toml b/examples/geometry/Cargo.toml deleted file mode 100644 index 22ede0e0..00000000 --- a/examples/geometry/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "geometry" -version = "0.1.0" -authors = ["Héctor Ramón Jiménez "] -edition = "2021" -publish = false - -[dependencies] -iced = { path = "../.." } -iced_native = { path = "../../native" } -iced_graphics = { path = "../../graphics" } diff --git a/examples/geometry/README.md b/examples/geometry/README.md deleted file mode 100644 index 4d5c81cb..00000000 --- a/examples/geometry/README.md +++ /dev/null @@ -1,18 +0,0 @@ -## Geometry - -A custom widget showcasing how to draw geometry with the `Mesh2D` primitive in [`iced_wgpu`](../../wgpu). - -The __[`main`]__ file contains all the code of the example. - - - -You can run it with `cargo run`: -``` -cargo run --package geometry -``` - -[`main`]: src/main.rs diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs deleted file mode 100644 index a8ce26f8..00000000 --- a/examples/geometry/src/main.rs +++ /dev/null @@ -1,222 +0,0 @@ -//! This example showcases a simple native custom widget that renders using -//! arbitrary low-level geometry. -//! -//TODO need to update this now that vertex data doesn't contain color data -mod rainbow { - use iced::Color; - // For now, to implement a custom native widget you will need to add - // `iced_native` and `iced_wgpu` to your dependencies. - // - // Then, you simply need to define your widget type and implement the - // `iced_native::Widget` trait with the `iced_wgpu::Renderer`. - // - // Of course, you can choose to make the implementation renderer-agnostic, - // if you wish to, by creating your own `Renderer` trait, which could be - // implemented by `iced_wgpu` and other renderers. - use iced_graphics::renderer::{self, Renderer}; - use iced_graphics::{Backend, Primitive}; - use iced_graphics::shader::Shader; - - use iced_native::widget::{self, Widget}; - use iced_native::{ - layout, Element, Layout, Length, Point, Rectangle, Size, Vector, - }; - - #[derive(Default)] - pub struct Rainbow; - - impl Rainbow { - pub fn new() -> Self { - Self - } - } - - pub fn rainbow() -> Rainbow { - Rainbow - } - - impl Widget> for Rainbow - where - B: Backend, - { - fn width(&self) -> Length { - Length::Fill - } - - fn height(&self) -> Length { - Length::Shrink - } - - fn layout( - &self, - _renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - let size = limits.width(Length::Fill).resolve(Size::ZERO); - - layout::Node::new(Size::new(size.width, size.width)) - } - - fn draw( - &self, - _tree: &widget::Tree, - renderer: &mut Renderer, - _theme: &T, - _style: &renderer::Style, - layout: Layout<'_>, - cursor_position: Point, - _viewport: &Rectangle, - ) { - use iced_graphics::triangle::{Mesh2D, Shader, Vertex2D}; - use iced_native::Renderer as _; - - let b = layout.bounds(); - - // R O Y G B I V - // let color_r = [1.0, 0.0, 0.0, 1.0]; - // let color_o = [1.0, 0.5, 0.0, 1.0]; - // let color_y = [1.0, 1.0, 0.0, 1.0]; - // let color_g = [0.0, 1.0, 0.0, 1.0]; - // let color_gb = [0.0, 1.0, 0.5, 1.0]; - // let color_b = [0.0, 0.2, 1.0, 1.0]; - // let color_i = [0.5, 0.0, 1.0, 1.0]; - // let color_v = [0.75, 0.0, 0.5, 1.0]; - - let posn_center = { - if b.contains(cursor_position) { - [cursor_position.x - b.x, cursor_position.y - b.y] - } else { - [b.width / 2.0, 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]; - - let mesh = Primitive::Mesh2D { - size: b.size(), - buffers: 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 - ], - }, - shader: Shader::Solid(Color::BLACK), - }; - - renderer.with_translation(Vector::new(b.x, b.y), |renderer| { - renderer.draw_primitive(mesh); - }); - } - } - - impl<'a, Message, B, T> From for Element<'a, Message, Renderer> - where - B: Backend, - { - fn from(rainbow: Rainbow) -> Self { - Self::new(rainbow) - } - } -} - -use iced::widget::{column, container, scrollable}; -use iced::{Alignment, Element, Length, Sandbox, Settings}; -use rainbow::rainbow; - -pub fn main() -> iced::Result { - Example::run(Settings::default()) -} - -struct Example; - -impl Sandbox for Example { - type Message = (); - - fn new() -> Self { - Example - } - - fn title(&self) -> String { - String::from("Custom 2D geometry - Iced") - } - - fn update(&mut self, _: ()) {} - - fn view(&self) -> Element<()> { - let content = column![ - rainbow(), - "In this example we draw a custom widget Rainbow, using \ - the Mesh2D primitive. This primitive supplies a list of \ - triangles, expressed as vertices and indices.", - "Move your cursor over it, and see the center vertex \ - follow you!", - "Every Vertex2D defines its own color. You could use the \ - Mesh2D primitive to render virtually any two-dimensional \ - geometry for your widget.", - ] - .padding(20) - .spacing(20) - .max_width(500) - .align_items(Alignment::Start); - - let scrollable = - scrollable(container(content).width(Length::Fill).center_x()); - - container(scrollable) - .width(Length::Fill) - .height(Length::Fill) - .center_y() - .into() - } -} diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 238c9a0f..14e117b3 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,14 +1,13 @@ use rand::{Rng, thread_rng}; use crate::canvas::{Cursor, Geometry}; -use iced::widget::canvas::{Cache, Fill, Frame}; +use iced::widget::canvas::{Cache, Fill, Frame, Gradient}; use iced::widget::{canvas, Canvas}; use iced::Settings; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, Renderer, Size, Theme, }; -use iced_graphics::gradient::Gradient; -use iced_graphics::widget::canvas::Style; +use iced_graphics::widget::canvas::fill; fn main() -> iced::Result { ModernArt::run(Settings { @@ -121,7 +120,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: Style::Solid(random_color()), + style: fill::Style::Solid(random_color()), .. Default::default() } ); @@ -130,7 +129,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, Fill { - style: Style::Gradient(&gradient( + style: fill::Style::Gradient(&gradient( top_left, Point::new(top_left.x + size.width, top_left.y + size.height) )), -- cgit From 1eb8d972ba60592da7bfc27fe7ec80138e64dd7b Mon Sep 17 00:00:00 2001 From: shan Date: Wed, 5 Oct 2022 16:07:43 -0700 Subject: Reduced memory transfer of OpenGL gradient uniform upload. Rearranged gradient uniforms on OpenGL side to be more performant. --- examples/modern_art/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 14e117b3..a7aa2d8a 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -91,7 +91,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let gradient = |top_left: Point, bottom_right: Point| -> Gradient { let mut builder = Gradient::linear(top_left, bottom_right); - let stops = thread_rng().gen_range(1..10u32); + let stops = thread_rng().gen_range(1..15u32); let mut i = 0; while i <= stops { -- cgit From cb7c4676543cd508dfae8d4dcbd9cc8b61b1a94e Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 07:28:05 -0700 Subject: Fixed lint issues & cleaned up some documentation. --- examples/clock/src/main.rs | 6 +++--- examples/modern_art/Cargo.toml | 3 +-- examples/modern_art/src/main.rs | 5 ++--- examples/solar_system/src/main.rs | 7 +++---- 4 files changed, 9 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 06ed44f0..be614201 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,6 +1,6 @@ use iced::executor; use iced::widget::canvas::{ - Cache, Cursor, Geometry, LineCap, Path, Stroke, Style, + Cache, Cursor, Geometry, LineCap, Path, Stroke, stroke, }; use iced::widget::{canvas, container}; use iced::{ @@ -111,7 +111,7 @@ impl canvas::Program for Clock { let thin_stroke = || -> Stroke { Stroke { width, - style: Style::Solid(Color::WHITE), + style: stroke::Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } @@ -120,7 +120,7 @@ impl canvas::Program for Clock { let wide_stroke = || -> Stroke { Stroke { width: width * 3.0, - style: Style::Solid(Color::WHITE), + style: stroke::Style::Solid(Color::WHITE), line_cap: LineCap::Round, ..Stroke::default() } diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml index 8242f7e4..a48361ae 100644 --- a/examples/modern_art/Cargo.toml +++ b/examples/modern_art/Cargo.toml @@ -7,5 +7,4 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } -iced_graphics = { path = "../../graphics" } -rand = "0.8.5" \ No newline at end of file +rand = "0.8.5" diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index a7aa2d8a..6a22b27f 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,13 +1,12 @@ use rand::{Rng, thread_rng}; use crate::canvas::{Cursor, Geometry}; -use iced::widget::canvas::{Cache, Fill, Frame, Gradient}; +use iced::widget::canvas::{Cache, Fill, Frame, Gradient, fill}; use iced::widget::{canvas, Canvas}; use iced::Settings; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, Renderer, Size, Theme, }; -use iced_graphics::widget::canvas::fill; fn main() -> iced::Result { ModernArt::run(Settings { @@ -139,4 +138,4 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { }; solid -} \ No newline at end of file +} diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 8d713ce0..5f90245c 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,7 +11,7 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke}; +use iced::widget::canvas::{Cursor, Path, Stroke, stroke}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -19,7 +19,6 @@ use iced::{ }; use std::time::Instant; -use crate::canvas::Style; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -85,7 +84,7 @@ impl Application for SolarSystem { } fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(10)).map(Message::Tick) + time::every(time::Duration::from_millis(10)).map(Message::Tick) } } @@ -179,7 +178,7 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { - style: Style::Solid(Color::from_rgba8(0, 153, 255, 0.1)), + style: stroke::Style::Solid(Color::from_rgba8(0, 153, 255, 0.1)), width: 1.0, line_dash: canvas::LineDash { offset: 0, -- cgit From 9c7bf417ac9c1ac72bcc55aa3cd5e8eb962243a2 Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 16:57:38 -0700 Subject: Added support for gradients to respect current frame transform. --- examples/modern_art/src/main.rs | 33 ++++++++++++++++---------------- examples/solar_system/src/main.rs | 40 ++++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 36 deletions(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 6a22b27f..0601fc44 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,12 +1,11 @@ -use rand::{Rng, thread_rng}; -use crate::canvas::{Cursor, Geometry}; -use iced::widget::canvas::{Cache, Fill, Frame, Gradient, fill}; -use iced::widget::{canvas, Canvas}; -use iced::Settings; +use iced::widget::canvas::{ + self, fill, Cache, Canvas, Cursor, Fill, Frame, Geometry, Gradient, +}; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, - Renderer, Size, Theme, + Renderer, Size, Theme, Settings }; +use rand::{thread_rng, Rng}; fn main() -> iced::Result { ModernArt::run(Settings { @@ -41,7 +40,7 @@ impl Application for ModernArt { String::from("Modern Art") } - fn update(&mut self, _message: Self::Message) -> Command { + fn update(&mut self, _message: Message) -> Command { Command::none() } @@ -94,10 +93,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let mut i = 0; while i <= stops { - builder = builder.add_stop( - i as f32 / stops as f32, - random_color() - ); + builder = builder.add_stop(i as f32 / stops as f32, random_color()); i += 1; } @@ -106,7 +102,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let top_left = Point::new( thread_rng().gen_range(0.0..bounds.width), - thread_rng().gen_range(0.0..bounds.height) + thread_rng().gen_range(0.0..bounds.height), ); let size = Size::new( @@ -120,8 +116,8 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { size, Fill { style: fill::Style::Solid(random_color()), - .. Default::default() - } + ..Default::default() + }, ); } else { frame.fill_rectangle( @@ -130,10 +126,13 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { Fill { style: fill::Style::Gradient(&gradient( top_left, - Point::new(top_left.x + size.width, top_left.y + size.height) + Point::new( + top_left.x + size.width, + top_left.y + size.height, + ), )), - .. Default::default() - } + ..Default::default() + }, ); }; diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 5f90245c..73ddb01e 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -10,14 +10,14 @@ use iced::application; use iced::executor; use iced::theme::{self, Theme}; use iced::time; -use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke, stroke}; +use iced::widget::canvas::{self, stroke, Cursor, Path, Stroke}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, Size, Subscription, Vector, }; +use crate::canvas::{fill, Fill, Gradient}; use std::time::Instant; pub fn main() -> iced::Result { @@ -178,7 +178,9 @@ impl canvas::Program for State { frame.stroke( &orbit, Stroke { - style: stroke::Style::Solid(Color::from_rgba8(0, 153, 255, 0.1)), + style: stroke::Style::Solid(Color::from_rgba8( + 0, 153, 255, 0.1, + )), width: 1.0, line_dash: canvas::LineDash { offset: 0, @@ -198,15 +200,23 @@ impl canvas::Program for State { 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)); + let earth_fill = Gradient::linear( + Point::new(-Self::EARTH_RADIUS, 0.0), + Point::new(Self::EARTH_RADIUS, 0.0), + ) + .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) + .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) + .build() + .unwrap(); + + frame.fill( + &earth, + Fill { + style: fill::Style::Gradient(&earth_fill), + ..Default::default() + }, + ); frame.with_save(|frame| { frame.rotate(rotation * 10.0); @@ -215,14 +225,6 @@ impl canvas::Program for State { let moon = Path::circle(Point::ORIGIN, Self::MOON_RADIUS); frame.fill(&moon, Color::WHITE); }); - - frame.fill( - &shadow, - Color { - a: 0.7, - ..Color::BLACK - }, - ); }); }); -- cgit From 72feba51bed41db0bc04b43167d5d3b43007fd44 Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 19:13:40 -0700 Subject: Fixed some imports/documentation. --- examples/solar_system/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 73ddb01e..6f38f480 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -10,14 +10,14 @@ use iced::application; use iced::executor; use iced::theme::{self, Theme}; use iced::time; -use iced::widget::canvas::{self, stroke, Cursor, Path, Stroke}; +use iced::widget::canvas; +use iced::widget::canvas::{Cursor, Path, Stroke, Fill, fill, Gradient, stroke}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, Size, Subscription, Vector, }; -use crate::canvas::{fill, Fill, Gradient}; use std::time::Instant; pub fn main() -> iced::Result { -- cgit From 12a87c54eb68b992676060c80e518ffb29445cfc Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 11:41:50 -0700 Subject: Added support for relative positioning of gradient fills. Addressed some PR feedback. --- examples/modern_art/src/main.rs | 41 +++++++++++++++++++++++---------------- examples/solar_system/src/main.rs | 7 +++++-- 2 files changed, 29 insertions(+), 19 deletions(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 0601fc44..362e4ad1 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use iced::widget::canvas::{ - self, fill, Cache, Canvas, Cursor, Fill, Frame, Geometry, Gradient, + self, Cache, Canvas, Cursor, Frame, Geometry, Gradient, Position, Location }; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, @@ -76,6 +76,20 @@ impl canvas::Program for ModernArt { } } +fn random_direction() -> Location { + match thread_rng().gen_range(0..8) { + 0 => Location::TopLeft, + 1 => Location::Top, + 2 => Location::TopRight, + 3 => Location::Right, + 4 => Location::BottomRight, + 5 => Location::Bottom, + 6 => Location::BottomLeft, + 7 => Location::Left, + _ => Location::TopLeft + } +} + fn generate_box(frame: &mut Frame, bounds: Size) -> bool { let solid = rand::random::(); @@ -87,8 +101,13 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { ) }; - let gradient = |top_left: Point, bottom_right: Point| -> Gradient { - let mut builder = Gradient::linear(top_left, bottom_right); + let gradient = |top_left: Point, size: Size| -> Gradient { + let mut builder = Gradient::linear(Position::Relative { + top_left, + size, + start: random_direction(), + end: random_direction() + }); let stops = thread_rng().gen_range(1..15u32); let mut i = 0; @@ -114,25 +133,13 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { frame.fill_rectangle( top_left, size, - Fill { - style: fill::Style::Solid(random_color()), - ..Default::default() - }, + random_color(), ); } else { frame.fill_rectangle( top_left, size, - Fill { - style: fill::Style::Gradient(&gradient( - top_left, - Point::new( - top_left.x + size.width, - top_left.y + size.height, - ), - )), - ..Default::default() - }, + &gradient(top_left, size), ); }; diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 6f38f480..9200391b 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -19,6 +19,7 @@ use iced::{ }; use std::time::Instant; +use crate::canvas::Position; pub fn main() -> iced::Result { SolarSystem::run(Settings { @@ -202,8 +203,10 @@ impl canvas::Program for State { let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS); let earth_fill = Gradient::linear( - Point::new(-Self::EARTH_RADIUS, 0.0), - Point::new(Self::EARTH_RADIUS, 0.0), + Position::Absolute { + start: Point::new(-Self::EARTH_RADIUS, 0.0), + end: Point::new(Self::EARTH_RADIUS, 0.0) + } ) .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) -- cgit From 3e600fe7754645ffdfca595060273b1e96c9a162 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 13:10:37 -0700 Subject: Adjusted reexports for clarity. --- examples/modern_art/src/main.rs | 2 +- examples/solar_system/src/main.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index 362e4ad1..fc203cff 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,5 +1,5 @@ use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Frame, Geometry, Gradient, Position, Location + self, Cache, Canvas, Cursor, Frame, Geometry, Gradient, gradient::Position, gradient::Location }; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 9200391b..10154621 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,7 +11,7 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke, Fill, fill, Gradient, stroke}; +use iced::widget::canvas::{Cursor, Path, Stroke, Fill, fill, Gradient, stroke, gradient::Position}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -19,7 +19,6 @@ use iced::{ }; use std::time::Instant; -use crate::canvas::Position; pub fn main() -> iced::Result { SolarSystem::run(Settings { -- cgit From a4a1262fa2d625be5ad7a37e409e0e9c399b09b6 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 16:28:13 -0700 Subject: Fixed import issue with canvas in the gradient mod for situations where canvas feature is not enabled. --- examples/solar_system/src/main.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 10154621..b638c6d0 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,7 +11,7 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke, Fill, fill, Gradient, stroke, gradient::Position}; +use iced::widget::canvas::{Cursor, Path, Stroke, Gradient, stroke, gradient::Position}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -212,13 +212,7 @@ impl canvas::Program for State { .build() .unwrap(); - frame.fill( - &earth, - Fill { - style: fill::Style::Gradient(&earth_fill), - ..Default::default() - }, - ); + frame.fill(&earth, &earth_fill); frame.with_save(|frame| { frame.rotate(rotation * 10.0); -- cgit From b95745340441835bd25b5cadc2342254631f8c05 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 04:35:16 +0100 Subject: Run `cargo fmt` --- examples/clock/src/main.rs | 2 +- examples/modern_art/src/main.rs | 21 +++++++-------------- examples/solar_system/src/main.rs | 14 +++++++------- 3 files changed, 15 insertions(+), 22 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index be614201..a389c54f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,6 +1,6 @@ use iced::executor; use iced::widget::canvas::{ - Cache, Cursor, Geometry, LineCap, Path, Stroke, stroke, + stroke, Cache, Cursor, Geometry, LineCap, Path, Stroke, }; use iced::widget::{canvas, container}; use iced::{ diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index fc203cff..db9b0491 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -1,9 +1,10 @@ use iced::widget::canvas::{ - self, Cache, Canvas, Cursor, Frame, Geometry, Gradient, gradient::Position, gradient::Location + self, gradient::Location, gradient::Position, Cache, Canvas, Cursor, Frame, + Geometry, Gradient, }; use iced::{ executor, Application, Color, Command, Element, Length, Point, Rectangle, - Renderer, Size, Theme, Settings + Renderer, Settings, Size, Theme, }; use rand::{thread_rng, Rng}; @@ -86,7 +87,7 @@ fn random_direction() -> Location { 5 => Location::Bottom, 6 => Location::BottomLeft, 7 => Location::Left, - _ => Location::TopLeft + _ => Location::TopLeft, } } @@ -106,7 +107,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { top_left, size, start: random_direction(), - end: random_direction() + end: random_direction(), }); let stops = thread_rng().gen_range(1..15u32); @@ -130,17 +131,9 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { ); if solid { - frame.fill_rectangle( - top_left, - size, - random_color(), - ); + frame.fill_rectangle(top_left, size, random_color()); } else { - frame.fill_rectangle( - top_left, - size, - &gradient(top_left, size), - ); + frame.fill_rectangle(top_left, size, &gradient(top_left, size)); }; solid diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index b638c6d0..09ac508d 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,7 +11,9 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{Cursor, Path, Stroke, Gradient, stroke, gradient::Position}; +use iced::widget::canvas::{ + gradient::Position, stroke, Cursor, Gradient, Path, Stroke, +}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -201,12 +203,10 @@ impl canvas::Program for State { let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS); - let earth_fill = Gradient::linear( - Position::Absolute { - start: Point::new(-Self::EARTH_RADIUS, 0.0), - end: Point::new(Self::EARTH_RADIUS, 0.0) - } - ) + let earth_fill = Gradient::linear(Position::Absolute { + start: Point::new(-Self::EARTH_RADIUS, 0.0), + end: Point::new(Self::EARTH_RADIUS, 0.0), + }) .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) .build() -- cgit From edce457365aae5e1aa20863389cdd28357234908 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 04:42:46 +0100 Subject: Fix `multitouch` example --- examples/multitouch/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/multitouch/src/main.rs b/examples/multitouch/src/main.rs index 0345ceb7..f5faae0f 100644 --- a/examples/multitouch/src/main.rs +++ b/examples/multitouch/src/main.rs @@ -2,7 +2,8 @@ //! a circle around each fingertip. This only works on touch-enabled //! computers like Microsoft Surface. use iced::widget::canvas::event; -use iced::widget::canvas::{self, Canvas, Cursor, Geometry, Stroke}; +use iced::widget::canvas::stroke::{self, Stroke}; +use iced::widget::canvas::{self, Canvas, Cursor, Geometry}; use iced::{ executor, touch, window, Application, Color, Command, Element, Length, Point, Rectangle, Settings, Subscription, Theme, @@ -186,7 +187,7 @@ impl canvas::Program for State { frame.stroke( &path, Stroke { - color: Color::BLACK, + style: stroke::Style::Solid(Color::BLACK), width: 3.0, ..Stroke::default() }, -- cgit From e07344428de93055ac4b4f7e02505bbcbc43ab98 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:19:58 +0100 Subject: Refactor imports in `solar_system` example --- examples/solar_system/src/main.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 09ac508d..8d27d154 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -11,9 +11,9 @@ use iced::executor; use iced::theme::{self, Theme}; use iced::time; use iced::widget::canvas; -use iced::widget::canvas::{ - gradient::Position, stroke, Cursor, Gradient, Path, Stroke, -}; +use iced::widget::canvas::gradient::{self, Gradient}; +use iced::widget::canvas::stroke::{self, Stroke}; +use iced::widget::canvas::{Cursor, Path}; use iced::window; use iced::{ Application, Color, Command, Element, Length, Point, Rectangle, Settings, @@ -203,14 +203,15 @@ impl canvas::Program for State { let earth = Path::circle(Point::ORIGIN, Self::EARTH_RADIUS); - let earth_fill = Gradient::linear(Position::Absolute { - start: Point::new(-Self::EARTH_RADIUS, 0.0), - end: Point::new(Self::EARTH_RADIUS, 0.0), - }) - .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) - .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) - .build() - .unwrap(); + let earth_fill = + Gradient::linear(gradient::Position::Absolute { + start: Point::new(-Self::EARTH_RADIUS, 0.0), + end: Point::new(Self::EARTH_RADIUS, 0.0), + }) + .add_stop(0.2, Color::from_rgb(0.15, 0.50, 1.0)) + .add_stop(0.8, Color::from_rgb(0.0, 0.20, 0.47)) + .build() + .expect("Build Earth fill gradient"); frame.fill(&earth, &earth_fill); -- cgit From 84d1b79fefc88534835fdfbe79bc0eb3b43627cf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:50:53 +0100 Subject: Move `mesh::Style` to `triangle` and reuse it in `fill` and `stroke` --- examples/modern_art/src/main.rs | 2 +- examples/solar_system/src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs index db9b0491..0dd21c74 100644 --- a/examples/modern_art/src/main.rs +++ b/examples/modern_art/src/main.rs @@ -133,7 +133,7 @@ fn generate_box(frame: &mut Frame, bounds: Size) -> bool { if solid { frame.fill_rectangle(top_left, size, random_color()); } else { - frame.fill_rectangle(top_left, size, &gradient(top_left, size)); + frame.fill_rectangle(top_left, size, gradient(top_left, size)); }; solid diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 8d27d154..56787a99 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -213,7 +213,7 @@ impl canvas::Program for State { .build() .expect("Build Earth fill gradient"); - frame.fill(&earth, &earth_fill); + frame.fill(&earth, earth_fill); frame.with_save(|frame| { frame.rotate(rotation * 10.0); -- cgit