mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-22 03:48:21 -06:00
add rect element
This commit is contained in:
parent
3d8803f465
commit
b28dca8721
|
@ -6,6 +6,7 @@ use crate::{
|
|||
state::StateRepo
|
||||
};
|
||||
|
||||
#[cfg(feature = "builtin_elements")] pub mod rect;
|
||||
#[cfg(feature = "builtin_elements")] pub mod container;
|
||||
#[cfg(feature = "builtin_elements")] pub mod spacer;
|
||||
#[cfg(feature = "builtin_elements")] pub mod progress_bar;
|
||||
|
|
46
kubi-ui/src/element/rect.rs
Normal file
46
kubi-ui/src/element/rect.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use glam::{vec2, Vec4};
|
||||
use crate::{state::StateRepo, LayoutInfo, measure::Response, draw::UiDrawCommand, UiSize};
|
||||
use super::UiElement;
|
||||
|
||||
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 {
|
||||
fn measure(&self, _state: &StateRepo, layout: &LayoutInfo) -> Response {
|
||||
Response {
|
||||
desired_size: vec2(
|
||||
match self.size.0 {
|
||||
UiSize::Auto => layout.max_size.x,
|
||||
UiSize::Percentage(percentage) => layout.max_size.x * percentage,
|
||||
UiSize::Pixels(pixels) => pixels,
|
||||
},
|
||||
match self.size.1 {
|
||||
UiSize::Auto => layout.max_size.y,
|
||||
UiSize::Percentage(percentage) => layout.max_size.y * percentage,
|
||||
UiSize::Pixels(pixels) => pixels,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn process(&self, measure: &Response, _state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCommand>) {
|
||||
if let Some(color) = self.color {
|
||||
draw.push(UiDrawCommand::Rectangle {
|
||||
position: layout.position,
|
||||
size: measure.desired_size,
|
||||
color,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue