remove state generic from a lot of things
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use crate::{
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, HasUi, IdLike, LayerId, WidgetData, WidgetId,
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, HasRsc, HasUi, IdLike, LayerId, WidgetData,
|
||||
WidgetId,
|
||||
util::{HashMap, TypeMap},
|
||||
};
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
@@ -21,7 +22,7 @@ impl<State: 'static> EventManager<State> {
|
||||
self.types.type_or_default()
|
||||
}
|
||||
|
||||
pub fn register<I: IdLike<State>, E: EventLike>(
|
||||
pub fn register<I: IdLike, E: EventLike>(
|
||||
&mut self,
|
||||
id: I,
|
||||
event: E,
|
||||
@@ -33,20 +34,28 @@ impl<State: 'static> EventManager<State> {
|
||||
pub fn type_key<E: EventLike>() -> TypeId {
|
||||
TypeId::of::<TypeEventManager<State, E::Event>>()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, id: WidgetId) {
|
||||
pub trait EventsLike {
|
||||
fn remove(&mut self, id: WidgetId);
|
||||
fn draw(&mut self, data: &WidgetData, active: &ActiveData);
|
||||
fn undraw(&mut self, data: &WidgetData, active: &ActiveData);
|
||||
}
|
||||
|
||||
impl<State: 'static> EventsLike for EventManager<State> {
|
||||
fn remove(&mut self, id: WidgetId) {
|
||||
for m in self.types.values_mut() {
|
||||
m.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, data: &WidgetData<State>, active: &ActiveData) {
|
||||
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<State>, active: &ActiveData) {
|
||||
fn undraw(&mut self, data: &WidgetData, active: &ActiveData) {
|
||||
for t in &data.event_mgrs {
|
||||
self.types.get_mut(t).unwrap().undraw(active);
|
||||
}
|
||||
@@ -99,7 +108,7 @@ impl<State, E: Event> Default for TypeEventManager<State, E> {
|
||||
impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
fn register(
|
||||
&mut self,
|
||||
id: impl IdLike<State>,
|
||||
id: impl IdLike,
|
||||
event: impl EventLike<Event = E>,
|
||||
f: impl for<'a> EventFn<State, E::Data<'a>>,
|
||||
) {
|
||||
@@ -112,7 +121,7 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
|
||||
pub fn run_fn<'a>(
|
||||
&mut self,
|
||||
id: impl IdLike<State>,
|
||||
id: impl IdLike,
|
||||
) -> impl for<'b> FnOnce(EventCtx<State, E::Data<'b>>) + 'a {
|
||||
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
|
||||
move |ctx| {
|
||||
@@ -128,21 +137,26 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasEvents: Sized + HasUi {
|
||||
fn run_event<E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike<Self>,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
);
|
||||
}
|
||||
pub trait HasEvents: Sized {
|
||||
type State: HasRsc<Rsc = Self>;
|
||||
|
||||
impl<State: HasUi + 'static> HasEvents for State {
|
||||
fn run_event<E: EventLike>(
|
||||
fn events(&mut self) -> &mut EventManager<Self::State>;
|
||||
|
||||
fn register_event<I: IdLike, E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike<Self>,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
) {
|
||||
let f = self.ui().events.get_type::<E>().run_fn(id);
|
||||
f(EventCtx { state: self, data })
|
||||
id: I,
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<Self::State, <E::Event as Event>::Data<'a>>,
|
||||
) where
|
||||
Self: HasUi,
|
||||
{
|
||||
let id = id.id();
|
||||
self.events().register(id, event, f);
|
||||
self.ui()
|
||||
.widgets
|
||||
.data_mut(id)
|
||||
.unwrap()
|
||||
.event_mgrs
|
||||
.insert(EventManager::<Self::State>::type_key::<E>());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user