use super::types::{
INPUT_MOD_CTRL, INPUT_MOD_NONE, InputEvent, InputEventKind, InputModifiers, Key,
};
use crate::{Error, Result};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum InputContext {
#[default]
Global,
NodeCanvas,
TextInput,
Timeline,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct KeyBinding {
pub key: Key,
pub modifiers: InputModifiers,
}
impl KeyBinding {
#[must_use]
pub fn matches(&self, event: &InputEvent) -> bool {
event.kind == InputEventKind::KeyDown
&& event.key == self.key
&& event.modifiers == self.modifiers
}
}
pub struct KeyAction {
pub name: String,
pub binding: KeyBinding,
pub context: InputContext,
pub allow_repeat: bool,
pub callback: Option<Box<dyn FnMut()>>,
}
impl std::fmt::Debug for KeyAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("KeyAction")
.field("name", &self.name)
.field("binding", &self.binding)
.field("context", &self.context)
.field("allow_repeat", &self.allow_repeat)
.field("callback", &self.callback.is_some())
.finish()
}
}
#[derive(Default)]
pub struct InputSystem {
actions: Vec<KeyAction>,
context: InputContext,
}
impl InputSystem {
pub fn new() -> Self {
Self::default()
}
pub fn register_action(&mut self, action: KeyAction) {
if let Some(existing) = self
.actions
.iter_mut()
.find(|existing| existing.name == action.name)
{
*existing = action;
return;
}
self.actions.push(action);
}
pub fn unregister_action(&mut self, name: &str) {
if let Some(index) = self.actions.iter().position(|action| action.name == name) {
self.actions.swap_remove(index);
}
}
pub fn set_callback(&mut self, name: &str, callback: impl FnMut() + 'static) {
if let Some(action) = self.actions.iter_mut().find(|action| action.name == name) {
action.callback = Some(Box::new(callback));
}
}
pub fn rebind(&mut self, name: &str, binding: KeyBinding) {
if let Some(action) = self.actions.iter_mut().find(|action| action.name == name) {
action.binding = binding;
}
}
pub fn set_context(&mut self, context: InputContext) {
self.context = context;
}
#[must_use]
pub const fn context(&self) -> InputContext {
self.context
}
#[must_use]
pub fn action_count(&self) -> usize {
self.actions.len()
}
pub fn dispatch(&mut self, event: &InputEvent) -> bool {
if event.kind != InputEventKind::KeyDown {
return false;
}
for action in &mut self.actions {
if action.context != InputContext::Global && action.context != self.context {
continue;
}
if action.binding.matches(event) {
if event.key_repeat && !action.allow_repeat {
return true;
}
if let Some(callback) = &mut action.callback {
callback();
}
return true;
}
}
false
}
pub fn register_defaults(&mut self) {
self.register_action(KeyAction {
name: "screenshot".into(),
binding: KeyBinding {
key: Key::F12,
modifiers: INPUT_MOD_NONE,
},
context: InputContext::Global,
allow_repeat: false,
callback: None,
});
self.register_action(KeyAction {
name: "record".into(),
binding: KeyBinding {
key: Key::R,
modifiers: INPUT_MOD_CTRL,
},
context: InputContext::Global,
allow_repeat: false,
callback: None,
});
self.register_action(KeyAction {
name: "toggle_camera".into(),
binding: KeyBinding {
key: Key::Space,
modifiers: INPUT_MOD_NONE,
},
context: InputContext::Global,
allow_repeat: false,
callback: None,
});
self.register_action(KeyAction {
name: "undo".into(),
binding: KeyBinding {
key: Key::Z,
modifiers: INPUT_MOD_CTRL,
},
context: InputContext::NodeCanvas,
allow_repeat: false,
callback: None,
});
self.register_action(KeyAction {
name: "redo".into(),
binding: KeyBinding {
key: Key::Y,
modifiers: INPUT_MOD_CTRL,
},
context: InputContext::NodeCanvas,
allow_repeat: false,
callback: None,
});
self.register_action(KeyAction {
name: "save".into(),
binding: KeyBinding {
key: Key::S,
modifiers: INPUT_MOD_CTRL,
},
context: InputContext::Global,
allow_repeat: false,
callback: None,
});
self.register_action(KeyAction {
name: "fit_view".into(),
binding: KeyBinding {
key: Key::F,
modifiers: INPUT_MOD_NONE,
},
context: InputContext::NodeCanvas,
allow_repeat: false,
callback: None,
});
}
pub fn load_bindings_yaml(&mut self, path: &str) -> Result<()> {
if path.is_empty() {
return Err(Error::invalid_argument(
"InputSystem::load_bindings_yaml requires a path",
));
}
Err(Error::missing_capability(
"InputSystem YAML persistence requires a YAML backend",
))
}
pub fn save_bindings_yaml(&self, path: &str) -> Result<()> {
if path.is_empty() {
return Err(Error::invalid_argument(
"InputSystem::save_bindings_yaml requires a path",
));
}
Err(Error::missing_capability(
"InputSystem YAML persistence requires a YAML backend",
))
}
}