Compare commits

2 Commits

Author SHA1 Message Date
96ef0c529b remove debug prints 2025-11-20 00:19:45 -05:00
a952b34a72 mistakes were fixed and sins were committed 2025-11-20 00:18:30 -05:00
18 changed files with 413 additions and 142 deletions

View File

@@ -10,8 +10,12 @@ impl Widget for Image {
painter.texture(&self.handle); painter.texture(&self.handle);
} }
fn desired_size(&mut self, _: &mut SizeCtx) -> Size { fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
Size::abs(self.handle.size()) Len::abs(self.handle.size().x)
}
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
Len::abs(self.handle.size().y)
} }
} }

View File

@@ -9,4 +9,12 @@ impl Widget for Masked {
painter.set_mask(painter.region()); painter.set_mask(painter.region());
painter.widget(&self.inner); painter.widget(&self.inner);
} }
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
} }

View File

@@ -7,11 +7,16 @@ pub struct Aligned {
impl Widget for Aligned { impl Widget for Aligned {
fn draw(&mut self, painter: &mut Painter) { fn draw(&mut self, painter: &mut Painter) {
let region = UiRegion::from_ui_size_align(painter.region_size(&self.inner), self.align); let region =
UiRegion::from_ui_size_align(painter.size(&self.inner).to_uivec2(), self.align);
painter.widget_within(&self.inner, region); painter.widget_within(&self.inner, region);
} }
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.size(&self.inner) ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
} }
} }

View File

@@ -11,7 +11,11 @@ impl Widget for Offset {
painter.widget_within(&self.inner, region); painter.widget_within(&self.inner, region);
} }
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.size(&self.inner) ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
} }
} }

View File

@@ -10,14 +10,23 @@ impl Widget for Pad {
painter.widget_within(&self.inner, self.padding.region()); painter.widget_within(&self.inner, self.padding.region());
} }
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
let mut size = ctx.size(&self.inner); let width = self.padding.left + self.padding.right;
if size.x.rest == 0.0 { let height = self.padding.top + self.padding.bottom;
size.x.abs += self.padding.left + self.padding.right; ctx.outer.abs.x -= width;
} ctx.outer.abs.y -= height;
if size.y.rest == 0.0 { let mut size = ctx.width(&self.inner);
size.y.abs += self.padding.top + self.padding.bottom; size.abs += width;
size
} }
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
let width = self.padding.left + self.padding.right;
let height = self.padding.top + self.padding.bottom;
ctx.outer.abs.x -= width;
ctx.outer.abs.y -= height;
let mut size = ctx.height(&self.inner);
size.abs += height;
size size
} }
} }

View File

@@ -6,22 +6,35 @@ pub struct Sized {
pub y: Option<Len>, pub y: Option<Len>,
} }
impl Sized {
fn apply_to_outer(&self, ctx: &mut SizeCtx) {
if let Some(x) = self.x {
let outer = ctx.outer.axis(Axis::X);
ctx.outer
.axis_mut(Axis::X)
.set(x.apply_rest().within_len(outer));
}
if let Some(y) = self.y {
let outer = ctx.outer.axis(Axis::Y);
ctx.outer
.axis_mut(Axis::Y)
.set(y.apply_rest().within_len(outer));
}
}
}
impl Widget for Sized { impl Widget for Sized {
fn draw(&mut self, painter: &mut Painter) { fn draw(&mut self, painter: &mut Painter) {
painter.widget(&self.inner); painter.widget(&self.inner);
} }
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
let rel = ctx.size.rel; self.apply_to_outer(ctx);
if let Some(x) = self.x { self.x.unwrap_or_else(|| ctx.width(&self.inner))
ctx.size.axis_mut(Axis::X).set(x.apply_rest(rel.x));
}
if let Some(y) = self.y {
ctx.size.axis_mut(Axis::Y).set(y.apply_rest(rel.y));
}
Size {
x: self.x.unwrap_or_else(|| ctx.size(&self.inner).x),
y: self.y.unwrap_or_else(|| ctx.size(&self.inner).y),
} }
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
self.apply_to_outer(ctx);
self.y.unwrap_or_else(|| ctx.height(&self.inner))
} }
} }

