rename stuff...

This commit is contained in:
griffi-gh 2024-03-23 19:42:53 +01:00
parent d1e1325068
commit 3ac83e161a
2 changed files with 45 additions and 9 deletions

View file

@ -1,9 +1,7 @@
use crate::rect::{Corners, FillColor};
pub mod point; pub mod point;
pub mod layer; pub mod layer;
use layer::{FrameLayer, RectLayer}; use layer::{FrameLayer, RectFrame};
///XXX: this is not used yet, and also kinda a mess, simplify? ///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) ///Maybe limit to a single layer? (aka `Frame` will be just one of the options)
@ -29,12 +27,32 @@ impl<T: Into<FrameLayer>> From<T> for Frame {
} }
impl 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] #[inline]
pub fn add(&mut self, layer: impl Into<FrameLayer>) -> &mut Self { pub fn add(&mut self, layer: impl Into<FrameLayer>) -> &mut Self {
self.layers.push(layer.into()); self.layers.push(layer.into());
self self
} }
/// Add a layer to the back of the frame
#[inline]
pub fn add_back(&mut self, layer: impl Into<FrameLayer>) -> &mut Self {
self.layers.insert(0, layer.into());
self
}
#[inline] #[inline]
pub fn finish(&mut self) -> Self { pub fn finish(&mut self) -> Self {
self.clone() self.clone()

View file

@ -15,11 +15,11 @@ pub(crate) trait FrameLayerImpl {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
#[enum_dispatch(FrameLayerImpl)] #[enum_dispatch(FrameLayerImpl)]
pub enum FrameLayer { pub enum FrameLayer {
Rect(RectLayer), Rect(RectFrame),
} }
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct RectLayer { pub struct RectFrame {
pub color: FillColor, pub color: FillColor,
pub image: Option<ImageHandle>, pub image: Option<ImageHandle>,
pub top_left: FramePoint2d, pub top_left: FramePoint2d,
@ -27,13 +27,13 @@ pub struct RectLayer {
pub corner_radius: Corners<f32>, pub corner_radius: Corners<f32>,
} }
impl<T: Into<FillColor>> From<T> for RectLayer { impl<T: Into<FillColor>> From<T> for RectFrame {
fn from(color: T) -> Self { fn from(color: T) -> Self {
Self::from_color(color) Self::from_color(color)
} }
} }
impl RectLayer { impl RectFrame {
pub fn from_color(color: impl Into<FillColor>) -> Self { pub fn from_color(color: impl Into<FillColor>) -> Self {
Self { Self {
color: color.into(), color: color.into(),
@ -64,9 +64,27 @@ impl RectLayer {
..Self::default() ..Self::default()
} }
} }
pub fn from_color_image_rounded(color: impl Into<FillColor>, image: ImageHandle, corner_radius: impl Into<Corners<f32>>) -> 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 { fn default() -> Self {
Self { Self {
color: FillColor::default(), 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) { fn draw(&self, draw: &mut UiDrawCommandList, parent_size: Vec2) {
//TODO: handle bottom_right < top_left //TODO: handle bottom_right < top_left
let top_left = self.top_left.resolve(parent_size); let top_left = self.top_left.resolve(parent_size);