update stuff to new api, rename examples

This commit is contained in:
griffi-gh 2024-02-20 19:57:02 +01:00
parent a0eb2cd42c
commit db4a6435c9
6 changed files with 36 additions and 18 deletions

View file

@ -47,7 +47,7 @@ fn main() {
hui.add(Container { hui.add(Container {
gap: 5., gap: 5.,
padding: Sides::all(5.), padding: Sides::all(5.),
align: (Alignment::Begin, Alignment::Center), align: (Alignment::Begin, Alignment::Center).into(),
size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
elements: vec![ elements: vec![
Box::new(ProgressBar { Box::new(ProgressBar {
@ -61,7 +61,7 @@ fn main() {
hui.add(Container { hui.add(Container {
gap: 5., gap: 5.,
padding: Sides::all(5.), padding: Sides::all(5.),
align: (Alignment::End, Alignment::Center), align: (Alignment::End, Alignment::Center).into(),
size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
elements: vec![ elements: vec![
Box::new(ProgressBar { Box::new(ProgressBar {
@ -70,7 +70,7 @@ fn main() {
}), }),
Box::new(Container { Box::new(Container {
size: (UiSize::Fraction(1.), UiSize::Auto), size: (UiSize::Fraction(1.), UiSize::Auto),
align: (Alignment::Center, Alignment::End), align: (Alignment::Center, Alignment::End).into(),
padding: Sides::all(5.), padding: Sides::all(5.),
gap: 10., gap: 10.,
elements: vec![ elements: vec![
@ -131,7 +131,7 @@ fn main() {
}), }),
], ],
..Default::default() ..Default::default()
}) }),
], ],
..Default::default() ..Default::default()
}, resolution); }, resolution);

View file

@ -48,13 +48,12 @@ fn main() {
let mom_ratio = (instant.elapsed().as_secs_f32() / 60.).powf(0.5); let mom_ratio = (instant.elapsed().as_secs_f32() / 60.).powf(0.5);
hui.add(Container { hui.add(Container {
align: (Alignment::Center, Alignment::Center), align: Alignment::Center.into(),
size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
background: Some(vec4(0.1, 0.1, 0.1, 1.)), background: Some(vec4(0.1, 0.1, 0.1, 1.)),
elements: vec![Box::new(Container { elements: vec![Box::new(Container {
gap: 5., gap: 5.,
padding: Sides::all(10.), padding: Sides::all(10.),
align: (Alignment::Begin, Alignment::Begin),
size: (UiSize::Static(450.), UiSize::Auto), size: (UiSize::Static(450.), UiSize::Auto),
background: Some(vec4(0.2, 0.2, 0.5, 1.)), background: Some(vec4(0.2, 0.2, 0.5, 1.)),
corner_radius: Some(Corners::all(8.)), corner_radius: Some(Corners::all(8.)),
@ -72,7 +71,7 @@ fn main() {
}); });
el.add(Container { el.add(Container {
direction: UiDirection::Horizontal, direction: UiDirection::Horizontal,
align: (Alignment::End, Alignment::Center), align: (Alignment::End, Alignment::Center).into(),
size: (UiSize::Fraction(1.), UiSize::Auto), size: (UiSize::Fraction(1.), UiSize::Auto),
elements: vec![Box::new(Text { elements: vec![Box::new(Text {
text: format!("{:.2}% ({:.1} GB)", mom_ratio * 100., mom_ratio * 10000.).into(), text: format!("{:.2}% ({:.1} GB)", mom_ratio * 100., mom_ratio * 10000.).into(),

View file

@ -38,10 +38,10 @@ fn main() {
hui.begin(); hui.begin();
hui.add(Container { hui.add(Container {
align: (Alignment::Center, Alignment::Center), align: Alignment::Center.into(),
size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
elements: vec![Box::new(Container { elements: vec![Box::new(Container {
align: (Alignment::Center, Alignment::Center), align: Alignment::Center.into(),
size: (UiSize::Fraction(0.5), UiSize::Fraction(0.5)), size: (UiSize::Fraction(0.5), UiSize::Fraction(0.5)),
background: Some(vec4(1., 0., 0., 1.)), background: Some(vec4(1., 0., 0., 1.)),
corner_radius: Some(Corners { corner_radius: Some(Corners {
@ -54,7 +54,7 @@ fn main() {
Box::new(Container { Box::new(Container {
padding: Sides::all(20.), padding: Sides::all(20.),
direction: UiDirection::Horizontal, direction: UiDirection::Horizontal,
align: (Alignment::Center, Alignment::Center), align: Alignment::Center.into(),
size: (UiSize::Auto, UiSize::Auto), size: (UiSize::Auto, UiSize::Auto),
background: Some(vec4(0.1, 0.1, 0.1, 0.5)), background: Some(vec4(0.1, 0.1, 0.1, 0.5)),
corner_radius: Some(Corners::all(8.)), corner_radius: Some(Corners::all(8.)),

View file

@ -40,7 +40,7 @@ fn main() {
hui.add(Container { hui.add(Container {
gap: 5., gap: 5.,
padding: Sides::all(5.), padding: Sides::all(5.),
align: (Alignment::Center, Alignment::Center), align: Alignment::Center.into(),
size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)),
elements: vec![ elements: vec![
Box::new(Text { Box::new(Text {

View file

@ -137,8 +137,15 @@ impl UiElement for Container {
//padding //padding
position += vec2(self.padding.left, self.padding.top); position += vec2(self.padding.left, self.padding.top);
//convert alignment to pri/sec axis based
//.0 = primary, .1 = secondary
let pri_sec_align = match self.direction {
UiDirection::Horizontal => (self.align.horizontal, self.align.vertical),
UiDirection::Vertical => (self.align.horizontal, self.align.vertical),
};
//alignment //alignment
match (self.align.0, self.direction) { match (pri_sec_align.0, self.direction) {
(Alignment::Begin, _) => (), (Alignment::Begin, _) => (),
(Alignment::Center, UiDirection::Horizontal) => { (Alignment::Center, UiDirection::Horizontal) => {
position.x += (ctx.measure.size.x - ctx.measure.hints.inner_content_size.unwrap().x) / 2. - self.padding.left; position.x += (ctx.measure.size.x - ctx.measure.hints.inner_content_size.unwrap().x) / 2. - self.padding.left;
@ -171,7 +178,7 @@ impl UiElement for Container {
}); });
//align (on sec. axis) //align (on sec. axis)
match (self.align.1, self.direction) { match (pri_sec_align.1, self.direction) {
(Alignment::Begin, _) => (), (Alignment::Begin, _) => (),
(Alignment::Center, UiDirection::Horizontal) => { (Alignment::Center, UiDirection::Horizontal) => {
el_layout.position.y += (ctx.measure.size.y - self.padding.bottom - self.padding.top - el_measure.size.y) / 2.; el_layout.position.y += (ctx.measure.size.y - self.padding.bottom - self.padding.top - el_measure.size.y) / 2.;

View file

@ -9,8 +9,10 @@ pub enum Alignment {
/// (left for horizontal, top for vertical alignment) /// (left for horizontal, top for vertical alignment)
#[default] #[default]
Begin = 0, Begin = 0,
/// Put the element in the center of the axis
/// Put the element in the center
Center = 1, Center = 1,
/// Put the element at the end of the axis\ /// Put the element at the end of the axis\
/// (right for horizontal, bottom for vertical alignment) /// (right for horizontal, bottom for vertical alignment)
End = 2, End = 2,
@ -43,24 +45,34 @@ pub struct Alignment2d {
pub vertical: Alignment, pub vertical: Alignment,
} }
impl Alignment2d {
#[inline]
pub const fn all(alignment: Alignment) -> Self {
Self {
horizontal: alignment,
vertical: alignment,
}
}
}
impl From<(Alignment, Alignment)> for Alignment2d { impl From<(Alignment, Alignment)> for Alignment2d {
#[inline]
fn from((horizontal, vertical): (Alignment, Alignment)) -> Self { fn from((horizontal, vertical): (Alignment, Alignment)) -> Self {
Self { horizontal, vertical } Self { horizontal, vertical }
} }
} }
impl From<[Alignment; 2]> for Alignment2d { impl From<[Alignment; 2]> for Alignment2d {
#[inline]
fn from([horizontal, vertical]: [Alignment; 2]) -> Self { fn from([horizontal, vertical]: [Alignment; 2]) -> Self {
Self { horizontal, vertical } Self { horizontal, vertical }
} }
} }
impl From<Alignment> for Alignment2d { impl From<Alignment> for Alignment2d {
#[inline]
fn from(alignment: Alignment) -> Self { fn from(alignment: Alignment) -> Self {
Self { Self::all(alignment)
horizontal: alignment,
vertical: alignment,
}
} }
} }