2021-12-23 15:11:38 +01:00
|
|
|
#include <hex/api/keybinding.hpp>
|
|
|
|
#include <imgui.h>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/ui/view.hpp>
|
2021-12-23 15:11:38 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
std::map<Shortcut, std::function<void()>> ShortcutManager::s_globalShortcuts;
|
|
|
|
|
2021-12-23 15:11:38 +01:00
|
|
|
void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const std::function<void()> &callback) {
|
2022-02-01 18:09:40 +01:00
|
|
|
ShortcutManager::s_globalShortcuts.insert({ shortcut, callback });
|
2021-12-23 15:11:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const std::function<void()> &callback) {
|
2022-01-24 20:53:17 +01:00
|
|
|
view->m_shortcuts.insert({ shortcut, callback });
|
2021-12-23 15:11:38 +01:00
|
|
|
}
|
|
|
|
|
2023-05-21 10:28:55 +02:00
|
|
|
static Shortcut getShortcut(bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) {
|
2021-12-23 15:11:38 +01:00
|
|
|
Shortcut pressedShortcut;
|
|
|
|
|
|
|
|
if (ctrl)
|
|
|
|
pressedShortcut += CTRL;
|
|
|
|
if (alt)
|
|
|
|
pressedShortcut += ALT;
|
|
|
|
if (shift)
|
|
|
|
pressedShortcut += SHIFT;
|
|
|
|
if (super)
|
|
|
|
pressedShortcut += SUPER;
|
2023-05-21 10:28:55 +02:00
|
|
|
if (focused)
|
|
|
|
pressedShortcut += CurrentView;
|
2021-12-23 15:11:38 +01:00
|
|
|
|
|
|
|
pressedShortcut += static_cast<Keys>(keyCode);
|
|
|
|
|
2022-07-30 22:01:49 +02:00
|
|
|
return pressedShortcut;
|
|
|
|
}
|
|
|
|
|
2023-04-08 12:08:45 +02:00
|
|
|
void ShortcutManager::process(const std::unique_ptr<View> ¤tView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) {
|
2023-05-21 10:28:55 +02:00
|
|
|
Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, focused, keyCode);
|
2022-07-30 22:01:49 +02:00
|
|
|
|
2023-05-21 10:28:55 +02:00
|
|
|
if (currentView->m_shortcuts.contains(pressedShortcut))
|
2021-12-23 15:11:38 +01:00
|
|
|
currentView->m_shortcuts[pressedShortcut]();
|
2022-07-30 22:01:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode) {
|
2023-05-21 10:28:55 +02:00
|
|
|
Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, false, keyCode);
|
2022-07-30 22:01:49 +02:00
|
|
|
|
|
|
|
if (ShortcutManager::s_globalShortcuts.contains(pressedShortcut))
|
2022-02-01 18:09:40 +01:00
|
|
|
ShortcutManager::s_globalShortcuts[pressedShortcut]();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::clearShortcuts() {
|
|
|
|
ShortcutManager::s_globalShortcuts.clear();
|
2021-12-23 15:11:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|