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
}
}

View File

@@ -1,6 +1,7 @@
use crate::{ use crate::{
Event, EventFn, EventLike, EventManager, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData, Event, EventFn, EventLike, EventManager, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData,
TextureHandle, Textures, Widget, WidgetHandle, WidgetId, WidgetLike, Widgets, TextureHandle, Textures, Widget, WidgetHandle, WidgetId, WidgetLike, Widgets,
ui::draw_state::DrawState,
util::{HashMap, TrackedArena, Vec2}, util::{HashMap, TrackedArena, Vec2},
}; };
use image::DynamicImage; use image::DynamicImage;
@@ -9,8 +10,12 @@ use std::{
sync::mpsc::{Receiver, Sender, channel}, sync::mpsc::{Receiver, Sender, channel},
}; };
mod draw_state;
mod painter; mod painter;
pub use painter::{ActiveData, Painter, SizeCtx}; mod size;
pub use painter::{ActiveData, Painter};
pub use size::*;
pub struct Ui<State> { pub struct Ui<State> {
// TODO: edit visibilities // TODO: edit visibilities
@@ -106,14 +111,14 @@ impl<State: 'static> Ui<State> {
pub fn update(&mut self) { pub fn update(&mut self) {
if self.full_redraw { if self.full_redraw {
self.redraw_all(); DrawState::new(self).redraw_all();
self.full_redraw = false; self.full_redraw = false;
} else if self.widgets.has_updates() { } else if self.widgets.has_updates() {
self.redraw_updates(); DrawState::new(self).redraw_updates();
} }
if self.resized { if self.resized {
self.resized = false; self.resized = false;
self.redraw_all(); DrawState::new(self).redraw_all();
} }
} }

View File

@@ -1,41 +1,32 @@
use std::ops::{Deref, DerefMut};
use crate::{ use crate::{
Axis, Len, RenderedText, Size, TextAttrs, TextBuffer, TextData, TextureHandle, Textures, Ui, Axis, Len, RenderedText, Size, SizeCtx, TextAttrs, TextBuffer, TextData, TextureHandle, Ui,
UiRegion, UiVec2, WidgetHandle, WidgetId, Widgets, UiRegion, UiVec2, WidgetHandle, WidgetId,
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst}, render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
util::{HashMap, HashSet, Vec2}, ui::draw_state::DrawState,
util::{HashMap, Vec2},
}; };
/// makes your surfaces look pretty /// makes your surfaces look pretty
pub struct Painter<'a, 'b, State> { pub struct Painter<'a, 'b, State> {
state: &'a mut DrawState<'b, State>, pub(super) state: &'a mut DrawState<'b, State>,
region: UiRegion, pub(super) region: UiRegion,
mask: MaskIdx, pub(super) mask: MaskIdx,
textures: Vec<TextureHandle>, pub(super) textures: Vec<TextureHandle>,
primitives: Vec<PrimitiveHandle>, pub(super) primitives: Vec<PrimitiveHandle>,
children: Vec<WidgetId>, pub(super) children: Vec<WidgetId>,
children_width: HashMap<WidgetId, (UiVec2, Len)>, pub(super) children_width: HashMap<WidgetId, (UiVec2, Len)>,
children_height: HashMap<WidgetId, (UiVec2, Len)>, pub(super) children_height: HashMap<WidgetId, (UiVec2, Len)>,
pub layer: usize, pub layer: usize,
id: WidgetId, pub(super) id: WidgetId,
}
/// state maintained between widgets during painting
pub struct DrawState<'a, State> {
ui: &'a mut Ui<State>,
cache_width: HashMap<WidgetId, (UiVec2, Len)>,
cache_height: HashMap<WidgetId, (UiVec2, Len)>,
draw_started: HashSet<WidgetId>,
} }
/// stores information for children about the highest level parent that needed their size /// stores information for children about the highest level parent that needed their size
/// so that they can redraw the parent if their size changes /// 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<(WidgetId, (UiVec2, Len))>, pub x: Option<(WidgetId, (UiVec2, Len))>,
y: Option<(WidgetId, (UiVec2, Len))>, pub y: Option<(WidgetId, (UiVec2, Len))>,
} }
/// important non rendering data for retained drawing /// important non rendering data for retained drawing
@@ -52,316 +43,6 @@ pub struct ActiveData {
pub layer: usize, pub layer: usize,
} }
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(),
}
}
/// 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);
}
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,
}
}
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: 'static> Ui<State> {
pub fn redraw_all(&mut self) {
for (id, active) in self.active.drain() {
let data = self.widgets.data(id).unwrap();
self.events.undraw(data, &active);
}
// free before bc nothing should exist
self.free();
if let Some(root) = &self.root {
self.draw(root.id());
}
}
fn draw(&mut self, id: WidgetId) {
self.layers.clear();
self.widgets.needs_redraw.clear();
let mut state = DrawState::new(self);
state.draw_inner(0, id, UiRegion::FULL, None, MaskIdx::NONE, None);
}
pub fn redraw_updates(&mut self) {
let mut state = DrawState::new(self);
while let Some(&id) = state.widgets.needs_redraw.iter().next() {
state.redraw(id);
}
self.free();
}
}
impl<'a, 'c, State: 'static> Painter<'a, 'c, State> { impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
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.state.layers.write( let h = self.state.layers.write(
@@ -488,121 +169,5 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
} }
} }
pub struct SizeCtx<'a, State> { impl<State: 'static> Ui<State> {
pub text: &'a mut TextData,
pub textures: &'a mut Textures,
source: WidgetId,
widgets: &'a Widgets<State>,
cache_width: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
cache_height: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
checked_width: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
checked_height: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
/// TODO: should this be pub? rn used for sized
pub outer: UiVec2,
output_size: Vec2,
id: WidgetId,
}
impl<State: 'static> SizeCtx<'_, State> {
pub fn id(&self) -> &WidgetId {
&self.id
}
pub fn source(&self) -> &WidgetId {
&self.source
}
fn width_inner(&mut self, id: WidgetId) -> Len {
// first check cache
// 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;
// get size of input id
self.id = id;
let len = self.widgets.get_dyn_dynamic(id).desired_width(self);
// restore vars & update cache + checked
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: WidgetId) -> 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: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Len {
self.width_inner(id.id())
}
pub fn height<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Len {
self.height_inner(id.id())
}
pub fn len_axis<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, axis: Axis) -> Len {
match axis {
Axis::X => self.width(id),
Axis::Y => self.height(id),
}
}
pub fn size<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Size {
Size {
x: self.width(id),
y: self.height(id),
}
}
pub fn px_size(&mut self) -> Vec2 {
self.outer.to_abs(self.output_size)
}
pub fn output_size(&mut self) -> Vec2 {
self.output_size
}
pub fn draw_text(&mut self, buffer: &mut TextBuffer, attrs: &TextAttrs) -> RenderedText {
self.text.draw(buffer, attrs, self.textures)
}
pub fn label(&self, id: WidgetId) -> &String {
self.widgets.label(id)
}
}
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
}
} }

