use bevy_ecs::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Component)]
pub enum Dimension {
Auto,
Px(f32),
Percent(f32),
}
impl Dimension {
pub const fn length(val: f32) -> Self {
Self::Px(val)
}
pub const fn percent(val: f32) -> Self {
Self::Percent(val)
}
pub const fn auto() -> Self {
Self::Auto
}
pub const fn zero() -> Self {
Self::Px(0.0)
}
}
pub trait TaffyZero {
const ZERO: Self;
}
impl TaffyZero for Dimension {
const ZERO: Self = Self::Px(0.0);
}
pub trait TaffyAuto {
const AUTO: Self;
}
impl TaffyAuto for Dimension {
const AUTO: Self = Self::Auto;
}
impl Default for Dimension {
fn default() -> Self {
Self::Auto
}
}
impl From<Dimension> for taffy::Dimension {
fn from(val: Dimension) -> Self {
match val {
Dimension::Auto => taffy::Dimension::auto(),
Dimension::Px(v) => taffy::Dimension::length(v),
Dimension::Percent(v) => taffy::Dimension::percent(v / 100.0),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Component)]
pub enum LengthPercentageAuto {
Auto,
Px(f32),
Percent(f32),
}
impl LengthPercentageAuto {
pub const fn length(val: f32) -> Self {
Self::Px(val)
}
pub const fn percent(val: f32) -> Self {
Self::Percent(val)
}
pub const fn auto() -> Self {
Self::Auto
}
pub const fn zero() -> Self {
Self::Px(0.0)
}
}
impl TaffyZero for LengthPercentageAuto {
const ZERO: Self = Self::Px(0.0);
}
impl TaffyAuto for LengthPercentageAuto {
const AUTO: Self = Self::Auto;
}
impl Default for LengthPercentageAuto {
fn default() -> Self {
Self::Auto
}
}
impl From<LengthPercentageAuto> for taffy::LengthPercentageAuto {
fn from(val: LengthPercentageAuto) -> Self {
match val {
LengthPercentageAuto::Auto => taffy::LengthPercentageAuto::auto(),
LengthPercentageAuto::Px(v) => taffy::LengthPercentageAuto::length(v),
LengthPercentageAuto::Percent(v) => taffy::LengthPercentageAuto::percent(v),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Component)]
pub enum LengthPercentage {
Px(f32),
Percent(f32),
}
impl LengthPercentage {
pub const fn length(val: f32) -> Self {
Self::Px(val)
}
pub const fn percent(val: f32) -> Self {
Self::Percent(val)
}
pub const fn zero() -> Self {
Self::Px(0.0)
}
}
impl TaffyZero for LengthPercentage {
const ZERO: Self = Self::Px(0.0);
}
impl Default for LengthPercentage {
fn default() -> Self {
Self::Px(0.0)
}
}
impl From<LengthPercentage> for taffy::LengthPercentage {
fn from(val: LengthPercentage) -> Self {
match val {
LengthPercentage::Px(v) => taffy::LengthPercentage::length(v),
LengthPercentage::Percent(v) => taffy::LengthPercentage::percent(v),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Component)]
pub struct Rect<T> {
pub left: T,
pub right: T,
pub top: T,
pub bottom: T,
}
impl<T: TaffyZero> Rect<T> {
pub const fn zero() -> Self
where
T: Copy,
{
Self {
left: T::ZERO,
right: T::ZERO,
top: T::ZERO,
bottom: T::ZERO,
}
}
}
impl<T: TaffyAuto> Rect<T> {
pub const fn auto() -> Self
where
T: Copy,
{
Self {
left: T::AUTO,
right: T::AUTO,
top: T::AUTO,
bottom: T::AUTO,
}
}
}
impl<T: Default> Default for Rect<T> {
fn default() -> Self {
Self {
left: T::default(),
right: T::default(),
top: T::default(),
bottom: T::default(),
}
}
}
impl From<Rect<LengthPercentageAuto>> for taffy::Rect<taffy::LengthPercentageAuto> {
fn from(val: Rect<LengthPercentageAuto>) -> Self {
taffy::Rect {
left: val.left.into(),
right: val.right.into(),
top: val.top.into(),
bottom: val.bottom.into(),
}
}
}
impl From<Rect<LengthPercentage>> for taffy::Rect<taffy::LengthPercentage> {
fn from(val: Rect<LengthPercentage>) -> Self {
taffy::Rect {
left: val.left.into(),
right: val.right.into(),
top: val.top.into(),
bottom: val.bottom.into(),
}
}
}