idek stuff like stack

This commit is contained in:
2025-08-15 22:59:58 -04:00
parent a7dfacb83e
commit f4aef3a983
15 changed files with 138 additions and 42 deletions

View File

@@ -18,9 +18,11 @@ impl<T: ColorNum> Color<T> {
pub const YELLOW: Self = Self::rgb(T::MAX, T::MAX, T::MIN);
pub const LIME: Self = Self::rgb(T::MID, T::MAX, T::MIN);
pub const GREEN: Self = Self::rgb(T::MIN, T::MAX, T::MIN);
pub const TURQUOISE: Self = Self::rgb(T::MIN, T::MAX, T::MID);
pub const CYAN: Self = Self::rgb(T::MIN, T::MAX, T::MAX);
pub const SKY: Self = Self::rgb(T::MIN, T::MID, T::MAX);
pub const BLUE: Self = Self::rgb(T::MIN, T::MIN, T::MAX);
pub const INDIGO: Self = Self::rgb(T::MIN, T::MID, T::MAX);
pub const PURPLE: Self = Self::rgb(T::MID, T::MIN, T::MAX);
pub const MAGENTA: Self = Self::rgb(T::MAX, T::MIN, T::MAX);
pub const fn new(r: T, g: T, b: T, a: T) -> Self {

View File

@@ -40,7 +40,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data]));
}
pub fn draw(&mut self, id: &WidgetId)
pub fn draw<W>(&mut self, id: &WidgetId<W>)
where
Ctx: 'static,
{

View File

@@ -6,12 +6,12 @@ use crate::{
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable, Default)]
pub struct UIPos {
pub struct UiPos {
pub anchor: Vec2,
pub offset: Vec2,
}
impl UIPos {
impl UiPos {
pub const fn anchor_offset(anchor_x: f32, anchor_y: f32, offset_x: f32, offset_y: f32) -> Self {
Self {
anchor: vec2(anchor_x, anchor_y),
@@ -23,6 +23,10 @@ impl UIPos {
Self::anchor_offset(0.5, 0.5, 0.0, 0.0)
}
pub const fn anchor(anchor: Vec2) -> Self {
Self::anchor_offset(anchor.x, anchor.y, 0.0, 0.0)
}
pub const fn top_left() -> Self {
Self::anchor_offset(0.0, 0.0, 0.0, 0.0)
}
@@ -31,12 +35,16 @@ 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;
pub const fn shift(&mut self, offset: Vec2) {
self.offset += offset;
}
pub const fn shifted(mut self, offset: Vec2) -> Self {
self.shift(offset);
self
}
pub const fn within(&self, region: &UiRegion) -> UIPos {
pub const fn within(&self, region: &UiRegion) -> UiPos {
let anchor = self
.anchor
.lerp(region.top_left.anchor, region.bot_right.anchor);
@@ -44,7 +52,7 @@ impl UIPos {
+ self
.anchor
.lerp(region.top_left.offset, region.bot_right.offset);
UIPos { anchor, offset }
UiPos { anchor, offset }
}
pub fn axis_mut(&mut self, axis: Axis) -> UIScalarView<'_> {
@@ -111,23 +119,32 @@ impl UIScalar {
#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct UiRegion {
pub top_left: UIPos,
pub bot_right: UIPos,
pub top_left: UiPos,
pub bot_right: UiPos,
}
impl UiRegion {
pub const fn full() -> Self {
Self {
top_left: UIPos::top_left(),
bot_right: UIPos::bottom_right(),
top_left: UiPos::top_left(),
bot_right: UiPos::bottom_right(),
}
}
pub fn center(size: Vec2) -> Self {
pub fn center() -> Self {
Self::anchor(Vec2::new(0.5, 0.5))
}
pub fn anchor(anchor: Vec2) -> Self {
Self {
top_left: UIPos::center().offset(-size / 2.0),
bot_right: UIPos::center().offset(size / 2.0),
top_left: UiPos::anchor(anchor),
bot_right: UiPos::anchor(anchor),
}
}
pub fn size(mut self, size: impl Into<Vec2>) -> Self {
let size = size.into();
self.top_left = self.top_left.shifted(-size / 2.0);
self.bot_right = self.bot_right.shifted(size / 2.0);
self
}
pub fn within(&self, parent: &Self) -> Self {
Self {
top_left: self.top_left.within(parent),
@@ -150,6 +167,33 @@ impl UiRegion {
std::mem::swap(&mut self.top_left, &mut self.bot_right);
}
pub fn shift(&mut self, offset: impl Into<Vec2>) {
let offset = offset.into();
self.top_left.shift(offset);
self.bot_right.shift(offset);
}
pub fn shifted(mut self, offset: impl Into<Vec2>) -> Self {
println!("before {:?}", self);
self.shift(offset);
println!("after {:?}", self);
self
}
pub fn top_left() -> Self {
Self {
top_left: UiPos::top_left(),
bot_right: UiPos::top_left(),
}
}
pub fn bottom_right() -> Self {
Self {
top_left: UiPos::bottom_right(),
bot_right: UiPos::bottom_right(),
}
}
pub fn to_screen(&self, size: Vec2) -> ScreenRect {
ScreenRect {
top_left: self.top_left.anchor * size + self.top_left.offset,

View File

@@ -147,7 +147,7 @@ impl<Ctx> Widgets<Ctx> {
Self(HashMap::new())
}
pub fn get_dyn(&self, id: &WidgetId) -> &dyn Widget<Ctx> {
pub fn get_dyn<W>(&self, id: &WidgetId<W>) -> &dyn Widget<Ctx> {
self.0.get(&id.id).unwrap().as_ref()
}

View File

@@ -1,4 +1,4 @@
use crate::util::{F32Util, impl_op};
use crate::{util::{impl_op, F32Util}, UiNum};
use std::ops::*;
#[repr(C)]
@@ -52,8 +52,11 @@ impl Neg for Vec2 {
}
}
impl From<(f32, f32)> for Vec2 {
fn from((x, y): (f32, f32)) -> Self {
Self { x, y }
impl<T: UiNum, U: UiNum> From<(T, U)> for Vec2 {
fn from((x, y): (T, U)) -> Self {
Self {
x: x.to_f32(),
y: y.to_f32(),
}
}
}