2024-02-27 13:31:12 -06:00
|
|
|
//! element layout, alignment and sizing
|
2024-02-20 10:30:26 -06:00
|
|
|
|
|
|
|
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:57:02 -06:00
|
|
|
|
|
|
|
/// Put the element in the center
|
2024-02-20 10:30:26 -06:00
|
|
|
Center = 1,
|
2024-02-20 12:57:02 -06:00
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2024-02-20 12:57:02 -06:00
|
|
|
impl Alignment2d {
|
2024-02-27 10:56:46 -06:00
|
|
|
/// Create a new `Alignment2d` with the same alignment for both axes
|
2024-02-20 12:57:02 -06:00
|
|
|
#[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 {
|
2024-02-20 12:57:02 -06:00
|
|
|
#[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 {
|
2024-02-20 12:57:02 -06:00
|
|
|
#[inline]
|
2024-02-20 12:48:32 -06:00
|
|
|
fn from([horizontal, vertical]: [Alignment; 2]) -> Self {
|
|
|
|
Self { horizontal, vertical }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Alignment> for Alignment2d {
|
2024-02-20 12:57:02 -06:00
|
|
|
#[inline]
|
2024-02-20 12:48:32 -06:00
|
|
|
fn from(alignment: Alignment) -> Self {
|
2024-02-20 12:57:02 -06:00
|
|
|
Self::all(alignment)
|
2024-02-20 12:48:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 10:56:46 -06:00
|
|
|
/// Represents a single size dimension of an UI element.\
|
|
|
|
/// Can be either a static size in pixels, a fraction the parent size or auto-calculated\
|
|
|
|
/// (Meaning of `auto` is entirely dependent on the element).
|
2024-02-26 09:33:55 -06:00
|
|
|
#[derive(Default, Debug, Clone, Copy, PartialEq)]
|
2024-02-27 13:31:12 -06:00
|
|
|
pub enum Size {
|
2024-02-20 10:30:26 -06:00
|
|
|
#[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-27 10:56:46 -06:00
|
|
|
///
|
|
|
|
/// Out of range values are allowed, but are not guaranteed to work as expected\
|
|
|
|
/// (especially with negative values)
|
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-27 13:31:12 -06:00
|
|
|
impl From<f32> for Size {
|
2024-02-26 09:33:55 -06:00
|
|
|
#[inline]
|
|
|
|
fn from(value: f32) -> Self {
|
|
|
|
Self::Static(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug, Clone, Copy, PartialEq)]
|
2024-02-27 13:31:12 -06:00
|
|
|
pub struct Size2d {
|
|
|
|
pub width: Size,
|
|
|
|
pub height: Size,
|
2024-02-26 09:33:55 -06:00
|
|
|
}
|
|
|
|
|
2024-02-27 13:31:12 -06:00
|
|
|
impl From<(Size, Size)> for Size2d {
|
2024-02-26 09:33:55 -06:00
|
|
|
#[inline]
|
2024-02-27 13:31:12 -06:00
|
|
|
fn from((width, height): (Size, Size)) -> Self {
|
2024-02-26 09:33:55 -06:00
|
|
|
Self { width, height }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//XXX: should this exist?
|
2024-02-27 13:31:12 -06:00
|
|
|
impl From<Size> for Size2d {
|
2024-02-26 09:33:55 -06:00
|
|
|
#[inline]
|
2024-02-27 13:31:12 -06:00
|
|
|
fn from(size: Size) -> Self {
|
2024-02-26 09:33:55 -06:00
|
|
|
Self {
|
|
|
|
width: size,
|
|
|
|
height: size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 10:56:46 -06:00
|
|
|
/// Represents the direction of the layout\
|
|
|
|
/// (for example, the direction of a container's children)\
|
|
|
|
///
|
|
|
|
/// - `Vertical` - Children are laid out from top to bottom\
|
|
|
|
/// - `Horizontal` - Children are laid out from left to right
|
2024-02-20 10:30:26 -06:00
|
|
|
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum UiDirection {
|
2024-02-27 10:56:46 -06:00
|
|
|
/// Children are laid out from top to bottom
|
2024-02-20 10:30:26 -06:00
|
|
|
#[default]
|
|
|
|
Vertical,
|
2024-02-27 10:56:46 -06:00
|
|
|
/// Children are laid out from left to right
|
2024-02-20 10:30:26 -06:00
|
|
|
Horizontal,
|
|
|
|
}
|
|
|
|
|
2024-02-27 10:56:46 -06:00
|
|
|
/// Represents the layout information required to measure, layout and render an element.\
|
|
|
|
/// Includes the position, maximum size, direction of the layout and other information
|
2024-02-20 10:30:26 -06:00
|
|
|
pub struct LayoutInfo {
|
2024-02-27 10:56:46 -06:00
|
|
|
/// Screen-space coordinates of the top-left corner of the element.\
|
|
|
|
/// Use this value during the layout step to render the element
|
|
|
|
///
|
|
|
|
/// Not available during the measure step (will be set to zero)
|
2024-02-20 10:30:26 -06:00
|
|
|
pub position: Vec2,
|
2024-02-27 10:56:46 -06:00
|
|
|
|
|
|
|
/// Maximum size the element is allowed to take up
|
2024-02-20 10:30:26 -06:00
|
|
|
pub max_size: Vec2,
|
2024-02-27 10:56:46 -06:00
|
|
|
|
|
|
|
/// Current direction of the layout\
|
|
|
|
/// (Usually matches direction of the parent container)
|
2024-02-20 10:30:26 -06:00
|
|
|
pub direction: UiDirection,
|
|
|
|
}
|