move event data out of widgetdata
This commit is contained in:
@@ -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<State> {
|
||||
widget_to_types: HashMap<WidgetId, HashSet<TypeId>>,
|
||||
types: TypeMap<dyn EventManagerLike<State>>,
|
||||
}
|
||||
|
||||
impl<State> Default for EventManager<State> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
widget_to_types: Default::default(),
|
||||
types: Default::default(),
|
||||
}
|
||||
}
|
||||
@@ -29,6 +31,10 @@ impl<State: 'static> EventManager<State> {
|
||||
f: impl for<'a> WidgetEventFn<State, <E::Event as Event>::Data<'a>, W>,
|
||||
) {
|
||||
self.get_type::<E>().register(id, event, f);
|
||||
self.widget_to_types
|
||||
.entry(id.id())
|
||||
.or_default()
|
||||
.insert(Self::type_key::<E>());
|
||||
}
|
||||
|
||||
pub fn type_key<E: EventLike>() -> TypeId {
|
||||
@@ -38,25 +44,25 @@ impl<State: 'static> EventManager<State> {
|
||||
|
||||
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<State: 'static> EventsLike for EventManager<State> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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::<Self::State>::type_key::<E>());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#![feature(associated_type_defaults)]
|
||||
#![feature(unsize)]
|
||||
#![feature(coerce_unsized)]
|
||||
#![feature(option_into_flat_iter)]
|
||||
|
||||
mod attr;
|
||||
mod event;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
use std::any::TypeId;
|
||||
|
||||
use crate::{Widget, util::HashSet};
|
||||
use crate::Widget;
|
||||
|
||||
pub struct WidgetData {
|
||||
pub widget: Box<dyn Widget>,
|
||||
pub label: String,
|
||||
pub event_mgrs: HashSet<TypeId>,
|
||||
/// dynamic borrow checking
|
||||
pub borrowed: bool,
|
||||
}
|
||||
@@ -20,7 +17,6 @@ impl WidgetData {
|
||||
widget: Box::new(widget),
|
||||
label,
|
||||
borrowed: false,
|
||||
event_mgrs: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user