Compare commits

3 Commits

Author SHA1 Message Date
172e7157be FINALLY FIXED STUPID TEST UI ISSUES (true painter.rs moment) + scrolling 2025-11-21 01:40:13 -05:00
e3b1ddc993 add comments 😱 2025-11-20 23:27:30 -05:00
5aef8c2201 lol comments 2025-11-20 23:06:59 -05:00
9 changed files with 145 additions and 72 deletions

View File

@@ -121,8 +121,10 @@ impl Client {
.span(Dir::DOWN) .span(Dir::DOWN)
.add_static(&mut ui); .add_static(&mut ui);
let texts = Span::empty(Dir::DOWN).gap(10).add_static(&mut ui); let texts = Span::empty(Dir::DOWN)
let msg_area = (Rect::new(Color::SKY), texts.scroll().masked()).stack(); .gap(10)
.add_static(&mut ui);
let msg_area = texts.scroll().masked().background(rect(Color::SKY));
let add_text = text("add") let add_text = text("add")
.editable() .editable()
.text_align(Align::LEFT) .text_align(Align::LEFT)
@@ -142,9 +144,8 @@ impl Client {
client.ui.text(id).select(ctx.cursor, ctx.size); client.ui.text(id).select(ctx.cursor, ctx.size);
client.focus = Some(id.clone()); client.focus = Some(id.clone());
}); });
let msg_box = (rect(Color::WHITE.darker(0.5)), text) let msg_box = text
.stack() .background(rect(Color::WHITE.darker(0.5)))
.size(StackSize::Child(1))
.add(&mut client.ui); .add(&mut client.ui);
client.ui[texts].children.push(msg_box.any()); client.ui[texts].children.push(msg_box.any());
}) })

View File

