hUI/hui/src/element/builtin/rect.rs

56 lines
1.4 KiB
Rust
Raw Normal View History

2024-02-17 14:43:46 -06:00
use glam::{vec2, Vec4};
use crate::{
2024-02-20 13:56:58 -06:00
background::Background,
2024-02-18 10:22:31 -06:00
draw::UiDrawCommand,
element::{MeasureContext, ProcessContext, UiElement},
2024-02-20 13:56:58 -06:00
layout::UiSize,
2024-02-17 14:43:46 -06:00
measure::Response,
2024-02-20 13:56:58 -06:00
rectangle::Corners
2024-02-17 14:43:46 -06:00
};
pub struct Rect {
pub size: (UiSize, UiSize),
2024-02-20 13:56:58 -06:00
pub color: Background,
2024-02-17 14:43:46 -06:00
}
impl Default for Rect {
fn default() -> Self {
Self {
2024-02-18 12:27:45 -06:00
size: (UiSize::Static(10.), UiSize::Static(10.)),
2024-02-20 13:56:58 -06:00
color: Vec4::new(0., 0., 0., 0.5).into(),
2024-02-17 14:43:46 -06:00
}
}
}
impl UiElement for Rect {
fn measure(&self, ctx: MeasureContext) -> Response {
2024-02-17 14:43:46 -06:00
Response {
size: vec2(
match self.size.0 {
UiSize::Auto => ctx.layout.max_size.x,
2024-02-18 12:27:45 -06:00
UiSize::Fraction(percentage) => ctx.layout.max_size.x * percentage,
UiSize::Static(pixels) => pixels,
2024-02-17 14:43:46 -06:00
},
match self.size.1 {
UiSize::Auto => ctx.layout.max_size.y,
2024-02-18 12:27:45 -06:00
UiSize::Fraction(percentage) => ctx.layout.max_size.y * percentage,
UiSize::Static(pixels) => pixels,
2024-02-17 14:43:46 -06:00
},
),
hints: Default::default(),
user_data: None
}
}
fn process(&self, ctx: ProcessContext) {
2024-02-20 13:56:58 -06:00
if !self.color.is_transparent() {
ctx.draw.add(UiDrawCommand::Rectangle {
position: ctx.layout.position,
size: ctx.measure.size,
2024-02-20 13:56:58 -06:00
color: self.color.corners().unwrap(),
2024-02-19 12:40:18 -06:00
rounded_corners: None,
2024-02-17 14:43:46 -06:00
});
}
}
}