made painter actually how I wanted it (draw now takes in an owned painter)

This commit is contained in:
2025-08-23 15:20:25 -04:00
parent 6fbdf9fbc8
commit 2ffb09bef0
10 changed files with 60 additions and 50 deletions

View File

@@ -6,17 +6,18 @@ use crate::{
pub struct Painter<'a, Ctx: 'static> {
nodes: &'a Widgets<Ctx>,
ctx: &'a mut Ctx,
sensors_map: &'a mut SensorMap<Ctx>,
sensors_map: &'a SensorMap<Ctx>,
active_sensors: &'a mut ActiveSensors,
primitives: Primitives,
pub region: UiRegion,
primitives: &'a mut Primitives,
region: UiRegion,
}
impl<'a, Ctx> Painter<'a, Ctx> {
pub fn new(
nodes: &'a Widgets<Ctx>,
primitives: &'a mut Primitives,
ctx: &'a mut Ctx,
sensors_map: &'a mut SensorMap<Ctx>,
sensors_map: &'a SensorMap<Ctx>,
active_sensors: &'a mut ActiveSensors,
) -> Self {
Self {
@@ -24,59 +25,56 @@ impl<'a, Ctx> Painter<'a, Ctx> {
ctx,
active_sensors,
sensors_map,
primitives: Primitives::default(),
primitives,
region: UiRegion::full(),
}
}
/// Writes a primitive to be rendered
pub fn write<P: Primitive>(&mut self, data: P) {
self.primitives.write(data, self.region);
}
/// Draws a widget but does NOT maintain the region.
/// Useful if you only have a single widget to draw.
pub fn draw_inner<W>(&mut self, id: &WidgetId<W>)
where
Ctx: 'static,
{
if self.sensors_map.get(&id.id).is_some() {
self.active_sensors.push((self.region, id.id.duplicate()));
}
self.nodes.get_dyn(id).draw(self);
}
/// Draws a widget and maintains the original region.
/// Useful if you need to draw multiple overlapping child widgets.
/// Draws a widget within this widget's region.
pub fn draw<W>(&mut self, id: &WidgetId<W>)
where
Ctx: 'static,
{
let old = self.region;
self.draw_inner(id);
self.region = old;
self.draw_at(id, self.region);
}
/// Draws a widget within an inner region and maintains the original region.
/// Useful if you need to draw multiple child widgets in select areas within the original
/// region.
pub fn draw_within(&mut self, id: &WidgetId, region: UiRegion)
/// Draws a widget somewhere within this one.
/// Useful for drawing child widgets in select areas.
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion)
where
Ctx: 'static,
{
let old = self.region;
self.region.select(&region);
self.draw_inner(id);
self.region = old;
self.draw_at(id, region.within(&self.region));
}
/// Draws a widget in an arbitrary region.
pub fn draw_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion)
where
Ctx: 'static,
{
if self.sensors_map.get(&id.id).is_some() {
self.active_sensors.push((region, id.id.duplicate()));
}
self.nodes.get_dyn(id).draw(Painter {
nodes: self.nodes,
ctx: self.ctx,
sensors_map: self.sensors_map,
active_sensors: self.active_sensors,
primitives: self.primitives,
region,
});
}
pub fn draw_texture(&mut self, handle: &TextureHandle) {
self.write(handle.inner);
}
pub fn finish(self) -> Primitives {
self.primitives
}
pub fn ctx_mut(&mut self) -> &mut Ctx {
pub fn ctx(&mut self) -> &mut Ctx {
self.ctx
}
}