diff options
| author | Sakarias Johansson <sakarias.johansson@goodbyekansas.com> | 2023-04-09 19:11:39 +0200 |
|---|---|---|
| committer | Sakarias Johansson <sakariasjohansson@hotmail.com> | 2023-04-14 16:39:04 +0200 |
| commit | 0ae6ca062f5936ae6f595f45ca0a78ed049452bc (patch) | |
| tree | 712111e3aa823de7dc85eadca8fd5f78124afa6b /racer-tracer/src/key_inputs.rs | |
| parent | 5f8faa17457426c4ca8c54bf67b5aa96eb7a52ea (diff) | |
| download | racer-tracer-0ae6ca062f5936ae6f595f45ca0a78ed049452bc.tar.gz racer-tracer-0ae6ca062f5936ae6f595f45ca0a78ed049452bc.tar.xz racer-tracer-0ae6ca062f5936ae6f595f45ca0a78ed049452bc.zip | |
✨ Add scene controller
Wanted to be able to for example move around freely in the scene but
also have something that would for example follow key frames and
render a gif.
Abstracted it with a scene controller. You can hook up keybinds and
other thigns for it as well. Right now there is only the interactive
scene controller which keep the behaviours previous to this change.
Now I could possibly switch it out with something that uses key frames
to render several images to create for example a gif.
List of other Misc changes:
- Add configuration setting for scene controller (`scene_controller`)
- Add configuration setting for renderer
- Add configuration setting for preview renderer (`preview_renderer`)
- Add clone to Config.
- Add from implementation for Renderer to be created from the config
object.
- Add cancel event to image action. An action could be blocking when
the application wants to exit. Actions can now listen to the cancel
event to exit early and not block.
- Fixed bug where WaitForSignal action would block after application
tries to exit.
- Add method to KeyInputs to be able to take a list of callbacks instead of
manually registering every callback one at the time.
Diffstat (limited to 'racer-tracer/src/key_inputs.rs')
| -rw-r--r-- | racer-tracer/src/key_inputs.rs | 67 |
1 files changed, 53 insertions, 14 deletions
diff --git a/racer-tracer/src/key_inputs.rs b/racer-tracer/src/key_inputs.rs index 1e3505e..9a7bde5 100644 --- a/racer-tracer/src/key_inputs.rs +++ b/racer-tracer/src/key_inputs.rs @@ -5,15 +5,20 @@ use slog::Logger; use crate::error::TracerError; -pub type KeyClosure<'a> = &'a (dyn Fn(f64) -> Result<(), TracerError> + Send + Sync); +pub enum KeyEvent { + Release, + Down, +} -pub struct KeyInputs<'a> { - is_down_callbacks: HashMap<Key, Vec<KeyClosure<'a>>>, - is_released_callbacks: HashMap<Key, Vec<KeyClosure<'a>>>, +type Callback<'func> = Box<dyn Fn(f64) -> Result<(), TracerError> + Send + Sync + 'func>; +pub type KeyCallback<'func> = (KeyEvent, Key, Callback<'func>); +pub struct KeyInputs<'func> { + is_down_callbacks: HashMap<Key, Vec<Callback<'func>>>, + is_released_callbacks: HashMap<Key, Vec<Callback<'func>>>, log: Logger, } -impl<'a> KeyInputs<'a> { +impl<'func, 'window> KeyInputs<'func> { pub fn new(log: Logger) -> Self { KeyInputs { is_down_callbacks: HashMap::new(), @@ -22,35 +27,69 @@ impl<'a> KeyInputs<'a> { } } - pub fn release(&mut self, key: Key, closure: KeyClosure<'a>) { + pub fn input( + event: KeyEvent, + key: Key, + callback: impl Fn(f64) -> Result<(), TracerError> + Send + Sync + 'func, + ) -> KeyCallback<'func> { + (event, key, Box::new(callback)) + } + + pub fn register_inputs(&mut self, inputs: Vec<KeyCallback<'func>>) { + inputs.into_iter().for_each(|(ev, key, closure)| match ev { + KeyEvent::Release => { + let callbacks = self + .is_released_callbacks + .entry(key) + .or_insert_with(Vec::new); + callbacks.push(closure); + } + KeyEvent::Down => { + let callbacks = self.is_down_callbacks.entry(key).or_insert_with(Vec::new); + callbacks.push(closure); + } + }) + } + + #[allow(dead_code)] + pub fn release( + &mut self, + key: Key, + closure: impl Fn(f64) -> Result<(), TracerError> + Send + Sync + 'func, + ) { let callbacks = self .is_released_callbacks .entry(key) .or_insert_with(Vec::new); - callbacks.push(closure); + callbacks.push(Box::new(closure)); } - pub fn down(&mut self, key: Key, closure: KeyClosure<'a>) { + #[allow(dead_code)] + pub fn down( + &mut self, + key: Key, + closure: impl Fn(f64) -> Result<(), TracerError> + Send + Sync + 'func, + ) { let callbacks = self.is_down_callbacks.entry(key).or_insert_with(Vec::new); - callbacks.push(closure); + callbacks.push(Box::new(closure)); } - pub fn update(&mut self, window: &Window, dt: f64) { + pub fn update(&self, window: &'window Window, dt: f64) { self.is_down_callbacks - .iter_mut() + .iter() .filter(|(key, _callbacks)| window.is_key_down(**key)) .for_each(|(_key, callbacks)| { - callbacks.iter_mut().for_each(|callback| { + callbacks.iter().for_each(|callback| { if let Err(e) = callback(dt) { error!(self.log, "Key callback error: {}", e); } }) }); self.is_released_callbacks - .iter_mut() + .iter() .filter(|(key, _callbacks)| window.is_key_released(**key)) .for_each(|(_key, callbacks)| { - callbacks.iter_mut().for_each(|callback| { + callbacks.iter().for_each(|callback| { if let Err(e) = callback(dt) { error!(self.log, "Key callback error: {}", e); } |
