hUI/hui/src/layout.rs

170 lines
3.9 KiB
Rust
Raw Normal View History

2024-02-20 10:30:26 -06:00
//! Layout related types and functions
use glam::Vec2;
2024-02-20 12:48:32 -06:00
/// Alignment along a single axis
2024-02-20 10:30:26 -06:00
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, PartialOrd, Ord)]
pub enum Alignment {
2024-02-20 12:48:32 -06:00
/// Put the element at the beginning of the axis\
/// (left for horizontal, top for vertical alignment)
2024-02-20 10:30:26 -06:00
#[default]
Begin = 0,
/// Put the element in the center
2024-02-20 10:30:26 -06:00
Center = 1,
2024-02-20 12:48:32 -06:00
/// Put the element at the end of the axis\
/// (right for horizontal, bottom for vertical alignment)
2024-02-20 10:30:26 -06:00
End = 2,
}
2024-02-20 12:48:32 -06:00
/// 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 Alignment2d {
#[inline]
pub const fn all(alignment: Alignment) -> Self {
Self {
horizontal: alignment,
vertical: alignment,
}
}
}
2024-02-20 12:48:32 -06:00
impl From<(Alignment, Alignment)> for Alignment2d {
#[inline]
2024-02-20 12:48:32 -06:00
fn from((horizontal, vertical): (Alignment, Alignment)) -> Self {
Self { horizontal, vertical }
}
}
impl From<[Alignment; 2]> for Alignment2d {
#[inline]
2024-02-20 12:48:32 -06:00
fn from([horizontal, vertical]: [Alignment; 2]) -> Self {
Self { horizontal, vertical }
}
}
impl From<Alignment> for Alignment2d {
#[inline]
2024-02-20 12:48:32 -06:00
fn from(alignment: Alignment) -> Self {
Self::all(alignment)
2024-02-20 12:48:32 -06:00
}
}
2024-02-26 09:33:55 -06:00
#[derive(Default, Debug, Clone, Copy, PartialEq)]
2024-02-20 10:30:26 -06:00
pub enum UiSize {
#[default]
2024-02-26 09:33:55 -06:00
/// Automatically calculate size based on content
2024-02-20 10:30:26 -06:00
Auto,
2024-02-26 09:33:55 -06:00
/// Size as a ratio of parent size\
/// Valid range: 0.0-1.0 (0-100%)
2024-02-20 10:30:26 -06:00
Fraction(f32),
2024-02-26 09:33:55 -06:00
/// Static size in pixels
2024-02-20 10:30:26 -06:00
Static(f32),
}
2024-02-26 09:33:55 -06:00
impl From<f32> for UiSize {
#[inline]
fn from(value: f32) -> Self {
Self::Static(value)
}
}
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct UiSize2d {
pub width: UiSize,
pub height: UiSize,
}
impl From<(UiSize, UiSize)> for UiSize2d {
#[inline]
fn from((width, height): (UiSize, UiSize)) -> Self {
Self { width, height }
}
}
//XXX: should this exist?
impl From<UiSize> for UiSize2d {
#[inline]
fn from(size: UiSize) -> Self {
Self {
width: size,
height: size,
}
}
}
//TODO?: add `UiSize2d` from `(Into<UiSize>, Into<UiSize>)` or Into<UiSize> conversion
/// Create a `UiSize` or `UiSize2d` from a literal
/// # Syntax:
/// - `auto` - `UiSize::Auto`
/// - `x` - `UiSize::Static(x)`
/// - `x%` - `UiSize::Fraction(x / 100.)`
///
/// If two values are provided, it creates a `UiSize2d` with the first value as width and the second as height
#[macro_export]
macro_rules! size {
(auto) => {
$crate::layout::UiSize::Auto
};
($x:literal) => {
$crate::layout::UiSize::Static($x as f32)
};
($x:literal %) => {
$crate::layout::UiSize::Fraction($x as f32 / 100.)
};
($x:literal , $y:tt $($ys:tt)?) => {
$crate::layout::UiSize2d {
width: $crate::layout::size!($x),
height: $crate::layout::size!($y $($ys)?),
}
};
($x:literal $($xs:tt)? , $y:tt $($ys:tt)?) => {
$crate::layout::UiSize2d {
width: $crate::layout::size!($x $($xs)?),
height: $crate::layout::size!($y $($ys)?),
}
};
}
pub use size;
2024-02-20 10:30:26 -06:00
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum UiDirection {
#[default]
Vertical,
Horizontal,
}
pub struct LayoutInfo {
///Not availabe during measuring step
pub position: Vec2,
pub max_size: Vec2,
pub direction: UiDirection,
}