Files
iris/src/layout/painter.rs
2025-08-14 15:05:55 -04:00

53 lines
1.4 KiB
Rust

use crate::{
UiRegion, WidgetId, Widgets,
primitive::{PrimitiveData, PrimitiveInstance, Primitives},
};
pub struct Painter<'a, Ctx> {
nodes: &'a Widgets<Ctx>,
ctx: &'a mut Ctx,
primitives: Primitives,
pub region: UiRegion,
}
impl<'a, Ctx> Painter<'a, Ctx> {
pub fn new(nodes: &'a Widgets<Ctx>, ctx: &'a mut Ctx) -> Self {
Self {
nodes,
ctx,
primitives: Primitives::default(),
region: UiRegion::full(),
}
}
pub fn write<Data: PrimitiveData>(&mut self, data: Data) {
let ptr = self.primitives.data.len() as u32;
let region = self.region;
self.primitives
.instances
.push(PrimitiveInstance { region, ptr });
self.primitives.data.push(Data::DISCRIM);
self.primitives
.data
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data]));
}
pub fn draw(&mut self, id: &WidgetId) where Ctx: 'static {
self.nodes.get_dyn(id).draw(self);
}
pub fn draw_within(&mut self, node: &WidgetId, region: UiRegion) where Ctx: 'static {
let old = self.region;
self.region.select(&region);
self.draw(node);
self.region = old;
}
pub fn finish(self) -> Primitives {
self.primitives
}
pub fn ctx_mut(&mut self) -> &mut Ctx {
self.ctx
}
}