195 lines
4.9 KiB
Rust
195 lines
4.9 KiB
Rust
use crate::{
|
|
ActiveSensors, HashMap, Painter, SenseCtx, Sensor, SensorMap, Widget, WidgetId, WidgetLike,
|
|
primitive::Primitives,
|
|
util::{IDTracker, Id},
|
|
};
|
|
use std::{
|
|
any::{Any, TypeId},
|
|
ops::{Index, IndexMut},
|
|
};
|
|
|
|
pub struct Ui<Ctx> {
|
|
ids: IDTracker,
|
|
base: Option<WidgetId>,
|
|
widgets: Widgets<Ctx>,
|
|
updates: Vec<WidgetId>,
|
|
active_sensors: ActiveSensors<Ctx>,
|
|
sensor_map: SensorMap<Ctx>,
|
|
primitives: Primitives,
|
|
full_redraw: bool,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub struct Widgets<Ctx>(HashMap<Id, Box<dyn Widget<Ctx>>>);
|
|
|
|
impl<Ctx> Ui<Ctx> {
|
|
pub fn add<W: Widget<Ctx>, Tag>(
|
|
&mut self,
|
|
w: impl WidgetLike<Ctx, Tag, Widget = W>,
|
|
) -> WidgetId<W> {
|
|
w.add(self)
|
|
}
|
|
|
|
pub fn add_widget<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
|
|
self.push(w)
|
|
}
|
|
|
|
pub fn push<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
|
|
let id = self.ids.next();
|
|
self.widgets.insert(id.duplicate(), w);
|
|
WidgetId::new(id, TypeId::of::<W>())
|
|
}
|
|
|
|
pub fn set<W: Widget<Ctx>>(&mut self, i: &WidgetId<W>, w: W) {
|
|
self.widgets.insert(i.id.duplicate(), w);
|
|
}
|
|
|
|
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) {
|
|
self.base = Some(w.add(self).erase_type());
|
|
self.full_redraw = true;
|
|
}
|
|
|
|
pub fn new() -> Self
|
|
where
|
|
Ctx: 'static,
|
|
{
|
|
Self::default()
|
|
}
|
|
|
|
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
|
|
self.widgets.get(id)
|
|
}
|
|
|
|
pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
|
self.widgets.get_mut(id)
|
|
}
|
|
|
|
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
|
|
WidgetId::new(self.ids.next(), TypeId::of::<W>())
|
|
}
|
|
|
|
pub fn redraw_all(&mut self, ctx: &mut Ctx)
|
|
where
|
|
Ctx: 'static,
|
|
{
|
|
self.active_sensors.clear();
|
|
let mut painter = Painter::new(
|
|
&self.widgets,
|
|
ctx,
|
|
&mut self.sensor_map,
|
|
&mut self.active_sensors,
|
|
);
|
|
if let Some(base) = &self.base {
|
|
painter.draw(base);
|
|
}
|
|
self.primitives = painter.finish();
|
|
}
|
|
|
|
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor<Ctx>) {
|
|
self.sensor_map
|
|
.entry(id.id.duplicate())
|
|
.or_default()
|
|
.push(f);
|
|
}
|
|
|
|
pub fn run_sensors(&mut self, ctx: &mut Ctx)
|
|
where
|
|
Ctx: SenseCtx + 'static,
|
|
{
|
|
for sensors in self.active_sensors.clone().iter().rev() {
|
|
for sensor in sensors {
|
|
if ctx.active(&sensor.trigger) {
|
|
(sensor.f.box_clone())(self, ctx);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn update(&mut self, ctx: &mut Ctx) -> Option<&Primitives>
|
|
where
|
|
Ctx: 'static,
|
|
{
|
|
if self.full_redraw {
|
|
self.redraw_all(ctx);
|
|
self.full_redraw = false;
|
|
return Some(&self.primitives);
|
|
}
|
|
if self.updates.is_empty() {
|
|
return None;
|
|
}
|
|
self.redraw_all(ctx);
|
|
self.updates.drain(..);
|
|
Some(&self.primitives)
|
|
}
|
|
|
|
pub fn needs_redraw(&self) -> bool {
|
|
self.full_redraw || !self.updates.is_empty()
|
|
}
|
|
}
|
|
|
|
impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
|
|
type Output = W;
|
|
|
|
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
|
|
self.get(id).unwrap()
|
|
}
|
|
}
|
|
|
|
impl<W: Widget<Ctx>, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> {
|
|
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
|
|
self.updates.push(id.clone().erase_type());
|
|
self.get_mut(id).unwrap()
|
|
}
|
|
}
|
|
|
|
impl<Ctx> Widgets<Ctx> {
|
|
pub fn new() -> Self {
|
|
Self(HashMap::new())
|
|
}
|
|
|
|
pub fn get_dyn<W>(&self, id: &WidgetId<W>) -> &dyn Widget<Ctx> {
|
|
self.0.get(&id.id).unwrap().as_ref()
|
|
}
|
|
|
|
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
|
|
self.0.get(&id.id).unwrap().as_any().downcast_ref()
|
|
}
|
|
|
|
pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
|
self.0.get_mut(&id.id).unwrap().as_any_mut().downcast_mut()
|
|
}
|
|
|
|
pub fn insert(&mut self, id: Id, widget: impl Widget<Ctx>) {
|
|
self.0.insert(id, Box::new(widget));
|
|
}
|
|
|
|
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget<Ctx>>) {
|
|
self.0.insert(id, widget);
|
|
}
|
|
}
|
|
|
|
impl<Ctx> dyn Widget<Ctx> {
|
|
pub fn as_any(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
|
|
pub fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<Ctx: 'static> Default for Ui<Ctx> {
|
|
fn default() -> Self {
|
|
Self {
|
|
ids: Default::default(),
|
|
base: Default::default(),
|
|
widgets: Widgets::new(),
|
|
updates: Default::default(),
|
|
primitives: Default::default(),
|
|
full_redraw: false,
|
|
active_sensors: Default::default(),
|
|
sensor_map: Default::default(),
|
|
}
|
|
}
|
|
}
|