View File

@@ -16,7 +16,7 @@ impl Widget for Span {
let mut child_region = UiRegion::full(); let mut child_region = UiRegion::full();
let mut axis = child_region.axis_mut(self.dir.axis); let mut axis = child_region.axis_mut(self.dir.axis);
axis.top_left.set(start); axis.top_left.set(start);
let len = painter.size(child).axis(self.dir.axis); let len = painter.len_axis(child, self.dir.axis);
if len.rest > 0.0 { if len.rest > 0.0 {
let offset = UiScalar::new(total.rel, total.abs); let offset = UiScalar::new(total.rel, total.abs);
let rel_end = UiScalar::from_anchor(len.rest / total.rest); let rel_end = UiScalar::from_anchor(len.rest / total.rest);
@@ -33,25 +33,18 @@ impl Widget for Span {
} }
} }
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
let sums = self.len_sum(ctx); match self.dir.axis {
let dir_len = if sums.rest == 0.0 && sums.rel == 0.0 { Axis::X => self.desired_len(ctx),
sums Axis::Y => self.desired_ortho(ctx),
} else {
Len::default()
};
let mut max_ortho = Len::ZERO;
for child in &self.children {
let len = ctx.size(child).axis(!self.dir.axis);
// TODO: rel shouldn't do this, but no easy way before actually calculating pixels
if len.rel > 0.0 || len.rest > 0.0 {
max_ortho.rest = 1.0;
max_ortho.abs = 0.0;
break;
} }
max_ortho.abs = max_ortho.abs.max(len.abs);
} }
Size::from_axis(self.dir.axis, dir_len, max_ortho)
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
match self.dir.axis {
Axis::X => self.desired_ortho(ctx),
Axis::Y => self.desired_len(ctx),
}
} }
} }
@@ -71,11 +64,85 @@ impl Span {
fn len_sum(&mut self, ctx: &mut SizeCtx) -> Len { fn len_sum(&mut self, ctx: &mut SizeCtx) -> Len {
let gap = self.gap * self.children.len().saturating_sub(1) as f32; let gap = self.gap * self.children.len().saturating_sub(1) as f32;
self.children.iter_mut().fold(Len::abs(gap), |mut s, id| { self.children.iter().fold(Len::abs(gap), |mut s, id| {
s += ctx.size(id).axis(self.dir.axis); // it's tempting to subtract the abs & rel from the ctx outer,
// but that would create inconsistent sizing if you put
// a rest first vs last & only speed up in one direction.
// I think this is only solvable by restricting how you can
// compute size, bc currently you need child to define parent's
// sectioning and you need parent's sectioning to define child.
// Fortunately, that doesn't matter in most cases
let len = ctx.len_axis(id, self.dir.axis);
s += len;
s s
}) })
} }
fn desired_len(&mut self, ctx: &mut SizeCtx) -> Len {
let len = self.len_sum(ctx);
if len.rest == 0.0 && len.rel == 0.0 {
len
} else {
Len::default()
}
}
fn desired_ortho(&mut self, ctx: &mut SizeCtx) -> Len {
// this is an awful hack to get text wrapping to work properly when in a downward span
if self.dir.axis == Axis::X {
// so....... this literally copies draw so that the lengths are correctly set in the
// context, which makes this slow and not cool
let total = self.len_sum(ctx);
let mut start = UiScalar::rel_min();
let outer = ctx.outer.axis(self.dir.axis);
let mut ortho_len = Len::ZERO;
for child in &self.children {
let mut child_region = UiRegion::full();
let mut axis = child_region.axis_mut(self.dir.axis);
axis.top_left.set(start);
let len = ctx.len_axis(child, self.dir.axis);
if len.rest > 0.0 {
let offset = UiScalar::new(total.rel, total.abs);
let rel_end = UiScalar::from_anchor(len.rest / total.rest);
start = rel_end.within(start, (UiScalar::rel_max() + start) - offset);
}
start.abs += len.abs;
start.rel += len.rel;
axis.bot_right.set(start);
// if self.dir.sign == Sign::Neg {
// child_region.flip(self.dir.axis);
// }
let scalar = child_region.size().axis(self.dir.axis);
ctx.outer
.axis_mut(self.dir.axis)
.set(scalar.within_len(outer));
let ortho = ctx.len_axis(child, !self.dir.axis);
// TODO: rel shouldn't do this, but no easy way before actually calculating pixels
if ortho.rel > 0.0 || ortho.rest > 0.0 {
ortho_len.rest = 1.0;
ortho_len.abs = 0.0;
break;
}
ortho_len.abs = ortho_len.abs.max(ortho.abs);
start.abs += self.gap;
}
ortho_len
} else {
let mut ortho_len = Len::ZERO;
let ortho = !self.dir.axis;
for child in &self.children {
let len = ctx.len_axis(child, ortho);
// TODO: rel shouldn't do this, but no easy way before actually calculating pixels
if len.rel > 0.0 || len.rest > 0.0 {
ortho_len.rest = 1.0;
ortho_len.abs = 0.0;
break;
}
ortho_len.abs = ortho_len.abs.max(len.abs);
}
ortho_len
}
}
} }
pub struct SpanBuilder<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> { pub struct SpanBuilder<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> {

View File

@@ -24,10 +24,17 @@ impl Widget for Stack {
} }
} }
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
match self.size { match self.size {
StackSize::Default => Size::default(), StackSize::Default => Len::default(),
StackSize::Child(i) => ctx.size(&self.children[i]), StackSize::Child(i) => ctx.width(&self.children[i]),
}
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
match self.size {
StackSize::Default => Len::default(),
StackSize::Child(i) => ctx.height(&self.children[i]),
} }
} }
} }

View File

@@ -12,11 +12,19 @@ impl Widget for WidgetPtr {
} }
} }
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
if let Some(id) = &self.inner { if let Some(id) = &self.inner {
ctx.size(id) ctx.width(id)
} else { } else {
Size::ZERO Len::ZERO
}
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
if let Some(id) = &self.inner {
ctx.height(id)
} else {
Len::ZERO
} }
} }
} }

View File

@@ -36,6 +36,14 @@ impl Widget for Rect {
inner_radius: self.inner_radius, inner_radius: self.inner_radius,
}); });
} }
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
Len::rest(1)
}
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
Len::rest(1)
}
} }
pub fn rect(color: UiColor) -> Rect { pub fn rect(color: UiColor) -> Rect {

View File

@@ -51,8 +51,12 @@ impl Widget for TextEdit {
} }
} }
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
Size::abs(self.view.draw(ctx).size()) Len::abs(self.view.draw(ctx).size().x)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
Len::abs(self.view.draw(ctx).size().y)
} }
} }

View File

@@ -91,8 +91,12 @@ impl Widget for Text {
painter.texture_within(&tex.handle, region); painter.texture_within(&tex.handle, region);
} }
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
Size::abs(self.update_buf(ctx).size()) Len::abs(self.update_buf(ctx).size().x)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
Len::abs(self.update_buf(ctx).size().y)
} }
} }

View File

