yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
keyboard_shortcuts.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Keyboard shortcut overlay system for yaze
3// Provides a centralized UI for discovering and managing keyboard shortcuts
4
5#ifndef YAZE_APP_GUI_KEYBOARD_SHORTCUTS_H
6#define YAZE_APP_GUI_KEYBOARD_SHORTCUTS_H
7
8#include <functional>
9#include <map>
10#include <string>
11#include <vector>
12
13#ifndef IMGUI_DEFINE_MATH_OPERATORS
14#define IMGUI_DEFINE_MATH_OPERATORS
15#endif
16
17#include "imgui/imgui.h"
18
19namespace yaze {
20namespace gui {
21
26enum class ShortcutContext {
27 kGlobal, // Active everywhere
28 kOverworld, // Only active in Overworld Editor
29 kDungeon, // Only active in Dungeon Editor
30 kGraphics, // Only active in Graphics Editor
31 kPalette, // Only active in Palette Editor
32 kSprite, // Only active in Sprite Editor
33 kMusic, // Only active in Music Editor
34 kMessage, // Only active in Message Editor
35 kEmulator, // Only active in Emulator
36 kCode, // Only active in Code/Assembly Editor
37};
38
43struct Shortcut {
44 std::string id; // Unique identifier
45 std::string description; // Human-readable description
46 ImGuiKey key; // Primary key
47 bool requires_ctrl = false;
48 bool requires_shift = false;
49 bool requires_alt = false;
50 std::string category; // "File", "Edit", "View", "Navigation", etc.
51 ShortcutContext context; // When this shortcut is active
52 std::function<void()> action; // Action to execute
53 bool enabled = true; // Whether the shortcut is currently enabled
54
55 // Display the shortcut as a string (e.g., "Ctrl+S")
56 std::string GetDisplayString() const;
57
58 // Check if this shortcut matches the current key state
59 bool Matches(ImGuiKey pressed_key, bool ctrl, bool shift, bool alt) const;
60};
61
86 public:
87 // Singleton access
88 static KeyboardShortcuts& Get();
89
90 // Register a new shortcut
91 void RegisterShortcut(const Shortcut& shortcut);
92
93 // Register a shortcut using parameters (convenience method)
94 void RegisterShortcut(const std::string& id,
95 const std::string& description,
96 ImGuiKey key,
97 bool ctrl, bool shift, bool alt,
98 const std::string& category,
99 ShortcutContext context,
100 std::function<void()> action);
101
102 // Unregister a shortcut by ID
103 void UnregisterShortcut(const std::string& id);
104
105 // Enable/disable a shortcut
106 void SetShortcutEnabled(const std::string& id, bool enabled);
107
108 // Process keyboard input and execute matching shortcuts
109 void ProcessInput();
110
111 // Draw the overlay UI (call every frame, handles visibility internally)
112 void DrawOverlay();
113
114 // Toggle overlay visibility
115 void ToggleOverlay();
116
117 // Show/hide overlay
118 void ShowOverlay() { show_overlay_ = true; }
119 void HideOverlay() { show_overlay_ = false; }
120 bool IsOverlayVisible() const { return show_overlay_; }
121
122 // Set the current editor context (called by editors when they become active)
125
126 // Get all shortcuts in a category
127 std::vector<const Shortcut*> GetShortcutsInCategory(const std::string& category) const;
128
129 // Get all shortcuts for the current context
130 std::vector<const Shortcut*> GetContextShortcuts() const;
131
132 // Get all registered categories
133 std::vector<std::string> GetCategories() const;
134
135 // Register default application shortcuts
137 std::function<void()> open_callback,
138 std::function<void()> save_callback,
139 std::function<void()> save_as_callback,
140 std::function<void()> close_callback,
141 std::function<void()> undo_callback,
142 std::function<void()> redo_callback,
143 std::function<void()> copy_callback,
144 std::function<void()> paste_callback,
145 std::function<void()> cut_callback,
146 std::function<void()> find_callback);
147
148 // Check if any text input is active (to avoid triggering shortcuts while typing)
149 static bool IsTextInputActive();
150
151 private:
152 KeyboardShortcuts() = default;
156
157 // Draw the overlay content
158 void DrawOverlayContent();
159
160 // Draw a single shortcut row
161 void DrawShortcutRow(const Shortcut& shortcut);
162
163 // Draw category section
164 void DrawCategorySection(const std::string& category,
165 const std::vector<const Shortcut*>& shortcuts);
166
167 // Check if a shortcut should be active in the current context
168 bool IsShortcutActiveInContext(const Shortcut& shortcut) const;
169
170 // Registered shortcuts (id -> shortcut)
171 std::map<std::string, Shortcut> shortcuts_;
172
173 // Category order for display
174 std::vector<std::string> category_order_ = {
175 "File", "Edit", "View", "Navigation", "Tools", "Editor", "Other"
176 };
177
178 // UI State
179 bool show_overlay_ = false;
180 char search_filter_[256] = {};
183
184 // Prevent repeated triggers while key is held
186};
187
191const char* ShortcutContextToString(ShortcutContext context);
192
196ShortcutContext EditorNameToContext(const std::string& editor_name);
197
198} // namespace gui
199} // namespace yaze
200
201#endif // YAZE_APP_GUI_KEYBOARD_SHORTCUTS_H
Manages keyboard shortcuts and provides an overlay UI.
KeyboardShortcuts(const KeyboardShortcuts &)=delete
void SetShortcutEnabled(const std::string &id, bool enabled)
void UnregisterShortcut(const std::string &id)
ShortcutContext GetCurrentContext() const
void SetCurrentContext(ShortcutContext context)
std::vector< std::string > category_order_
std::vector< std::string > GetCategories() const
std::map< std::string, Shortcut > shortcuts_
void DrawShortcutRow(const Shortcut &shortcut)
std::vector< const Shortcut * > GetContextShortcuts() const
KeyboardShortcuts & operator=(const KeyboardShortcuts &)=delete
std::vector< const Shortcut * > GetShortcutsInCategory(const std::string &category) const
static KeyboardShortcuts & Get()
void DrawCategorySection(const std::string &category, const std::vector< const Shortcut * > &shortcuts)
void RegisterShortcut(const Shortcut &shortcut)
void RegisterDefaultShortcuts(std::function< void()> open_callback, std::function< void()> save_callback, std::function< void()> save_as_callback, std::function< void()> close_callback, std::function< void()> undo_callback, std::function< void()> redo_callback, std::function< void()> copy_callback, std::function< void()> paste_callback, std::function< void()> cut_callback, std::function< void()> find_callback)
bool IsShortcutActiveInContext(const Shortcut &shortcut) const
const char * ShortcutContextToString(ShortcutContext context)
Convert ShortcutContext to display string.
ShortcutContext EditorNameToContext(const std::string &editor_name)
Convert editor type name to ShortcutContext.
ShortcutContext
Defines the context in which a shortcut is active.
Represents a keyboard shortcut with its associated action.
ShortcutContext context
std::string GetDisplayString() const
std::function< void()> action
bool Matches(ImGuiKey pressed_key, bool ctrl, bool shift, bool alt) const