ori_core/layout/
alignment.rsuse super::{Size, Vector};
#[repr(C)]
#[derive(Copy, Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Alignment {
pub x: f32,
pub y: f32,
}
impl Alignment {
pub const CENTER: Self = Self::new(0.5, 0.5);
pub const TOP_LEFT: Self = Self::new(0.0, 0.0);
pub const TOP: Self = Self::new(0.5, 0.0);
pub const TOP_RIGHT: Self = Self::new(1.0, 0.0);
pub const LEFT: Self = Self::new(0.0, 0.5);
pub const RIGHT: Self = Self::new(1.0, 0.5);
pub const BOTTOM_LEFT: Self = Self::new(0.0, 1.0);
pub const BOTTOM: Self = Self::new(0.5, 1.0);
pub const BOTTOM_RIGHT: Self = Self::new(1.0, 1.0);
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub fn align(self, content: Size, container: Size) -> Vector {
Vector::new(
self.x * (container.width - content.width),
self.y * (container.height - content.height),
)
}
}
impl From<(f32, f32)> for Alignment {
fn from((x, y): (f32, f32)) -> Self {
Self::new(x, y)
}
}
impl From<[f32; 2]> for Alignment {
fn from([x, y]: [f32; 2]) -> Self {
Self::new(x, y)
}
}