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 {
21 kGlobal,
22 kEditor,
23 kPanel
24 };
25 std::string name;
27 std::vector<ImGuiKey> keys;
28 std::function<void()> callback;
29};
30
31std::vector<ImGuiKey> ParseShortcut(const std::string& shortcut);
32
33std::string PrintShortcut(const std::vector<ImGuiKey>& keys);
34
36 public:
37 void RegisterShortcut(const std::string& name,
38 const std::vector<ImGuiKey>& keys,
40 shortcuts_[name] = {name, scope, keys};
41 }
42 void RegisterShortcut(const std::string& name,
43 const std::vector<ImGuiKey>& keys,
44 std::function<void()> callback,
46 shortcuts_[name] = {name, scope, keys, callback};
47 }
48
49 void RegisterShortcut(const std::string& name, ImGuiKey key,
50 std::function<void()> callback,
52 shortcuts_[name] = {name, scope, {key}, callback};
53 }
54
61 void RegisterCommand(const std::string& name,
62 std::function<void()> callback,
64 shortcuts_[name] = {name, scope, {}, callback}; // Empty key vector
65 }
66
67 void ExecuteShortcut(const std::string& name) const {
68 shortcuts_.at(name).callback();
69 }
70
71 // Access the shortcut and print the readable name of the shortcut for menus
72 const Shortcut& GetShortcut(const std::string& name) const {
73 return shortcuts_.at(name);
74 }
75
76 const Shortcut* FindShortcut(const std::string& name) const {
77 auto it = shortcuts_.find(name);
78 return it != shortcuts_.end() ? &it->second : nullptr;
79 }
80
81 // Get shortcut callback function
82 std::function<void()> GetCallback(const std::string& name) const {
83 return shortcuts_.at(name).callback;
84 }
85
86 const std::string GetKeys(const std::string& name) const {
87 return PrintShortcut(shortcuts_.at(name).keys);
88 }
89
90 const std::unordered_map<std::string, Shortcut>& GetShortcuts() const {
91 return shortcuts_;
92 }
93 bool UpdateShortcutKeys(const std::string& name,
94 const std::vector<ImGuiKey>& keys);
95 std::vector<Shortcut> GetShortcutsByScope(Shortcut::Scope scope) const {
96 std::vector<Shortcut> result;
97 result.reserve(shortcuts_.size());
98 for (const auto& [_, sc] : shortcuts_) {
99 if (sc.scope == scope) result.push_back(sc);
100 }
101 return result;
102 }
103
104 // Convenience methods for registering common shortcuts
105 void RegisterStandardShortcuts(std::function<void()> save_callback,
106 std::function<void()> open_callback,
107 std::function<void()> close_callback,
108 std::function<void()> find_callback,
109 std::function<void()> settings_callback);
110
111 void RegisterWindowNavigationShortcuts(std::function<void()> focus_left,
112 std::function<void()> focus_right,
113 std::function<void()> focus_up,
114 std::function<void()> focus_down,
115 std::function<void()> close_window,
116 std::function<void()> split_horizontal,
117 std::function<void()> split_vertical);
118
119 private:
120 std::unordered_map<std::string, Shortcut> shortcuts_;
121};
122
123void ExecuteShortcuts(const ShortcutManager& shortcut_manager);
124
125} // namespace editor
126} // namespace yaze
127
128#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