refactor painter

This commit is contained in:
2025-12-16 00:48:14 -05:00
parent 2183fbd3cb
commit 71f3beaf94
5 changed files with 470 additions and 457 deletions

332
core/src/ui/draw_state.rs Normal file
View File

@@ -0,0 +1,332 @@
use crate::{
ActiveData, Len, Painter, SizeCtx, Ui, UiRegion, UiVec2, WidgetId,
render::MaskIdx,
ui::painter::ResizeRef,
util::{HashMap, HashSet},
};
use std::ops::{Deref, DerefMut};
/// state maintained between widgets during painting
pub struct DrawState<'a, State> {
pub(super) ui: &'a mut Ui<State>,
cache_width: HashMap<WidgetId, (UiVec2, Len)>,
cache_height: HashMap<WidgetId, (UiVec2, Len)>,
draw_started: HashSet<WidgetId>,
}
impl<'a, State: 'static> DrawState<'a, State> {
pub fn new(ui: &'a mut Ui<State>) -> Self {
Self {
ui,
cache_width: Default::default(),
cache_height: Default::default(),
draw_started: Default::default(),
}
}
pub fn redraw_updates(&mut self) {
while let Some(&id) = self.widgets.needs_redraw.iter().next() {
self.redraw(id);
}
self.free();
}
/// redraws a widget that's currently active (drawn)
/// can be called on something already drawn or removed,
/// will just return if so
pub fn redraw(&mut self, id: WidgetId) {
self.widgets.needs_redraw.remove(&id);
if self.draw_started.contains(&id) {
return;
}
let Some(active) = self.ui.active.get(&id) else {
return;
};
let mut resize = active.resize;
// set resize back after redrawing
let finish = |s: &mut Self, resize| {
if let Some(active) = s.active.get_mut(&id) {
// might need to get_or_insert here instead of just assuming
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
let mut ret = false;
if let Some((rid, (outer, old_desired))) = &mut resize.x {
let new_desired = self
.size_ctx(
id,
*outer,
id,
&mut Default::default(),
&mut Default::default(),
)
.width_inner(id);
if new_desired != *old_desired {
// unsure if I need to walk down the tree here
self.redraw(*rid);
*old_desired = new_desired;
if self.draw_started.contains(&id) {
ret = true;
}
}
}
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 = self
.size_ctx(
id,
*outer,
id,
&mut Default::default(),
&mut Default::default(),
)
.height_inner(id);
if new_desired != *old_desired {
self.redraw(*rid);
*old_desired = new_desired;
if self.draw_started.contains(&id) {
ret = true;
}
}
}
if ret {
return finish(self, resize);
}
let Some(active) = self.remove(id, false) else {
return;
};
self.draw_inner(
active.layer,
id,
active.region,
active.parent,
active.mask,
Some(active.children),
);
finish(self, resize);
}
pub(super) fn size_ctx<'b>(
&'b mut self,
source: WidgetId,
outer: UiVec2,
id: WidgetId,
checked_width: &'b mut HashMap<WidgetId, (UiVec2, Len)>,
checked_height: &'b mut HashMap<WidgetId, (UiVec2, Len)>,
) -> SizeCtx<'b, State> {
SizeCtx {
source,
cache_width: &mut self.cache_width,
cache_height: &mut self.cache_height,
text: &mut self.ui.text,
textures: &mut self.ui.textures,
widgets: &self.ui.widgets,
outer,
output_size: self.ui.output_size,
checked_width,
checked_height,
id,
}
}
pub fn redraw_all(&mut self) {
// update event managers
for (id, active) in self.ui.active.drain() {
let data = self.ui.widgets.data(id).unwrap();
self.ui.events.undraw(data, &active);
}
// free before bc nothing should exist
self.free();
self.layers.clear();
self.widgets.needs_redraw.clear();
if let Some(id) = &self.ui.root {
self.draw_inner(0, id.id(), UiRegion::FULL, None, MaskIdx::NONE, None);
}
}
pub(super) fn draw_inner(
&mut self,
layer: usize,
id: WidgetId,
region: UiRegion,
parent: Option<WidgetId>,
mask: MaskIdx,
old_children: Option<Vec<WidgetId>>,
) {
// 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.draw_started.contains(&id) {
// panic!(
// "Cannot draw the same widget ({}) twice (1)",
// self.widgets.data(&id).unwrap().label
// );
// }
let mut old_children = old_children.unwrap_or_default();
let mut resize = ResizeRef::default();
if let Some(active) = self.ui.active.get_mut(&id)
&& !self.ui.widgets.needs_redraw.contains(&id)
{
// check to see if we can skip drawing first
if active.parent != parent {
panic!("Cannot draw the same widget twice (2)");
}
if active.region == region {
return;
} else if active.region.size() == region.size() {
// TODO: epsilon?
let from = active.region;
self.mov(id, from, region);
return;
}
// if not, then maintain resize and track old children to remove unneeded
let active = self.remove(id, false).unwrap();
old_children = active.children;
resize = active.resize;
}
// draw widget
self.draw_started.insert(id);
let mut painter = Painter {
state: self,
region,
mask,
layer,
id,
textures: Vec::new(),
primitives: Vec::new(),
children: Vec::new(),
children_width: Default::default(),
children_height: Default::default(),
};
let mut widget = painter.state.widgets.get_dyn_dynamic(id);
widget.draw(&mut painter);
drop(widget);
let Painter {
state: _,
region,
mask,
textures,
primitives,
children,
children_width,
children_height,
layer,
id,
} = painter;
// add to active
let active = ActiveData {
id,
region,
parent,
textures,
primitives,
children,
resize,
mask,
layer,
};
// set resize for children who's size this widget depends on
for (cid, outer) in children_width {
if let Some(w) = self.active.get_mut(&cid)
&& w.resize.x.is_none()
{
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))
}
}
// remove old children that weren't kept
for c in &old_children {
if !active.children.contains(c) {
self.remove_rec(*c);
}
}
// update modules
let data = self.ui.widgets.data(id).unwrap();
self.ui.events.draw(data, &active);
self.active.insert(id, active);
}
fn mov(&mut self, id: WidgetId, from: UiRegion, to: UiRegion) {
let active = self.ui.active.get_mut(&id).unwrap();
for h in &active.primitives {
let region = self.ui.layers[h.layer].region_mut(h);
*region = region.outside(&from).within(&to);
}
active.region = active.region.outside(&from).within(&to);
// children will not be changed, so this technically should not be needed
// probably need unsafe
let children = active.children.clone();
for child in children {
self.mov(child, from, to);
}
}
/// NOTE: instance textures are cleared and self.textures freed
fn remove(&mut self, id: WidgetId, undraw: bool) -> Option<ActiveData> {
let mut active = self.active.remove(&id);
if let Some(active) = &mut active {
for h in &active.primitives {
let mask = self.layers.free(h);
if mask != MaskIdx::NONE {
self.masks.remove(mask);
}
}
active.textures.clear();
self.textures.free();
if undraw {
let data = self.ui.widgets.data(id).unwrap();
self.ui.events.undraw(data, active);
}
}
active
}
fn remove_rec(&mut self, id: WidgetId) -> Option<ActiveData> {
let inst = self.remove(id, true);
if let Some(inst) = &inst {
for c in &inst.children {
self.remove_rec(*c);
}
}
inst
}
}
impl<State> Deref for DrawState<'_, State> {
type Target = Ui<State>;
fn deref(&self) -> &Self::Target {
self.ui
}
}
impl<State> DerefMut for DrawState<'_, State> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.ui
}
}