make painter not stupid (size ctx is kinda tho)
This commit is contained in:
608
core/src/ui/painter.rs
Normal file
608
core/src/ui/painter.rs
Normal file
@@ -0,0 +1,608 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use crate::{
|
||||
Axis, Len, RenderedText, Size, TextAttrs, TextBuffer, TextData, TextureHandle, Textures, Ui,
|
||||
UiRegion, UiVec2, WidgetHandle, WidgetId, Widgets,
|
||||
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
|
||||
util::{HashMap, HashSet, Vec2},
|
||||
};
|
||||
|
||||
/// makes your surfaces look pretty
|
||||
pub struct Painter<'a, 'b, State> {
|
||||
state: &'a mut DrawState<'b, State>,
|
||||
|
||||
region: UiRegion,
|
||||
mask: MaskIdx,
|
||||
textures: Vec<TextureHandle>,
|
||||
primitives: Vec<PrimitiveHandle>,
|
||||
children: Vec<WidgetId>,
|
||||
children_width: HashMap<WidgetId, (UiVec2, Len)>,
|
||||
children_height: HashMap<WidgetId, (UiVec2, Len)>,
|
||||
pub layer: usize,
|
||||
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
|
||||
/// so that they can redraw the parent if their size changes
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct ResizeRef {
|
||||
x: Option<(WidgetId, (UiVec2, Len))>,
|
||||
y: Option<(WidgetId, (UiVec2, Len))>,
|
||||
}
|
||||
|
||||
/// important non rendering data for retained drawing
|
||||
#[derive(Debug)]
|
||||
pub struct ActiveData {
|
||||
pub id: WidgetId,
|
||||
pub region: UiRegion,
|
||||
pub parent: Option<WidgetId>,
|
||||
pub textures: Vec<TextureHandle>,
|
||||
pub primitives: Vec<PrimitiveHandle>,
|
||||
pub children: Vec<WidgetId>,
|
||||
pub resize: ResizeRef,
|
||||
pub mask: MaskIdx,
|
||||
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> {
|
||||
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
let h = self.state.layers.write(
|
||||
self.layer,
|
||||
PrimitiveInst {
|
||||
id: self.id,
|
||||
primitive,
|
||||
region,
|
||||
mask_idx: self.mask,
|
||||
},
|
||||
);
|
||||
if self.mask != MaskIdx::NONE {
|
||||
// TODO: I have no clue if this works at all :joy:
|
||||
self.state.masks.push_ref(self.mask);
|
||||
}
|
||||
self.primitives.push(h);
|
||||
}
|
||||
|
||||
/// Writes a primitive to be rendered
|
||||
pub fn primitive<P: Primitive>(&mut self, primitive: P) {
|
||||
self.primitive_at(primitive, self.region)
|
||||
}
|
||||
|
||||
pub fn primitive_within<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
self.primitive_at(primitive, region.within(&self.region));
|
||||
}
|
||||
|
||||
pub fn set_mask(&mut self, region: UiRegion) {
|
||||
assert!(self.mask == MaskIdx::NONE);
|
||||
self.mask = self.state.masks.push(Mask { region });
|
||||
}
|
||||
|
||||
/// Draws a widget within this widget's region.
|
||||
pub fn widget<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) {
|
||||
self.widget_at(id, self.region);
|
||||
}
|
||||
|
||||
/// Draws a widget somewhere within this one.
|
||||
/// Useful for drawing child widgets in select areas.
|
||||
pub fn widget_within<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, region: UiRegion) {
|
||||
self.widget_at(id, region.within(&self.region));
|
||||
}
|
||||
|
||||
fn widget_at<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, region: UiRegion) {
|
||||
self.children.push(id.id());
|
||||
self.state
|
||||
.draw_inner(self.layer, id.id(), region, Some(self.id), self.mask, None);
|
||||
}
|
||||
|
||||
pub fn texture_within(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
self.textures.push(handle.clone());
|
||||
self.primitive_at(handle.primitive(), region.within(&self.region));
|
||||
}
|
||||
|
||||
pub fn texture(&mut self, handle: &TextureHandle) {
|
||||
self.textures.push(handle.clone());
|
||||
self.primitive(handle.primitive());
|
||||
}
|
||||
|
||||
pub fn texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
self.textures.push(handle.clone());
|
||||
self.primitive_at(handle.primitive(), region);
|
||||
}
|
||||
|
||||
/// returns (handle, offset from top left)
|
||||
pub fn render_text(&mut self, buffer: &mut TextBuffer, attrs: &TextAttrs) -> RenderedText {
|
||||
self.state
|
||||
.ui
|
||||
.text
|
||||
.draw(buffer, attrs, &mut self.state.ui.textures)
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
self.region
|
||||
}
|
||||
|
||||
pub fn size<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Size {
|
||||
self.size_ctx().size(id)
|
||||
}
|
||||
|
||||
pub fn len_axis<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, axis: Axis) -> Len {
|
||||
match axis {
|
||||
Axis::X => self.size_ctx().width(id),
|
||||
Axis::Y => self.size_ctx().height(id),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn output_size(&self) -> Vec2 {
|
||||
self.state.output_size
|
||||
}
|
||||
|
||||
pub fn px_size(&mut self) -> Vec2 {
|
||||
self.region.size().to_abs(self.state.output_size)
|
||||
}
|
||||
|
||||
pub fn text_data(&mut self) -> &mut TextData {
|
||||
&mut self.state.text
|
||||
}
|
||||
|
||||
pub fn child_layer(&mut self) {
|
||||
self.layer = self.state.layers.child(self.layer);
|
||||
}
|
||||
|
||||
pub fn next_layer(&mut self) {
|
||||
self.layer = self.state.layers.next(self.layer);
|
||||
}
|
||||
|
||||
pub fn label(&self) -> &str {
|
||||
&self.state.widgets.data(self.id).unwrap().label
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &WidgetId {
|
||||
&self.id
|
||||
}
|
||||
|
||||
pub fn size_ctx(&mut self) -> SizeCtx<'_, State> {
|
||||
self.state.size_ctx(
|
||||
self.id,
|
||||
self.region.size(),
|
||||
self.id,
|
||||
&mut self.children_width,
|
||||
&mut self.children_height,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SizeCtx<'a, 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user