mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
127 lines
4.7 KiB
Rust
127 lines
4.7 KiB
Rust
use crate::*;
|
|
use std::marker::PhantomData;
|
|
|
|
/// [Input] state that can be matched against a [Value].
|
|
pub trait DslInput: Input { fn matches_dsl (&self, token: &str) -> bool; }
|
|
|
|
/// A pre-configured mapping of input events to commands.
|
|
pub trait KeyMap<S, C: Dsl<S> + Command<S>, I: DslInput> {
|
|
/// Try to find a command that matches the current input event.
|
|
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C>;
|
|
}
|
|
|
|
/// A [SourceIter] can be a [KeyMap].
|
|
impl<'source, S, C: Dsl<S> + Command<S>, I: DslInput> KeyMap<S, C, I> for SourceIter<'source> {
|
|
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
|
|
let mut iter = self.clone();
|
|
while let Some((token, rest)) = iter.next() {
|
|
iter = rest;
|
|
match token {
|
|
Token { value: Value::Exp(0, exp_iter), .. } => {
|
|
let mut exp_iter = exp_iter.clone();
|
|
match exp_iter.next() {
|
|
Some(Token { value: Value::Sym(binding), .. }) => {
|
|
if input.matches_dsl(binding) {
|
|
if let Some(command) = Dsl::take_from(state, &mut exp_iter)? {
|
|
return Ok(Some(command))
|
|
}
|
|
}
|
|
},
|
|
_ => panic!("invalid config (expected symbol)")
|
|
}
|
|
},
|
|
_ => panic!("invalid config (expected expression)")
|
|
}
|
|
}
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
/// A [TokenIter] can be a [KeyMap].
|
|
impl<'source, S, C: Dsl<S> + Command<S>, I: DslInput> KeyMap<S, C, I> for TokenIter<'source> {
|
|
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
|
|
let mut iter = self.clone();
|
|
while let Some(next) = iter.next() {
|
|
match next {
|
|
Token { value: Value::Exp(0, exp_iter), .. } => {
|
|
let mut e = exp_iter.clone();
|
|
match e.next() {
|
|
Some(Token { value: Value::Sym(binding), .. }) => {
|
|
if input.matches_dsl(binding) {
|
|
if let Some(command) = C::take_from(state, &mut e)? {
|
|
return Ok(Some(command))
|
|
}
|
|
}
|
|
},
|
|
_ => panic!("invalid config (expected symbol, got: {exp_iter:?})")
|
|
}
|
|
},
|
|
_ => panic!("invalid config (expected expression, got: {next:?})")
|
|
}
|
|
}
|
|
Ok(None)
|
|
}
|
|
}
|
|
|
|
pub type InputLayerCond<S> = Box<dyn Fn(&S)->Usually<bool> + Send + Sync>;
|
|
|
|
/// A collection of pre-configured mappings of input events to commands,
|
|
/// which may be made available subject to given conditions.
|
|
pub struct InputMap<S, C, I, M>
|
|
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
|
|
__: PhantomData<(S, C, I)>,
|
|
pub layers: Vec<(InputLayerCond<S>, M)>,
|
|
}
|
|
|
|
impl<S, C, I, M> Default for InputMap<S, C, I, M>
|
|
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
|
|
fn default () -> Self {
|
|
Self { __: PhantomData, layers: vec![] }
|
|
}
|
|
}
|
|
|
|
impl<S: 'static, C, I, M> InputMap<S, C, I, M>
|
|
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
|
|
pub fn new (keymap: M) -> Self {
|
|
Self::default().layer(keymap)
|
|
}
|
|
pub fn layer (mut self, keymap: M) -> Self {
|
|
self.add_layer(keymap);
|
|
self
|
|
}
|
|
pub fn add_layer (&mut self, keymap: M) -> &mut Self {
|
|
self.add_layer_if(Box::new(|_|Ok(true)), keymap);
|
|
self
|
|
}
|
|
pub fn layer_if (mut self, condition: InputLayerCond<S>, keymap: M) -> Self {
|
|
self.add_layer_if(condition, keymap);
|
|
self
|
|
}
|
|
pub fn add_layer_if (&mut self, condition: InputLayerCond<S>, keymap: M) -> &mut Self {
|
|
self.layers.push((Box::new(condition), keymap));
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<S, C, I, M> std::fmt::Debug for InputMap<S, C, I, M>
|
|
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<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())
|
|
}
|
|
}
|
|
|
|
/// An [InputMap] can be a [KeyMap].
|
|
impl<S, C, I, M> KeyMap<S, C, I> for InputMap<S, C, I, M>
|
|
where C: Dsl<S> + Command<S>, I: DslInput, M: KeyMap<S, C, I> + Send + Sync {
|
|
fn keybind_resolve (&self, state: &S, input: &I) -> Perhaps<C> {
|
|
for (condition, keymap) in self.layers.iter() {
|
|
if !condition(state)? {
|
|
continue
|
|
}
|
|
if let Some(command) = keymap.keybind_resolve(state, input)? {
|
|
return Ok(Some(command))
|
|
}
|
|
}
|
|
Ok(None)
|
|
}
|
|
}
|