From 76d1ce07f42cfb24c96a60c4448d1989ccb9472b Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Mon, 23 Oct 2023 16:05:37 +0200 Subject: [PATCH] use fill paint --- src/main.rs | 10 +++---- src/nodes/primitives.rs | 62 ++++++++++------------------------------- 2 files changed, 19 insertions(+), 53 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1a9165a..ed773b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use std::ops::Deref; use std::sync::{Arc, RwLock, Weak}; use femtovg::renderer::OpenGl; -use femtovg::{Canvas, Color}; +use femtovg::{Canvas, Color, Paint}; use glutin::surface::Surface; use glutin::{context::PossiblyCurrentContext, display::Display}; use glutin_winit::DisplayBuilder; @@ -65,7 +65,7 @@ fn main() { ..Default::default() } }, - color: Color::rgb(255, 0, 0), + fill: Paint::color(Color::rgb(255, 0, 0)), radius: 10. }))); root.children.push(Arc::new(RwLock::new(Layout { @@ -95,7 +95,7 @@ fn main() { ..Default::default() } }, - color: Color::rgb(0, 255, 0), + fill: Paint::color(Color::rgb(0, 255, 0)), radius: 5. })), Arc::new(RwLock::new(nodes::primitives::Rectangle { @@ -109,7 +109,7 @@ fn main() { ..Default::default() } }, - color: Color::rgb(0, 255, 255), + fill: Paint::color(Color::rgb(0, 255, 255)), radius: 5. })) ] @@ -126,7 +126,7 @@ fn main() { ..Default::default() } }, - color: Color::rgb(0, 0, 255), + fill: Paint::color(Color::rgb(0, 0, 255)), radius: 0. }))); let groot: Arc>> = Arc::new(RwLock::new(root)); diff --git a/src/nodes/primitives.rs b/src/nodes/primitives.rs index b22248c..40836a8 100644 --- a/src/nodes/primitives.rs +++ b/src/nodes/primitives.rs @@ -5,7 +5,7 @@ use crate::nodes::{Node, NodeChildren, RenderContext, Style}; #[derive(Clone, Default, Debug)] pub struct Rectangle { pub style: Style, - pub color: Color, + pub fill: Paint, pub radius: f32 } @@ -13,7 +13,7 @@ impl Rectangle { pub(crate) fn new() -> Rectangle { Rectangle { style: Style::default(), - color: Color::rgb(0, 0, 0), + fill: Paint::color(Color::rgb(0, 0, 0)), radius: 0. } } @@ -27,51 +27,17 @@ impl Node for Rectangle { None } fn render_pre_children(&self, context: &mut RenderContext, layout: Layout) { - if self.radius > 0. { - let mut path = Path::new(); - path.rounded_rect( - 0., - 0., - layout.size.width, - layout.size.height, - self.radius - ); - context.canvas.fill_path( - &path, - &Paint::color(self.color) - ); - } else { - context.fill_rect( - 0, - 0, - layout.size.width as u32, - layout.size.height as u32, - self.color - ); - } + let mut path = Path::new(); + path.rounded_rect( + 0., + 0., + layout.size.width, + layout.size.height, + self.radius + ); + context.canvas.fill_path( + &path, + &self.fill + ); } - // fn render(&self, context: &mut RenderContext, layout: Layout, _render_children: &dyn Fn(&mut RenderContext)) { - // if self.radius > 0. { - // let mut path = Path::new(); - // path.rounded_rect( - // 0., - // 0., - // layout.size.width, - // layout.size.height, - // self.radius - // ); - // context.canvas.fill_path( - // &path, - // &Paint::color(self.color) - // ); - // } else { - // context.canvas.clear_rect( - // 0, - // 0, - // layout.size.width as u32, - // layout.size.height as u32, - // self.color - // ); - // } - // } } \ No newline at end of file