Add bindable keyboard shortcuts
This commit is contained in:
+206
-2
@@ -1,11 +1,13 @@
|
||||
use std::{
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
cmp::Ordering,
|
||||
collections::{hash_map::Entry, HashMap, HashSet},
|
||||
fmt::Display,
|
||||
str::FromStr,
|
||||
sync::{Arc, RwLock},
|
||||
sync::{Arc, Mutex, RwLock},
|
||||
};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use egui::{Key, KeyboardShortcut, Modifiers};
|
||||
use gilrs::{ev::Code, Axis, Button, Gamepad, GamepadId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
@@ -454,3 +456,205 @@ struct PersistedGamepadMapping {
|
||||
default_buttons: Vec<(Code, VBKey)>,
|
||||
default_axes: Vec<(Code, (VBKey, VBKey))>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Shortcut {
|
||||
pub shortcut: KeyboardShortcut,
|
||||
pub command: Command,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
|
||||
pub enum Command {
|
||||
OpenRom,
|
||||
Quit,
|
||||
FrameAdvance,
|
||||
Reset,
|
||||
PauseResume,
|
||||
// if you update this, update Command::all and add a default
|
||||
}
|
||||
|
||||
impl Command {
|
||||
pub fn all() -> [Self; 5] {
|
||||
[
|
||||
Self::OpenRom,
|
||||
Self::Quit,
|
||||
Self::PauseResume,
|
||||
Self::Reset,
|
||||
Self::FrameAdvance,
|
||||
]
|
||||
}
|
||||
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::OpenRom => "Open ROM",
|
||||
Self::Quit => "Exit",
|
||||
Self::PauseResume => "Pause/Resume",
|
||||
Self::Reset => "Reset",
|
||||
Self::FrameAdvance => "Frame Advance",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Command {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(self.name())
|
||||
}
|
||||
}
|
||||
|
||||
struct Shortcuts {
|
||||
all: Vec<(Command, KeyboardShortcut)>,
|
||||
by_command: HashMap<Command, KeyboardShortcut>,
|
||||
}
|
||||
|
||||
impl Default for Shortcuts {
|
||||
fn default() -> Self {
|
||||
let mut shortcuts = Shortcuts {
|
||||
all: vec![],
|
||||
by_command: HashMap::new(),
|
||||
};
|
||||
shortcuts.set(
|
||||
Command::OpenRom,
|
||||
KeyboardShortcut::new(Modifiers::COMMAND, Key::O),
|
||||
);
|
||||
shortcuts.set(
|
||||
Command::Quit,
|
||||
KeyboardShortcut::new(Modifiers::COMMAND, Key::Q),
|
||||
);
|
||||
shortcuts.set(
|
||||
Command::PauseResume,
|
||||
KeyboardShortcut::new(Modifiers::NONE, Key::F5),
|
||||
);
|
||||
shortcuts.set(
|
||||
Command::Reset,
|
||||
KeyboardShortcut::new(Modifiers::SHIFT, Key::F5),
|
||||
);
|
||||
shortcuts.set(
|
||||
Command::FrameAdvance,
|
||||
KeyboardShortcut::new(Modifiers::NONE, Key::F6),
|
||||
);
|
||||
shortcuts
|
||||
}
|
||||
}
|
||||
|
||||
impl Shortcuts {
|
||||
fn set(&mut self, command: Command, shortcut: KeyboardShortcut) {
|
||||
if self.by_command.insert(command, shortcut).is_some() {
|
||||
for (cmd, sht) in &mut self.all {
|
||||
if *cmd == command {
|
||||
*sht = shortcut;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.all.push((command, shortcut));
|
||||
}
|
||||
self.all.sort_by(|l, r| order_shortcut(l.1, r.1));
|
||||
}
|
||||
|
||||
fn unset(&mut self, command: Command) {
|
||||
if self.by_command.remove(&command).is_some() {
|
||||
self.all.retain(|(c, _)| *c != command);
|
||||
}
|
||||
}
|
||||
|
||||
fn save(&self) -> PersistedShortcuts {
|
||||
let mut shortcuts = PersistedShortcuts { shortcuts: vec![] };
|
||||
for command in Command::all() {
|
||||
let shortcut = self.by_command.get(&command).copied();
|
||||
shortcuts.shortcuts.push((command, shortcut));
|
||||
}
|
||||
shortcuts
|
||||
}
|
||||
}
|
||||
|
||||
fn order_shortcut(left: KeyboardShortcut, right: KeyboardShortcut) -> Ordering {
|
||||
left.logical_key.cmp(&right.logical_key).then_with(|| {
|
||||
specificity(left.modifiers)
|
||||
.cmp(&specificity(right.modifiers))
|
||||
.reverse()
|
||||
})
|
||||
}
|
||||
|
||||
fn specificity(modifiers: egui::Modifiers) -> usize {
|
||||
let mut mods = 0;
|
||||
if modifiers.alt {
|
||||
mods += 1;
|
||||
}
|
||||
if modifiers.command || modifiers.ctrl {
|
||||
mods += 1;
|
||||
}
|
||||
if modifiers.shift {
|
||||
mods += 1;
|
||||
}
|
||||
mods
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PersistedShortcuts {
|
||||
shortcuts: Vec<(Command, Option<KeyboardShortcut>)>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ShortcutProvider {
|
||||
persistence: Persistence,
|
||||
shortcuts: Arc<Mutex<Shortcuts>>,
|
||||
}
|
||||
|
||||
impl ShortcutProvider {
|
||||
pub fn new(persistence: Persistence) -> Self {
|
||||
let mut shortcuts = Shortcuts::default();
|
||||
if let Ok(saved) = persistence.load_config::<PersistedShortcuts>("shortcuts") {
|
||||
for (command, shortcut) in saved.shortcuts {
|
||||
if let Some(shortcut) = shortcut {
|
||||
shortcuts.set(command, shortcut);
|
||||
} else {
|
||||
shortcuts.unset(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
Self {
|
||||
persistence,
|
||||
shortcuts: Arc::new(Mutex::new(shortcuts)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shortcut_for(&self, command: Command) -> Option<KeyboardShortcut> {
|
||||
let lock = self.shortcuts.lock().unwrap();
|
||||
lock.by_command.get(&command).copied()
|
||||
}
|
||||
|
||||
pub fn consume_all(&self, input: &mut egui::InputState) -> HashSet<Command> {
|
||||
let lock = self.shortcuts.lock().unwrap();
|
||||
lock.all
|
||||
.iter()
|
||||
.filter_map(|(command, shortcut)| input.consume_shortcut(shortcut).then_some(*command))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn set(&self, command: Command, shortcut: KeyboardShortcut) {
|
||||
let updated = {
|
||||
let mut lock = self.shortcuts.lock().unwrap();
|
||||
lock.set(command, shortcut);
|
||||
lock.save()
|
||||
};
|
||||
let _ = self.persistence.save_config("shortcuts", &updated);
|
||||
}
|
||||
|
||||
pub fn unset(&self, command: Command) {
|
||||
let updated = {
|
||||
let mut lock = self.shortcuts.lock().unwrap();
|
||||
lock.unset(command);
|
||||
lock.save()
|
||||
};
|
||||
let _ = self.persistence.save_config("shortcuts", &updated);
|
||||
}
|
||||
|
||||
pub fn reset(&self) {
|
||||
let updated = {
|
||||
let mut lock = self.shortcuts.lock().unwrap();
|
||||
*lock = Shortcuts::default();
|
||||
lock.save()
|
||||
};
|
||||
let _ = self.persistence.save_config("shortcuts", &updated);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user