REAL SENSORS
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
HashMap, Painter, SenseCtx, UiRegion, Widget, WidgetId, WidgetLike,
|
||||
ActiveSensors, HashMap, Painter, SenseCtx, Sensor, SensorMap, Widget, WidgetId, WidgetLike,
|
||||
primitive::Primitives,
|
||||
util::{IDTracker, Id},
|
||||
};
|
||||
@@ -13,7 +13,8 @@ pub struct Ui<Ctx> {
|
||||
base: Option<WidgetId>,
|
||||
widgets: Widgets<Ctx>,
|
||||
updates: Vec<WidgetId>,
|
||||
sensors: Sensors<Ctx>,
|
||||
active_sensors: ActiveSensors<Ctx>,
|
||||
sensor_map: SensorMap<Ctx>,
|
||||
primitives: Primitives,
|
||||
full_redraw: bool,
|
||||
}
|
||||
@@ -21,27 +22,6 @@ pub struct Ui<Ctx> {
|
||||
#[derive(Default)]
|
||||
pub struct Widgets<Ctx>(HashMap<Id, Box<dyn Widget<Ctx>>>);
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Sense {
|
||||
Click,
|
||||
Hover,
|
||||
}
|
||||
pub type Sensors<Ctx> = Vec<(SenseTrigger, Box<dyn SenseFn<Ctx>>)>;
|
||||
pub trait SenseFn_<Ctx> = Fn(&mut Ui<Ctx>, &mut Ctx) + 'static;
|
||||
pub type SenseShape = UiRegion;
|
||||
pub struct SenseTrigger {
|
||||
pub shape: SenseShape,
|
||||
pub sense: Sense,
|
||||
}
|
||||
pub trait SenseFn<Ctx>: SenseFn_<Ctx> {
|
||||
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>>;
|
||||
}
|
||||
impl<F: SenseFn_<Ctx> + Clone, Ctx> SenseFn<Ctx> for F {
|
||||
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> Ui<Ctx> {
|
||||
pub fn add<W: Widget<Ctx>, Tag>(
|
||||
&mut self,
|
||||
@@ -69,7 +49,10 @@ impl<Ctx> Ui<Ctx> {
|
||||
self.full_redraw = true;
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
pub fn new() -> Self
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
Self::default()
|
||||
}
|
||||
|
||||
@@ -89,23 +72,35 @@ impl<Ctx> Ui<Ctx> {
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
self.sensors.clear();
|
||||
let mut painter = Painter::new(&self.widgets, ctx, &mut self.sensors);
|
||||
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,
|
||||
Ctx: SenseCtx + 'static,
|
||||
{
|
||||
for (t, f) in self.sensors.iter().rev() {
|
||||
if ctx.active(t) {
|
||||
let f = f.as_ref().box_clone();
|
||||
f(self, ctx);
|
||||
return;
|
||||
for sensors in self.active_sensors.clone().iter().rev() {
|
||||
for sensor in sensors {
|
||||
if ctx.active(&sensor.trigger) {
|
||||
(sensor.f.box_clone())(self, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,7 +178,7 @@ impl<Ctx> dyn Widget<Ctx> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> Default for Ui<Ctx> {
|
||||
impl<Ctx: 'static> Default for Ui<Ctx> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
ids: Default::default(),
|
||||
@@ -192,7 +187,8 @@ impl<Ctx> Default for Ui<Ctx> {
|
||||
updates: Default::default(),
|
||||
primitives: Default::default(),
|
||||
full_redraw: false,
|
||||
sensors: Default::default(),
|
||||
active_sensors: Default::default(),
|
||||
sensor_map: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user