hUI/hui/src/frame.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

2024-03-22 19:57:07 -05:00
pub mod point;
pub mod layer;
2024-03-23 13:53:32 -05:00
use glam::Vec2;
use layer::{FrameLayer, FrameLayerImpl};
use crate::draw::UiDrawCommandList;
2024-03-22 12:33:34 -05:00
2024-03-22 19:57:07 -05:00
///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)
2024-03-22 20:05:44 -05:00
///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!
2024-03-22 19:57:07 -05:00
///aka Frame::Rectangle, Frame::NinePatch, ...
2024-03-22 13:35:00 -05:00
2024-03-22 19:57:07 -05:00
/// 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>
2024-03-22 12:33:34 -05:00
}
2024-03-23 11:54:47 -05:00
impl<T: Into<FrameLayer>> From<T> for Frame {
2024-03-23 09:09:05 -05:00
fn from(layer: T) -> Self {
2024-03-22 19:57:07 -05:00
let mut frame = Self::default();
2024-03-23 09:09:05 -05:00
frame.add(layer.into());
2024-03-22 19:57:07 -05:00
frame
2024-03-22 12:33:34 -05:00
}
}
2024-03-22 19:57:07 -05:00
impl Frame {
2024-03-23 13:42:53 -05:00
/// 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
2024-03-22 20:05:44 -05:00
#[inline]
2024-03-22 19:57:07 -05:00
pub fn add(&mut self, layer: impl Into<FrameLayer>) -> &mut Self {
self.layers.push(layer.into());
self
2024-03-22 12:33:34 -05:00
}
2024-03-22 20:05:44 -05:00
2024-03-23 13:42:53 -05:00
/// 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
}
2024-03-22 20:05:44 -05:00
#[inline]
pub fn finish(&mut self) -> Self {
self.clone()
}
2024-03-23 13:53:32 -05:00
pub(crate) fn draw(&self, draw: &mut UiDrawCommandList, position: Vec2, parent_size: Vec2) {
for layer in &self.layers {
layer.draw(draw, position, parent_size);
}
}
2024-03-22 12:33:34 -05:00
}