Files
iris/src/layout/painter.rs
2025-09-25 21:19:47 -04:00

331 lines
9.4 KiB
Rust

use crate::{
layout::{
ActiveWidgets, Layers, Modules, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
Textures, UiRegion, UiVec2, Vec2, WidgetId, Widgets,
},
render::{Primitive, PrimitiveHandle},
util::{HashMap, HashSet, Id, IdUtil},
};
pub struct Painter<'a, 'c> {
ctx: &'a mut PainterCtx<'c>,
region: UiRegion,
textures: Vec<TextureHandle>,
primitives: Vec<PrimitiveHandle>,
children: Vec<Id>,
sized_children: HashMap<Id, UiVec2>,
pub layer: usize,
id: Id,
}
pub struct PainterCtx<'a> {
pub widgets: &'a Widgets,
pub active: &'a mut ActiveWidgets,
pub layers: &'a mut Layers,
pub textures: &'a mut Textures,
pub text: &'a mut TextData,
pub screen_size: Vec2,
pub modules: &'a mut Modules,
drawing: HashSet<Id>,
}
pub struct WidgetInstance {
pub id: Id,
pub region: UiRegion,
pub parent: Option<Id>,
pub textures: Vec<TextureHandle>,
pub primitives: Vec<PrimitiveHandle>,
pub children: Vec<Id>,
pub resize: Option<(Id, UiVec2)>,
pub layer: usize,
}
impl<'a> PainterCtx<'a> {
pub fn new(
widgets: &'a Widgets,
layers: &'a mut Layers,
active: &'a mut ActiveWidgets,
modules: &'a mut Modules,
textures: &'a mut Textures,
text: &'a mut TextData,
screen_size: Vec2,
) -> Self {
Self {
widgets,
active,
layers,
textures,
text,
screen_size,
modules,
drawing: HashSet::default(),
}
}
pub fn redraw(&mut self, id: &Id) {
self.drawing.clear();
let Some(active) = self.active.get(id) else {
return;
};
if let Some((rid, size)) = &active.resize {
let checked = &mut HashMap::default();
let mut ctx = SizeCtx {
checked,
text: self.text,
textures: self.textures,
widgets: self.widgets,
};
let desired = ctx.size_inner(id);
if *size != desired {
self.redraw(&rid.duplicate());
if self.drawing.contains(id) {
return;
}
}
}
let Some(active) = self.remove(id) else {
return;
};
self.draw_inner(
active.layer,
id,
active.region,
active.parent.duplicate(),
Some(active.children),
);
self.active.get_mut(id).unwrap().resize = active.resize;
}
pub fn draw(&mut self, id: &Id) {
self.drawing.clear();
self.layers.clear();
self.draw_inner(0, id, UiRegion::full(), None, None);
}
fn draw_inner(
&mut self,
layer: usize,
id: &Id,
region: UiRegion,
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 (1)");
}
let mut old_children = old_children.unwrap_or_default();
let mut resize = None;
if let Some(active) = self.active.get_mut(id) {
if active.parent != parent {
panic!("Cannot draw the same widget twice (2)");
}
if active.region == region {
return;
}
// TODO:
// else if active.region.size() == region.size() { move }
let active = self.remove(id).unwrap();
old_children = active.children;
resize = active.resize;
}
self.drawing.insert(id.duplicate());
let mut painter = Painter {
region,
layer,
id: id.duplicate(),
textures: Vec::new(),
primitives: Vec::new(),
ctx: self,
children: Vec::new(),
sized_children: Default::default(),
};
// draw widgets
painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter);
let sized_children = painter.sized_children;
// add to active
let instance = WidgetInstance {
id: id.duplicate(),
region,
parent,
textures: painter.textures,
primitives: painter.primitives,
children: painter.children,
resize,
layer,
};
for (cid, size) in sized_children {
if let Some(w) = self.active.get_mut(&cid) {
w.resize = Some((id.duplicate(), size))
}
}
for c in &old_children {
if !instance.children.contains(c) {
self.remove_rec(c);
}
}
for m in self.modules.iter_mut() {
m.on_draw(&instance);
}
self.active.insert(id.duplicate(), instance);
}
/// NOTE: instance textures are cleared and self.textures freed
fn remove(&mut self, id: &Id) -> Option<WidgetInstance> {
let mut inst = self.active.remove(id);
if let Some(inst) = &mut inst {
for h in &inst.primitives {
self.layers.free(h);
}
inst.textures.clear();
self.textures.free();
for m in self.modules.iter_mut() {
m.on_undraw(inst);
}
}
inst
}
fn remove_rec(&mut self, id: &Id) -> Option<WidgetInstance> {
let inst = self.remove(id);
if let Some(inst) = &inst {
for c in &inst.children {
self.remove_rec(c);
}
}
inst
}
}
impl<'a, 'c> Painter<'a, 'c> {
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
let h = self
.ctx
.layers
.write(self.layer, self.id.duplicate(), primitive, region);
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));
}
/// Draws a widget within this widget's region.
pub fn widget<W>(&mut self, id: &WidgetId<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>(&mut self, id: &WidgetId<W>, region: UiRegion) {
self.widget_at(id, region.within(&self.region));
}
fn widget_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
self.children.push(id.id.duplicate());
self.ctx
.draw_inner(self.layer, &id.id, region, Some(self.id.duplicate()), 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,
) -> (TextureHandle, TextOffset) {
self.ctx.text.draw(buffer, attrs, self.ctx.textures)
}
pub fn region(&self) -> UiRegion {
self.region
}
pub fn size<W>(&mut self, id: &WidgetId<W>) -> UiVec2 {
self.size_ctx().size(id)
}
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
SizeCtx {
text: self.ctx.text,
textures: self.ctx.textures,
widgets: self.ctx.widgets,
checked: &mut self.sized_children,
}
}
pub fn text_data(&mut self) -> &mut TextData {
self.ctx.text
}
pub fn child_layer(&mut self) {
self.layer = self.ctx.layers.child(self.layer);
}
pub fn next_layer(&mut self) {
self.layer = self.ctx.layers.next(self.layer);
}
}
pub struct SizeCtx<'a> {
pub text: &'a mut TextData,
pub textures: &'a mut Textures,
widgets: &'a Widgets,
checked: &'a mut HashMap<Id, UiVec2>,
}
impl SizeCtx<'_> {
fn size_inner(&mut self, id: &Id) -> UiVec2 {
let size = self.widgets.get_dyn_dynamic(id).desired_size(self);
self.checked.insert(id.duplicate(), size);
size
}
pub fn size<W>(&mut self, id: &WidgetId<W>) -> UiVec2 {
// TODO: determine if this is useful
// if let Some(size) = self.checked.get(&id.id) {
// return Some(*size);
// }
self.size_inner(&id.id)
}
pub fn draw_text(
&mut self,
buffer: &mut TextBuffer,
attrs: &TextAttrs,
) -> (TextureHandle, TextOffset) {
self.text.draw(buffer, attrs, self.textures)
}
}