alignment!!!

This commit is contained in:
2025-08-28 21:55:34 -04:00
parent 46c7d8ba26
commit 1204e3728e
11 changed files with 190 additions and 86 deletions

View File

@@ -1,5 +1,5 @@
use crate::{
layout::{Axis, Corner, Vec2, vec2},
layout::{Align, Axis, Vec2},
util::{F32Util, impl_op},
};
@@ -20,10 +20,6 @@ impl UiPos {
}
}
pub const fn center() -> Self {
Self::anchor(vec2(0.5, 0.5))
}
pub const fn anchor(anchor: Vec2) -> Self {
Self {
anchor,
@@ -38,10 +34,6 @@ impl UiPos {
}
}
pub const fn corner(corner: Corner) -> Self {
Self::anchor(corner.anchor())
}
pub const fn shift(&mut self, offset: impl const Into<Vec2>) {
self.offset += offset.into();
}
@@ -90,6 +82,18 @@ impl UiPos {
}
}
impl const From<Align> for UiPos {
fn from(align: Align) -> Self {
Self::anchor(align.anchor())
}
}
impl Align {
pub fn pos(self) -> UiPos {
UiPos::from(self)
}
}
#[derive(Clone, Copy, Debug)]
pub struct UIScalar {
pub anchor: f32,
@@ -137,8 +141,8 @@ pub struct UiRegion {
impl UiRegion {
pub const fn full() -> Self {
Self {
top_left: UiPos::corner(Corner::TopLeft),
bot_right: UiPos::corner(Corner::BotRight),
top_left: Align::TopLeft.into(),
bot_right: Align::BotRight.into(),
}
}
pub fn anchor(anchor: Vec2) -> Self {
@@ -153,9 +157,6 @@ impl UiRegion {
bot_right: self.bot_right.within(parent),
}
}
pub fn select(&mut self, inner: &Self) {
*self = inner.within(self);
}
pub fn axis_mut(&mut self, axis: Axis) -> UIRegionAxisView<'_> {
UIRegionAxisView {
top_left: self.top_left.axis_mut(axis),
@@ -188,12 +189,27 @@ impl UiRegion {
}
pub fn center(&self) -> UiPos {
UiPos::center().within(self)
Align::Center.pos().within(self)
}
pub fn in_size(&self, size: Vec2) -> Vec2 {
self.bot_right.to_size(size) - self.top_left.to_size(size)
}
pub fn select_aligned(&self, size: Vec2, align: Align) -> Self {
Self::from_size_align(size, align).within(self)
}
pub fn from_size_align(size: Vec2, align: Align) -> Self {
let mut top_left = UiPos::from(align);
top_left.offset -= size * align.anchor();
let mut bot_right = UiPos::from(align);
bot_right.offset += size * (Vec2::ONE - align.anchor());
Self {
top_left,
bot_right,
}
}
}
#[derive(Debug)]