use std::str::FromStr;
#[cfg(feature = "reflect")]
use bevy::prelude::Reflect;
use crate::Oriented;
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "reflect", derive(Reflect))]
pub enum Alignment {
#[default]
Start,
Center,
End,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[cfg_attr(feature = "reflect", derive(Reflect))]
#[doc(alias = "justification")]
pub enum Distribution {
#[default]
Start,
FillMain,
End,
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub(crate) struct CrossAlign {
cross_parent_size: f32,
align: Alignment,
}
impl CrossAlign {
pub const fn new(parent_size: Oriented<f32>, align: Alignment) -> Self {
CrossAlign { cross_parent_size: parent_size.cross, align }
}
pub fn offset(self, cross_child_size: f32) -> f32 {
match self.align {
Alignment::Start => 0.0,
Alignment::Center => (self.cross_parent_size - cross_child_size) / 2.0,
Alignment::End => self.cross_parent_size - cross_child_size,
}
}
}
impl FromStr for Distribution {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"dS" => Ok(Self::Start),
"dE" => Ok(Self::End),
"dC" => Ok(Self::FillMain),
_ => Err(()),
}
}
}
impl FromStr for Alignment {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"aS" => Ok(Self::Start),
"aE" => Ok(Self::End),
"aC" => Ok(Self::Center),
_ => Err(()),
}
}
}