Compare commits

..

No commits in common. "63f862c2be35f11cd42123ccc5454757ead1ed41" and "711942567fe7df7bccd8af4f9dd8560b449503ed" have entirely different histories.

6 changed files with 18 additions and 159 deletions

View file

@ -11,9 +11,6 @@ glam = { version = "0.24", features = ["approx"] }
glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf", optional = true }
log = "0.4"
[dev-dependencies]
glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf" }
[features]
default = ["backend_glium", "builtin_elements"]
backend_glium = ["dep:glium"]

View file

@ -1,3 +0,0 @@
pub fn main() {
//TODO ui demo
}

View file

@ -6,7 +6,6 @@ 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;

View file

@ -1,58 +1,30 @@
use glam::{Vec2, vec2, Vec4};
use glam::{Vec2, Vec4};
use crate::{UiDirection, LayoutInfo, draw::UiDrawCommand, measure::Response, state::StateRepo, UiSize};
use super::UiElement;
pub enum Alignment {
#[derive(Default, Clone, Copy, Debug)]
pub struct ContainerBorders {
pub top: Option<(Vec4, f32)>,
pub bottom: Option<(Vec4, f32)>,
pub left: Option<(Vec4, f32)>,
pub right: Option<(Vec4, f32)>,
}
pub enum ContainerAlign {
Begin,
Center,
End,
}
pub struct Border {
pub color: Vec4,
pub width: f32,
}
#[derive(Default, Clone, Copy, PartialEq, Eq, Debug)]
pub struct Sides<T> {
pub top: T,
pub bottom: T,
pub left: T,
pub right: T,
}
impl<T: Clone> Sides<T> {
#[inline]
pub fn all(value: T) -> Self {
Self {
top: value.clone(),
bottom: value.clone(),
left: value.clone(),
right: value,
}
}
#[inline]
pub fn horizontal_vertical(horizontal: T, vertical: T) -> Self {
Self {
top: vertical.clone(),
bottom: vertical,
left: horizontal.clone(),
right: horizontal,
}
}
}
pub struct Container {
pub min_size: (UiSize, UiSize),
pub max_size: (UiSize, UiSize),
pub direction: UiDirection,
//pub reverse: bool,
pub gap: f32,
pub padding: Sides<f32>,
pub align: (Alignment, Alignment),
pub padding: f32,
pub align: (ContainerAlign, ContainerAlign),
pub background: Option<Vec4>,
pub borders: Sides<Option<Border>>,
pub borders: ContainerBorders,
pub clip: bool,
pub elements: Vec<Box<dyn UiElement>>,
}
@ -63,10 +35,9 @@ impl Default for Container {
min_size: (UiSize::Auto, UiSize::Auto),
max_size: (UiSize::Auto, UiSize::Auto),
direction: UiDirection::Vertical,
//reverse: false,
gap: 0.,
padding: Sides::all(0.),
align: (Alignment::Center, Alignment::Begin),
padding: 0.,
align: (ContainerAlign::Center, ContainerAlign::Begin),
background: Default::default(),
borders: Default::default(),
clip: Default::default(),
@ -82,7 +53,7 @@ impl UiElement for Container {
for element in &self.elements {
let measure = element.measure(state, &LayoutInfo {
position: layout.position + size,
max_size: layout.max_size, // - size, //TODO
max_size: layout.max_size - size,
direction: self.direction,
});
match self.direction {
@ -103,67 +74,14 @@ impl UiElement for Container {
}
fn process(&self, measure: &Response, state: &mut StateRepo, layout: &LayoutInfo, draw: &mut Vec<UiDrawCommand>) {
let mut position = layout.position;
//background
if let Some(color) = self.background {
draw.push(UiDrawCommand::Rectangle {
position,
position: layout.position,
size: measure.desired_size,
color
});
}
//padding
position += vec2(self.padding.left, self.padding.top);
//alignment
//TODO: REQUIRES MAX MEASURE SIZES!
// match self.align.0 {
// Alignment::Begin => (),
// Alignment::Center => {
// position.x += (layout.max_size.x - measure.desired_size.x) / 2.;
// },
// Alignment::End => {
// position.x += layout.max_size.x - measure.desired_size.x;
// }
// }
// match self.align.1 {
// Alignment::Begin => (),
// Alignment::Center => {
// position.y += (layout.max_size.y - measure.desired_size.y) / 2.;
// },
// Alignment::End => {
// position.y += layout.max_size.y - measure.desired_size.y;
// }
// }
for element in &self.elements {
//(passing max size from layout rather than actual bounds for the sake of consistency with measure() above)
//measure
let el_measure = element.measure(state, &LayoutInfo {
position,
max_size: layout.max_size,
direction: self.direction,
});
//process
element.process(&el_measure, state, &LayoutInfo {
position,
max_size: layout.max_size,
direction: self.direction,
}, draw);
//layout
match self.direction {
UiDirection::Horizontal => {
position.x += el_measure.desired_size.x + self.gap;
},
UiDirection::Vertical => {
position.y += el_measure.desired_size.y + self.gap;
}
}
//TODO draw borders
}
}
}

View file

@ -1,46 +0,0 @@
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,
});
}
}
}

View file

@ -4,12 +4,6 @@ use super::UiElement;
pub struct Spacer(f32);
impl Default for Spacer {
fn default() -> Self {
Self(5.)
}
}
impl UiElement for Spacer {
fn measure(&self, state: &StateRepo, layout: &LayoutInfo) -> Response {
Response {