hUI/hui/src/layout.rs

88 lines
2.1 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,
2024-02-20 12:48:32 -06:00
/// Put the element in the center of the axis
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 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<Alignment> for Alignment2d {
fn from(alignment: Alignment) -> Self {
Self {
horizontal: alignment,
vertical: alignment,
}
}
}
2024-02-20 10:30:26 -06:00
#[derive(Default, Debug, Clone, Copy)]
pub enum UiSize {
#[default]
Auto,
Fraction(f32),
Static(f32),
}
#[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,
}