diff options
Diffstat (limited to 'examples/geometry')
-rw-r--r-- | examples/geometry/Cargo.toml | 4 | ||||
-rw-r--r-- | examples/geometry/src/main.rs | 121 |
2 files changed, 54 insertions, 71 deletions
diff --git a/examples/geometry/Cargo.toml b/examples/geometry/Cargo.toml index 22ede0e0..6068d651 100644 --- a/examples/geometry/Cargo.toml +++ b/examples/geometry/Cargo.toml @@ -6,6 +6,4 @@ edition = "2021" publish = false [dependencies] -iced = { path = "../.." } -iced_native = { path = "../../native" } -iced_graphics = { path = "../../graphics" } +iced = { path = "../..", features = ["advanced"] } diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index 9bacce7f..3bc7f46b 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -1,24 +1,12 @@ //! This example showcases a simple native custom widget that renders using //! arbitrary low-level geometry. mod rainbow { - // 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::triangle::ColoredVertex2D; - use iced_graphics::{Backend, Primitive}; - - use iced_native::layout; - use iced_native::widget::{self, Widget}; - use iced_native::{ - Element, Layout, Length, Point, Rectangle, Size, Vector, - }; + use iced::advanced::graphics::color; + use iced::advanced::layout::{self, Layout}; + use iced::advanced::renderer; + use iced::advanced::widget::{self, Widget}; + use iced::mouse; + use iced::{Element, Length, Rectangle, Renderer, Size, Theme, Vector}; #[derive(Debug, Clone, Copy, Default)] pub struct Rainbow; @@ -27,10 +15,7 @@ mod rainbow { Rainbow } - impl<Message, B, T> Widget<Message, Renderer<B, T>> for Rainbow - where - B: Backend, - { + impl<Message> Widget<Message, Renderer> for Rainbow { fn width(&self) -> Length { Length::Fill } @@ -41,7 +26,7 @@ mod rainbow { fn layout( &self, - _renderer: &Renderer<B, T>, + _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { let size = limits.width(Length::Fill).resolve(Size::ZERO); @@ -52,17 +37,17 @@ mod rainbow { fn draw( &self, _tree: &widget::Tree, - renderer: &mut Renderer<B, T>, - _theme: &T, + renderer: &mut Renderer, + _theme: &Theme, _style: &renderer::Style, layout: Layout<'_>, - cursor_position: Point, + cursor: mouse::Cursor, _viewport: &Rectangle, ) { - use iced_graphics::triangle::Mesh2D; - use iced_native::Renderer as _; + use iced::advanced::graphics::mesh::{self, Mesh, SolidVertex2D}; + use iced::advanced::Renderer as _; - let b = layout.bounds(); + let bounds = layout.bounds(); // R O Y G B I V let color_r = [1.0, 0.0, 0.0, 1.0]; @@ -75,61 +60,61 @@ mod rainbow { 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] + if let Some(cursor_position) = cursor.position_in(bounds) { + [cursor_position.x, cursor_position.y] } else { - [b.width / 2.0, b.height / 2.0] + [bounds.width / 2.0, bounds.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::SolidMesh { - size: b.size(), - buffers: Mesh2D { + let posn_t = [bounds.width / 2.0, 0.0]; + let posn_tr = [bounds.width, 0.0]; + let posn_r = [bounds.width, bounds.height / 2.0]; + let posn_br = [bounds.width, bounds.height]; + let posn_b = [(bounds.width / 2.0), bounds.height]; + let posn_bl = [0.0, bounds.height]; + let posn_l = [0.0, bounds.height / 2.0]; + + let mesh = Mesh::Solid { + size: bounds.size(), + buffers: mesh::Indexed { vertices: vec![ - ColoredVertex2D { + SolidVertex2D { position: posn_center, - color: [1.0, 1.0, 1.0, 1.0], + color: color::pack([1.0, 1.0, 1.0, 1.0]), }, - ColoredVertex2D { + SolidVertex2D { position: posn_tl, - color: color_r, + color: color::pack(color_r), }, - ColoredVertex2D { + SolidVertex2D { position: posn_t, - color: color_o, + color: color::pack(color_o), }, - ColoredVertex2D { + SolidVertex2D { position: posn_tr, - color: color_y, + color: color::pack(color_y), }, - ColoredVertex2D { + SolidVertex2D { position: posn_r, - color: color_g, + color: color::pack(color_g), }, - ColoredVertex2D { + SolidVertex2D { position: posn_br, - color: color_gb, + color: color::pack(color_gb), }, - ColoredVertex2D { + SolidVertex2D { position: posn_b, - color: color_b, + color: color::pack(color_b), }, - ColoredVertex2D { + SolidVertex2D { position: posn_bl, - color: color_i, + color: color::pack(color_i), }, - ColoredVertex2D { + SolidVertex2D { position: posn_l, - color: color_v, + color: color::pack(color_v), }, ], indices: vec![ @@ -145,16 +130,16 @@ mod rainbow { }, }; - renderer.with_translation(Vector::new(b.x, b.y), |renderer| { - renderer.draw_primitive(mesh); - }); + renderer.with_translation( + Vector::new(bounds.x, bounds.y), + |renderer| { + renderer.draw_mesh(mesh); + }, + ); } } - impl<'a, Message, B, T> From<Rainbow> for Element<'a, Message, Renderer<B, T>> - where - B: Backend, - { + impl<'a, Message> From<Rainbow> for Element<'a, Message, Renderer> { fn from(rainbow: Rainbow) -> Self { Self::new(rainbow) } |