switch to element defined span lens + better size fn

This commit is contained in:
2025-11-13 14:27:31 -05:00
parent 8755c04feb
commit 73afea8c35
17 changed files with 767 additions and 646 deletions

View File

@@ -1,6 +1,9 @@
use std::ops::Not;
use crate::layout::{Vec2, vec2};
use crate::{
layout::{UiNum, UiVec2, Vec2, vec2},
util::impl_op,
};
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum Axis {
@@ -100,3 +103,172 @@ impl Align {
}
}
#[derive(Default, Clone, Copy, PartialEq)]
pub struct Size {
pub x: Len,
pub y: Len,
}
#[derive(Clone, Copy, PartialEq)]
pub struct Len {
pub abs: f32,
pub rel: f32,
pub rest: f32,
}
impl<N: UiNum> From<N> for Len {
fn from(value: N) -> Self {
Len::abs(value.to_f32())
}
}
impl<Nx: UiNum, Ny: UiNum> From<(Nx, Ny)> for Size {
fn from((x, y): (Nx, Ny)) -> Self {
Self {
x: x.into(),
y: y.into(),
}
}
}
impl Size {
pub const ZERO: Self = Self {
x: Len::ZERO,
y: Len::ZERO,
};
pub fn abs(v: Vec2) -> Self {
Self {
x: Len::abs(v.x),
y: Len::abs(v.y),
}
}
pub fn to_uivec2(self) -> UiVec2 {
UiVec2 {
rel: Vec2 {
x: self.x.total_rel(),
y: self.y.total_rel(),
},
abs: Vec2 {
x: self.x.abs,
y: self.y.abs,
},
}
}
pub fn from_axis(axis: Axis, aligned: Len, ortho: Len) -> Self {
match axis {
Axis::X => Self {
x: aligned,
y: ortho,
},
Axis::Y => Self {
x: ortho,
y: aligned,
},
}
}
pub fn axis(&self, axis: Axis) -> Len {
match axis {
Axis::X => self.x,
Axis::Y => self.y,
}
}
}
impl Len {
pub const ZERO: Self = Self {
abs: 0.0,
rel: 0.0,
rest: 0.0,
};
pub fn total_rel(&self) -> f32 {
if self.rest > 0.0 {
self.rel.max(1.0)
} else {
self.rel
}
}
pub fn abs(abs: impl UiNum) -> Self {
Self {
abs: abs.to_f32(),
rel: 0.0,
rest: 0.0,
}
}
pub fn rel(rel: impl UiNum) -> Self {
Self {
abs: 0.0,
rel: rel.to_f32(),
rest: 0.0,
}
}
pub fn rest(ratio: impl UiNum) -> Self {
Self {
abs: 0.0,
rel: 0.0,
rest: ratio.to_f32(),
}
}
}
pub mod len_fns {
use super::*;
pub fn abs(abs: impl UiNum) -> Len {
Len {
abs: abs.to_f32(),
rel: 0.0,
rest: 0.0,
}
}
pub fn rel(rel: impl UiNum) -> Len {
Len {
abs: 0.0,
rel: rel.to_f32(),
rest: 0.0,
}
}
pub fn rest(ratio: impl UiNum) -> Len {
Len {
abs: 0.0,
rel: 0.0,
rest: ratio.to_f32(),
}
}
}
impl_op!(Len Add add; abs rel rest);
impl_op!(Len Sub sub; abs rel rest);
impl_op!(Size Add add; x y);
impl_op!(Size Sub sub; x y);
impl Default for Len {
fn default() -> Self {
Self::rest(1.0)
}
}
impl std::fmt::Display for Size {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
impl std::fmt::Display for Len {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.abs != 0.0 {
write!(f, "{} abs;", self.abs)?;
}
if self.rel != 0.0 {
write!(f, "{} rel;", self.abs)?;
}
if self.rest != 0.0 {
write!(f, "{} leftover;", self.abs)?;
}
Ok(())
}
}