move event data out of widgetdata
This commit is contained in:
@@ -1,17 +1,19 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ActiveData, Event, EventCtx, EventFn, EventIdCtx, EventLike, IdLike, LayerId, Widget,
|
ActiveData, Event, EventCtx, EventFn, EventIdCtx, EventLike, IdLike, LayerId, Widget,
|
||||||
WidgetData, WidgetEventFn, WidgetId, WidgetRef,
|
WidgetEventFn, WidgetId, WidgetRef,
|
||||||
util::{HashMap, TypeMap},
|
util::{HashMap, HashSet, TypeMap},
|
||||||
};
|
};
|
||||||
use std::{any::TypeId, rc::Rc};
|
use std::{any::TypeId, rc::Rc};
|
||||||
|
|
||||||
pub struct EventManager<State> {
|
pub struct EventManager<State> {
|
||||||
|
widget_to_types: HashMap<WidgetId, HashSet<TypeId>>,
|
||||||
types: TypeMap<dyn EventManagerLike<State>>,
|
types: TypeMap<dyn EventManagerLike<State>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State> Default for EventManager<State> {
|
impl<State> Default for EventManager<State> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
widget_to_types: Default::default(),
|
||||||
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>,
|
f: impl for<'a> WidgetEventFn<State, <E::Event as Event>::Data<'a>, W>,
|
||||||
) {
|
) {
|
||||||
self.get_type::<E>().register(id, event, f);
|
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 {
|
pub fn type_key<E: EventLike>() -> TypeId {
|
||||||
@@ -38,25 +44,25 @@ impl<State: 'static> EventManager<State> {
|
|||||||
|
|
||||||
pub trait EventsLike {
|
pub trait EventsLike {
|
||||||
fn remove(&mut self, id: WidgetId);
|
fn remove(&mut self, id: WidgetId);
|
||||||
fn draw(&mut self, data: &WidgetData, active: &ActiveData);
|
fn draw(&mut self, active: &ActiveData);
|
||||||
fn undraw(&mut self, data: &WidgetData, active: &ActiveData);
|
fn undraw(&mut self, active: &ActiveData);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State: 'static> EventsLike for EventManager<State> {
|
impl<State: 'static> EventsLike for EventManager<State> {
|
||||||
fn remove(&mut self, id: WidgetId) {
|
fn remove(&mut self, id: WidgetId) {
|
||||||
for m in self.types.values_mut() {
|
for t in self.widget_to_types.get(&id).into_flat_iter() {
|
||||||
m.remove(id);
|
self.types.get_mut(t).unwrap().remove(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&mut self, data: &WidgetData, active: &ActiveData) {
|
fn draw(&mut self, active: &ActiveData) {
|
||||||
for t in &data.event_mgrs {
|
for t in self.widget_to_types.get(&active.id).into_flat_iter() {
|
||||||
self.types.get_mut(t).unwrap().draw(active);
|
self.types.get_mut(t).unwrap().draw(active);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn undraw(&mut self, data: &WidgetData, active: &ActiveData) {
|
fn undraw(&mut self, active: &ActiveData) {
|
||||||
for t in &data.event_mgrs {
|
for t in self.widget_to_types.get(&active.id).into_flat_iter() {
|
||||||
self.types.get_mut(t).unwrap().undraw(active);
|
self.types.get_mut(t).unwrap().undraw(active);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,12 +25,6 @@ pub trait HasEvents: Sized + HasState + HasUi {
|
|||||||
Self::State: 'static,
|
Self::State: 'static,
|
||||||
{
|
{
|
||||||
self.events_mut().register(id, event, f);
|
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(associated_type_defaults)]
|
||||||
#![feature(unsize)]
|
#![feature(unsize)]
|
||||||
#![feature(coerce_unsized)]
|
#![feature(coerce_unsized)]
|
||||||
|
#![feature(option_into_flat_iter)]
|
||||||
|
|
||||||
mod attr;
|
mod attr;
|
||||||
mod event;
|
mod event;
|
||||||
|
|||||||
@@ -80,9 +80,8 @@ impl<'a> DrawState<'a> {
|
|||||||
|
|
||||||
pub fn redraw_all(&mut self) {
|
pub fn redraw_all(&mut self) {
|
||||||
// free all resources & cache
|
// free all resources & cache
|
||||||
for (id, active) in self.ui.active.drain() {
|
for (_, active) in self.ui.active.drain() {
|
||||||
let data = self.ui.widgets.data(id).unwrap();
|
self.events.undraw(&active);
|
||||||
self.events.undraw(data, &active);
|
|
||||||
}
|
}
|
||||||
self.ui.cache.clear();
|
self.ui.cache.clear();
|
||||||
self.ui.free(self.events);
|
self.ui.free(self.events);
|
||||||
@@ -170,8 +169,7 @@ impl<'a> DrawState<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update modules
|
// update modules
|
||||||
let data = self.ui.widgets.data(id).unwrap();
|
self.events.draw(&active);
|
||||||
self.events.draw(data, &active);
|
|
||||||
self.active.insert(id, active);
|
self.active.insert(id, active);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,8 +200,7 @@ impl<'a> DrawState<'a> {
|
|||||||
active.textures.clear();
|
active.textures.clear();
|
||||||
self.textures.free();
|
self.textures.free();
|
||||||
if undraw {
|
if undraw {
|
||||||
let data = self.ui.widgets.data(id).unwrap();
|
self.events.undraw(active);
|
||||||
self.events.undraw(data, active);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
active
|
active
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
use std::any::TypeId;
|
use crate::Widget;
|
||||||
|
|
||||||
use crate::{Widget, util::HashSet};
|
|
||||||
|
|
||||||
pub struct WidgetData {
|
pub struct WidgetData {
|
||||||
pub widget: Box<dyn Widget>,
|
pub widget: Box<dyn Widget>,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub event_mgrs: HashSet<TypeId>,
|
|
||||||
/// dynamic borrow checking
|
/// dynamic borrow checking
|
||||||
pub borrowed: bool,
|
pub borrowed: bool,
|
||||||
}
|
}
|
||||||
@@ -20,7 +17,6 @@ impl WidgetData {
|
|||||||
widget: Box::new(widget),
|
widget: Box::new(widget),
|
||||||
label,
|
label,
|
||||||
borrowed: false,
|
borrowed: false,
|
||||||
event_mgrs: Default::default(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -148,7 +148,7 @@ impl<State: RunEvents> SensorUi<State> for State {
|
|||||||
let mut active = std::mem::take(&mut self.events_mut().get_type::<CursorSense>().active);
|
let mut active = std::mem::take(&mut self.events_mut().get_type::<CursorSense>().active);
|
||||||
for layer in layers.indices().rev() {
|
for layer in layers.indices().rev() {
|
||||||
let mut sensed = false;
|
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 shape = self.ui().active.get(id).unwrap().region;
|
||||||
let region = shape.to_px(window_size);
|
let region = shape.to_px(window_size);
|
||||||
let in_shape = cursor.exists && region.contains(cursor.pos);
|
let in_shape = cursor.exists && region.contains(cursor.pos);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#![feature(gen_blocks)]
|
#![feature(gen_blocks)]
|
||||||
#![feature(associated_type_defaults)]
|
#![feature(associated_type_defaults)]
|
||||||
#![feature(unsize)]
|
#![feature(unsize)]
|
||||||
|
#![feature(option_into_flat_iter)]
|
||||||
|
|
||||||
pub mod default;
|
pub mod default;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
|
|||||||
Reference in New Issue
Block a user