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

@@ -1,4 +1,4 @@
use crate::{Painter, UINum, UiRegion, Widget, WidgetId}; use crate::{Painter, UiNum, UiRegion, Widget, WidgetId};
pub struct Regioned { pub struct Regioned {
pub region: UiRegion, pub region: UiRegion,
@@ -38,7 +38,7 @@ impl Padding {
} }
} }
impl<T: UINum> From<T> for Padding { impl<T: UiNum> From<T> for Padding {
fn from(amt: T) -> Self { fn from(amt: T) -> Self {
Self::uniform(amt.to_f32()) Self::uniform(amt.to_f32())
} }

View File

@@ -3,6 +3,7 @@ mod num;
mod rect; mod rect;
mod sense; mod sense;
mod span; mod span;
mod stack;
mod trait_fns; mod trait_fns;
pub use frame::*; pub use frame::*;
@@ -10,4 +11,5 @@ pub use num::*;
pub use rect::*; pub use rect::*;
pub use sense::*; pub use sense::*;
pub use span::*; pub use span::*;
pub use stack::*;
pub use trait_fns::*; pub use trait_fns::*;

View File

@@ -1,20 +1,20 @@
pub trait UINum { pub trait UiNum {
fn to_f32(self) -> f32; fn to_f32(self) -> f32;
} }
impl UINum for f32 { impl UiNum for f32 {
fn to_f32(self) -> f32 { fn to_f32(self) -> f32 {
self self
} }
} }
impl UINum for u32 { impl UiNum for u32 {
fn to_f32(self) -> f32 { fn to_f32(self) -> f32 {
self as f32 self as f32
} }
} }
impl UINum for i32 { impl UiNum for i32 {
fn to_f32(self) -> f32 { fn to_f32(self) -> f32 {
self as f32 self as f32
} }

View File

@@ -1,4 +1,4 @@
use crate::{Painter, UiColor, Widget, primitive::RoundedRectData}; use crate::{primitive::RoundedRectData, Painter, UiNum, UiColor, Widget};
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Rect { pub struct Rect {
@@ -21,6 +21,10 @@ impl Rect {
self.color = color; self.color = color;
self self
} }
pub fn radius(mut self, radius: impl UiNum) -> Self {
self.radius = radius.to_f32();
self
}
} }
impl<Ctx> Widget<Ctx> for Rect { impl<Ctx> Widget<Ctx> for Rect {

View File

@@ -4,7 +4,7 @@ pub trait SenseCtx: 'static {
fn active(&mut self, trigger: &SenseTrigger) -> bool; fn active(&mut self, trigger: &SenseTrigger) -> bool;
} }
pub trait Sensable<W, Ctx: SenseCtx, Tag> { pub trait Sensable<W, Ctx, Tag> {
fn sense( fn sense(
self, self,
sense: Sense, sense: Sense,
@@ -21,7 +21,7 @@ pub trait Sensable<W, Ctx: SenseCtx, Tag> {
W: Widget<Ctx>; W: Widget<Ctx>;
} }
impl<W: WidgetLike<Ctx, Tag>, Ctx: SenseCtx, Tag> Sensable<W::Widget, Ctx, Tag> for W { impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
fn sense(self, sense: Sense, f: impl SenseFn<Ctx> + Clone) -> impl WidgetIdFn<W::Widget, Ctx> { fn sense(self, sense: Sense, f: impl SenseFn<Ctx> + Clone) -> impl WidgetIdFn<W::Widget, Ctx> {
move |ui| { move |ui| {
let id = self.add(ui); let id = self.add(ui);

View File

@@ -1,4 +1,4 @@
use crate::{Dir, Painter, Sign, UINum, UiRegion, UIScalar, Widget, WidgetId}; use crate::{Dir, Painter, Sign, UiNum, UiRegion, UIScalar, Widget, WidgetId};
pub struct Span { pub struct Span {
pub children: Vec<(WidgetId, SpanLen)>, pub children: Vec<(WidgetId, SpanLen)>,
@@ -80,19 +80,19 @@ pub enum SpanLen {
Relative(f32), Relative(f32),
} }
pub fn fixed<N: UINum>(x: N) -> SpanLen { pub fn fixed<N: UiNum>(x: N) -> SpanLen {
SpanLen::Fixed(x.to_f32()) SpanLen::Fixed(x.to_f32())
} }
pub fn ratio<N: UINum>(x: N) -> SpanLen { pub fn ratio<N: UiNum>(x: N) -> SpanLen {
SpanLen::Ratio(x.to_f32()) SpanLen::Ratio(x.to_f32())
} }
pub fn rel<N: UINum>(x: N) -> SpanLen { pub fn rel<N: UiNum>(x: N) -> SpanLen {
SpanLen::Relative(x.to_f32()) SpanLen::Relative(x.to_f32())
} }
impl<N: UINum> From<N> for SpanLen { impl<N: UiNum> From<N> for SpanLen {
fn from(value: N) -> Self { fn from(value: N) -> Self {
Self::Ratio(value.to_f32()) Self::Ratio(value.to_f32())
} }

13
src/core/stack.rs Normal file
View File

@@ -0,0 +1,13 @@
use crate::{Widget, WidgetId};
pub struct Stack {
pub children: Vec<WidgetId>,
}
impl<Ctx> Widget<Ctx> for Stack {
fn draw(&self, painter: &mut crate::Painter<Ctx>) {
for child in &self.children {
painter.draw(child);
}
}
}

View File

@@ -1,12 +1,13 @@
use super::*; use super::*;
use crate::{UiRegion, Vec2, WidgetArrLike, WidgetFn, WidgetLike}; use crate::{UiRegion, Vec2, WidgetArrLike, WidgetFn, WidgetLike};
pub trait CoreWidget<Ctx: 'static, Tag> { pub trait CoreWidget<W: 'static, Ctx: 'static, Tag> {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx>; fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx>;
fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx>; fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx>;
fn region(self, region: UiRegion) -> impl WidgetFn<Regioned, Ctx>;
} }
impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<Ctx, Tag> for W { impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<W::Widget, Ctx, Tag> for W {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx> { fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Regioned, Ctx> {
|ui| Regioned { |ui| Regioned {
region: padding.into().region(), region: padding.into().region(),
@@ -16,7 +17,14 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<Ctx, Tag> for W {
fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx> { fn center(self, size: impl Into<Vec2>) -> impl WidgetFn<Regioned, Ctx> {
|ui| Regioned { |ui| Regioned {
region: UiRegion::center(size.into()), region: UiRegion::center().size(size.into()),
inner: self.add(ui).erase_type(),
}
}
fn region(self, region: UiRegion) -> impl WidgetFn<Regioned, Ctx> {
move |ui| Regioned {
region,
inner: self.add(ui).erase_type(), inner: self.add(ui).erase_type(),
} }
} }
@@ -24,10 +32,11 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<Ctx, Tag> for W {
pub trait CoreWidgetArr<const LEN: usize, Ctx: 'static, Tag> { pub trait CoreWidgetArr<const LEN: usize, Ctx: 'static, Tag> {
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx>; fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx>;
fn stack(self) -> impl WidgetFn<Stack, Ctx>;
} }
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx: 'static, Tag> CoreWidgetArr<LEN, Ctx, Tag> impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx: 'static, Tag>
for Wa CoreWidgetArr<LEN, Ctx, Tag> for Wa
{ {
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx> { fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetFn<Span, Ctx> {
let lengths = lengths.map(Into::into); let lengths = lengths.map(Into::into);
@@ -36,4 +45,9 @@ impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx: 'static, Tag> Core
children: self.ui(ui).arr.into_iter().zip(lengths).collect(), children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
} }
} }
fn stack(self) -> impl WidgetFn<Stack, Ctx> {
move |ui| Stack {
children: self.ui(ui).arr.to_vec(),
}
}
} }

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 YELLOW: Self = Self::rgb(T::MAX, T::MAX, T::MIN);
pub const LIME: Self = Self::rgb(T::MID, 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 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 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 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 MAGENTA: Self = Self::rgb(T::MAX, T::MIN, T::MAX);
pub const fn new(r: T, g: T, b: T, a: T) -> Self { 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])); .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 where
Ctx: 'static, Ctx: 'static,
{ {

View File

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

View File

@@ -147,7 +147,7 @@ impl<Ctx> Widgets<Ctx> {
Self(HashMap::new()) 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() 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::*; use std::ops::*;
#[repr(C)] #[repr(C)]
@@ -52,8 +52,11 @@ impl Neg for Vec2 {
} }
} }
impl From<(f32, f32)> for Vec2 { impl<T: UiNum, U: UiNum> From<(T, U)> for Vec2 {
fn from((x, y): (f32, f32)) -> Self { fn from((x, y): (T, U)) -> Self {
Self { x, y } Self {
x: x.to_f32(),
y: y.to_f32(),
}
} }
} }

View File

@@ -106,7 +106,21 @@ impl Client {
) )
.span(Dir::RIGHT, [1, 1, 1]), .span(Dir::RIGHT, [1, 1, 1]),
); );
ui.set_base((buttons, pad_test.pad(10).id(&main)).span(Dir::DOWN, [fixed(40), ratio(1)])); ui.set_base(
(
buttons,
(
pad_test.pad(10).id(&main),
Rect::new(Color::PURPLE).radius(30).region(
UiRegion::bottom_right()
.size((150, 150))
.shifted((-75, -75)),
),
)
.stack(),
)
.span(Dir::DOWN, [fixed(40), ratio(1)]),
);
(ui, UiIds { test }) (ui, UiIds { test })
} }

View File

@@ -26,7 +26,7 @@ macro_rules! impl_op {
} }
} }
} }
impl $opa for $T { impl const $opa for $T {
fn $fna(&mut self, rhs: Self) { fn $fna(&mut self, rhs: Self) {
$(self.$field.$fna(rhs.$field);)* $(self.$field.$fna(rhs.$field);)*
} }
@@ -49,7 +49,7 @@ macro_rules! impl_op {
} }
} }
} }
impl $opa<f32> for $T { impl const $opa<f32> for $T {
fn $fna(&mut self, rhs: f32) { fn $fna(&mut self, rhs: f32) {
$(self.$field.$fna(rhs);)* $(self.$field.$fna(rhs);)*
} }