🚧 Working on actions system

This commit is contained in:
Manuel Cillero 2024-03-25 17:38:39 +01:00
parent 2ea0a1698e
commit e732244a2e
15 changed files with 165 additions and 153 deletions

View file

@ -1,4 +1,4 @@
use crate::core::action::{Action, ActionsList};
use crate::core::action::{Action, ActionTrait, ActionsList};
use crate::{LazyStatic, TypeId};
use std::collections::HashMap;
@ -24,9 +24,10 @@ pub fn add_action(action: Action) {
}
}
pub fn dispatch_actions<B, F>(key_action: KeyAction, f: F)
pub fn dispatch_actions<A, B, F>(key_action: KeyAction, f: F)
where
F: FnMut(&Action) -> B,
A: ActionTrait,
F: FnMut(&A) -> B,
{
if let Some(list) = ACTIONS.read().unwrap().get(&key_action) {
list.iter_map(f)

View file

@ -14,7 +14,3 @@ pub trait ActionTrait: AnyBase + Send + Sync {
0
}
}
pub fn action_ref<A: 'static>(action: &dyn ActionTrait) -> &A {
action.as_any_ref().downcast_ref::<A>().unwrap()
}

View file

@ -1,4 +1,6 @@
use crate::core::action::ActionTrait;
use crate::core::AnyTo;
use crate::trace;
use crate::AutoDefault;
use std::sync::{Arc, RwLock};
@ -21,12 +23,25 @@ impl ActionsList {
list.sort_by_key(|a| a.weight());
}
pub fn iter_map<B, F>(&self, f: F)
pub fn iter_map<A, B, F>(&self, mut f: F)
where
Self: Sized,
F: FnMut(&Action) -> B,
A: ActionTrait,
F: FnMut(&A) -> B,
{
let _: Vec<_> = self.0.read().unwrap().iter().map(f).collect();
let _: Vec<_> = self
.0
.read()
.unwrap()
.iter()
.map(|a| {
if let Some(action) = (&**a).downcast_ref::<A>() {
f(action);
} else {
trace::error!("Failed to downcast action of type {}", (&**a).type_name());
}
})
.collect();
}
}