2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/shortcut_manager.hpp>
|
2021-12-23 15:11:38 +01:00
|
|
|
#include <imgui.h>
|
2023-11-17 14:46:21 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-12-23 15:11:38 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/ui/view.hpp>
|
2021-12-23 15:11:38 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2023-07-26 13:50:51 +02:00
|
|
|
namespace {
|
|
|
|
|
2023-11-17 14:46:21 +01:00
|
|
|
std::map<Shortcut, ShortcutManager::ShortcutEntry> s_globalShortcuts;
|
|
|
|
std::atomic<bool> s_paused;
|
|
|
|
std::optional<Shortcut> s_prevShortcut;
|
2023-07-26 13:50:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void ShortcutManager::addGlobalShortcut(const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function<void()> &callback) {
|
2023-11-17 14:46:21 +01:00
|
|
|
s_globalShortcuts.insert({ shortcut, { shortcut, unlocalizedName, callback } });
|
2021-12-23 15:11:38 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 12:22:28 +01:00
|
|
|
void ShortcutManager::addShortcut(View *view, const Shortcut &shortcut, const UnlocalizedString &unlocalizedName, const std::function<void()> &callback) {
|
2023-11-17 14:46:21 +01:00
|
|
|
view->m_shortcuts.insert({ shortcut + CurrentView, { shortcut, unlocalizedName, 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-11-17 20:27:42 +01:00
|
|
|
static void processShortcut(const Shortcut &shortcut, const std::map<Shortcut, ShortcutManager::ShortcutEntry> &shortcuts) {
|
2023-11-17 14:46:21 +01:00
|
|
|
if (s_paused) return;
|
|
|
|
|
|
|
|
if (ImGui::IsPopupOpen(ImGuiID(0), ImGuiPopupFlags_AnyPopupId))
|
2023-11-17 20:27:42 +01:00
|
|
|
return;
|
2023-11-17 14:46:21 +01:00
|
|
|
|
2023-11-17 20:27:42 +01:00
|
|
|
if (shortcuts.contains(shortcut + AllowWhileTyping)) {
|
|
|
|
shortcuts.at(shortcut + AllowWhileTyping).callback();
|
|
|
|
} else if (shortcuts.contains(shortcut)) {
|
2023-06-11 21:41:11 +02:00
|
|
|
if (!ImGui::GetIO().WantTextInput)
|
2023-11-17 20:27:42 +01:00
|
|
|
shortcuts.at(shortcut).callback();
|
2023-06-11 21:41:11 +02:00
|
|
|
}
|
2022-07-30 22:01:49 +02:00
|
|
|
}
|
|
|
|
|
2023-11-17 20:27:42 +01:00
|
|
|
void ShortcutManager::process(const std::unique_ptr<View> ¤tView, bool ctrl, bool alt, bool shift, bool super, bool focused, u32 keyCode) {
|
|
|
|
Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, focused, keyCode);
|
2023-11-17 14:46:21 +01:00
|
|
|
if (keyCode != 0)
|
2023-11-17 20:27:42 +01:00
|
|
|
s_prevShortcut = Shortcut(pressedShortcut.getKeys());
|
2023-11-17 14:46:21 +01:00
|
|
|
|
2023-11-17 20:27:42 +01:00
|
|
|
processShortcut(pressedShortcut, currentView->m_shortcuts);
|
|
|
|
}
|
2023-11-17 14:46:21 +01:00
|
|
|
|
2023-11-17 20:27:42 +01:00
|
|
|
void ShortcutManager::processGlobals(bool ctrl, bool alt, bool shift, bool super, u32 keyCode) {
|
|
|
|
Shortcut pressedShortcut = getShortcut(ctrl, alt, shift, super, false, keyCode);
|
|
|
|
if (keyCode != 0)
|
|
|
|
s_prevShortcut = Shortcut(pressedShortcut.getKeys());
|
2023-11-17 14:46:21 +01:00
|
|
|
|
2023-11-17 20:27:42 +01:00
|
|
|
processShortcut(pressedShortcut, s_globalShortcuts);
|
2022-02-01 18:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::clearShortcuts() {
|
2023-07-26 13:50:51 +02:00
|
|
|
s_globalShortcuts.clear();
|
2021-12-23 15:11:38 +01:00
|
|
|
}
|
|
|
|
|
2023-11-17 14:46:21 +01:00
|
|
|
void ShortcutManager::resumeShortcuts() {
|
|
|
|
s_paused = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShortcutManager::pauseShortcuts() {
|
|
|
|
s_paused = true;
|
|
|
|
s_prevShortcut.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Shortcut> ShortcutManager::getPreviousShortcut() {
|
|
|
|
return s_prevShortcut;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ShortcutManager::ShortcutEntry> ShortcutManager::getGlobalShortcuts() {
|
|
|
|
std::vector<ShortcutManager::ShortcutEntry> result;
|
|
|
|
|
|
|
|
for (auto &[shortcut, entry] : s_globalShortcuts)
|
|
|
|
result.push_back(entry);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-27 16:33:49 +01:00
|
|
|
std::vector<ShortcutManager::ShortcutEntry> ShortcutManager::getViewShortcuts(const View *view) {
|
2023-11-17 14:46:21 +01:00
|
|
|
std::vector<ShortcutManager::ShortcutEntry> result;
|
|
|
|
|
|
|
|
for (auto &[shortcut, entry] : view->m_shortcuts)
|
|
|
|
result.push_back(entry);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-18 14:34:16 +01:00
|
|
|
static bool updateShortcutImpl(const Shortcut &oldShortcut, const Shortcut &newShortcut, std::map<Shortcut, ShortcutManager::ShortcutEntry> &shortcuts) {
|
2023-11-24 20:17:01 +01:00
|
|
|
if (shortcuts.contains(oldShortcut)) {
|
|
|
|
if (shortcuts.contains(newShortcut))
|
2023-11-18 14:34:16 +01:00
|
|
|
return false;
|
|
|
|
|
2023-11-24 20:17:01 +01:00
|
|
|
shortcuts[newShortcut] = shortcuts[oldShortcut];
|
|
|
|
shortcuts[newShortcut].shortcut = newShortcut;
|
|
|
|
shortcuts.erase(oldShortcut);
|
2023-11-18 14:34:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShortcutManager::updateShortcut(const Shortcut &oldShortcut, const Shortcut &newShortcut, View *view) {
|
2023-11-17 14:46:21 +01:00
|
|
|
if (oldShortcut == newShortcut)
|
2023-11-18 14:34:16 +01:00
|
|
|
return true;
|
2023-11-17 14:46:21 +01:00
|
|
|
|
2023-11-18 14:34:16 +01:00
|
|
|
bool result;
|
2023-11-17 14:46:21 +01:00
|
|
|
if (view != nullptr) {
|
2023-11-24 20:17:01 +01:00
|
|
|
result = updateShortcutImpl(oldShortcut + CurrentView, newShortcut + CurrentView , view->m_shortcuts);
|
2023-11-17 14:46:21 +01:00
|
|
|
} else {
|
2023-11-18 14:34:16 +01:00
|
|
|
result = updateShortcutImpl(oldShortcut, newShortcut, s_globalShortcuts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
for (auto &[priority, menuItem] : ContentRegistry::Interface::impl::getMenuItems()) {
|
|
|
|
if (menuItem.view == view && *menuItem.shortcut == oldShortcut) {
|
|
|
|
*menuItem.shortcut = newShortcut;
|
|
|
|
break;
|
|
|
|
}
|
2023-11-17 14:46:21 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-18 14:34:16 +01:00
|
|
|
|
|
|
|
return result;
|
2023-11-17 14:46:21 +01:00
|
|
|
}
|
|
|
|
|
2021-12-23 15:11:38 +01:00
|
|
|
}
|