diff --git a/hui-examples/examples/test.rs b/hui-examples/examples/align.rs similarity index 92% rename from hui-examples/examples/test.rs rename to hui-examples/examples/align.rs index 0b55397..1c1c0fd 100644 --- a/hui-examples/examples/test.rs +++ b/hui-examples/examples/align.rs @@ -47,7 +47,7 @@ fn main() { hui.add(Container { gap: 5., padding: Sides::all(5.), - align: (Alignment::Begin, Alignment::Center), + align: (Alignment::Begin, Alignment::Center).into(), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), elements: vec![ Box::new(ProgressBar { @@ -61,7 +61,7 @@ fn main() { hui.add(Container { gap: 5., padding: Sides::all(5.), - align: (Alignment::End, Alignment::Center), + align: (Alignment::End, Alignment::Center).into(), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), elements: vec![ Box::new(ProgressBar { @@ -70,7 +70,7 @@ fn main() { }), Box::new(Container { size: (UiSize::Fraction(1.), UiSize::Auto), - align: (Alignment::Center, Alignment::End), + align: (Alignment::Center, Alignment::End).into(), padding: Sides::all(5.), gap: 10., elements: vec![ @@ -131,7 +131,7 @@ fn main() { }), ], ..Default::default() - }) + }), ], ..Default::default() }, resolution); diff --git a/hui-examples/examples/mom_downloader.rs b/hui-examples/examples/mom_downloader.rs index 6855523..4e93409 100644 --- a/hui-examples/examples/mom_downloader.rs +++ b/hui-examples/examples/mom_downloader.rs @@ -48,13 +48,12 @@ fn main() { let mom_ratio = (instant.elapsed().as_secs_f32() / 60.).powf(0.5); hui.add(Container { - align: (Alignment::Center, Alignment::Center), + align: Alignment::Center.into(), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), background: Some(vec4(0.1, 0.1, 0.1, 1.)), elements: vec![Box::new(Container { gap: 5., padding: Sides::all(10.), - align: (Alignment::Begin, Alignment::Begin), size: (UiSize::Static(450.), UiSize::Auto), background: Some(vec4(0.2, 0.2, 0.5, 1.)), corner_radius: Some(Corners::all(8.)), @@ -72,7 +71,7 @@ fn main() { }); el.add(Container { direction: UiDirection::Horizontal, - align: (Alignment::End, Alignment::Center), + align: (Alignment::End, Alignment::Center).into(), size: (UiSize::Fraction(1.), UiSize::Auto), elements: vec![Box::new(Text { text: format!("{:.2}% ({:.1} GB)", mom_ratio * 100., mom_ratio * 10000.).into(), diff --git a/hui-examples/examples/rounded.rs b/hui-examples/examples/rounded_rect.rs similarity index 90% rename from hui-examples/examples/rounded.rs rename to hui-examples/examples/rounded_rect.rs index c8de0df..f75798e 100644 --- a/hui-examples/examples/rounded.rs +++ b/hui-examples/examples/rounded_rect.rs @@ -38,10 +38,10 @@ fn main() { hui.begin(); hui.add(Container { - align: (Alignment::Center, Alignment::Center), + align: Alignment::Center.into(), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), elements: vec![Box::new(Container { - align: (Alignment::Center, Alignment::Center), + align: Alignment::Center.into(), size: (UiSize::Fraction(0.5), UiSize::Fraction(0.5)), background: Some(vec4(1., 0., 0., 1.)), corner_radius: Some(Corners { @@ -54,7 +54,7 @@ fn main() { Box::new(Container { padding: Sides::all(20.), direction: UiDirection::Horizontal, - align: (Alignment::Center, Alignment::Center), + align: Alignment::Center.into(), size: (UiSize::Auto, UiSize::Auto), background: Some(vec4(0.1, 0.1, 0.1, 0.5)), corner_radius: Some(Corners::all(8.)), diff --git a/hui-examples/examples/test2.rs b/hui-examples/examples/text_center.rs similarity index 93% rename from hui-examples/examples/test2.rs rename to hui-examples/examples/text_center.rs index 421c904..1f58aa3 100644 --- a/hui-examples/examples/test2.rs +++ b/hui-examples/examples/text_center.rs @@ -40,7 +40,7 @@ fn main() { hui.add(Container { gap: 5., padding: Sides::all(5.), - align: (Alignment::Center, Alignment::Center), + align: Alignment::Center.into(), size: (UiSize::Fraction(1.), UiSize::Fraction(1.)), elements: vec![ Box::new(Text { diff --git a/hui/src/element/builtin/container.rs b/hui/src/element/builtin/container.rs index 35fcc96..913b2bb 100644 --- a/hui/src/element/builtin/container.rs +++ b/hui/src/element/builtin/container.rs @@ -137,8 +137,15 @@ impl UiElement for Container { //padding 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 - match (self.align.0, self.direction) { + match (pri_sec_align.0, self.direction) { (Alignment::Begin, _) => (), (Alignment::Center, UiDirection::Horizontal) => { 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) - match (self.align.1, self.direction) { + match (pri_sec_align.1, self.direction) { (Alignment::Begin, _) => (), (Alignment::Center, UiDirection::Horizontal) => { el_layout.position.y += (ctx.measure.size.y - self.padding.bottom - self.padding.top - el_measure.size.y) / 2.; diff --git a/hui/src/layout.rs b/hui/src/layout.rs index be0647b..7c7130a 100644 --- a/hui/src/layout.rs +++ b/hui/src/layout.rs @@ -9,8 +9,10 @@ pub enum Alignment { /// (left for horizontal, top for vertical alignment) #[default] Begin = 0, - /// Put the element in the center of the axis + + /// Put the element in the center Center = 1, + /// Put the element at the end of the axis\ /// (right for horizontal, bottom for vertical alignment) End = 2, @@ -43,24 +45,34 @@ pub struct Alignment2d { pub vertical: Alignment, } +impl Alignment2d { + #[inline] + pub const fn all(alignment: Alignment) -> Self { + Self { + horizontal: alignment, + vertical: alignment, + } + } +} + impl From<(Alignment, Alignment)> for Alignment2d { + #[inline] fn from((horizontal, vertical): (Alignment, Alignment)) -> Self { Self { horizontal, vertical } } } impl From<[Alignment; 2]> for Alignment2d { + #[inline] fn from([horizontal, vertical]: [Alignment; 2]) -> Self { Self { horizontal, vertical } } } impl From for Alignment2d { + #[inline] fn from(alignment: Alignment) -> Self { - Self { - horizontal: alignment, - vertical: alignment, - } + Self::all(alignment) } }