yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
shortcut_manager.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_SYSTEM_SHORTCUT_MANAGER_H
2#define YAZE_APP_EDITOR_SYSTEM_SHORTCUT_MANAGER_H
3
4#include <functional>
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9// Must define before including imgui.h
10#ifndef IMGUI_DEFINE_MATH_OPERATORS
11#define IMGUI_DEFINE_MATH_OPERATORS
12#endif
13
14#include "imgui/imgui.h"
15
16namespace yaze {
17namespace editor {
18
19struct Shortcut {
20 enum class Scope { kGlobal, kEditor, kPanel };
21 std::string name;
23 std::vector<ImGuiKey> keys;
24 std::function<void()> callback;
25};
26
27std::vector<ImGuiKey> ParseShortcut(const std::string& shortcut);
28
29std::string PrintShortcut(const std::vector<ImGuiKey>& keys);
30
32 public:
33 void RegisterShortcut(const std::string& name,
34 const std::vector<ImGuiKey>& keys,
36 shortcuts_[name] = {name, scope, keys};
37 }
38 void RegisterShortcut(const std::string& name,
39 const std::vector<ImGuiKey>& keys,
40 std::function<void()> callback,
42 shortcuts_[name] = {name, scope, keys, callback};
43 }
44
45 void RegisterShortcut(const std::string& name, ImGuiKey key,
46 std::function<void()> callback,
48 shortcuts_[name] = {name, scope, {key}, callback};
49 }
50
57 void RegisterCommand(const std::string& name, std::function<void()> callback,
59 shortcuts_[name] = {name, scope, {}, callback}; // Empty key vector
60 }
61
62 void ExecuteShortcut(const std::string& name) const {
63 shortcuts_.at(name).callback();
64 }
65
66 // Access the shortcut and print the readable name of the shortcut for menus
67 const Shortcut& GetShortcut(const std::string& name) const {
68 return shortcuts_.at(name);
69 }
70
71 const Shortcut* FindShortcut(const std::string& name) const {
72 auto it = shortcuts_.find(name);
73 return it != shortcuts_.end() ? &it->second : nullptr;
74 }
75
76 // Get shortcut callback function
77 std::function<void()> GetCallback(const std::string& name) const {
78 return shortcuts_.at(name).callback;
79 }
80
81 const std::string GetKeys(const std::string& name) const {
82 return PrintShortcut(shortcuts_.at(name).keys);
83 }
84
85 const std::unordered_map<std::string, Shortcut>& GetShortcuts() const {
86 return shortcuts_;
87 }
88 bool UpdateShortcutKeys(const std::string& name,
89 const std::vector<ImGuiKey>& keys);
90 std::vector<Shortcut> GetShortcutsByScope(Shortcut::Scope scope) const {
91 std::vector<Shortcut> result;
92 result.reserve(shortcuts_.size());
93 for (const auto& [_, sc] : shortcuts_) {
94 if (sc.scope == scope)
95 result.push_back(sc);
96 }
97 return result;
98 }
99
100 // Convenience methods for registering common shortcuts
101 void RegisterStandardShortcuts(std::function<void()> save_callback,
102 std::function<void()> open_callback,
103 std::function<void()> close_callback,
104 std::function<void()> find_callback,
105 std::function<void()> settings_callback);
106
107 void RegisterWindowNavigationShortcuts(std::function<void()> focus_left,
108 std::function<void()> focus_right,
109 std::function<void()> focus_up,
110 std::function<void()> focus_down,
111 std::function<void()> close_window,
112 std::function<void()> split_horizontal,
113 std::function<void()> split_vertical);
114
115 private:
116 std::unordered_map<std::string, Shortcut> shortcuts_;
117};
118
119void ExecuteShortcuts(const ShortcutManager& shortcut_manager);
120
121} // namespace editor
122} // namespace yaze
123
124#endif // YAZE_APP_EDITOR_SYSTEM_SHORTCUT_MANAGER_H
const Shortcut & GetShortcut(const std::string &name) const
const Shortcut * FindShortcut(const std::string &name) const
const std::string GetKeys(const std::string &name) const
void RegisterCommand(const std::string &name, std::function< void()> callback, Shortcut::Scope scope=Shortcut::Scope::kGlobal)
Register a command without keyboard shortcut (command palette only)
void RegisterShortcut(const std::string &name, const std::vector< ImGuiKey > &keys, Shortcut::Scope scope=Shortcut::Scope::kGlobal)
void RegisterShortcut(const std::string &name, ImGuiKey key, std::function< void()> callback, Shortcut::Scope scope=Shortcut::Scope::kGlobal)
std::function< void()> GetCallback(const std::string &name) const
std::vector< Shortcut > GetShortcutsByScope(Shortcut::Scope scope) const
void RegisterShortcut(const std::string &name, const std::vector< ImGuiKey > &keys, std::function< void()> callback, Shortcut::Scope scope=Shortcut::Scope::kGlobal)
void RegisterStandardShortcuts(std::function< void()> save_callback, std::function< void()> open_callback, std::function< void()> close_callback, std::function< void()> find_callback, std::function< void()> settings_callback)
void RegisterWindowNavigationShortcuts(std::function< void()> focus_left, std::function< void()> focus_right, std::function< void()> focus_up, std::function< void()> focus_down, std::function< void()> close_window, std::function< void()> split_horizontal, std::function< void()> split_vertical)
void ExecuteShortcut(const std::string &name) const
std::unordered_map< std::string, Shortcut > shortcuts_
const std::unordered_map< std::string, Shortcut > & GetShortcuts() const
bool UpdateShortcutKeys(const std::string &name, const std::vector< ImGuiKey > &keys)
std::vector< ImGuiKey > ParseShortcut(const std::string &shortcut)
std::string PrintShortcut(const std::vector< ImGuiKey > &keys)
void ExecuteShortcuts(const ShortcutManager &shortcut_manager)
std::function< void()> callback
std::vector< ImGuiKey > keys