@@ -136,6 +136,7 @@ impl Size {
x: Len::ZERO, x: Len::ZERO,
y: Len::ZERO, y: Len::ZERO,
}; };
pub fn abs(v: Vec2) -> Self { pub fn abs(v: Vec2) -> Self {
Self { Self {
x: Len::abs(v.x), x: Len::abs(v.x),
@@ -143,8 +144,22 @@ impl Size {
} }
} }
pub fn to_uivec2(self, rel_len: Vec2) -> UiVec2 { pub fn rel(v: Vec2) -> Self {
UiVec2::from_scalars(self.x.apply_rest(rel_len.x), self.y.apply_rest(rel_len.y)) Self {
x: Len::rel(v.x),
y: Len::rel(v.y),
}
}
pub fn rest(v: Vec2) -> Self {
Self {
x: Len::rest(v.x),
y: Len::rest(v.y),
}
}
pub fn to_uivec2(self) -> UiVec2 {
UiVec2::from_scalars(self.x.apply_rest(), self.y.apply_rest())
} }
pub fn from_axis(axis: Axis, aligned: Len, ortho: Len) -> Self { pub fn from_axis(axis: Axis, aligned: Len, ortho: Len) -> Self {
@@ -175,14 +190,10 @@ impl Len {
rest: 0.0, rest: 0.0,
}; };
pub fn apply_rest(&self, rel_len: f32) -> UiScalar { pub fn apply_rest(&self) -> UiScalar {
UiScalar { UiScalar {
rel: if self.rest > 0.0 { rel: self.rel + if self.rest > 0.0 { 1.0 } else { 0.0 },
self.rel.max(1.0) abs: self.abs,
} else {
self.rel
} * rel_len,
abs: if self.rest > 0.0 { 0.0 } else { self.abs },
} }
} }

View File

@@ -1,7 +1,7 @@
use crate::{ use crate::{
layout::{ layout::{
Layers, Modules, Size, TextAttrs, TextBuffer, TextData, TextTexture, TextureHandle, Axis, Layers, Len, Modules, Size, TextAttrs, TextBuffer, TextData, TextTexture,
Textures, UiRegion, UiVec2, Vec2, WidgetId, Widgets, TextureHandle, Textures, UiRegion, UiVec2, Vec2, WidgetId, Widgets,
}, },
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst}, render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
util::{HashMap, HashSet, Id, TrackedArena}, util::{HashMap, HashSet, Id, TrackedArena},
@@ -14,7 +14,8 @@ pub struct Painter<'a, 'c> {
textures: Vec<TextureHandle>, textures: Vec<TextureHandle>,
primitives: Vec<PrimitiveHandle>, primitives: Vec<PrimitiveHandle>,
children: Vec<Id>, children: Vec<Id>,
sized_children: HashMap<Id, Size>, children_width: HashMap<Id, (UiVec2, Len)>,
children_height: HashMap<Id, (UiVec2, Len)>,
/// whether this widget depends on region's final pixel size or not /// whether this widget depends on region's final pixel size or not
/// TODO: decide if point (pt) should be used here instead of px /// TODO: decide if point (pt) should be used here instead of px
pub layer: usize, pub layer: usize,
@@ -31,9 +32,17 @@ pub struct PainterCtx<'a> {
pub screen_size: Vec2, pub screen_size: Vec2,
pub modules: &'a mut Modules, pub modules: &'a mut Modules,
pub px_dependent: &'a mut HashSet<Id>, pub px_dependent: &'a mut HashSet<Id>,
pub cache_width: HashMap<Id, (UiVec2, Len)>,
pub cache_height: HashMap<Id, (UiVec2, Len)>,
draw_started: HashSet<Id>, draw_started: HashSet<Id>,
} }
#[derive(Default)]
pub struct ResizeRef {
x: Option<(Id, (UiVec2, Len))>,
y: Option<(Id, (UiVec2, Len))>,
}
pub struct WidgetInstance { pub struct WidgetInstance {
pub id: Id, pub id: Id,
pub region: UiRegion, pub region: UiRegion,
@@ -41,10 +50,9 @@ pub struct WidgetInstance {
pub textures: Vec<TextureHandle>, pub textures: Vec<TextureHandle>,
pub primitives: Vec<PrimitiveHandle>, pub primitives: Vec<PrimitiveHandle>,
pub children: Vec<Id>, pub children: Vec<Id>,
pub resize: Option<Id>, pub resize: ResizeRef,
pub mask: MaskIdx, pub mask: MaskIdx,
pub layer: usize, pub layer: usize,
pub desired_size: Size,
} }
#[derive(Default)] #[derive(Default)]
@@ -72,7 +80,9 @@ impl<'a> PainterCtx<'a> {
modules: &mut data.modules, modules: &mut data.modules,
px_dependent: &mut data.px_dependent, px_dependent: &mut data.px_dependent,
masks: &mut data.masks, masks: &mut data.masks,
draw_started: HashSet::default(), cache_width: Default::default(),
cache_height: Default::default(),
draw_started: Default::default(),
} }
} }
@@ -84,19 +94,51 @@ impl<'a> PainterCtx<'a> {
return; return;
}; };
if let Some(rid) = active.resize { // TODO: this is stupid having 2 of these
let desired = SizeCtx { if let Some((rid, (outer, old_desired))) = active.resize.x {
checked: &mut Default::default(), let new_desired = SizeCtx {
source: id,
cache_width: &mut self.cache_width,
cache_height: &mut self.cache_height,
text: self.text, text: self.text,
textures: self.textures, textures: self.textures,
widgets: self.widgets, widgets: self.widgets,
size: UiVec2::FULL_SIZE, outer,
screen_size: self.screen_size, output_size: self.screen_size,
checked_width: &mut Default::default(),
checked_height: &mut Default::default(),
px_dependent: &mut Default::default(), px_dependent: &mut Default::default(),
id, id,
} }
.size_inner(id, active.region.size()); .width_inner(id);
if active.desired_size != desired { if new_desired != old_desired {
self.redraw(rid);
if self.draw_started.contains(&id) {
return;
}
}
}
let Some(active) = self.active.get(&id) else {
return;
};
if let Some((rid, (outer, old_desired))) = active.resize.y {
let new_desired = SizeCtx {
source: id,
cache_width: &mut self.cache_width,
cache_height: &mut self.cache_height,
text: self.text,
textures: self.textures,
widgets: self.widgets,
outer,
output_size: self.screen_size,
checked_width: &mut Default::default(),
checked_height: &mut Default::default(),
px_dependent: &mut Default::default(),
id,
}
.height_inner(id);
if new_desired != old_desired {
self.redraw(rid); self.redraw(rid);
if self.draw_started.contains(&id) { if self.draw_started.contains(&id) {
return; return;
@@ -147,7 +189,7 @@ impl<'a> PainterCtx<'a> {
); );
} }
let mut old_children = old_children.unwrap_or_default(); let mut old_children = old_children.unwrap_or_default();
let mut resize = None; let mut resize = ResizeRef::default();
if let Some(active) = self.active.get_mut(&id) { if let Some(active) = self.active.get_mut(&id) {
if active.parent != parent { if active.parent != parent {
panic!("Cannot draw the same widget twice (2)"); panic!("Cannot draw the same widget twice (2)");
@@ -167,18 +209,6 @@ impl<'a> PainterCtx<'a> {
self.draw_started.insert(id); self.draw_started.insert(id);
let desired_size = SizeCtx {
text: self.text,
textures: self.textures,
widgets: self.widgets,
checked: &mut Default::default(),
screen_size: self.screen_size,
px_dependent: &mut Default::default(),
id,
size: region.size(),
}
.size_raw(id);
let mut painter = Painter { let mut painter = Painter {
region, region,
mask, mask,
@@ -188,13 +218,15 @@ impl<'a> PainterCtx<'a> {
primitives: Vec::new(), primitives: Vec::new(),
ctx: self, ctx: self,
children: Vec::new(), children: Vec::new(),
sized_children: Default::default(), children_width: Default::default(),
children_height: Default::default(),
}; };
// draw widgets // draw widgets
painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter); painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter);
let sized_children = painter.sized_children; let children_width = painter.children_width;
let children_height = painter.children_height;
// add to active // add to active
let instance = WidgetInstance { let instance = WidgetInstance {
@@ -206,14 +238,20 @@ impl<'a> PainterCtx<'a> {
children: painter.children, children: painter.children,
resize, resize,
mask: painter.mask, mask: painter.mask,
desired_size,
layer, layer,
}; };
for cid in sized_children.keys() { for (cid, outer) in children_width {
if let Some(w) = self.active.get_mut(cid) if let Some(w) = self.active.get_mut(&cid)
&& w.resize.is_none() && w.resize.x.is_none()
{ {
w.resize = Some(id) w.resize.x = Some((id, outer))
}
}
for (cid, outer) in children_height {
if let Some(w) = self.active.get_mut(&cid)
&& w.resize.y.is_none()
{
w.resize.y = Some((id, outer))
} }
} }
for c in &old_children { for c in &old_children {
@@ -230,8 +268,6 @@ impl<'a> PainterCtx<'a> {
fn mov(&mut self, id: Id, from: UiRegion, to: UiRegion) { fn mov(&mut self, id: Id, from: UiRegion, to: UiRegion) {
let active = self.active.get_mut(&id).unwrap(); let active = self.active.get_mut(&id).unwrap();
// children will not be changed, so this technically should not be needed
// probably need unsafe
for h in &active.primitives { for h in &active.primitives {
let region = self.layers[h.layer].primitives.region_mut(h); let region = self.layers[h.layer].primitives.region_mut(h);
*region = region.outside(&from).within(&to); *region = region.outside(&from).within(&to);
@@ -240,6 +276,8 @@ impl<'a> PainterCtx<'a> {
for m in self.modules.iter_mut() { for m in self.modules.iter_mut() {
m.on_move(active); m.on_move(active);
} }
// children will not be changed, so this technically should not be needed
// probably need unsafe
let children = active.children.clone(); let children = active.children.clone();
for child in children { for child in children {
self.mov(child, from, to); self.mov(child, from, to);
@@ -354,8 +392,11 @@ impl<'a, 'c> Painter<'a, 'c> {
self.size_ctx().size(id) self.size_ctx().size(id)
} }
pub fn region_size<W>(&mut self, id: &WidgetId<W>) -> UiVec2 { pub fn len_axis<W>(&mut self, id: &WidgetId<W>, axis: Axis) -> Len {
self.size_ctx().size(id).to_uivec2(self.region.size().rel) match axis {
Axis::X => self.size_ctx().width(id),
Axis::Y => self.size_ctx().height(id),
}
} }
pub fn size_ctx(&mut self) -> SizeCtx<'_> { pub fn size_ctx(&mut self) -> SizeCtx<'_> {
@@ -363,11 +404,15 @@ impl<'a, 'c> Painter<'a, 'c> {
text: self.ctx.text, text: self.ctx.text,
textures: self.ctx.textures, textures: self.ctx.textures,
widgets: self.ctx.widgets, widgets: self.ctx.widgets,
checked: &mut self.sized_children, output_size: self.ctx.screen_size,
screen_size: self.ctx.screen_size,
px_dependent: self.ctx.px_dependent, px_dependent: self.ctx.px_dependent,
checked_width: &mut self.children_width,
checked_height: &mut self.children_height,
cache_width: &mut self.ctx.cache_width,
cache_height: &mut self.ctx.cache_height,
source: self.id,
id: self.id, id: self.id,
size: self.region.size(), outer: self.region.size(),
} }
} }
@@ -396,41 +441,98 @@ impl<'a, 'c> Painter<'a, 'c> {
pub struct SizeCtx<'a> { pub struct SizeCtx<'a> {
pub text: &'a mut TextData, pub text: &'a mut TextData,
pub textures: &'a mut Textures, pub textures: &'a mut Textures,
source: Id,
widgets: &'a Widgets, widgets: &'a Widgets,
px_dependent: &'a mut HashSet<Id>, px_dependent: &'a mut HashSet<Id>,
checked: &'a mut HashMap<Id, Size>, cache_width: &'a mut HashMap<Id, (UiVec2, Len)>,
cache_height: &'a mut HashMap<Id, (UiVec2, Len)>,
checked_width: &'a mut HashMap<Id, (UiVec2, Len)>,
checked_height: &'a mut HashMap<Id, (UiVec2, Len)>,
/// TODO: should this be pub? rn used for sized /// TODO: should this be pub? rn used for sized
pub size: UiVec2, pub outer: UiVec2,
screen_size: Vec2, output_size: Vec2,
id: Id, id: Id,
} }
impl SizeCtx<'_> { impl SizeCtx<'_> {
fn size_inner(&mut self, id: Id, size: UiVec2) -> Size { pub fn id(&self) -> &Id {
let self_size = self.size; &self.id
self.size = size;
let size = self.widgets.get_dyn_dynamic(id).desired_size(self);
self.size = self_size;
self.checked.insert(id, size);
size
} }
pub fn source(&self) -> &Id {
&self.source
}
fn width_inner(&mut self, id: Id) -> Len {
if let Some(&(outer, len)) = self.cache_width.get(&id)
&& outer == self.outer
{
self.checked_width.insert(id, (self.outer, len));
return len;
}
let self_outer = self.outer;
let self_id = self.id;
self.id = id;
let len = self.widgets.get_dyn_dynamic(id).desired_width(self);
self.outer = self_outer;
self.id = self_id;
self.cache_width.insert(id, (self.outer, len));
self.checked_width.insert(id, (self.outer, len));
len
}
// TODO: should be refactored to share code w width_inner
fn height_inner(&mut self, id: Id) -> Len {
if let Some(&(outer, len)) = self.cache_height.get(&id)
&& outer == self.outer
{
self.checked_height.insert(id, (self.outer, len));
return len;
}
let self_outer = self.outer;
let self_id = self.id;
self.id = id;
let len = self.widgets.get_dyn_dynamic(id).desired_height(self);
self.outer = self_outer;
self.id = self_id;
self.cache_height.insert(id, (self.outer, len));
self.checked_height.insert(id, (self.outer, len));
len
}
pub fn width<W>(&mut self, id: &WidgetId<W>) -> Len {
self.width_inner(id.id)
}
pub fn height<W>(&mut self, id: &WidgetId<W>) -> Len {
self.height_inner(id.id)
}
pub fn len_axis<W>(&mut self, id: &WidgetId<W>, axis: Axis) -> Len {
match axis {
Axis::X => self.width(id),
Axis::Y => self.height(id),
}
}
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Size { pub fn size<W>(&mut self, id: &WidgetId<W>) -> Size {
// if let Some(&size) = self.checked.get(&id.id) { Size {
// return size; x: self.width(id),
// } y: self.height(id),
self.size_inner(id.id, self.size)
} }
fn size_raw(&mut self, id: Id) -> Size {
self.size_inner(id, self.size)
} }
pub fn px_size(&mut self) -> Vec2 { pub fn px_size(&mut self) -> Vec2 {
self.px_dependent.insert(self.id); // WARNING IF UNCOMMENT: self.id is no longer valid
self.size.to_abs(self.screen_size) // self.px_dependent.insert(self.id);
self.outer.to_abs(self.output_size)
} }
pub fn draw_text(&mut self, buffer: &mut TextBuffer, attrs: &TextAttrs) -> TextTexture { pub fn draw_text(&mut self, buffer: &mut TextBuffer, attrs: &TextAttrs) -> TextTexture {
self.text.draw(buffer, attrs, self.textures) self.text.draw(buffer, attrs, self.textures)
} }
pub fn label(&self) -> &String {
self.widgets.label(&self.id) pub fn label(&self, id: &Id) -> &String {
self.widgets.label(id)
} }
} }

View File

@@ -6,7 +6,7 @@ use crate::{
}; };
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, bytemuck::Pod, bytemuck::Zeroable, Default)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, bytemuck::Pod, bytemuck::Zeroable, Default)]
pub struct UiVec2 { pub struct UiVec2 {
pub rel: Vec2, pub rel: Vec2,
pub abs: Vec2, pub abs: Vec2,
@@ -123,7 +123,7 @@ impl UiVec2 {
impl Display for UiVec2 { impl Display for UiVec2 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "rel {} / abs {}", self.rel, self.abs) write!(f, "rel{};abs{}", self.rel, self.abs)
} }
} }
@@ -212,6 +212,10 @@ impl UiScalar {
abs: offset, abs: offset,
} }
} }
pub fn within_len(&self, len: UiScalar) -> Self {
self.within(UiScalar::ZERO, len)
}
} }
#[repr(C)] #[repr(C)]

View File

@@ -6,7 +6,7 @@ use crate::{
IdLike, PainterCtx, PainterData, PixelRegion, StaticWidgetId, TextureHandle, Vec2, Widget, IdLike, PainterCtx, PainterData, PixelRegion, StaticWidgetId, TextureHandle, Vec2, Widget,
WidgetId, WidgetLike, WidgetId, WidgetLike,
}, },
util::Id, util::{HashSet, Id},
}; };
use std::{ use std::{
any::{Any, TypeId}, any::{Any, TypeId},
@@ -18,7 +18,7 @@ pub struct Ui {
// TODO: make this at least pub(super) // TODO: make this at least pub(super)
pub(crate) data: PainterData, pub(crate) data: PainterData,
root: Option<WidgetId>, root: Option<WidgetId>,
updates: Vec<Id>, updates: HashSet<Id>,
recv: Receiver<Id>, recv: Receiver<Id>,
pub(super) send: Sender<Id>, pub(super) send: Sender<Id>,
full_redraw: bool, full_redraw: bool,
@@ -142,7 +142,7 @@ impl Ui {
// self.redraw_all(); // self.redraw_all();
// } // }
let mut ctx = PainterCtx::new(&mut self.data); let mut ctx = PainterCtx::new(&mut self.data);
for id in self.updates.drain(..) { for id in self.updates.drain() {
ctx.redraw(id); ctx.redraw(id);
} }
self.free(); self.free();
@@ -172,7 +172,7 @@ impl Ui {
} }
pub fn text(&mut self, id: &impl IdLike<TextEdit>) -> TextEditCtx<'_> { pub fn text(&mut self, id: &impl IdLike<TextEdit>) -> TextEditCtx<'_> {
self.updates.push(id.id()); self.updates.insert(id.id());
TextEditCtx { TextEditCtx {
text: self.data.widgets.get_mut(id).unwrap(), text: self.data.widgets.get_mut(id).unwrap(),
font_system: &mut self.data.text.font_system, font_system: &mut self.data.text.font_system,
@@ -196,7 +196,6 @@ impl Ui {
" pixel region: {}", " pixel region: {}",
inst.region.to_px(self.data.output_size) inst.region.to_px(self.data.output_size)
); );
println!(" desired_size: {}", inst.desired_size);
println!("}}"); println!("}}");
} }
} }
@@ -212,7 +211,7 @@ impl<W: Widget> Index<&WidgetId<W>> for Ui {
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui { impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output { fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id); self.updates.insert(id.id);
self.get_mut(id).unwrap() self.get_mut(id).unwrap()
} }
} }
@@ -227,7 +226,7 @@ impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui { impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output { fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id); self.updates.insert(id.id);
self.data.widgets.get_mut(&id).unwrap() self.data.widgets.get_mut(&id).unwrap()
} }
} }

