FINALLY FIXED STUPID TEST UI ISSUES (true painter.rs moment) + scrolling
This commit is contained in:
@@ -24,7 +24,7 @@ pub struct Painter<'a, 'c> {
|
||||
}
|
||||
|
||||
/// context for a painter; lets you draw and redraw widgets
|
||||
pub struct PainterCtx<'a> {
|
||||
struct PainterCtx<'a> {
|
||||
pub widgets: &'a Widgets,
|
||||
pub active: &'a mut HashMap<Id, WidgetInstance>,
|
||||
pub layers: &'a mut Layers,
|
||||
@@ -35,6 +35,7 @@ pub struct PainterCtx<'a> {
|
||||
pub modules: &'a mut Modules,
|
||||
pub cache_width: HashMap<Id, (UiVec2, Len)>,
|
||||
pub cache_height: HashMap<Id, (UiVec2, Len)>,
|
||||
pub needs_redraw: HashSet<Id>,
|
||||
draw_started: HashSet<Id>,
|
||||
}
|
||||
|
||||
@@ -75,26 +76,11 @@ pub struct PainterData {
|
||||
}
|
||||
|
||||
impl<'a> PainterCtx<'a> {
|
||||
pub fn new(data: &'a mut PainterData) -> Self {
|
||||
Self {
|
||||
widgets: &data.widgets,
|
||||
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(),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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: Id) {
|
||||
self.needs_redraw.remove(&id);
|
||||
if self.draw_started.contains(&id) {
|
||||
return;
|
||||
}
|
||||
@@ -113,6 +99,7 @@ impl<'a> PainterCtx<'a> {
|
||||
|
||||
// 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 = SizeCtx {
|
||||
source: id,
|
||||
@@ -129,15 +116,17 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
.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) {
|
||||
return finish(self, resize);
|
||||
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 = SizeCtx {
|
||||
source: id,
|
||||
cache_width: &mut self.cache_width,
|
||||
@@ -156,11 +145,15 @@ impl<'a> PainterCtx<'a> {
|
||||
self.redraw(*rid);
|
||||
*old_desired = new_desired;
|
||||
if self.draw_started.contains(&id) {
|
||||
return finish(self, resize);
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ret {
|
||||
return finish(self, resize);
|
||||
}
|
||||
|
||||
let Some(active) = self.remove(id) else {
|
||||
return;
|
||||
};
|
||||
@@ -176,12 +169,6 @@ impl<'a> PainterCtx<'a> {
|
||||
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(
|
||||
&mut self,
|
||||
layer: usize,
|
||||
@@ -205,7 +192,9 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
let mut old_children = old_children.unwrap_or_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 {
|
||||
panic!("Cannot draw the same widget twice (2)");
|
||||
@@ -335,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> {
|
||||
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
let h = self.ctx.layers.write(
|
||||
@@ -458,6 +480,10 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
pub fn label(&self) -> &str {
|
||||
&self.ctx.widgets.data(&self.id).unwrap().label
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &Id {
|
||||
&self.id
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SizeCtx<'a> {
|
||||
@@ -486,12 +512,14 @@ impl SizeCtx<'_> {
|
||||
|
||||
fn width_inner(&mut self, id: Id) -> Len {
|
||||
// first check cache
|
||||
if let Some(&(outer, len)) = self.cache_width.get(&id)
|
||||
&& outer == self.outer
|
||||
{
|
||||
self.checked_width.insert(id, (self.outer, len));
|
||||
return len;
|
||||
}
|
||||
// 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
|
||||
// if let Some(&(outer, len)) = self.cache_width.get(&id)
|
||||
// && 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_id = self.id;
|
||||
@@ -508,12 +536,13 @@ impl SizeCtx<'_> {
|
||||
|
||||
// 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;
|
||||
}
|
||||
// if let Some(&(outer, len)) = self.cache_height.get(&id)
|
||||
// && outer == self.outer
|
||||
// {
|
||||
// prntln!("FAIL {id:?}");
|
||||
// self.checked_height.insert(id, (self.outer, len));
|
||||
// return len;
|
||||
// }
|
||||
let self_outer = self.outer;
|
||||
let self_id = self.id;
|
||||
self.id = id;
|
||||
|
||||
@@ -3,7 +3,7 @@ use image::DynamicImage;
|
||||
use crate::{
|
||||
core::{TextEdit, TextEditCtx},
|
||||
layout::{
|
||||
IdLike, PainterCtx, PainterData, PixelRegion, StaticWidgetId, TextureHandle, Vec2, Widget,
|
||||
IdLike, PainterData, PixelRegion, StaticWidgetId, TextureHandle, Vec2, Widget,
|
||||
WidgetId, WidgetInstance, WidgetLike,
|
||||
},
|
||||
util::{HashSet, Id},
|
||||
@@ -109,9 +109,8 @@ impl Ui {
|
||||
}
|
||||
// free before bc nothing should exist
|
||||
self.free();
|
||||
let mut ctx = PainterCtx::new(&mut self.data);
|
||||
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) {
|
||||
// if self.updates.drain(..).next().is_some() {
|
||||
// self.redraw_all();
|
||||
// }
|
||||
let mut ctx = PainterCtx::new(&mut self.data);
|
||||
for id in self.updates.drain() {
|
||||
ctx.redraw(id);
|
||||
}
|
||||
self.data.redraw(std::mem::take(&mut self.updates));
|
||||
self.free();
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,11 @@ impl Widgets {
|
||||
}
|
||||
|
||||
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> {
|
||||
|
||||
Reference in New Issue
Block a user