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 // 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 auto GetShortcuts() const { return shortcuts_; }
86 bool UpdateShortcutKeys(const std::string& name,
87 const std::vector<ImGuiKey>& keys);
88 std::vector<Shortcut> GetShortcutsByScope(Shortcut::Scope scope) const {
89 std::vector<Shortcut> result;
90 result.reserve(shortcuts_.size());
91 for (const auto& [_, sc] : shortcuts_) {
92 if (sc.scope == scope) result.push_back(sc);
93 }
94 return result;
95 }
96
97 // Convenience methods for registering common shortcuts
98 void RegisterStandardShortcuts(std::function<void()> save_callback,
99 std::function<void()> open_callback,
100 std::function<void()> close_callback,
101 std::function<void()> find_callback,
102 std::function<void()> settings_callback);
103
104 void RegisterWindowNavigationShortcuts(std::function<void()> focus_left,
105 std::function<void()> focus_right,
106 std::function<void()> focus_up,
107 std::function<void()> focus_down,
108 std::function<void()> close_window,
109 std::function<void()> split_horizontal,
110 std::function<void()> split_vertical);
111
112 private:
113 std::unordered_map<std::string, Shortcut> shortcuts_;
114};
115
116void ExecuteShortcuts(const ShortcutManager& shortcut_manager);
117
118} // namespace editor
119} // namespace yaze
120
121#endif // YAZE_APP_EDITOR_SYSTEM_SHORTCUT_MANAGER_H
const Shortcut & GetShortcut(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_
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