move widgets on draw if region size is same

This commit is contained in:
2025-09-27 16:11:30 -04:00
parent 5f2dffc189
commit 95f049acb4
13 changed files with 204 additions and 176 deletions

View File

@@ -1,6 +1,6 @@
use crate::{
layout::{Align, Axis, Vec2},
util::{F32Util, impl_op},
util::{LerpUtil, impl_op},
};
#[repr(C)]
@@ -52,23 +52,26 @@ impl UiVec2 {
}
pub const fn within(&self, region: &UiRegion) -> UiVec2 {
let anchor = self.rel.lerp(region.top_left.rel, region.bot_right.rel);
let offset = self.abs + self.rel.lerp(region.top_left.abs, region.bot_right.abs);
UiVec2 {
rel: anchor,
abs: offset,
}
let rel = self.rel.lerp(region.top_left.rel, region.bot_right.rel);
let abs = self.abs + self.rel.lerp(region.top_left.abs, region.bot_right.abs);
UiVec2 { rel, abs }
}
pub const fn outside(&self, region: &UiRegion) -> UiVec2 {
let rel = self.rel.lerp_inv(region.top_left.rel, region.bot_right.rel);
let abs = self.abs - rel.lerp(region.top_left.abs, region.bot_right.abs);
UiVec2 { rel, abs }
}
pub fn axis_mut(&mut self, axis: Axis) -> UiScalarView<'_> {
match axis {
Axis::X => UiScalarView {
anchor: &mut self.rel.x,
offset: &mut self.abs.x,
rel: &mut self.rel.x,
abs: &mut self.abs.x,
},
Axis::Y => UiScalarView {
anchor: &mut self.rel.y,
offset: &mut self.abs.y,
rel: &mut self.rel.y,
abs: &mut self.abs.y,
},
}
}
@@ -110,6 +113,9 @@ impl UiVec2 {
}
}
impl_op!(UiVec2 Add add; rel abs);
impl_op!(UiVec2 Sub sub; rel abs);
impl const From<Align> for UiVec2 {
fn from(align: Align) -> Self {
Self::anchor(align.anchor())
@@ -204,6 +210,12 @@ impl UiRegion {
bot_right: self.bot_right.within(parent),
}
}
pub fn outside(&self, parent: &Self) -> Self {
Self {
top_left: self.top_left.outside(parent),
bot_right: self.bot_right.outside(parent),
}
}
pub fn axis_mut(&mut self, axis: Axis) -> UIRegionAxisView<'_> {
UIRegionAxisView {
top_left: self.top_left.axis_mut(axis),
@@ -243,6 +255,10 @@ impl UiRegion {
self.bot_right.to_size(size) - self.top_left.to_size(size)
}
pub fn size(&self) -> UiVec2 {
self.bot_right - self.top_left
}
pub fn select_aligned(&self, size: Vec2, align: Align) -> Self {
Self::from_size_align(size, align).within(self)
}
@@ -292,19 +308,19 @@ pub struct UIRegionAxisView<'a> {
}
pub struct UiScalarView<'a> {
pub anchor: &'a mut f32,
pub offset: &'a mut f32,
pub rel: &'a mut f32,
pub abs: &'a mut f32,
}
impl UiScalarView<'_> {
pub fn set(&mut self, scalar: UiScalar) {
*self.anchor = scalar.rel;
*self.offset = scalar.abs;
*self.rel = scalar.rel;
*self.abs = scalar.abs;
}
pub fn get(self) -> UiScalar {
UiScalar {
rel: *self.anchor,
abs: *self.offset,
rel: *self.rel,
abs: *self.abs,
}
}
}