From 3ac83e161a10160b05f55fe5badedc7a55958d54 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sat, 23 Mar 2024 19:42:53 +0100 Subject: [PATCH] rename stuff... --- hui/src/frame.rs | 24 +++++++++++++++++++++--- hui/src/frame/layer.rs | 30 ++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/hui/src/frame.rs b/hui/src/frame.rs index 44a0a81..27c8fca 100644 --- a/hui/src/frame.rs +++ b/hui/src/frame.rs @@ -1,9 +1,7 @@ -use crate::rect::{Corners, FillColor}; - pub mod point; pub mod layer; -use layer::{FrameLayer, RectLayer}; +use layer::{FrameLayer, RectFrame}; ///XXX: this is not used yet, and also kinda a mess, simplify? ///Maybe limit to a single layer? (aka `Frame` will be just one of the options) @@ -29,12 +27,32 @@ impl> From for Frame { } impl Frame { + /// Get the layer with the given index + #[inline] + pub fn layer(&self, index: usize) -> Option<&FrameLayer> { + self.layers.get(index) + } + + /// Get a mutable reference to the layer with the given index + #[inline] + pub fn layer_mut(&mut self, index: usize) -> Option<&mut FrameLayer> { + self.layers.get_mut(index) + } + + /// Add a layer to the frame #[inline] pub fn add(&mut self, layer: impl Into) -> &mut Self { self.layers.push(layer.into()); self } + /// Add a layer to the back of the frame + #[inline] + pub fn add_back(&mut self, layer: impl Into) -> &mut Self { + self.layers.insert(0, layer.into()); + self + } + #[inline] pub fn finish(&mut self) -> Self { self.clone() diff --git a/hui/src/frame/layer.rs b/hui/src/frame/layer.rs index e2d568e..40df3a7 100644 --- a/hui/src/frame/layer.rs +++ b/hui/src/frame/layer.rs @@ -15,11 +15,11 @@ pub(crate) trait FrameLayerImpl { #[derive(Clone, Copy)] #[enum_dispatch(FrameLayerImpl)] pub enum FrameLayer { - Rect(RectLayer), + Rect(RectFrame), } #[derive(Clone, Copy)] -pub struct RectLayer { +pub struct RectFrame { pub color: FillColor, pub image: Option, pub top_left: FramePoint2d, @@ -27,13 +27,13 @@ pub struct RectLayer { pub corner_radius: Corners, } -impl> From for RectLayer { +impl> From for RectFrame { fn from(color: T) -> Self { Self::from_color(color) } } -impl RectLayer { +impl RectFrame { pub fn from_color(color: impl Into) -> Self { Self { color: color.into(), @@ -64,9 +64,27 @@ impl RectLayer { ..Self::default() } } + + pub fn from_color_image_rounded(color: impl Into, image: ImageHandle, corner_radius: impl Into>) -> Self { + Self { + color: color.into(), + image: Some(image), + corner_radius: corner_radius.into(), + ..Self::default() + } + } + + /// Inset the rectangle by the given amount + pub fn inset(self, inset: f32) -> Self { + Self { + top_left: self.top_left + Vec2::splat(inset).into(), + bottom_right: self.bottom_right - Vec2::splat(inset).into(), + ..self + } + } } -impl Default for RectLayer { +impl Default for RectFrame { fn default() -> Self { Self { color: FillColor::default(), @@ -78,7 +96,7 @@ impl Default for RectLayer { } } -impl FrameLayerImpl for RectLayer { +impl FrameLayerImpl for RectFrame { fn draw(&self, draw: &mut UiDrawCommandList, parent_size: Vec2) { //TODO: handle bottom_right < top_left let top_left = self.top_left.resolve(parent_size);