From 3ae14b4efdf50c36a4f9bf5e8b99a0a8a472c502 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Tue, 20 Feb 2024 21:18:48 +0100 Subject: [PATCH] add image node --- ui/src/nodes/image.rs | 60 +++++++++++++++++++++++++++++++++++++++++++ ui/src/nodes/mod.rs | 7 +++-- 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 ui/src/nodes/image.rs diff --git a/ui/src/nodes/image.rs b/ui/src/nodes/image.rs new file mode 100644 index 0000000..8b39463 --- /dev/null +++ b/ui/src/nodes/image.rs @@ -0,0 +1,60 @@ +use std::fmt::Debug; +use femtovg::{ImageId, Paint, Path}; +use crate::{events::handler::EventHandlerDatabase, SharedNode, WeakNode}; +use super::{Node, NodeChildren, Style}; + +#[derive(Debug)] +/// Simple image node. +/// This is basically just a wrapper around Rectangle node with a fill of type Paint::image. +/// Use that if you need more options. +pub struct Image { + pub style: Style, + /// The image to be rendered. You are responsible for freeing the image data. + pub image: ImageId, + /// Image width - note that you also have to set the style accordingly for it to render correctly, this is more about scaling the image + pub width: f32, + /// Image height - note that you also have to set the style accordingly for it to render correctly, this is more about scaling the image + pub height: f32, + /// Border radius + pub radius: f32, + pub events: EventHandlerDatabase, + pub parent: Option +} + +impl Node for Image { + fn style(&self) -> &Style { + &self.style + } + + fn children(&self) -> Option<&NodeChildren> { + None + } + + fn render_pre_children(&self, context: &mut super::RenderContext, layout: taffy::prelude::Layout) { + let mut path = Path::new(); + path.rounded_rect( + 0., + 0., + layout.size.width, + layout.size.height, + self.radius + ); + context.canvas.fill_path( + &path, + &Paint::image(self.image, 0., 0., self.width, self.height, 0., 1.) + ); + } + + fn event_handlers(&self) -> Option { + Some(self.events.handlers.clone()) + } + fn set_parent(&mut self, parent: Option) { + self.parent = parent; + } + fn parent(&self) -> Option { + match &self.parent { + Some(parent) => parent.upgrade(), + None => None + } + } +} \ No newline at end of file diff --git a/ui/src/nodes/mod.rs b/ui/src/nodes/mod.rs index 9858638..2b86ca3 100644 --- a/ui/src/nodes/mod.rs +++ b/ui/src/nodes/mod.rs @@ -1,5 +1,6 @@ pub mod layout; pub mod primitives; +pub mod image; use std::fmt::Debug; use std::sync::Arc; @@ -7,13 +8,15 @@ use femtovg::{Canvas, Color}; use taffy::layout::Layout; use taffy::Taffy; use crate::events::Location; -use crate::events::handler::{EventHandlerDatabase, InnerEventHandlerDataset}; +use crate::events::handler::InnerEventHandlerDataset; use crate::{NodeLayoutMap, NodePtr, CurrentRenderer, SharedNode, WeakNode}; pub use taffy::style::Style as TaffyStyle; +pub type CanvasRenderer = Canvas; + pub struct RenderContext { - pub canvas: Canvas, + pub canvas: CanvasRenderer, pub node_layout: NodeLayoutMap, pub taffy: Taffy, pub mouse: NodePtr,