initial global widget store
This commit is contained in:
1 parent
7f4846a2d3
commit
6156c66a20
31 files changed
+536
-352
No files matched your search
+18
-21
@@ -1,15 +1,12 @@
|
||||
use crate::prelude::*;
|
||||
use crate::util::Id;
|
||||
|
||||
use std::{
|
||||
ops::{BitOr, Deref, DerefMut},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
layout::{UiModule, UiRegion, Vec2},
|
||||
util::HashMap,
|
||||
};
|
||||
use std::{
|
||||
ops::{BitOr, Deref, DerefMut},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub enum CursorButton {
|
||||
@@ -100,10 +97,10 @@ pub enum ActivationState {
|
||||
/// but I'm not sure how or if worth it
|
||||
pub struct Sensor<Ctx, Data> {
|
||||
pub senses: CursorSenses,
|
||||
pub f: Rc<dyn EventFn<Ctx, Data>>,
|
||||
pub f: Box<dyn EventFn<Ctx, Data>>,
|
||||
}
|
||||
|
||||
pub type SensorMap<Ctx, Data> = HashMap<Id, SensorGroup<Ctx, Data>>;
|
||||
pub type SensorMap<Ctx, Data> = HashMap<WidgetId, SensorGroup<Ctx, Data>>;
|
||||
pub type SenseShape = UiRegion;
|
||||
pub struct SensorGroup<Ctx, Data> {
|
||||
pub hover: ActivationState,
|
||||
@@ -122,7 +119,7 @@ pub struct CursorData {
|
||||
|
||||
pub struct CursorModule<Ctx> {
|
||||
map: SensorMap<Ctx, CursorData>,
|
||||
active: HashMap<usize, HashMap<Id, SenseShape>>,
|
||||
active: HashMap<usize, HashMap<WidgetId, SenseShape>>,
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> UiModule for CursorModule<Ctx> {
|
||||
@@ -141,7 +138,7 @@ impl<Ctx: 'static> UiModule for CursorModule<Ctx> {
|
||||
}
|
||||
}
|
||||
|
||||
fn on_remove(&mut self, id: &Id) {
|
||||
fn on_remove(&mut self, id: &WidgetId) {
|
||||
self.map.remove(id);
|
||||
for layer in self.active.values_mut() {
|
||||
layer.remove(id);
|
||||
@@ -307,34 +304,34 @@ impl Event for CursorSense {
|
||||
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static>
|
||||
EventModule<E, Ctx> for CursorModule<Ctx>
|
||||
{
|
||||
fn register(&mut self, id: Id, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
|
||||
fn register(&mut self, id: WidgetId, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
|
||||
// TODO: does not add to active if currently active
|
||||
self.map.entry(id).or_default().sensors.push(Sensor {
|
||||
senses: senses.into(),
|
||||
f: Rc::new(f),
|
||||
f: Box::new(f),
|
||||
});
|
||||
}
|
||||
|
||||
fn run<'a>(
|
||||
&self,
|
||||
id: &Id,
|
||||
fn run(
|
||||
&mut self,
|
||||
id: WidgetId,
|
||||
event: E,
|
||||
) -> Option<impl Fn(EventCtx<Ctx, <E as Event>::Data>) + use<'a, E, Ctx>> {
|
||||
) -> Option<impl FnOnce(EventCtx<Ctx, <E as Event>::Data>)> {
|
||||
let senses = event.into();
|
||||
if let Some(group) = self.map.get(id) {
|
||||
if let Some(group) = self.map.get_mut(&id) {
|
||||
let fs: Vec<_> = group
|
||||
.sensors
|
||||
.iter()
|
||||
.iter_mut()
|
||||
.filter_map(|sensor| {
|
||||
if sensor.senses.iter().any(|s| senses.contains(s)) {
|
||||
Some(sensor.f.clone())
|
||||
Some(&mut sensor.f)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Some(move |ctx: EventCtx<Ctx, CursorData>| {
|
||||
for f in &fs {
|
||||
for f in fs {
|
||||
f(EventCtx {
|
||||
state: ctx.state,
|
||||
ui: ctx.ui,
|
||||
|
||||
Reference in new issue
Block a user