diff --git a/core/src/event/manager.rs b/core/src/event/manager.rs index 1c49664..0e737c3 100644 --- a/core/src/event/manager.rs +++ b/core/src/event/manager.rs @@ -1,17 +1,19 @@ use crate::{ ActiveData, Event, EventCtx, EventFn, EventIdCtx, EventLike, IdLike, LayerId, Widget, - WidgetData, WidgetEventFn, WidgetId, WidgetRef, - util::{HashMap, TypeMap}, + WidgetEventFn, WidgetId, WidgetRef, + util::{HashMap, HashSet, TypeMap}, }; use std::{any::TypeId, rc::Rc}; pub struct EventManager { + widget_to_types: HashMap>, types: TypeMap>, } impl Default for EventManager { fn default() -> Self { Self { + widget_to_types: Default::default(), types: Default::default(), } } @@ -29,6 +31,10 @@ impl EventManager { f: impl for<'a> WidgetEventFn::Data<'a>, W>, ) { self.get_type::().register(id, event, f); + self.widget_to_types + .entry(id.id()) + .or_default() + .insert(Self::type_key::()); } pub fn type_key() -> TypeId { @@ -38,25 +44,25 @@ impl EventManager { 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); + fn draw(&mut self, active: &ActiveData); + fn undraw(&mut self, active: &ActiveData); } impl EventsLike for EventManager { fn remove(&mut self, id: WidgetId) { - for m in self.types.values_mut() { - m.remove(id); + for t in self.widget_to_types.get(&id).into_flat_iter() { + self.types.get_mut(t).unwrap().remove(id); } } - fn draw(&mut self, data: &WidgetData, active: &ActiveData) { - for t in &data.event_mgrs { + fn draw(&mut self, active: &ActiveData) { + for t in self.widget_to_types.get(&active.id).into_flat_iter() { self.types.get_mut(t).unwrap().draw(active); } } - fn undraw(&mut self, data: &WidgetData, active: &ActiveData) { - for t in &data.event_mgrs { + fn undraw(&mut self, active: &ActiveData) { + for t in self.widget_to_types.get(&active.id).into_flat_iter() { self.types.get_mut(t).unwrap().undraw(active); } } diff --git a/core/src/event/rsc.rs b/core/src/event/rsc.rs index 26004a7..0da7214 100644 --- a/core/src/event/rsc.rs +++ b/core/src/event/rsc.rs @@ -25,12 +25,6 @@ pub trait HasEvents: Sized + HasState + HasUi { Self::State: 'static, { self.events_mut().register(id, event, f); - self.ui_mut() - .widgets - .data_mut(id) - .unwrap() - .event_mgrs - .insert(EventManager::::type_key::()); } } diff --git a/core/src/lib.rs b/core/src/lib.rs index 145c54a..f60415a 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -11,6 +11,7 @@ #![feature(associated_type_defaults)] #![feature(unsize)] #![feature(coerce_unsized)] +#![feature(option_into_flat_iter)] mod attr; mod event; diff --git a/core/src/ui/draw_state.rs b/core/src/ui/draw_state.rs index 4bd29f2..555acb7 100644 --- a/core/src/ui/draw_state.rs +++ b/core/src/ui/draw_state.rs @@ -80,9 +80,8 @@ impl<'a> DrawState<'a> { pub fn redraw_all(&mut self) { // free all resources & cache - for (id, active) in self.ui.active.drain() { - let data = self.ui.widgets.data(id).unwrap(); - self.events.undraw(data, &active); + for (_, active) in self.ui.active.drain() { + self.events.undraw(&active); } self.ui.cache.clear(); self.ui.free(self.events); @@ -170,8 +169,7 @@ impl<'a> DrawState<'a> { } // update modules - let data = self.ui.widgets.data(id).unwrap(); - self.events.draw(data, &active); + self.events.draw(&active); self.active.insert(id, active); } @@ -202,8 +200,7 @@ impl<'a> DrawState<'a> { active.textures.clear(); self.textures.free(); if undraw { - let data = self.ui.widgets.data(id).unwrap(); - self.events.undraw(data, active); + self.events.undraw(active); } } active diff --git a/core/src/widget/data.rs b/core/src/widget/data.rs index 68ef4f3..8d693e7 100644 --- a/core/src/widget/data.rs +++ b/core/src/widget/data.rs @@ -1,11 +1,8 @@ -use std::any::TypeId; - -use crate::{Widget, util::HashSet}; +use crate::Widget; pub struct WidgetData { pub widget: Box, pub label: String, - pub event_mgrs: HashSet, /// dynamic borrow checking pub borrowed: bool, } @@ -20,7 +17,6 @@ impl WidgetData { widget: Box::new(widget), label, borrowed: false, - event_mgrs: Default::default(), } } } diff --git a/src/default/sense.rs b/src/default/sense.rs index ff45e26..446c395 100644 --- a/src/default/sense.rs +++ b/src/default/sense.rs @@ -148,7 +148,7 @@ impl SensorUi for State { let mut active = std::mem::take(&mut self.events_mut().get_type::().active); for layer in layers.indices().rev() { let mut sensed = false; - for (id, sensor) in active.get_mut(&layer).into_iter().flatten() { + for (id, sensor) in active.get_mut(&layer).into_flat_iter() { let shape = self.ui().active.get(id).unwrap().region; let region = shape.to_px(window_size); let in_shape = cursor.exists && region.contains(cursor.pos); diff --git a/src/lib.rs b/src/lib.rs index ff2fd78..7d776fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ #![feature(gen_blocks)] #![feature(associated_type_defaults)] #![feature(unsize)] +#![feature(option_into_flat_iter)] pub mod default; pub mod event;