Use id instead of type_id for actions

Currently, both are the same thing, so the logic is not changed.
This commit is contained in:
Kirill Bulatov 2023-07-17 12:24:56 +03:00
parent 10a1df3faa
commit eaa8224076
5 changed files with 30 additions and 37 deletions

View file

@ -1,39 +1,36 @@
use smallvec::SmallVec;
use std::{
any::{Any, TypeId},
collections::HashMap,
};
use std::{any::TypeId, collections::HashMap};
use super::Binding;
#[derive(Default)]
pub struct Keymap {
bindings: Vec<Binding>,
binding_indices_by_action_type: HashMap<TypeId, SmallVec<[usize; 3]>>,
binding_indices_by_action_id: HashMap<TypeId, SmallVec<[usize; 3]>>,
}
impl Keymap {
pub fn new(bindings: Vec<Binding>) -> Self {
let mut binding_indices_by_action_type = HashMap::new();
let mut binding_indices_by_action_id = HashMap::new();
for (ix, binding) in bindings.iter().enumerate() {
binding_indices_by_action_type
.entry(binding.action().type_id())
binding_indices_by_action_id
.entry(binding.action().id())
.or_insert_with(SmallVec::new)
.push(ix);
}
Self {
binding_indices_by_action_type,
binding_indices_by_action_id,
bindings,
}
}
pub(crate) fn bindings_for_action_type(
pub(crate) fn bindings_for_action(
&self,
action_type: TypeId,
action_id: TypeId,
) -> impl Iterator<Item = &'_ Binding> {
self.binding_indices_by_action_type
.get(&action_type)
self.binding_indices_by_action_id
.get(&action_id)
.map(SmallVec::as_slice)
.unwrap_or(&[])
.iter()
@ -42,8 +39,8 @@ impl Keymap {
pub(crate) fn add_bindings<T: IntoIterator<Item = Binding>>(&mut self, bindings: T) {
for binding in bindings {
self.binding_indices_by_action_type
.entry(binding.action().as_any().type_id())
self.binding_indices_by_action_id
.entry(binding.action().id())
.or_default()
.push(self.bindings.len());
self.bindings.push(binding);
@ -52,7 +49,7 @@ impl Keymap {
pub(crate) fn clear(&mut self) {
self.bindings.clear();
self.binding_indices_by_action_type.clear();
self.binding_indices_by_action_id.clear();
}
pub fn bindings(&self) -> &Vec<Binding> {