center w size

This commit is contained in:
iris committed 2025-08-10 20:40:14 -04:00
1 parent f2cbf90d1d
commit 132113f09e
5 files changed
+56 -17

No files matched your search

+19 -4
View File
@@ -1,10 +1,10 @@
use crate::primitive::{Point, point::point};
use crate::primitive::{Vec2, vec2::point};
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
pub struct UIPos {
pub anchor: Point,
pub offset: Point,
pub anchor: Vec2,
pub offset: Vec2,
}
impl UIPos {
@@ -15,6 +15,10 @@ impl UIPos {
}
}
pub const fn center() -> Self {
Self::anchor_offset(0.0, 0.0, 0.0, 0.0)
}
pub const fn top_left() -> Self {
Self::anchor_offset(-1.0, -1.0, 0.0, 0.0)
}
@@ -23,6 +27,11 @@ impl UIPos {
Self::anchor_offset(1.0, 1.0, 0.0, 0.0)
}
pub const fn offset(mut self, offset: Vec2) -> Self {
self.offset = offset;
self
}
pub const fn within(&self, region: &UIRegion) -> UIPos {
let lerp = self.anchor_01();
let anchor = region.top_left.anchor.lerp(region.bot_right.anchor, lerp);
@@ -30,7 +39,7 @@ impl UIPos {
UIPos { anchor, offset }
}
pub const fn anchor_01(&self) -> Point {
pub const fn anchor_01(&self) -> Vec2 {
(self.anchor + 1.0) / 2.0
}
@@ -67,6 +76,12 @@ impl UIRegion {
bot_right: UIPos::bottom_right(),
}
}
pub fn center(size: Vec2) -> Self {
Self {
top_left: UIPos::center().offset(-size / 2.0),
bot_right: UIPos::center().offset(size / 2.0),
}
}
pub fn within(&self, parent: &Self) -> Self {
Self {
top_left: self.top_left.within(parent),