From a0eb2cd42cbe6d76e152fd0f35baeb1fb1f6ac25 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Tue, 20 Feb 2024 19:48:32 +0100 Subject: [PATCH] add `Alignment2d`, better docs --- hui/src/element/builtin/container.rs | 12 +++---- hui/src/layout.rs | 54 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/hui/src/element/builtin/container.rs b/hui/src/element/builtin/container.rs index 16d2d69..35fcc96 100644 --- a/hui/src/element/builtin/container.rs +++ b/hui/src/element/builtin/container.rs @@ -1,10 +1,6 @@ use glam::{Vec2, vec2, Vec4}; use crate::{ - layout::{Alignment, LayoutInfo, UiDirection, UiSize}, - rectangle::{Corners, Sides}, - draw::{RoundedCorners, UiDrawCommand}, - element::{MeasureContext, ProcessContext, UiElement}, - measure::{Hints, Response}, + draw::{RoundedCorners, UiDrawCommand}, element::{MeasureContext, ProcessContext, UiElement}, layout::{Alignment, Alignment2d, LayoutInfo, UiDirection, UiSize}, measure::{Hints, Response}, rectangle::{Corners, Sides} }; pub struct Border { @@ -21,12 +17,12 @@ pub struct Container { pub gap: f32, pub padding: Sides, ///Primary/secondary axis - pub align: (Alignment, Alignment), + pub align: Alignment2d, pub background: Option, pub borders: Sides>, + pub corner_radius: Option>, //pub clip: bool, //TODO clip children pub elements: Vec>, - pub corner_radius: Option>, } impl Default for Container { @@ -39,7 +35,7 @@ impl Default for Container { //reverse: false, gap: 0., padding: Sides::all(0.), - align: (Alignment::Begin, Alignment::Begin), + align: Alignment2d::default(), background: Default::default(), borders: Default::default(), elements: Vec::new(), diff --git a/hui/src/layout.rs b/hui/src/layout.rs index 4492e9d..be0647b 100644 --- a/hui/src/layout.rs +++ b/hui/src/layout.rs @@ -2,14 +2,68 @@ use glam::Vec2; +/// Alignment along a single axis #[derive(Clone, Copy, PartialEq, Eq, Debug, Default, PartialOrd, Ord)] pub enum Alignment { + /// Put the element at the beginning of the axis\ + /// (left for horizontal, top for vertical alignment) #[default] Begin = 0, + /// Put the element in the center of the axis Center = 1, + /// Put the element at the end of the axis\ + /// (right for horizontal, bottom for vertical alignment) End = 2, } +/// Represents alignment in 2D space +/// +/// - `horizontal` - alignment *along* x-axis (horizontal)\ +/// - `vertical` - alignment *along* y-axis (vertical) +#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, PartialOrd, Ord)] +pub struct Alignment2d { + /// Alignment *along* horizontal axis (X) + /// + /// ```text + /// ├───────[ ]──────┤ + /// ↑↑ ↑↑ ↑↑ + /// Begin Center End + /// ``` + pub horizontal: Alignment, + + /// Alignment *along* vertical axis (Y) + /// + /// ```text + /// ┬ ←─ Begin + /// │ + /// [ ] ←─ Center + /// │ + /// ┴ ←─ End + /// ``` + pub vertical: Alignment, +} + +impl From<(Alignment, Alignment)> for Alignment2d { + fn from((horizontal, vertical): (Alignment, Alignment)) -> Self { + Self { horizontal, vertical } + } +} + +impl From<[Alignment; 2]> for Alignment2d { + fn from([horizontal, vertical]: [Alignment; 2]) -> Self { + Self { horizontal, vertical } + } +} + +impl From for Alignment2d { + fn from(alignment: Alignment) -> Self { + Self { + horizontal: alignment, + vertical: alignment, + } + } +} + #[derive(Default, Debug, Clone, Copy)] pub enum UiSize { #[default]