View File

@@ -2,7 +2,7 @@ use crate::{
layout::UiNum, layout::UiNum,
util::{DivOr, impl_op}, util::{DivOr, impl_op},
}; };
use std::{marker::Destruct, ops::*}; use std::{hash::Hash, marker::Destruct, ops::*};
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
@@ -11,6 +11,15 @@ pub struct Vec2 {
pub y: f32, pub y: f32,
} }
impl Eq for Vec2 {}
impl Hash for Vec2 {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write_u32(self.x.to_bits());
state.write_u32(self.y.to_bits());
}
}
pub const fn vec2(x: impl const UiNum, y: impl const UiNum) -> Vec2 { pub const fn vec2(x: impl const UiNum, y: impl const UiNum) -> Vec2 {
Vec2::new(x.to_f32(), y.to_f32()) Vec2::new(x.to_f32(), y.to_f32())
} }

View File

@@ -1,16 +1,21 @@
use crate::layout::{Painter, Size, SizeCtx, StaticWidgetId, Ui, WidgetId, WidgetIdFn}; use crate::layout::{Len, Painter, SizeCtx, StaticWidgetId, Ui, WidgetId, WidgetIdFn};
use std::{any::Any, marker::PhantomData}; use std::{any::Any, marker::PhantomData};
pub trait Widget: Any { pub trait Widget: Any {
fn draw(&mut self, painter: &mut Painter); fn draw(&mut self, painter: &mut Painter);
fn desired_size(&mut self, _: &mut SizeCtx) -> Size { fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len;
Size::default() fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len;
}
} }
impl Widget for () { impl Widget for () {
fn draw(&mut self, _: &mut Painter) {} fn draw(&mut self, _: &mut Painter) {}
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
Len::ZERO
}
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
Len::ZERO
}
} }
pub struct WidgetTag; pub struct WidgetTag;