yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
platform_keys.cc
Go to the documentation of this file.
2
3#include <string>
4
5#ifdef __EMSCRIPTEN__
6#include <emscripten.h>
7#endif
8
9namespace yaze {
10namespace gui {
11
12namespace {
13
14// Cache the platform detection result
17
18#ifdef __EMSCRIPTEN__
19// JavaScript function to detect macOS in browser
20EM_JS(bool, js_is_mac_platform, (), {
21 return navigator.platform.toUpperCase().indexOf('MAC') >= 0;
22});
23#endif
24
26 if (g_platform_initialized) return;
27
28#ifdef __EMSCRIPTEN__
30 js_is_mac_platform() ? Platform::kWebMac : Platform::kWebOther;
31#elif defined(__APPLE__)
33#elif defined(_WIN32)
35#else
37#endif
38
40}
41
42} // namespace
43
45 InitializePlatform();
46 return g_cached_platform;
47}
48
51 return p == Platform::kMacOS || p == Platform::kWebMac;
52}
53
54const char* GetCtrlDisplayName() {
55 return IsMacPlatform() ? "Cmd" : "Ctrl";
56}
57
58const char* GetAltDisplayName() { return IsMacPlatform() ? "Opt" : "Alt"; }
59
60const char* GetKeyName(ImGuiKey key) {
61 // Handle special keys
62 switch (key) {
63 case ImGuiKey_Space:
64 return "Space";
65 case ImGuiKey_Tab:
66 return "Tab";
67 case ImGuiKey_Enter:
68 return "Enter";
69 case ImGuiKey_Escape:
70 return "Esc";
71 case ImGuiKey_Backspace:
72 return "Backspace";
73 case ImGuiKey_Delete:
74 return "Delete";
75 case ImGuiKey_Insert:
76 return "Insert";
77 case ImGuiKey_Home:
78 return "Home";
79 case ImGuiKey_End:
80 return "End";
81 case ImGuiKey_PageUp:
82 return "PageUp";
83 case ImGuiKey_PageDown:
84 return "PageDown";
85 case ImGuiKey_LeftArrow:
86 return "Left";
87 case ImGuiKey_RightArrow:
88 return "Right";
89 case ImGuiKey_UpArrow:
90 return "Up";
91 case ImGuiKey_DownArrow:
92 return "Down";
93
94 // Function keys
95 case ImGuiKey_F1:
96 return "F1";
97 case ImGuiKey_F2:
98 return "F2";
99 case ImGuiKey_F3:
100 return "F3";
101 case ImGuiKey_F4:
102 return "F4";
103 case ImGuiKey_F5:
104 return "F5";
105 case ImGuiKey_F6:
106 return "F6";
107 case ImGuiKey_F7:
108 return "F7";
109 case ImGuiKey_F8:
110 return "F8";
111 case ImGuiKey_F9:
112 return "F9";
113 case ImGuiKey_F10:
114 return "F10";
115 case ImGuiKey_F11:
116 return "F11";
117 case ImGuiKey_F12:
118 return "F12";
119
120 // Letter keys
121 case ImGuiKey_A:
122 return "A";
123 case ImGuiKey_B:
124 return "B";
125 case ImGuiKey_C:
126 return "C";
127 case ImGuiKey_D:
128 return "D";
129 case ImGuiKey_E:
130 return "E";
131 case ImGuiKey_F:
132 return "F";
133 case ImGuiKey_G:
134 return "G";
135 case ImGuiKey_H:
136 return "H";
137 case ImGuiKey_I:
138 return "I";
139 case ImGuiKey_J:
140 return "J";
141 case ImGuiKey_K:
142 return "K";
143 case ImGuiKey_L:
144 return "L";
145 case ImGuiKey_M:
146 return "M";
147 case ImGuiKey_N:
148 return "N";
149 case ImGuiKey_O:
150 return "O";
151 case ImGuiKey_P:
152 return "P";
153 case ImGuiKey_Q:
154 return "Q";
155 case ImGuiKey_R:
156 return "R";
157 case ImGuiKey_S:
158 return "S";
159 case ImGuiKey_T:
160 return "T";
161 case ImGuiKey_U:
162 return "U";
163 case ImGuiKey_V:
164 return "V";
165 case ImGuiKey_W:
166 return "W";
167 case ImGuiKey_X:
168 return "X";
169 case ImGuiKey_Y:
170 return "Y";
171 case ImGuiKey_Z:
172 return "Z";
173
174 // Number keys
175 case ImGuiKey_0:
176 return "0";
177 case ImGuiKey_1:
178 return "1";
179 case ImGuiKey_2:
180 return "2";
181 case ImGuiKey_3:
182 return "3";
183 case ImGuiKey_4:
184 return "4";
185 case ImGuiKey_5:
186 return "5";
187 case ImGuiKey_6:
188 return "6";
189 case ImGuiKey_7:
190 return "7";
191 case ImGuiKey_8:
192 return "8";
193 case ImGuiKey_9:
194 return "9";
195
196 // Punctuation
197 case ImGuiKey_Comma:
198 return ",";
199 case ImGuiKey_Period:
200 return ".";
201 case ImGuiKey_Slash:
202 return "/";
203 case ImGuiKey_Semicolon:
204 return ";";
205 case ImGuiKey_Apostrophe:
206 return "'";
207 case ImGuiKey_LeftBracket:
208 return "[";
209 case ImGuiKey_RightBracket:
210 return "]";
211 case ImGuiKey_Backslash:
212 return "\\";
213 case ImGuiKey_Minus:
214 return "-";
215 case ImGuiKey_Equal:
216 return "=";
217 case ImGuiKey_GraveAccent:
218 return "`";
219
220 default:
221 return "?";
222 }
223}
224
225std::string FormatShortcut(const std::vector<ImGuiKey>& keys) {
226 if (keys.empty()) return "";
227
228 std::string result;
229 bool has_primary = false;
230 bool has_shift = false;
231 bool has_alt = false;
232 bool has_super = false;
233 ImGuiKey main_key = ImGuiKey_None;
234
235 // First pass: identify modifiers and main key
236 for (ImGuiKey key : keys) {
237 int key_value = static_cast<int>(key);
238 if (key_value & ImGuiMod_Mask_) {
239 if (key_value & ImGuiMod_Ctrl) has_primary = true;
240 if (key_value & ImGuiMod_Shift) has_shift = true;
241 if (key_value & ImGuiMod_Alt) has_alt = true;
242 if (key_value & ImGuiMod_Super) has_super = true;
243 continue;
244 }
245 if (key == ImGuiMod_Shortcut) {
246 has_primary = true;
247 continue;
248 }
249 if (key == ImGuiMod_Shift) {
250 has_shift = true;
251 continue;
252 }
253 if (key == ImGuiMod_Alt) {
254 has_alt = true;
255 continue;
256 }
257 if (key == ImGuiMod_Super) {
258 has_super = true;
259 continue;
260 }
261
262 main_key = key;
263 }
264
265 // Build display string with modifiers in consistent order
266 // On macOS: primary modifier displays as "Cmd"
267 // On other platforms: primary modifier displays as "Ctrl"
268 if (has_primary) {
269 result += GetCtrlDisplayName();
270 result += "+";
271 }
272 if (has_super) {
273 // Super key (Cmd on macOS, Win/Super elsewhere)
274 result += IsMacPlatform() ? "Cmd" : "Win";
275 result += "+";
276 }
277 if (has_alt) {
278 result += GetAltDisplayName();
279 result += "+";
280 }
281 if (has_shift) {
282 result += "Shift+";
283 }
284
285 // Add the main key
286 if (main_key != ImGuiKey_None) {
287 result += GetKeyName(main_key);
288 }
289
290 return result;
291}
292
293std::string FormatCtrlShortcut(ImGuiKey key) {
294 return FormatShortcut({ImGuiMod_Ctrl, key});
295}
296
297std::string FormatCtrlShiftShortcut(ImGuiKey key) {
298 return FormatShortcut({ImGuiMod_Ctrl, ImGuiMod_Shift, key});
299}
300
301} // namespace gui
302} // namespace yaze
const char * GetCtrlDisplayName()
Get the display name for the primary modifier key.
std::string FormatCtrlShiftShortcut(ImGuiKey key)
Convenience function for Ctrl+Shift+key shortcuts.
Platform
Runtime platform detection for display string customization.
const char * GetKeyName(ImGuiKey key)
Get the ImGui key name as a string.
Platform GetCurrentPlatform()
Get the current platform at runtime.
std::string FormatShortcut(const std::vector< ImGuiKey > &keys)
Format a list of ImGui keys into a human-readable shortcut string.
bool IsMacPlatform()
Check if running on macOS (native or web)
const char * GetAltDisplayName()
Get the display name for the secondary modifier key.
std::string FormatCtrlShortcut(ImGuiKey key)
Convenience function for Ctrl+key shortcuts.