2024-02-17 14:43:46 -06:00
|
|
|
use glam::{vec2, Vec4};
|
|
|
|
use crate::{
|
2024-02-17 21:04:02 -06:00
|
|
|
draw::{UiDrawCommand, UiDrawCommands},
|
|
|
|
element::{MeasureContext, ProcessContext, UiElement},
|
2024-02-17 14:43:46 -06:00
|
|
|
measure::Response,
|
2024-02-17 21:04:02 -06:00
|
|
|
state::StateRepo,
|
|
|
|
LayoutInfo, UiSize
|
2024-02-17 14:43:46 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Rect {
|
|
|
|
pub size: (UiSize, UiSize),
|
|
|
|
pub color: Option<Vec4>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Rect {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
size: (UiSize::Pixels(10.), UiSize::Pixels(10.)),
|
|
|
|
color: Some(Vec4::new(0., 0., 0., 0.5)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UiElement for Rect {
|
2024-02-17 21:04:02 -06:00
|
|
|
fn measure(&self, ctx: MeasureContext) -> Response {
|
2024-02-17 14:43:46 -06:00
|
|
|
Response {
|
|
|
|
size: vec2(
|
|
|
|
match self.size.0 {
|
2024-02-17 21:04:02 -06:00
|
|
|
UiSize::Auto => ctx.layout.max_size.x,
|
|
|
|
UiSize::Percentage(percentage) => ctx.layout.max_size.x * percentage,
|
2024-02-17 14:43:46 -06:00
|
|
|
UiSize::Pixels(pixels) => pixels,
|
|
|
|
},
|
|
|
|
match self.size.1 {
|
2024-02-17 21:04:02 -06:00
|
|
|
UiSize::Auto => ctx.layout.max_size.y,
|
|
|
|
UiSize::Percentage(percentage) => ctx.layout.max_size.y * percentage,
|
2024-02-17 14:43:46 -06:00
|
|
|
UiSize::Pixels(pixels) => pixels,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
hints: Default::default(),
|
|
|
|
user_data: None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-17 21:04:02 -06:00
|
|
|
fn process(&self, ctx: ProcessContext) {
|
2024-02-17 14:43:46 -06:00
|
|
|
if let Some(color) = self.color {
|
2024-02-17 21:04:02 -06:00
|
|
|
ctx.draw.add(UiDrawCommand::Rectangle {
|
|
|
|
position: ctx.layout.position,
|
|
|
|
size: ctx.measure.size,
|
2024-02-17 14:43:46 -06:00
|
|
|
color,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|