refactor events
This commit is contained in:
151
core/src/event/manager.rs
Normal file
151
core/src/event/manager.rs
Normal file
@@ -0,0 +1,151 @@
|
||||
use crate::{
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, IdLike, LayerId, Ui, WidgetData, WidgetId,
|
||||
util::HashMap,
|
||||
};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EventManager {
|
||||
types: HashMap<TypeId, Box<dyn EventManagerLike>>,
|
||||
}
|
||||
|
||||
impl EventManager {
|
||||
pub fn get_type<E: EventLike, Ctx: 'static>(&mut self) -> &mut TypeEventManager<E::Event, Ctx> {
|
||||
self.types
|
||||
.entry(Self::type_key::<E, Ctx>())
|
||||
.or_insert(Box::new(TypeEventManager::<E::Event, Ctx>::default()))
|
||||
.as_any()
|
||||
.downcast_mut()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn register<I: IdLike, E: EventLike, Ctx: 'static>(
|
||||
&mut self,
|
||||
id: I,
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<Ctx, <E::Event as Event>::Data<'a>>,
|
||||
) {
|
||||
self.get_type::<E, Ctx>().register(id, event, f);
|
||||
}
|
||||
|
||||
pub fn type_key<E: EventLike, Ctx: 'static>() -> TypeId {
|
||||
TypeId::of::<TypeEventManager<E::Event, Ctx>>()
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, id: WidgetId) {
|
||||
for m in self.types.values_mut() {
|
||||
m.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, data: &WidgetData, active: &ActiveData) {
|
||||
for t in &data.event_mgrs {
|
||||
self.types.get_mut(t).unwrap().draw(active);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn undraw(&mut self, data: &WidgetData, active: &ActiveData) {
|
||||
for t in &data.event_mgrs {
|
||||
self.types.get_mut(t).unwrap().undraw(active);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait EventManagerLike {
|
||||
fn remove(&mut self, id: WidgetId);
|
||||
fn draw(&mut self, data: &ActiveData);
|
||||
fn undraw(&mut self, data: &ActiveData);
|
||||
fn as_any(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
type EventData<E, Ctx> = (E, Rc<dyn for<'a> EventFn<Ctx, <E as Event>::Data<'a>>>);
|
||||
pub struct TypeEventManager<E: Event, Ctx> {
|
||||
// TODO: reduce visiblity!!
|
||||
pub active: HashMap<LayerId, HashMap<WidgetId, E::State>>,
|
||||
map: HashMap<WidgetId, Vec<EventData<E, Ctx>>>,
|
||||
}
|
||||
|
||||
impl<E: Event, Ctx: 'static> EventManagerLike for TypeEventManager<E, Ctx> {
|
||||
fn remove(&mut self, id: WidgetId) {
|
||||
self.map.remove(&id);
|
||||
for layer in self.active.values_mut() {
|
||||
layer.remove(&id);
|
||||
}
|
||||
}
|
||||
fn draw(&mut self, data: &ActiveData) {
|
||||
self.active
|
||||
.entry(data.layer)
|
||||
.or_default()
|
||||
.entry(data.id)
|
||||
.or_default();
|
||||
}
|
||||
fn undraw(&mut self, data: &ActiveData) {
|
||||
if let Some(layer) = self.active.get_mut(&data.layer) {
|
||||
layer.remove(&data.id);
|
||||
}
|
||||
}
|
||||
fn as_any(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Event, Ctx> Default for TypeEventManager<E, Ctx> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
active: Default::default(),
|
||||
map: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Event, Ctx: 'static> TypeEventManager<E, Ctx> {
|
||||
fn register(
|
||||
&mut self,
|
||||
id: impl IdLike,
|
||||
event: impl EventLike<Event = E>,
|
||||
f: impl for<'a> EventFn<Ctx, E::Data<'a>>,
|
||||
) {
|
||||
let event = event.into_event();
|
||||
self.map
|
||||
.entry(id.id())
|
||||
.or_default()
|
||||
.push((event, Rc::new(f)));
|
||||
}
|
||||
|
||||
pub fn run_fn<'a>(
|
||||
&mut self,
|
||||
id: impl IdLike,
|
||||
) -> impl for<'b> FnOnce(EventCtx<Ctx, E::Data<'b>>) + 'a {
|
||||
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
|
||||
move |ctx| {
|
||||
for (e, f) in fs {
|
||||
if e.should_run(ctx.data) {
|
||||
f(EventCtx {
|
||||
ui: ctx.ui,
|
||||
state: ctx.state,
|
||||
data: ctx.data,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
pub fn run_event<E: EventLike, Ctx: 'static>(
|
||||
&mut self,
|
||||
ctx: &mut Ctx,
|
||||
id: impl IdLike,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
) {
|
||||
let f = self.data.events.get_type::<E, Ctx>().run_fn(id);
|
||||
f(EventCtx {
|
||||
ui: self,
|
||||
state: ctx,
|
||||
data,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user