From 8cab7222a27038967503d8661b6b04c19301919a Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Sat, 24 Jul 2021 01:09:24 +0200 Subject: [PATCH] Hints backend (#234) * Generate hints from list of actions * Remove redundant Key from output * Improve formatting * Change logging verbosity for hints --- core/editor/src/communication/dispatcher.rs | 1 + core/editor/src/input/input_mapper.rs | 36 ++++++++++++++++++--- core/editor/src/input/keyboard.rs | 3 ++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/core/editor/src/communication/dispatcher.rs b/core/editor/src/communication/dispatcher.rs index fe521b8f..d77375c4 100644 --- a/core/editor/src/communication/dispatcher.rs +++ b/core/editor/src/communication/dispatcher.rs @@ -33,6 +33,7 @@ impl Dispatcher { ) || MessageDiscriminant::from(&message).local_name().ends_with("MouseMove")) { log::trace!("Message: {}", message.to_discriminant().local_name()); + log::trace!("Hints:{}", self.input_mapper.hints(self.collect_actions())); } match message { NoOp => (), diff --git a/core/editor/src/input/input_mapper.rs b/core/editor/src/input/input_mapper.rs index 78991a02..3acc8333 100644 --- a/core/editor/src/input/input_mapper.rs +++ b/core/editor/src/input/input_mapper.rs @@ -1,11 +1,12 @@ -use crate::consts::{MINUS_KEY_ZOOM_RATE, PLUS_KEY_ZOOM_RATE}; -use crate::message_prelude::*; -use crate::tool::ToolType; - use super::{ keyboard::{Key, KeyStates, NUMBER_OF_KEYS}, InputPreprocessor, }; +use crate::consts::{MINUS_KEY_ZOOM_RATE, PLUS_KEY_ZOOM_RATE}; +use crate::message_prelude::*; +use crate::tool::ToolType; + +use std::fmt::Write; const NUDGE_AMOUNT: f64 = 1.; const SHIFT_NUDGE_AMOUNT: f64 = 10.; @@ -16,6 +17,7 @@ pub enum InputMapperMessage { PointerMove, MouseScroll, KeyUp(Key), + #[child] KeyDown(Key), } @@ -281,6 +283,32 @@ pub struct InputMapper { mapping: Mapping, } +impl InputMapper { + pub fn hints(&self, actions: ActionList) -> String { + let mut output = String::new(); + let actions: Vec = actions + .into_iter() + .flatten() + .filter(|a| !matches!(*a, MessageDiscriminant::Tool(ToolMessageDiscriminant::SelectTool) | MessageDiscriminant::Global(_))) + .collect(); + self.mapping + .key_down + .iter() + .enumerate() + .filter_map(|(i, m)| { + let ma = + m.0.iter() + .find_map(|m| actions.iter().find_map(|a| (a == &m.action.to_discriminant()).then(|| m.action.to_discriminant()))); + + ma.map(|a| unsafe { (std::mem::transmute_copy::(&i), a) }) + }) + .for_each(|(k, a)| { + let _ = write!(output, "{}: {}, ", k.to_discriminant().local_name(), a.local_name().split('.').last().unwrap()); + }); + output.replace("Key", "") + } +} + impl MessageHandler for InputMapper { fn process_action(&mut self, message: InputMapperMessage, data: (&InputPreprocessor, ActionList), responses: &mut VecDeque) { let (input, actions) = data; diff --git a/core/editor/src/input/keyboard.rs b/core/editor/src/input/keyboard.rs index b0573eec..e06dedf7 100644 --- a/core/editor/src/input/keyboard.rs +++ b/core/editor/src/input/keyboard.rs @@ -1,3 +1,5 @@ +use crate::message_prelude::*; + pub const NUMBER_OF_KEYS: usize = Key::NumKeys as usize; // Edit this to specify the storage type used // TODO: Increase size of type @@ -9,6 +11,7 @@ const STORAGE_SIZE_BITS: usize = 1 << STORAGE_SIZE; const KEY_MASK_STORAGE_LENGTH: usize = (NUMBER_OF_KEYS + STORAGE_SIZE_BITS - 1) >> STORAGE_SIZE; pub type KeyStates = BitVector; +#[impl_message(Message, InputMapperMessage, KeyDown)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Key { UnknownKey,