111
core/src/ui/size.rs Normal file
View File

@@ -0,0 +1,111 @@
use crate::{
Axis, Len, RenderedText, Size, TextAttrs, TextBuffer, TextData, Textures, UiVec2, WidgetHandle,
WidgetId, Widgets,
util::{HashMap, Vec2},
};
pub struct SizeCtx<'a, State> {
pub text: &'a mut TextData,
pub textures: &'a mut Textures,
pub(super) source: WidgetId,
pub(super) widgets: &'a Widgets<State>,
pub(super) cache_width: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
pub(super) cache_height: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
pub(super) checked_width: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
pub(super) checked_height: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
/// TODO: should this be pub? rn used for sized
pub outer: UiVec2,
pub(super) output_size: Vec2,
pub(super) id: WidgetId,
}
impl<State: 'static> SizeCtx<'_, State> {
pub fn id(&self) -> &WidgetId {
&self.id
}
pub fn source(&self) -> &WidgetId {
&self.source
}
pub(super) fn width_inner(&mut self, id: WidgetId) -> Len {
// first check cache
// 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;
// get size of input id
self.id = id;
let len = self.widgets.get_dyn_dynamic(id).desired_width(self);
// restore vars & update cache + checked
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
pub(super) fn height_inner(&mut self, id: WidgetId) -> 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: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Len {
self.width_inner(id.id())
}
pub fn height<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Len {
self.height_inner(id.id())
}
pub fn len_axis<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, axis: Axis) -> Len {
match axis {
Axis::X => self.width(id),
Axis::Y => self.height(id),
}
}
pub fn size<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Size {
Size {
x: self.width(id),
y: self.height(id),
}
}
pub fn px_size(&mut self) -> Vec2 {
self.outer.to_abs(self.output_size)
}
pub fn output_size(&mut self) -> Vec2 {
self.output_size
}
pub fn draw_text(&mut self, buffer: &mut TextBuffer, attrs: &TextAttrs) -> RenderedText {
self.text.draw(buffer, attrs, self.textures)
}
pub fn label(&self, id: WidgetId) -> &String {
self.widgets.label(id)
}
}

View File

@@ -18,8 +18,8 @@ pub struct UiRenderer {
} }
impl UiRenderer { impl UiRenderer {
pub fn update<State>(&mut self, updates: &mut Ui<State>) { pub fn update<State>(&mut self, ui: &mut Ui<State>) {
self.ui.update(&self.device, &self.queue, updates); self.ui.update(&self.device, &self.queue, ui);
} }
pub fn draw(&mut self) { pub fn draw(&mut self) {