82 lines
2.2 KiB
Rust
82 lines
2.2 KiB
Rust
use crate::{
|
|
ActiveSensor, ActiveSensors, SenseTrigger, SensorMap, UiRegion, WidgetId, Widgets,
|
|
primitive::{PrimitiveData, PrimitiveInstance, Primitives},
|
|
};
|
|
|
|
pub struct Painter<'a, Ctx: 'static> {
|
|
nodes: &'a Widgets<Ctx>,
|
|
ctx: &'a mut Ctx,
|
|
sensors_map: &'a mut SensorMap<Ctx>,
|
|
active_sensors: &'a mut ActiveSensors<Ctx>,
|
|
primitives: Primitives,
|
|
pub region: UiRegion,
|
|
}
|
|
|
|
impl<'a, Ctx> Painter<'a, Ctx> {
|
|
pub fn new(
|
|
nodes: &'a Widgets<Ctx>,
|
|
ctx: &'a mut Ctx,
|
|
sensors_map: &'a mut SensorMap<Ctx>,
|
|
active_sensors: &'a mut ActiveSensors<Ctx>,
|
|
) -> Self {
|
|
Self {
|
|
nodes,
|
|
ctx,
|
|
active_sensors,
|
|
sensors_map,
|
|
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<W>(&mut self, id: &WidgetId<W>)
|
|
where
|
|
Ctx: 'static,
|
|
{
|
|
if let Some(sensors) = self.sensors_map.get(&id.id) {
|
|
self.active_sensors.push(
|
|
sensors
|
|
.iter()
|
|
.map(|sensor| ActiveSensor {
|
|
trigger: SenseTrigger {
|
|
shape: self.region,
|
|
sense: sensor.sense,
|
|
},
|
|
f: sensor.f.box_clone(),
|
|
})
|
|
.collect(),
|
|
);
|
|
}
|
|
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(®ion);
|
|
self.draw(node);
|
|
self.region = old;
|
|
}
|
|
|
|
pub fn finish(self) -> Primitives {
|
|
self.primitives
|
|
}
|
|
|
|
pub fn ctx_mut(&mut self) -> &mut Ctx {
|
|
self.ctx
|
|
}
|
|
}
|