Migrate geometry example to new Renderer API

This commit is contained in:
Héctor Ramón Jiménez 2021-10-30 20:42:09 +07:00
parent 77bab6beee
commit e5e477aa69
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -11,10 +11,11 @@ mod rainbow {
// if you wish to, by creating your own `Renderer` trait, which could be // if you wish to, by creating your own `Renderer` trait, which could be
// implemented by `iced_wgpu` and other renderers. // implemented by `iced_wgpu` and other renderers.
use iced_graphics::renderer::{self, Renderer}; use iced_graphics::renderer::{self, Renderer};
use iced_graphics::Backend; use iced_graphics::{Backend, Primitive};
use iced_native::{ use iced_native::{
layout, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget, layout, Element, Hasher, Layout, Length, Point, Rectangle, Size,
Vector, Widget,
}; };
pub struct Rainbow; pub struct Rainbow;
@ -51,102 +52,101 @@ mod rainbow {
fn draw( fn draw(
&self, &self,
_renderer: &mut Renderer<B>, renderer: &mut Renderer<B>,
_style: &renderer::Style, _style: &renderer::Style,
_layout: Layout<'_>, layout: Layout<'_>,
_cursor_position: Point, cursor_position: Point,
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
// use iced_graphics::triangle::{Mesh2D, Vertex2D}; use iced_graphics::triangle::{Mesh2D, Vertex2D};
// let b = layout.bounds(); use iced_native::Renderer as _;
// // R O Y G B I V let b = layout.bounds();
// 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 = { // R O Y G B I V
// if b.contains(cursor_position) { let color_r = [1.0, 0.0, 0.0, 1.0];
// [cursor_position.x - b.x, cursor_position.y - b.y] let color_o = [1.0, 0.5, 0.0, 1.0];
// } else { let color_y = [1.0, 1.0, 0.0, 1.0];
// [b.width / 2.0, b.height / 2.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_tl = [0.0, 0.0]; let posn_center = {
// let posn_t = [b.width / 2.0, 0.0]; if b.contains(cursor_position) {
// let posn_tr = [b.width, 0.0]; [cursor_position.x - b.x, cursor_position.y - b.y]
// let posn_r = [b.width, b.height / 2.0]; } else {
// let posn_br = [b.width, b.height]; [b.width / 2.0, b.height / 2.0]
// 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 posn_tl = [0.0, 0.0];
// Primitive::Translate { let posn_t = [b.width / 2.0, 0.0];
// translation: Vector::new(b.x, b.y), let posn_tr = [b.width, 0.0];
// content: Box::new(Primitive::Mesh2D { let posn_r = [b.width, b.height / 2.0];
// size: b.size(), let posn_br = [b.width, b.height];
// buffers: Mesh2D { let posn_b = [(b.width / 2.0), b.height];
// vertices: vec![ let posn_bl = [0.0, b.height];
// Vertex2D { let posn_l = [0.0, b.height / 2.0];
// position: posn_center,
// color: [1.0, 1.0, 1.0, 1.0], let mesh = Primitive::Mesh2D {
// }, size: b.size(),
// Vertex2D { buffers: Mesh2D {
// position: posn_tl, vertices: vec![
// color: color_r, Vertex2D {
// }, position: posn_center,
// Vertex2D { color: [1.0, 1.0, 1.0, 1.0],
// position: posn_t, },
// color: color_o, Vertex2D {
// }, position: posn_tl,
// Vertex2D { color: color_r,
// position: posn_tr, },
// color: color_y, Vertex2D {
// }, position: posn_t,
// Vertex2D { color: color_o,
// position: posn_r, },
// color: color_g, Vertex2D {
// }, position: posn_tr,
// Vertex2D { color: color_y,
// position: posn_br, },
// color: color_gb, Vertex2D {
// }, position: posn_r,
// Vertex2D { color: color_g,
// position: posn_b, },
// color: color_b, Vertex2D {
// }, position: posn_br,
// Vertex2D { color: color_gb,
// position: posn_bl, },
// color: color_i, Vertex2D {
// }, position: posn_b,
// Vertex2D { color: color_b,
// position: posn_l, },
// color: color_v, Vertex2D {
// }, position: posn_bl,
// ], color: color_i,
// indices: vec![ },
// 0, 1, 2, // TL Vertex2D {
// 0, 2, 3, // T position: posn_l,
// 0, 3, 4, // TR color: color_v,
// 0, 4, 5, // R },
// 0, 5, 6, // BR ],
// 0, 6, 7, // B indices: vec![
// 0, 7, 8, // BL 0, 1, 2, // TL
// 0, 8, 1, // L 0, 2, 3, // T
// ], 0, 3, 4, // TR
// }, 0, 4, 5, // R
// }), 0, 5, 6, // BR
// }, 0, 6, 7, // B
// mouse::Interaction::default(), 0, 7, 8, // BL
// ) 0, 8, 1, // L
// TODO ],
},
};
renderer.with_translation(Vector::new(b.x, b.y), |renderer| {
renderer.draw_primitive(mesh);
});
} }
} }