preparation
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::any::Any;
|
||||
|
||||
use crate::{
|
||||
layout::{Ui, UiRegion, Vec2, WidgetId},
|
||||
util::{HashMap, Id, bitflags},
|
||||
@@ -42,15 +44,15 @@ pub struct SensorGroup {
|
||||
pub cursor: ActivationState,
|
||||
pub sensors: Vec<Sensor>,
|
||||
}
|
||||
pub trait SenseFn: FnMut(&mut Ui) + 'static {
|
||||
fn box_clone(&self) -> Box<dyn SenseFn>;
|
||||
}
|
||||
impl<F: FnMut(&mut Ui) + 'static + Clone> SenseFn for F {
|
||||
fn box_clone(&self) -> Box<dyn SenseFn> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
pub struct SenseCtx {
|
||||
pub cursor: Vec2,
|
||||
pub size: Vec2,
|
||||
}
|
||||
|
||||
pub trait SenseFn: FnMut(&mut Ui, SenseCtx) + 'static {}
|
||||
impl<F: FnMut(&mut Ui, SenseCtx) + 'static> SenseFn for F {}
|
||||
|
||||
impl Ui {
|
||||
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor) {
|
||||
self.sensor_map
|
||||
@@ -72,7 +74,11 @@ impl Ui {
|
||||
|
||||
for sensor in &mut group.sensors {
|
||||
if should_run(sensor.senses, group.cursor, group.hover) {
|
||||
(sensor.f.box_clone())(self);
|
||||
let ctx = SenseCtx {
|
||||
cursor: cursor.pos - region.top_left,
|
||||
size: region.bot_right - region.top_left,
|
||||
};
|
||||
(sensor.f)(self, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,21 +138,3 @@ impl ActivationState {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for SensorGroup {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
hover: self.hover,
|
||||
cursor: self.cursor,
|
||||
sensors: self.sensors.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Clone for Sensor {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
senses: self.senses,
|
||||
f: self.f.box_clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user