yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
shortcut_manager.cc
Go to the documentation of this file.
1#include "shortcut_manager.h"
2
3#include <algorithm>
4#include <string>
5
6#include "app/gui/input.h"
7#include "imgui/imgui.h"
8
9namespace yaze {
10namespace editor {
11
12namespace {
13constexpr std::pair<ImGuiKey, const char*> kKeyNames[] = {
14 {ImGuiKey_Tab, "Tab"},
15 {ImGuiKey_LeftArrow, "Left"},
16 {ImGuiKey_RightArrow, "Right"},
17 {ImGuiKey_UpArrow, "Up"},
18 {ImGuiKey_DownArrow, "Down"},
19 {ImGuiKey_PageUp, "PageUp"},
20 {ImGuiKey_PageDown, "PageDown"},
21 {ImGuiKey_Home, "Home"},
22 {ImGuiKey_End, "End"},
23 {ImGuiKey_Insert, "Insert"},
24 {ImGuiKey_Delete, "Delete"},
25 {ImGuiKey_Backspace, "Backspace"},
26 {ImGuiKey_Space, "Space"},
27 {ImGuiKey_Enter, "Enter"},
28 {ImGuiKey_Escape, "Escape"},
29 {ImGuiMod_Ctrl, "Ctrl"},
30 {ImGuiMod_Shift, "Shift"},
31 {ImGuiMod_Alt, "Alt"},
32 {ImGuiMod_Super, "Super"},
33 {ImGuiKey_A, "A"},
34 {ImGuiKey_B, "B"},
35 {ImGuiKey_C, "C"},
36 {ImGuiKey_D, "D"},
37 {ImGuiKey_E, "E"},
38 {ImGuiKey_F, "F"},
39 {ImGuiKey_G, "G"},
40 {ImGuiKey_H, "H"},
41 {ImGuiKey_I, "I"},
42 {ImGuiKey_J, "J"},
43 {ImGuiKey_K, "K"},
44 {ImGuiKey_L, "L"},
45 {ImGuiKey_M, "M"},
46 {ImGuiKey_N, "N"},
47 {ImGuiKey_O, "O"},
48 {ImGuiKey_P, "P"},
49 {ImGuiKey_Q, "Q"},
50 {ImGuiKey_R, "R"},
51 {ImGuiKey_S, "S"},
52 {ImGuiKey_T, "T"},
53 {ImGuiKey_U, "U"},
54 {ImGuiKey_V, "V"},
55 {ImGuiKey_W, "W"},
56 {ImGuiKey_X, "X"},
57 {ImGuiKey_Y, "Y"},
58 {ImGuiKey_Z, "Z"},
59 {ImGuiKey_F1, "F1"},
60 {ImGuiKey_F2, "F2"},
61 {ImGuiKey_F3, "F3"},
62 {ImGuiKey_F4, "F4"},
63 {ImGuiKey_F5, "F5"},
64 {ImGuiKey_F6, "F6"},
65 {ImGuiKey_F7, "F7"},
66 {ImGuiKey_F8, "F8"},
67 {ImGuiKey_F9, "F9"},
68 {ImGuiKey_F10, "F10"},
69 {ImGuiKey_F11, "F11"},
70 {ImGuiKey_F12, "F12"},
71 {ImGuiKey_F13, "F13"},
72 {ImGuiKey_F14, "F14"},
73 {ImGuiKey_F15, "F15"},
74};
75
76constexpr const char* GetKeyName(ImGuiKey key) {
77 for (const auto& pair : kKeyNames) {
78 if (pair.first == key) {
79 return pair.second;
80 }
81 }
82 return "";
83}
84} // namespace
85
86std::string PrintShortcut(const std::vector<ImGuiKey>& keys) {
87 std::string shortcut;
88 for (size_t i = keys.size(); i > 0; --i) {
89 shortcut += GetKeyName(keys[i - 1]);
90 if (i > 1) {
91 shortcut += "+";
92 }
93 }
94 return shortcut;
95}
96
97constexpr std::string kCtrlKey = "Ctrl";
98constexpr std::string kAltKey = "Alt";
99constexpr std::string kShiftKey = "Shift";
100constexpr std::string kSuperKey = "Super";
101
102std::vector<ImGuiKey> ParseShortcut(const std::string& shortcut) {
103 std::vector<ImGuiKey> shortcuts;
104 // Search for special keys and the + symbol to combine with the
105 // MapKeyToImGuiKey function
106 size_t start = 0;
107 size_t end = shortcut.find(kCtrlKey);
108 if (end != std::string::npos) {
109 shortcuts.push_back(ImGuiMod_Ctrl);
110 start = end + kCtrlKey.size();
111 }
112
113 end = shortcut.find(kAltKey, start);
114 if (end != std::string::npos) {
115 shortcuts.push_back(ImGuiMod_Alt);
116 start = end + kAltKey.size();
117 }
118
119 end = shortcut.find(kShiftKey, start);
120 if (end != std::string::npos) {
121 shortcuts.push_back(ImGuiMod_Shift);
122 start = end + kShiftKey.size();
123 }
124
125 end = shortcut.find(kSuperKey, start);
126 if (end != std::string::npos) {
127 shortcuts.push_back(ImGuiMod_Super);
128 start = end + kSuperKey.size();
129 }
130
131 // Parse the rest of the keys
132 while (start < shortcut.size()) {
133 shortcuts.push_back(gui::MapKeyToImGuiKey(shortcut[start]));
134 start++;
135 }
136
137 return shortcuts;
138}
139
140void ExecuteShortcuts(const ShortcutManager& shortcut_manager) {
141 // Check for keyboard shortcuts using the shortcut manager
142 for (const auto& shortcut : shortcut_manager.GetShortcuts()) {
143 bool keys_pressed = true;
144 // Check for all the keys in the shortcut
145 for (const auto& key : shortcut.second.keys) {
146 if (key == ImGuiMod_Ctrl) {
147 keys_pressed &= ImGui::GetIO().KeyCtrl;
148 } else if (key == ImGuiMod_Alt) {
149 keys_pressed &= ImGui::GetIO().KeyAlt;
150 } else if (key == ImGuiMod_Shift) {
151 keys_pressed &= ImGui::GetIO().KeyShift;
152 } else if (key == ImGuiMod_Super) {
153 keys_pressed &= ImGui::GetIO().KeySuper;
154 } else {
155 keys_pressed &= ImGui::IsKeyDown(key);
156 }
157 if (!keys_pressed) {
158 break;
159 }
160 }
161 if (keys_pressed) {
162 shortcut.second.callback();
163 }
164 }
165}
166
167} // namespace editor
168} // namespace yaze
constexpr std::pair< ImGuiKey, const char * > kKeyNames[]
Editors are the view controllers for the application.
constexpr std::string kCtrlKey
constexpr std::string kShiftKey
std::vector< ImGuiKey > ParseShortcut(const std::string &shortcut)
std::string PrintShortcut(const std::vector< ImGuiKey > &keys)
constexpr std::string kSuperKey
void ExecuteShortcuts(const ShortcutManager &shortcut_manager)
constexpr std::string kAltKey
ImGuiKey MapKeyToImGuiKey(char key)
Definition input.cc:274
Main namespace for the application.
Definition controller.cc:18