@@ -6,7 +6,6 @@ pub struct Masked {
impl Widget for Masked { impl Widget for Masked {
fn draw(&mut self, painter: &mut Painter) { fn draw(&mut self, painter: &mut Painter) {
println!("yahoo! {}", painter.region());
painter.set_mask(painter.region()); painter.set_mask(painter.region());
painter.widget(&self.inner); painter.widget(&self.inner);
} }

View File

@@ -9,6 +9,7 @@ mod stack;
pub use align::*; pub use align::*;
pub use offset::*; pub use offset::*;
pub use pad::*; pub use pad::*;
pub use scroll::*;
pub use sized::*; pub use sized::*;
pub use span::*; pub use span::*;
pub use stack::*; pub use stack::*;

View File

@@ -3,9 +3,10 @@ use crate::prelude::*;
pub struct Scroll { pub struct Scroll {
inner: WidgetId, inner: WidgetId,
axis: Axis, axis: Axis,
snap_end: bool,
amt: f32, amt: f32,
draw_len: f32, snap_end: bool,
container_len: f32,
content_len: f32,
} }
impl Widget for Scroll { impl Widget for Scroll {
@@ -17,10 +18,14 @@ impl Widget for Scroll {
.apply_rest() .apply_rest()
.within_len(container_len) .within_len(container_len)
.to_abs(output_len); .to_abs(output_len);
let container_len = container_len.to_abs(output_len); self.container_len = container_len.to_abs(output_len);
self.draw_len = content_len; self.content_len = content_len;
// let region = UiRegion::FULL.offset(self.amt); if self.snap_end {
// painter.widget_within(&self.inner, region); self.amt = self.content_len - self.container_len;
}
self.update_amt();
let region = UiRegion::FULL.offset(Vec2::from_axis(self.axis, -self.amt, 0.0));
painter.widget_within(&self.inner, region);
} }
fn desired_width(&mut self, _: &mut SizeCtx) -> Len { fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
@@ -33,7 +38,26 @@ impl Widget for Scroll {
} }
impl Scroll { impl Scroll {
pub fn new(inner: WidgetId, axis: Axis) -> Self {
Self {
inner,
axis,
amt: 0.0,
snap_end: true,
container_len: 0.0,
content_len: 0.0,
}
}
pub fn update_amt(&mut self) {
self.amt = self.amt.max(0.0);
let len = (self.content_len - self.container_len).max(0.0);
self.amt = self.amt.min(len);
self.snap_end = self.amt == len;
}
pub fn scroll(&mut self, amt: f32) { pub fn scroll(&mut self, amt: f32) {
self.amt += amt; self.amt -= amt;
self.update_amt();
} }
} }

View File

@@ -11,9 +11,6 @@ impl Widget for Span {
fn draw(&mut self, painter: &mut Painter) { fn draw(&mut self, painter: &mut Painter) {
let total = self.len_sum(&mut painter.size_ctx()); let total = self.len_sum(&mut painter.size_ctx());
let mut start = UiScalar::rel_min(); let mut start = UiScalar::rel_min();
if painter.label() == "fricker" {
println!("redraw");
}
for child in &self.children { for child in &self.children {
let mut span = UiSpan::FULL; let mut span = UiSpan::FULL;
span.start = start; span.start = start;

View File

@@ -10,7 +10,7 @@ pub trait CoreWidget<W, Tag> {
fn width(self, len: impl Into<Len>) -> impl WidgetFn<Sized>; fn width(self, len: impl Into<Len>) -> impl WidgetFn<Sized>;
fn height(self, len: impl Into<Len>) -> impl WidgetFn<Sized>; fn height(self, len: impl Into<Len>) -> impl WidgetFn<Sized>;
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Offset>; fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Offset>;
fn scroll(self) -> impl WidgetIdFn<Offset>; fn scroll(self) -> impl WidgetIdFn<Scroll>;
fn masked(self) -> impl WidgetFn<Masked>; fn masked(self) -> impl WidgetFn<Masked>;
fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack>; fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack>;
fn z_offset(self, offset: usize) -> impl WidgetFn<Stack>; fn z_offset(self, offset: usize) -> impl WidgetFn<Stack>;
@@ -77,11 +77,14 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
} }
} }
fn scroll(self) -> impl WidgetIdFn<Offset> { fn scroll(self) -> impl WidgetIdFn<Scroll> {
self.offset(UiVec2::ZERO) move |ui| {
Scroll::new(self.add(ui).any(), Axis::Y)
.edit_on(CursorSense::Scroll, |w, data| { .edit_on(CursorSense::Scroll, |w, data| {
w.amt += UiVec2::abs(data.scroll_delta * 50.0); w.scroll(data.scroll_delta.y * 50.0);
}) })
.add(ui)
}
} }
fn masked(self) -> impl WidgetFn<Masked> { fn masked(self) -> impl WidgetFn<Masked> {

View File

@@ -7,6 +7,7 @@ use crate::{
util::{HashMap, HashSet, Id, TrackedArena}, util::{HashMap, HashSet, Id, TrackedArena},
}; };
/// makes your surfaces look pretty
pub struct Painter<'a, 'c> { pub struct Painter<'a, 'c> {
ctx: &'a mut PainterCtx<'c>, ctx: &'a mut PainterCtx<'c>,
region: UiRegion, region: UiRegion,
@@ -22,7 +23,8 @@ pub struct Painter<'a, 'c> {
id: Id, id: Id,
} }
pub struct PainterCtx<'a> { /// context for a painter; lets you draw and redraw widgets
struct PainterCtx<'a> {
pub widgets: &'a Widgets, pub widgets: &'a Widgets,
pub active: &'a mut HashMap<Id, WidgetInstance>, pub active: &'a mut HashMap<Id, WidgetInstance>,
pub layers: &'a mut Layers, pub layers: &'a mut Layers,
@@ -33,15 +35,19 @@ pub struct PainterCtx<'a> {
pub modules: &'a mut Modules, pub modules: &'a mut Modules,
pub cache_width: HashMap<Id, (UiVec2, Len)>, pub cache_width: HashMap<Id, (UiVec2, Len)>,
pub cache_height: HashMap<Id, (UiVec2, Len)>, pub cache_height: HashMap<Id, (UiVec2, Len)>,
pub needs_redraw: HashSet<Id>,
draw_started: HashSet<Id>, draw_started: HashSet<Id>,
} }
/// stores information for children about the highest level parent that needed their size
/// so that they can redraw the parent if their size changes
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, Default)]
pub struct ResizeRef { pub struct ResizeRef {
x: Option<(Id, (UiVec2, Len))>, x: Option<(Id, (UiVec2, Len))>,
y: Option<(Id, (UiVec2, Len))>, y: Option<(Id, (UiVec2, Len))>,
} }
/// important non rendering data for retained drawing
#[derive(Debug)] #[derive(Debug)]
pub struct WidgetInstance { pub struct WidgetInstance {
pub id: Id, pub id: Id,
@@ -55,6 +61,7 @@ pub struct WidgetInstance {
pub layer: usize, pub layer: usize,
} }
/// data to be stored in Ui to create PainterCtxs easily
#[derive(Default)] #[derive(Default)]
pub struct PainterData { pub struct PainterData {
pub widgets: Widgets, pub widgets: Widgets,
@@ -69,23 +76,11 @@ pub struct PainterData {
} }
impl<'a> PainterCtx<'a> { impl<'a> PainterCtx<'a> {
pub fn new(data: &'a mut PainterData) -> Self { /// redraws a widget that's currently active (drawn)
Self { /// can be called on something already drawn or removed,
widgets: &data.widgets, /// will just return if so
active: &mut data.active,
layers: &mut data.layers,
textures: &mut data.textures,
text: &mut data.text,
output_size: data.output_size,
modules: &mut data.modules,
masks: &mut data.masks,
cache_width: Default::default(),
cache_height: Default::default(),
draw_started: Default::default(),
}
}
pub fn redraw(&mut self, id: Id) { pub fn redraw(&mut self, id: Id) {
self.needs_redraw.remove(&id);
if self.draw_started.contains(&id) { if self.draw_started.contains(&id) {
return; return;
} }
@@ -94,13 +89,17 @@ impl<'a> PainterCtx<'a> {
}; };
let mut resize = active.resize; let mut resize = active.resize;
// set resize back after redrawing
let finish = |s: &mut Self, resize| { let finish = |s: &mut Self, resize| {
if let Some(active) = s.active.get_mut(&id) { if let Some(active) = s.active.get_mut(&id) {
// might need to get_or_insert here instead of just assuming
active.resize = resize; active.resize = resize;
} }
}; };
// check if a parent depends on the desired size of this, if so then redraw it first
// TODO: this is stupid having 2 of these, don't ask me what the consequences are // TODO: this is stupid having 2 of these, don't ask me what the consequences are
let mut ret = false;
if let Some((rid, (outer, old_desired))) = &mut resize.x { if let Some((rid, (outer, old_desired))) = &mut resize.x {
let new_desired = SizeCtx { let new_desired = SizeCtx {
source: id, source: id,
@@ -117,15 +116,17 @@ impl<'a> PainterCtx<'a> {
} }
.width_inner(id); .width_inner(id);
if new_desired != *old_desired { if new_desired != *old_desired {
// unsure if I need to walk down the tree here
self.redraw(*rid); self.redraw(*rid);
*old_desired = new_desired; *old_desired = new_desired;
if self.draw_started.contains(&id) { if self.draw_started.contains(&id) {
return finish(self, resize); ret = true;
} }
} }
} }
if let Some((rid, (outer, old_desired))) = &mut resize.y { if let Some((rid, (outer, old_desired))) = &mut resize.y {
// NOTE: might need hack in Span here (or also do it properly here)
let new_desired = SizeCtx { let new_desired = SizeCtx {
source: id, source: id,
cache_width: &mut self.cache_width, cache_width: &mut self.cache_width,
@@ -144,10 +145,14 @@ impl<'a> PainterCtx<'a> {
self.redraw(*rid); self.redraw(*rid);
*old_desired = new_desired; *old_desired = new_desired;
if self.draw_started.contains(&id) { if self.draw_started.contains(&id) {
ret = true;
}
}
}
if ret {
return finish(self, resize); return finish(self, resize);
} }
}
}
let Some(active) = self.remove(id) else { let Some(active) = self.remove(id) else {
return; return;
@@ -164,12 +169,6 @@ impl<'a> PainterCtx<'a> {
finish(self, resize); finish(self, resize);
} }
pub fn draw(&mut self, id: Id) {
self.draw_started.clear();
self.layers.clear();
self.draw_inner(0, id, UiRegion::FULL, None, MaskIdx::NONE, None);
}
fn draw_inner( fn draw_inner(
&mut self, &mut self,
layer: usize, layer: usize,
@@ -193,7 +192,10 @@ 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 = ResizeRef::default(); let mut resize = ResizeRef::default();
if let Some(active) = self.active.get_mut(&id) { if let Some(active) = self.active.get_mut(&id)
&& !self.needs_redraw.contains(&id)
{
// check to see if we can skip drawing first
if active.parent != parent { if active.parent != parent {
panic!("Cannot draw the same widget twice (2)"); panic!("Cannot draw the same widget twice (2)");
} }
@@ -205,11 +207,13 @@ impl<'a> PainterCtx<'a> {
self.mov(id, from, region); self.mov(id, from, region);
return; return;
} }
// if not, then maintain resize and track old children to remove unneeded
let active = self.remove(id).unwrap(); let active = self.remove(id).unwrap();
old_children = active.children; old_children = active.children;
resize = active.resize; resize = active.resize;
} }
// draw widget
self.draw_started.insert(id); self.draw_started.insert(id);
let mut painter = Painter { let mut painter = Painter {
@@ -225,7 +229,6 @@ impl<'a> PainterCtx<'a> {
children_height: Default::default(), children_height: Default::default(),
}; };
// draw widgets
painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter); painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter);
let children_width = painter.children_width; let children_width = painter.children_width;
@@ -243,6 +246,7 @@ impl<'a> PainterCtx<'a> {
mask: painter.mask, mask: painter.mask,
layer, layer,
}; };
// set resize for children who's size this widget depends on
for (cid, outer) in children_width { 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.x.is_none() && w.resize.x.is_none()
@@ -257,12 +261,15 @@ impl<'a> PainterCtx<'a> {
w.resize.y = Some((id, outer)) w.resize.y = Some((id, outer))
} }
} }
// remove old children that weren't kept
for c in &old_children { for c in &old_children {
if !instance.children.contains(c) { if !instance.children.contains(c) {
self.remove_rec(*c); self.remove_rec(*c);
} }
} }
// update modules
for m in self.modules.iter_mut() { for m in self.modules.iter_mut() {
m.on_draw(&instance); m.on_draw(&instance);
} }
@@ -317,6 +324,39 @@ impl<'a> PainterCtx<'a> {
} }
} }
impl PainterData {
fn ctx(&mut self, needs_redraw: HashSet<Id>) -> PainterCtx<'_> {
PainterCtx {
widgets: &self.widgets,
active: &mut self.active,
layers: &mut self.layers,
textures: &mut self.textures,
text: &mut self.text,
output_size: self.output_size,
modules: &mut self.modules,
masks: &mut self.masks,
cache_width: Default::default(),
cache_height: Default::default(),
draw_started: Default::default(),
needs_redraw,
}
}
pub fn draw(&mut self, id: Id) {
let mut ctx = self.ctx(Default::default());
ctx.draw_started.clear();
ctx.layers.clear();
ctx.draw_inner(0, id, UiRegion::FULL, None, MaskIdx::NONE, None);
}
pub fn redraw(&mut self, ids: HashSet<Id>) {
let mut ctx = self.ctx(ids);
while let Some(&id) = ctx.needs_redraw.iter().next() {
ctx.redraw(id);
}
}
}
impl<'a, 'c> Painter<'a, 'c> { impl<'a, 'c> Painter<'a, 'c> {
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) { fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
let h = self.ctx.layers.write( let h = self.ctx.layers.write(
@@ -440,6 +480,10 @@ impl<'a, 'c> Painter<'a, 'c> {
pub fn label(&self) -> &str { pub fn label(&self) -> &str {
&self.ctx.widgets.data(&self.id).unwrap().label &self.ctx.widgets.data(&self.id).unwrap().label
} }
pub fn id(&self) -> &Id {
&self.id
}
} }
pub struct SizeCtx<'a> { pub struct SizeCtx<'a> {
@@ -467,16 +511,22 @@ impl SizeCtx<'_> {
} }
fn width_inner(&mut self, id: Id) -> Len { fn width_inner(&mut self, id: Id) -> Len {
if let Some(&(outer, len)) = self.cache_width.get(&id) // first check cache
&& outer == self.outer // TODO: is this needed? broken rn bc does not store children during upper size check,
{ // so if something actually using check_* hits cache it fails to add them
self.checked_width.insert(id, (self.outer, len)); // if let Some(&(outer, len)) = self.cache_width.get(&id)
return len; // && outer == self.outer
} // {
// self.checked_width.insert(id, (self.outer, len));
// return len;
// }
// store self vars that need to be maintained
let self_outer = self.outer; let self_outer = self.outer;
let self_id = self.id; let self_id = self.id;
// get size of input id
self.id = id; self.id = id;
let len = self.widgets.get_dyn_dynamic(id).desired_width(self); let len = self.widgets.get_dyn_dynamic(id).desired_width(self);
// restore vars & update cache + checked
self.outer = self_outer; self.outer = self_outer;
self.id = self_id; self.id = self_id;
self.cache_width.insert(id, (self.outer, len)); self.cache_width.insert(id, (self.outer, len));
@@ -486,12 +536,13 @@ impl SizeCtx<'_> {
// TODO: should be refactored to share code w width_inner // TODO: should be refactored to share code w width_inner
fn height_inner(&mut self, id: Id) -> Len { fn height_inner(&mut self, id: Id) -> Len {
if let Some(&(outer, len)) = self.cache_height.get(&id) // if let Some(&(outer, len)) = self.cache_height.get(&id)
&& outer == self.outer // && outer == self.outer
{ // {
self.checked_height.insert(id, (self.outer, len)); // prntln!("FAIL {id:?}");
return len; // self.checked_height.insert(id, (self.outer, len));
} // return len;
// }
let self_outer = self.outer; let self_outer = self.outer;
let self_id = self.id; let self_id = self.id;
self.id = id; self.id = id;

View File

@@ -3,7 +3,7 @@ use image::DynamicImage;
use crate::{ use crate::{
core::{TextEdit, TextEditCtx}, core::{TextEdit, TextEditCtx},
layout::{ layout::{
IdLike, PainterCtx, PainterData, PixelRegion, StaticWidgetId, TextureHandle, Vec2, Widget, IdLike, PainterData, PixelRegion, StaticWidgetId, TextureHandle, Vec2, Widget,
WidgetId, WidgetInstance, WidgetLike, WidgetId, WidgetInstance, WidgetLike,
}, },
util::{HashSet, Id}, util::{HashSet, Id},
@@ -109,9 +109,8 @@ impl Ui {
} }
// free before bc nothing should exist // free before bc nothing should exist
self.free(); self.free();
let mut ctx = PainterCtx::new(&mut self.data);
if let Some(root) = &self.root { if let Some(root) = &self.root {
ctx.draw(root.id); self.data.draw(root.id);
} }
} }
@@ -138,13 +137,7 @@ impl Ui {
} }
fn redraw_updates(&mut self) { fn redraw_updates(&mut self) {
// if self.updates.drain(..).next().is_some() { self.data.redraw(std::mem::take(&mut self.updates));
// self.redraw_all();
// }
let mut ctx = PainterCtx::new(&mut self.data);
for id in self.updates.drain() {
ctx.redraw(id);
}
self.free(); self.free();
} }

View File

@@ -56,7 +56,11 @@ impl Widgets {
} }
pub fn insert<W: Widget>(&mut self, id: Id, widget: W) { pub fn insert<W: Widget>(&mut self, id: Id, widget: W) {
self.insert_any(id, Box::new(widget), std::any::type_name::<W>().to_string()); let mut label = std::any::type_name::<W>().to_string();
if let (Some(first), Some(last)) = (label.find(":"), label.rfind(":")) {
label = label.split_at(first).0.to_string() + "::" + label.split_at(last + 1).1;
}
self.insert_any(id, Box::new(widget), label);
} }
pub fn data(&self, id: &Id) -> Option<&WidgetData> { pub fn data(&self, id: &Id) -> Option<&WidgetData> {