IDC FINALLY OH MY GOD (I think like ctx + resize propagation + some other stuff)

This commit is contained in:
2025-09-11 00:59:26 -04:00
parent 709a2d0e17
commit 242c3b992e
15 changed files with 476 additions and 281 deletions

View File

@@ -2,8 +2,8 @@ use std::ops::Range;
use crate::{
layout::{
Active, Cursor, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset,
TextureHandle, Textures, UiRegion, Vec2, WidgetId, Widgets,
Active, Cursor, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle, Textures,
UiRegion, Vec2, WidgetId, Widgets,
},
render::{Primitive, PrimitiveHandle, Primitives},
util::{HashSet, Id},
@@ -15,12 +15,12 @@ pub struct Painter<'a, 'c> {
textures: Vec<TextureHandle>,
primitives: Vec<PrimitiveHandle>,
children: Vec<Id>,
sized_children: HashSet<Id>,
id: Id,
}
pub struct PainterCtx<'a> {
pub widgets: &'a Widgets,
pub sensor_map: &'a SensorMap,
pub active: &'a mut Active,
pub primitives: &'a mut Primitives,
pub textures: &'a mut Textures,
@@ -36,6 +36,7 @@ pub struct WidgetInstance {
pub textures: Vec<TextureHandle>,
pub primitives: Vec<PrimitiveHandle>,
pub children: Vec<Id>,
pub resize: Option<Id>,
pub span: Range<usize>,
}
@@ -43,7 +44,6 @@ impl<'a> PainterCtx<'a> {
pub fn new(
widgets: &'a Widgets,
primitives: &'a mut Primitives,
sensor_map: &'a SensorMap,
active: &'a mut Active,
textures: &'a mut Textures,
text: &'a mut TextData,
@@ -51,7 +51,6 @@ impl<'a> PainterCtx<'a> {
) -> Self {
Self {
widgets,
sensor_map,
active,
primitives,
textures,
@@ -63,11 +62,24 @@ impl<'a> PainterCtx<'a> {
pub fn redraw(&mut self, id: &Id) {
self.drawing.clear();
let Some(active) = self.active.widgets.get(id) else {
return;
};
if let Some(rid) = &active.resize {
self.redraw(&rid.duplicate());
if self.drawing.contains(id) {
return;
}
}
let Some(active) = self.remove(id) else {
return;
};
self.primitives.set_pos(active.span.start);
self.draw_inner(id, active.region, active.parent(), Some(active.children));
self.active.widgets.get_mut(id).unwrap().resize = active.resize;
let delta = self.primitives.apply(active.span.clone());
if delta != 0
&& let Some(parent) = active.parent
@@ -90,13 +102,20 @@ impl<'a> PainterCtx<'a> {
parent: Option<Id>,
old_children: Option<Vec<Id>>,
) {
// I have no idea if these checks work lol
// the idea is u can't redraw stuff u already drew,
// and if parent is different then there's another copy with a different parent
// but this has a very weird issue where you can't move widgets unless u remove first
// so swapping is impossible rn I think?
// there's definitely better solutions like a counter (>1 = panic) but don't care rn
if self.drawing.contains(id) {
panic!("Cannot draw the same widget twice");
panic!("Cannot draw the same widget twice (1)");
}
let mut old_children = old_children.unwrap_or_default();
let mut resize = None;
if let Some(active) = self.active.widgets.get(id) {
if active.parent != parent {
panic!("Cannot draw the same widget twice");
panic!("Cannot draw the same widget twice (2)");
}
if active.region == region {
self.primitives.skip(&active.span);
@@ -106,6 +125,7 @@ impl<'a> PainterCtx<'a> {
// else if active.region.size() == region.size() { move }
let active = self.remove(id).unwrap();
old_children = active.children;
resize = active.resize;
}
let mut painter = Painter {
@@ -115,6 +135,7 @@ impl<'a> PainterCtx<'a> {
primitives: Vec::new(),
ctx: self,
children: Vec::new(),
sized_children: HashSet::new(),
};
// draw widgets
@@ -122,6 +143,8 @@ impl<'a> PainterCtx<'a> {
painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter);
let end_i = painter.ctx.primitives.cur_pos();
let sized_children = painter.sized_children;
// add to active
let instance = WidgetInstance {
id: id.duplicate(),
@@ -131,14 +154,20 @@ impl<'a> PainterCtx<'a> {
span: start_i..end_i,
primitives: painter.primitives,
children: painter.children,
resize,
};
for cid in sized_children {
if let Some(w) = self.active.widgets.get_mut(&cid) {
w.resize = Some(id.duplicate())
}
}
for c in &old_children {
if !instance.children.contains(c) {
self.remove_rec(c);
}
}
self.active.add(id, instance, self.sensor_map);
self.active.add(id, instance, self.widgets);
}
fn remove(&mut self, id: &Id) -> Option<WidgetInstance> {
@@ -168,8 +197,6 @@ impl<'a> PainterCtx<'a> {
let instance = self.active.widgets.get_mut(&parent).unwrap();
let end = &mut instance.span.end;
*end = end.strict_add_signed(delta);
// ids are supposed to be unique so theoretically no cloning is needed
// leaving for now for testing
let parent = instance.parent();
for child in instance
.children
@@ -186,6 +213,7 @@ impl<'a> PainterCtx<'a> {
fn shift_child(&mut self, child: &Id, start: usize, delta: isize) {
let instance = self.active.widgets.get_mut(child).unwrap();
// = also prevents the original id from getting shifted
if instance.span.start <= start {
return;
}
@@ -263,6 +291,7 @@ impl<'a, 'c> Painter<'a, 'c> {
}
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
self.sized_children.insert(id.id.duplicate());
self.ctx
.widgets
.get_dyn_dynamic(&id.id)
@@ -275,10 +304,36 @@ impl<'a, 'c> Painter<'a, 'c> {
text: self.ctx.text,
textures: self.ctx.textures,
widgets: self.ctx.widgets,
checked: &mut self.sized_children,
}
}
}
pub struct SizeCtx<'a> {
pub size: Vec2,
pub text: &'a mut TextData,
pub textures: &'a mut Textures,
widgets: &'a Widgets,
checked: &'a mut HashSet<Id>,
}
impl SizeCtx<'_> {
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
self.checked.insert(id.id.duplicate());
self.widgets.get_dyn_dynamic(&id.id).get_size(self)
}
pub fn draw_text(
&mut self,
buffer: &mut TextBuffer,
content: &str,
attrs: &TextAttrs,
cursor: &Cursor,
) -> (TextureHandle, TextOffset) {
self.text
.draw(buffer, content, attrs, cursor, self.textures)
}
}
impl WidgetInstance {
pub fn parent(&self) -> Option<Id> {
self.parent.as_ref().map(|p| p.duplicate())