mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
input, dsl: implement InputMap command matching
Some checks failed
/ build (push) Has been cancelled
Some checks failed
/ build (push) Has been cancelled
This commit is contained in:
parent
35ad371205
commit
4ec51d5b69
1 changed files with 51 additions and 25 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
/// A [Command] that can be constructed from a [Token].
|
/// A [Command] that can be constructed from a [Token].
|
||||||
pub trait DslCommand<'a, C>: TryFromDsl<'a, C> + Command<C> {}
|
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,
|
/// A collection of pre-configured mappings of input events to commands,
|
||||||
/// which may be made available subject to given conditions.
|
/// 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<(
|
layers: Vec<(
|
||||||
Box<dyn Fn(&S)->bool + Send + Sync + 'a>,
|
fn(&S)->bool,
|
||||||
Box<dyn KeyMap<'a, S, C, I> + Send + Sync + 'a>
|
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 {
|
fn default () -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
__: &PhantomData,
|
||||||
layers: vec![]
|
layers: vec![]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S, C: DslCommand<'a, S>, I: DslInput> InputMap<'a, S, C, I> {
|
impl<'a, S, C, I, M> InputMap<'a, S, C, I, M>
|
||||||
pub fn new (keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a) -> Self {
|
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)
|
Self::default().layer(keymap)
|
||||||
}
|
}
|
||||||
pub fn layer (
|
pub fn layer (mut self, keymap: M) -> Self {
|
||||||
mut self, keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
|
|
||||||
) -> Self {
|
|
||||||
self.add_layer(keymap);
|
self.add_layer(keymap);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn add_layer (
|
pub fn add_layer (&mut self, keymap: M) -> &mut Self {
|
||||||
&mut self, keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
|
|
||||||
) -> &mut Self {
|
|
||||||
self.add_layer_if(|_|true, keymap);
|
self.add_layer_if(|_|true, keymap);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn layer_if (
|
pub fn layer_if (mut self, condition: fn(&S)->bool, keymap: M) -> Self {
|
||||||
mut self,
|
|
||||||
condition: impl Fn(&S)->bool + Send + Sync + 'a,
|
|
||||||
keymap: impl KeyMap<'a, S, C, I> + Send + Sync + 'a
|
|
||||||
) -> Self {
|
|
||||||
self.add_layer_if(condition, keymap);
|
self.add_layer_if(condition, keymap);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn add_layer_if (
|
pub fn add_layer_if (&mut self, condition: fn(&S)->bool, keymap: M) -> &mut Self {
|
||||||
&mut self,
|
self.layers.push((condition, keymap));
|
||||||
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)));
|
|
||||||
self
|
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].
|
/// 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> {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue