input, dsl: implement InputMap command matching
Some checks failed
/ build (push) Has been cancelled

This commit is contained in:
🪞👃🪞 2025-04-28 23:28:53 +03:00
parent 35ad371205
commit 4ec51d5b69

View file

@ -1,4 +1,5 @@
use crate::*;
use std::marker::PhantomData;
/// A [Command] that can be constructed from a [Token].
pub trait DslCommand<'a, C>: TryFromDsl<'a, C> + Command<C> {}
@ -71,59 +72,84 @@ impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for TokenIter
/// A collection of pre-configured mappings of input events to commands,
/// which may be made available subject to given conditions.
pub struct InputMap<'a, S, C: DslCommand<'a, S>, I: DslInput> {
pub struct InputMap<'a, S, C: DslCommand<'a, S>, I: DslInput, M: KeyMap<'a, S, C, I> + Send + Sync> {
__: &'a PhantomData<(S, C, I)>,
layers: Vec<(
Box<dyn Fn(&S)->bool + Send + Sync + 'a>,
Box<dyn KeyMap<'a, S, C, I> + Send + Sync + 'a>
fn(&S)->bool,
M
)>,
}
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> Default for InputMap<'a, S, C, I> {
impl<'a, S, C, I, M> Default for InputMap<'a, S, C, I, M>
where
C: DslCommand<'a, S>,
I: DslInput,
M: KeyMap<'a, S, C, I> + Send + Sync
{
fn default () -> Self {
Self {
__: &PhantomData,
layers: vec![]
}
}
}
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> InputMap<'a, S, C, I> {
pub fn new (keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a) -> Self {
impl<'a, S, C, I, M> InputMap<'a, S, C, I, M>
where
C: DslCommand<'a, S>,
I: DslInput,
M: KeyMap<'a, S, C, I> + Send + Sync
{
pub fn new (keymap: M) -> Self {
Self::default().layer(keymap)
}
pub fn layer (
mut self, keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
) -> Self {
pub fn layer (mut self, keymap: M) -> Self {
self.add_layer(keymap);
self
}
pub fn add_layer (
&mut self, keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
) -> &mut Self {
pub fn add_layer (&mut self, keymap: M) -> &mut Self {
self.add_layer_if(|_|true, keymap);
self
}
pub fn layer_if (
mut self,
condition: impl Fn(&S)->bool + Send + Sync + 'a,
keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
) -> Self {
pub fn layer_if (mut self, condition: fn(&S)->bool, keymap: M) -> Self {
self.add_layer_if(condition, keymap);
self
}
pub fn add_layer_if (
&mut self,
condition: impl Fn(&S)->bool + Send + Sync + 'a,
keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
) -> &mut Self {
self.layers.push((Box::new(condition), Box::new(keymap)));
pub fn add_layer_if (&mut self, condition: fn(&S)->bool, keymap: M) -> &mut Self {
self.layers.push((condition, keymap));
self
}
}
impl<'a, S, C, I, M> std::fmt::Debug for InputMap<'a, S, C, I, M>
where
C: DslCommand<'a, S>,
I: DslInput,
M: KeyMap<'a, S, C, I> + Send + Sync
{
fn fmt (&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "[InputMap: {} layer(s)]", self.layers.len());
Ok(())
}
}
/// An [InputMap] can be a [KeyMap].
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> KeyMap<'a, S, C, I> for InputMap<'a, S, C, I> {
impl<'a, S, C, I, M> KeyMap<'a, S, C, I> for InputMap<'a, S, C, I, M>
where
C: DslCommand<'a, S>,
I: DslInput,
M: KeyMap<'a, S, C, I> + Send + Sync
{
fn command (&'a self, state: &'a S, input: &'a I) -> Option<C> {
todo!()
for (condition, keymap) in self.layers.iter() {
if !condition(state) {
continue
}
if let Some(command) = keymap.command(state, input) {
return Some(command)
}
}
None
}
}