mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 03:36:42 +01:00
This commit is contained in:
parent
b7bb6119aa
commit
22d63eed9c
4 changed files with 1 additions and 112 deletions
|
|
@ -105,7 +105,7 @@ pub const fn peek_src <'a> (source: &'a str) -> Option<Token<'a>> {
|
||||||
}),
|
}),
|
||||||
_ => token.error(Unexpected(c))
|
_ => token.error(Unexpected(c))
|
||||||
},
|
},
|
||||||
Str(s) => match c {
|
Str(_) => match c {
|
||||||
'"' => return Some(token),
|
'"' => return Some(token),
|
||||||
_ => token.grow_str(),
|
_ => token.grow_str(),
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,36 +0,0 @@
|
||||||
use crate::*;
|
|
||||||
use std::time::Duration;
|
|
||||||
use std::thread::JoinHandle;
|
|
||||||
|
|
||||||
/// Event source
|
|
||||||
pub trait Input: Send + Sync + Sized {
|
|
||||||
/// Type of input event
|
|
||||||
type Event;
|
|
||||||
/// Result of handling input
|
|
||||||
type Handled; // TODO: make this an Option<Box dyn Command<Self>> containing the undo
|
|
||||||
/// Currently handled event
|
|
||||||
fn event (&self) -> &Self::Event;
|
|
||||||
/// Whether component should exit
|
|
||||||
fn is_done (&self) -> bool;
|
|
||||||
/// Mark component as done
|
|
||||||
fn done (&self);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Input thread entrypoint.
|
|
||||||
pub trait InputRun<T> {
|
|
||||||
fn run_input (engine: T, state: Self, timer: Duration) -> JoinHandle<()>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle input through a mutable reference.
|
|
||||||
pub trait Handle<E: Input>: Send + Sync {
|
|
||||||
fn handle (&mut self, _input: &E) -> Perhaps<E::Handled> {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle input through an immutable reference (e.g. [Arc<RwLock>] or [Arc<Mutex>])
|
|
||||||
pub trait HandleRef<E: Input>: Send + Sync {
|
|
||||||
fn handle (&self, _input: &E) -> Perhaps<E::Handled> {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,73 +0,0 @@
|
||||||
use crate::*;
|
|
||||||
|
|
||||||
pub struct EventMap<'a, S, I: PartialEq, C> {
|
|
||||||
pub bindings: &'a [(I, &'a dyn Fn(&S) -> Option<C>)],
|
|
||||||
pub fallback: Option<&'a dyn Fn(&S, &I) -> Option<C>>
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, S, I: PartialEq, C> EventMap<'a, S, I, C> {
|
|
||||||
pub fn handle (&self, state: &S, input: &I) -> Option<C> {
|
|
||||||
for (binding, handler) in self.bindings.iter() {
|
|
||||||
if input == binding {
|
|
||||||
return handler(state)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(fallback) = self.fallback {
|
|
||||||
fallback(state, input)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export] macro_rules! keymap {
|
|
||||||
(
|
|
||||||
$(<$lt:lifetime>)? $KEYS:ident = |$state:ident: $State:ty, $input:ident: $Input:ty| $Command:ty
|
|
||||||
{ $($key:expr => $handler:expr),* $(,)? } $(,)?
|
|
||||||
) => {
|
|
||||||
pub const $KEYS: EventMap<'static, $State, $Input, $Command> = EventMap {
|
|
||||||
fallback: None,
|
|
||||||
bindings: &[ $(($key, &|$state|Some($handler)),)* ]
|
|
||||||
};
|
|
||||||
input_to_command!($(<$lt>)? $Command: |$state: $State, input: $Input|$KEYS.handle($state, input)?);
|
|
||||||
};
|
|
||||||
(
|
|
||||||
$(<$lt:lifetime>)? $KEYS:ident = |$state:ident: $State:ty, $input:ident: $Input:ty| $Command:ty
|
|
||||||
{ $($key:expr => $handler:expr),* $(,)? }, $default:expr
|
|
||||||
) => {
|
|
||||||
pub const $KEYS: EventMap<'static, $State, $Input, $Command> = EventMap {
|
|
||||||
fallback: Some(&|$state, $input|Some($default)),
|
|
||||||
bindings: &[ $(($key, &|$state|Some($handler)),)* ]
|
|
||||||
};
|
|
||||||
input_to_command!($(<$lt>)? $Command: |$state: $State, input: $Input|$KEYS.handle($state, input)?);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[macro_export] macro_rules! input_to_command {
|
|
||||||
(<$($l:lifetime),+> $Command:ty: |$state:ident:$State:ty, $input:ident:$Input:ty| $handler:expr) => {
|
|
||||||
impl<$($l),+> InputToCommand<$Input, $State> for $Command {
|
|
||||||
fn input_to_command ($state: &$State, $input: &$Input) -> Option<Self> {
|
|
||||||
Some($handler)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
($Command:ty: |$state:ident:$State:ty, $input:ident:$Input:ty| $handler:expr) => {
|
|
||||||
impl InputToCommand<$Input, $State> for $Command {
|
|
||||||
fn input_to_command ($state: &$State, $input: &$Input) -> Option<Self> {
|
|
||||||
Some($handler)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait InputToCommand<I, S>: Command<S> + Sized {
|
|
||||||
fn input_to_command (state: &S, input: &I) -> Option<Self>;
|
|
||||||
fn execute_with_state (state: &mut S, input: &I) -> Perhaps<bool> {
|
|
||||||
Ok(if let Some(command) = Self::input_to_command(state, input) {
|
|
||||||
let _undo = command.execute(state)?;
|
|
||||||
Some(true)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
use crate::*;
|
|
||||||
|
|
||||||
/** Implement `Command` for given `State` and `handler` */
|
/** Implement `Command` for given `State` and `handler` */
|
||||||
#[macro_export] macro_rules! command {
|
#[macro_export] macro_rules! command {
|
||||||
($(<$($l:lifetime),+>)?|$self:ident:$Command:ty,$state:ident:$State:ty|$handler:expr) => {
|
($(<$($l:lifetime),+>)?|$self:ident:$Command:ty,$state:ident:$State:ty|$handler:expr) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue