This commit is contained in:
2025-08-15 15:48:00 -04:00
parent c5aa0a02e2
commit 78ea738b8e
8 changed files with 222 additions and 52 deletions

View File

@@ -1,20 +1,22 @@
use crate::{
UiRegion, WidgetId, Widgets,
SenseFn, SenseTrigger, Sensors, UiRegion, WidgetId, Widgets,
primitive::{PrimitiveData, PrimitiveInstance, Primitives},
};
pub struct Painter<'a, Ctx> {
nodes: &'a Widgets<Ctx>,
ctx: &'a mut Ctx,
sensors: &'a mut Sensors<Ctx>,
primitives: Primitives,
pub region: UiRegion,
}
impl<'a, Ctx> Painter<'a, Ctx> {
pub fn new(nodes: &'a Widgets<Ctx>, ctx: &'a mut Ctx) -> Self {
pub fn new(nodes: &'a Widgets<Ctx>, ctx: &'a mut Ctx, sensors: &'a mut Sensors<Ctx>) -> Self {
Self {
nodes,
ctx,
sensors,
primitives: Primitives::default(),
region: UiRegion::full(),
}
@@ -31,17 +33,27 @@ impl<'a, Ctx> Painter<'a, Ctx> {
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data]));
}
pub fn draw(&mut self, id: &WidgetId) where Ctx: 'static {
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 {
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 sense(&mut self, trigger: SenseTrigger, f: Box<dyn SenseFn<Ctx>>) {
self.sensors.push((trigger, f));
}
pub fn finish(self) -> Primitives {
self.primitives
}