From deaf7309012c318d120939156fcb9be8d815f0b6 Mon Sep 17 00:00:00 2001 From: shadow cat Date: Tue, 11 Nov 2025 13:55:36 -0500 Subject: [PATCH] app work --- TODO | 2 ++ src/core/position/pad.rs | 21 ++++++++++++++++++++- src/core/position/span.rs | 13 ++++++++++++- src/core/trait_fns.rs | 1 + src/layout/painter.rs | 16 ++++++++++++++++ src/layout/pos.rs | 14 +++++++++++++- src/layout/ui.rs | 13 +++++++++++++ src/layout/vec2.rs | 6 ++++++ src/layout/widget.rs | 4 ++++ 9 files changed, 87 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index b0e9aa9..ac8ddc3 100644 --- a/TODO +++ b/TODO @@ -8,6 +8,8 @@ text masks r just made to bare minimum work +move span lengths to the children themselves + scaling could be just a simple scaling factor that multiplies abs and need to ensure text uses raw abs and not scaled abs diff --git a/src/core/position/pad.rs b/src/core/position/pad.rs index 0b7f318..aba4503 100644 --- a/src/core/position/pad.rs +++ b/src/core/position/pad.rs @@ -38,7 +38,8 @@ pub struct Padding { } impl Padding { - pub fn uniform(amt: f32) -> Self { + pub fn uniform(amt: impl UiNum) -> Self { + let amt = amt.to_f32(); Self { left: amt, right: amt, @@ -54,6 +55,24 @@ impl Padding { region.bot_right.abs.y -= self.bottom; region } + pub fn x(amt: impl UiNum) -> Self { + let amt = amt.to_f32(); + Self { + left: amt, + right: amt, + top: 0.0, + bottom: 0.0, + } + } + pub fn y(amt: impl UiNum) -> Self { + let amt = amt.to_f32(); + Self { + left: 0.0, + right: 0.0, + top: amt, + bottom: amt, + } + } } impl From for Padding { diff --git a/src/core/position/span.rs b/src/core/position/span.rs index 5856672..3fa7de3 100644 --- a/src/core/position/span.rs +++ b/src/core/position/span.rs @@ -3,6 +3,7 @@ use crate::prelude::*; pub struct Span { pub children: Vec<(WidgetId, SpanLen)>, pub dir: Dir, + pub spacing: f32, } impl Widget for Span { @@ -36,6 +37,7 @@ impl Widget for Span { child_region.flip(self.dir.axis); } painter.widget_within(child, child_region); + start.abs += self.spacing; } } @@ -45,7 +47,10 @@ impl Widget for Span { let dir_len = if total.ratio != 0.0 { UiScalar::rel_max() } else { - UiScalar::new(total.rel, total.abs) + UiScalar::new( + total.rel, + total.abs + self.spacing * self.children.len().saturating_sub(1) as f32, + ) }; let mut max_ortho = UiScalar::ZERO; for (child, _) in &self.children { @@ -68,9 +73,15 @@ impl Span { Self { children: Vec::new(), dir, + spacing: 0.0, } } + pub fn spacing(mut self, spacing: impl UiNum) -> Self { + self.spacing = spacing.to_f32(); + self + } + fn setup(&mut self, ctx: &mut SizeCtx) -> SpanLenSums { self.children .iter_mut() diff --git a/src/core/trait_fns.rs b/src/core/trait_fns.rs index a009698..6a6cd3a 100644 --- a/src/core/trait_fns.rs +++ b/src/core/trait_fns.rs @@ -78,6 +78,7 @@ impl, Tag> CoreWidgetArr StackBuilder { diff --git a/src/layout/painter.rs b/src/layout/painter.rs index c9b2f68..3724fb7 100644 --- a/src/layout/painter.rs +++ b/src/layout/painter.rs @@ -44,6 +44,7 @@ pub struct WidgetInstance { pub resize: Option<(Id, UiVec2)>, pub mask: MaskIdx, pub layer: usize, + pub desired_size: UiVec2, } #[derive(Default)] @@ -178,6 +179,17 @@ impl<'a> PainterCtx<'a> { // draw widgets painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter); + let desired_size = SizeCtx { + text: painter.ctx.text, + textures: painter.ctx.textures, + widgets: painter.ctx.widgets, + checked: &mut Default::default(), + screen_size: painter.ctx.screen_size, + px_dependent: painter.ctx.px_dependent, + id: painter.id, + region: UiRegion::full(), + } + .size_raw(id); let sized_children = painter.sized_children; @@ -191,6 +203,7 @@ impl<'a> PainterCtx<'a> { children: painter.children, resize, mask: painter.mask, + desired_size, layer, }; for (cid, size) in sized_children { @@ -394,6 +407,9 @@ impl SizeCtx<'_> { // } self.size_inner(id.id, self.region) } + fn size_raw(&mut self, id: Id) -> UiVec2 { + self.size_inner(id, self.region) + } pub fn size_within(&mut self, id: &WidgetId, region: UiRegion) -> UiVec2 { self.size_inner(id.id, region.within(&self.region)) } diff --git a/src/layout/pos.rs b/src/layout/pos.rs index e6d6507..ff0bd59 100644 --- a/src/layout/pos.rs +++ b/src/layout/pos.rs @@ -1,4 +1,4 @@ -use std::marker::Destruct; +use std::{fmt::Display, marker::Destruct}; use crate::{ layout::{Align, Axis, UiNum, Vec2}, @@ -114,6 +114,12 @@ impl UiVec2 { } } +impl Display for UiVec2 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "rel {} / abs {}", self.rel, self.abs) + } +} + impl_op!(UiVec2 Add add; rel abs); impl_op!(UiVec2 Sub sub; rel abs); @@ -307,6 +313,12 @@ impl UiRegion { } } +impl Display for UiRegion { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} -> {}", self.top_left, self.bot_right) + } +} + #[derive(Debug)] pub struct ScreenRegion { pub top_left: Vec2, diff --git a/src/layout/ui.rs b/src/layout/ui.rs index 20dfe32..85447bf 100644 --- a/src/layout/ui.rs +++ b/src/layout/ui.rs @@ -170,6 +170,19 @@ impl Ui { font_system: &mut self.data.text.font_system, } } + + pub fn debug(&self, label: &str) { + for (id, inst) in &self.data.active { + let l = &self.data.widgets.data(id).unwrap().label; + if l != label { + continue; + } + println!("\"{label}\" {{"); + println!(" region: {}", inst.region); + println!(" desired_size: {}", inst.desired_size); + println!("}}"); + } + } } impl Index<&WidgetId> for Ui { diff --git a/src/layout/vec2.rs b/src/layout/vec2.rs index 344571e..190df0b 100644 --- a/src/layout/vec2.rs +++ b/src/layout/vec2.rs @@ -96,3 +96,9 @@ impl std::fmt::Debug for Vec2 { write!(f, "({}, {})", self.x, self.y) } } + +impl std::fmt::Display for Vec2 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "({}, {})", self.x, self.y) + } +} diff --git a/src/layout/widget.rs b/src/layout/widget.rs index f386bb1..0d39ffb 100644 --- a/src/layout/widget.rs +++ b/src/layout/widget.rs @@ -9,6 +9,10 @@ pub trait Widget: Any { } } +impl Widget for () { + fn draw(&mut self, painter: &mut Painter) {} +} + pub struct WidgetTag; pub struct FnTag;