mirror of
https://github.com/griffi-gh/hUI.git
synced 2024-12-22 12:28:19 -06:00
frame api redesign :3
This commit is contained in:
parent
fb2f3c739e
commit
f8b80040f3
|
@ -26,7 +26,7 @@ document-features = "0.2"
|
|||
derive_setters = "0.1"
|
||||
derive_more = "0.99"
|
||||
tinyset = "0.4"
|
||||
enum_dispatch = "0.3"
|
||||
#enum_dispatch = "0.3"
|
||||
|
||||
[features]
|
||||
default = ["builtin_elements", "builtin_font", "pixel_perfect_text"]
|
||||
|
|
|
@ -1,67 +1,12 @@
|
|||
pub mod point;
|
||||
pub mod layer;
|
||||
use glam::Vec2;
|
||||
use layer::{FrameLayer, FrameLayerImpl};
|
||||
use crate::draw::UiDrawCommandList;
|
||||
|
||||
///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)
|
||||
///Because currently, this is just a duplicate of the dormal draw command system, but with a different name...
|
||||
///Then, there's no need for the positioning stuff too, which is a bit overkill and is kinda code duplication too!
|
||||
///aka Frame::Rectangle, Frame::NinePatch, ...
|
||||
pub mod point;
|
||||
mod rect;
|
||||
pub mod stack;
|
||||
|
||||
/// A frame, which can contain multiple layers
|
||||
///
|
||||
/// Use these to construct complex backgrounds
|
||||
#[derive(Default, Clone)]
|
||||
pub struct Frame {
|
||||
/// Layers of the frame
|
||||
layers: Vec<FrameLayer>
|
||||
}
|
||||
|
||||
impl<T: Into<FrameLayer>> From<T> for Frame {
|
||||
fn from(layer: T) -> Self {
|
||||
let mut frame = Self::default();
|
||||
frame.add(layer.into());
|
||||
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<FrameLayer>) -> &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<FrameLayer>) -> &mut Self {
|
||||
self.layers.insert(0, layer.into());
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn finish(&mut self) -> Self {
|
||||
self.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) {
|
||||
for layer in &self.layers {
|
||||
layer.draw(draw, position, parent_size);
|
||||
}
|
||||
}
|
||||
pub use rect::FrameRect;
|
||||
|
||||
pub trait Frame {
|
||||
fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2);
|
||||
}
|
||||
|
|
|
@ -1,25 +1,13 @@
|
|||
use glam::Vec2;
|
||||
use enum_dispatch::enum_dispatch;
|
||||
use crate::{
|
||||
color,
|
||||
draw::{ImageHandle, RoundedCorners, UiDrawCommand, UiDrawCommandList},
|
||||
rect::{Corners, FillColor},
|
||||
};
|
||||
use super::point::FramePoint2d;
|
||||
|
||||
#[enum_dispatch]
|
||||
pub(crate) trait FrameLayerImpl {
|
||||
fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2);
|
||||
}
|
||||
use super::{Frame, point::FramePoint2d};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[enum_dispatch(FrameLayerImpl)]
|
||||
pub enum FrameLayer {
|
||||
Rect(RectFrame),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct RectFrame {
|
||||
pub struct FrameRect {
|
||||
/// Background color of the frame\
|
||||
///
|
||||
/// If the container has a background texture, it will be multiplied by this color
|
||||
|
@ -44,13 +32,25 @@ pub struct RectFrame {
|
|||
pub corner_radius: Corners<f32>,
|
||||
}
|
||||
|
||||
impl<T: Into<FillColor>> From<T> for RectFrame {
|
||||
fn from(color: T) -> Self {
|
||||
// impl<T: Into<FillColor>> From<T> for FrameRect {
|
||||
// fn from(color: T) -> Self {
|
||||
// Self::from_color(color)
|
||||
// }
|
||||
// }
|
||||
|
||||
impl From<FillColor> for FrameRect {
|
||||
fn from(color: FillColor) -> Self {
|
||||
Self::from_color(color)
|
||||
}
|
||||
}
|
||||
|
||||
impl RectFrame {
|
||||
impl From<ImageHandle> for FrameRect {
|
||||
fn from(image: ImageHandle) -> Self {
|
||||
Self::from_image(image)
|
||||
}
|
||||
}
|
||||
|
||||
impl FrameRect {
|
||||
pub fn from_color(color: impl Into<FillColor>) -> Self {
|
||||
Self {
|
||||
color: color.into(),
|
||||
|
@ -58,14 +58,6 @@ impl RectFrame {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_color_rounded(color: impl Into<FillColor>, corner_radius: impl Into<Corners<f32>>) -> Self {
|
||||
Self {
|
||||
color: color.into(),
|
||||
corner_radius: corner_radius.into(),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_image(image: ImageHandle) -> Self {
|
||||
Self {
|
||||
color: color::WHITE.into(),
|
||||
|
@ -82,17 +74,17 @@ impl RectFrame {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_color_image_rounded(color: impl Into<FillColor>, image: ImageHandle, corner_radius: impl Into<Corners<f32>>) -> Self {
|
||||
pub fn with_corner_radius(radius: impl Into<Corners<f32>>) -> Self {
|
||||
Self {
|
||||
color: color.into(),
|
||||
image: Some(image),
|
||||
corner_radius: corner_radius.into(),
|
||||
corner_radius: radius.into(),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Inset the rectangle by the given amount
|
||||
pub fn inset(self, inset: f32) -> Self {
|
||||
//TODO: deprecate and replace
|
||||
|
||||
/// Inset the rectangle by the given amount in pixels
|
||||
pub fn with_inset(self, inset: f32) -> Self {
|
||||
Self {
|
||||
top_left: self.top_left + Vec2::splat(inset).into(),
|
||||
bottom_right: self.bottom_right - Vec2::splat(inset).into(),
|
||||
|
@ -101,7 +93,7 @@ impl RectFrame {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for RectFrame {
|
||||
impl Default for FrameRect {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
color: FillColor::default(),
|
||||
|
@ -113,7 +105,7 @@ impl Default for RectFrame {
|
|||
}
|
||||
}
|
||||
|
||||
impl FrameLayerImpl for RectFrame {
|
||||
impl Frame for FrameRect {
|
||||
fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) {
|
||||
//TODO: handle bottom_right < top_left
|
||||
let top_left = self.top_left.resolve(parent_size);
|
22
hui/src/frame/stack.rs
Normal file
22
hui/src/frame/stack.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
use glam::Vec2;
|
||||
use crate::draw::UiDrawCommandList;
|
||||
use super::Frame;
|
||||
|
||||
pub struct FrameStack(pub Box<dyn Frame>, pub Box<dyn Frame>);
|
||||
|
||||
impl Frame for FrameStack {
|
||||
fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) {
|
||||
self.0.draw(draw, position, parent_size);
|
||||
self.1.draw(draw, position, parent_size);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FrameStackExt: Frame {
|
||||
fn stack(self, other: impl Frame + 'static) -> FrameStack;
|
||||
}
|
||||
|
||||
impl<T: Frame + 'static> FrameStackExt for T {
|
||||
fn stack(self, other: impl Frame + 'static) -> FrameStack {
|
||||
FrameStack(Box::new(self), Box::new(other))
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue