diff --git a/src/editor.rs b/src/editor.rs index b3e50b1..c2cf980 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -458,7 +458,7 @@ impl Editor { self.mouse.clear(); } - if rl.is_key_pressed(KeyboardKey::KEY_ESCAPE) { + if globals.input.is_pressed(ActionId::ToggleMenu) { self.popup = match self.popup { Popup::Success | Popup::Failure => { self.dismissed_end = true; @@ -499,17 +499,22 @@ impl Editor { if globals.input.is_pressed(ActionId::StepSim) { self.step_pressed() } - if rl.is_key_pressed(KeyboardKey::KEY_ENTER) { + if globals.input.is_pressed(ActionId::StartSim) { match self.sim_state { SimState::Editing => { self.init_sim(); self.sim_state = SimState::Running; } - SimState::Running => { + SimState::Stepping => self.sim_state = SimState::Running, + SimState::Running => (), + } + } else if globals.input.is_pressed(ActionId::StopSim) { + match self.sim_state { + SimState::Running | SimState::Stepping => { self.sim_state = SimState::Editing; self.popup = Popup::None; } - SimState::Stepping => self.sim_state = SimState::Running, + SimState::Editing => (), } } @@ -552,9 +557,7 @@ impl Editor { if self.sim_state == SimState::Editing { if let Some(clipboard) = &mut globals.clipboard { - if rl.is_key_down(KeyboardKey::KEY_LEFT_CONTROL) - && rl.is_key_pressed(KeyboardKey::KEY_V) - { + if globals.input.is_pressed(ActionId::Paste) { if let Ok(text) = clipboard.get_text() { let b = Board::from_user_str(&text); self.pasting_board = Some(b); @@ -1042,8 +1045,7 @@ impl Editor { self.tooltip.add(188, y, 40, 40, "Copy"); if simple_button((d, &self.mouse), 188, y, 40, 40) - || (d.is_key_pressed(KeyboardKey::KEY_C) - && d.is_key_down(KeyboardKey::KEY_LEFT_CONTROL)) + || globals.input.is_pressed(ActionId::Copy) { let board = self.get_selected_as_board(selection); if let Some(clipboard) = &mut globals.clipboard { diff --git a/src/input.rs b/src/input.rs index a8ece5a..823470b 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, mem::transmute}; +use std::{collections::BTreeMap, mem::transmute}; use raylib::{ ffi::{KeyboardKey, MouseButton}, @@ -6,13 +6,18 @@ use raylib::{ }; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord)] #[repr(u8)] pub enum ActionId { - StepSim, Undo, Redo, - // just like in C, because this way doesn't need more depenedencies + Copy, + Paste, + ToggleMenu, + StartSim, + StopSim, + StepSim, + // just like in C, because this way doesn't need more dependencies _ActionIdSize, } @@ -26,9 +31,14 @@ impl Default for Input { trigger: InputTrigger::Key(key), }]; }; - bind_key(ActionId::StepSim, vec![], KEY_SPACE); bind_key(ActionId::Undo, vec![KEY_LEFT_CONTROL], KEY_Z); bind_key(ActionId::Redo, vec![KEY_LEFT_CONTROL], KEY_Y); + bind_key(ActionId::Copy, vec![KEY_LEFT_CONTROL], KEY_C); + bind_key(ActionId::Paste, vec![KEY_LEFT_CONTROL], KEY_V); + bind_key(ActionId::ToggleMenu, vec![], KEY_ESCAPE); + bind_key(ActionId::StartSim, vec![], KEY_ENTER); + bind_key(ActionId::StopSim, vec![], KEY_ENTER); + bind_key(ActionId::StepSim, vec![], KEY_SPACE); Self { bindings, @@ -46,7 +56,7 @@ enum BindingState { Released, } -type InputMap = HashMap>; +type InputMap = BTreeMap>; #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(from = "InputMap", into = "InputMap")]