yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
command_manager.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_SYSTEM_COMMAND_MANAGER_H
2#define YAZE_APP_EDITOR_SYSTEM_COMMAND_MANAGER_H
3
4#include <functional>
5#include <memory>
6#include <string>
7#include <unordered_map>
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
20 public:
21 CommandManager() = default;
22 ~CommandManager() = default;
23
24 using Command = std::function<void()>;
25
26 struct CommandInfo {
29 std::string name;
30 std::string desc;
31 CommandInfo(Command command, char mnemonic, const std::string &name,
32 const std::string &desc)
33 : command(std::move(command)),
35 name(name),
36 desc(desc) {}
37 CommandInfo() = default;
38 };
39
40 // Simplified command structure without recursive types
41 struct CommandGroup {
43 std::unordered_map<std::string, CommandInfo> subcommands;
44
45 CommandGroup() = default;
47 };
48
49 void RegisterPrefix(const std::string &group_name, const char prefix,
50 const std::string &name, const std::string &desc) {
51 commands_[group_name].main_command = {nullptr, prefix, name, desc};
52 }
53
54 void RegisterSubcommand(const std::string &group_name,
55 const std::string &shortcut, const char mnemonic,
56 const std::string &name, const std::string &desc,
57 Command command) {
58 commands_[group_name].subcommands[shortcut] = {command, mnemonic, name, desc};
59 }
60
61 void RegisterCommand(const std::string &shortcut, Command command,
62 char mnemonic, const std::string &name,
63 const std::string &desc) {
64 commands_[shortcut].main_command = {std::move(command), mnemonic, name, desc};
65 }
66
67 void ExecuteCommand(const std::string &shortcut) {
68 if (commands_.find(shortcut) != commands_.end()) {
69 commands_[shortcut].main_command.command();
70 }
71 }
72
73 void ShowWhichKey();
74
75 void SaveKeybindings(const std::string &filepath);
76 void LoadKeybindings(const std::string &filepath);
77
78 private:
79 std::unordered_map<std::string, CommandGroup> commands_;
80};
81
82} // namespace editor
83} // namespace yaze
84
85#endif // YAZE_APP_EDITOR_SYSTEM_COMMAND_MANAGER_H
void RegisterSubcommand(const std::string &group_name, const std::string &shortcut, const char mnemonic, const std::string &name, const std::string &desc, Command command)
void LoadKeybindings(const std::string &filepath)
std::function< void()> Command
void ExecuteCommand(const std::string &shortcut)
std::unordered_map< std::string, CommandGroup > commands_
void SaveKeybindings(const std::string &filepath)
void RegisterCommand(const std::string &shortcut, Command command, char mnemonic, const std::string &name, const std::string &desc)
void RegisterPrefix(const std::string &group_name, const char prefix, const std::string &name, const std::string &desc)
Main namespace for the application.
std::unordered_map< std::string, CommandInfo > subcommands
CommandInfo(Command command, char mnemonic, const std::string &name, const std::string &desc)