Refactor CommandManager to Simplify Command Structure
- Updated CommandManager to replace the recursive CommandInfoOrPrefix structure with a simplified CommandGroup structure, enhancing clarity and usability. - Modified ShowWhichKey, SaveKeybindings, and LoadKeybindings methods to accommodate the new command structure, ensuring consistent handling of main commands and their associated subcommands. - Improved code readability and maintainability by streamlining command registration and execution processes.
This commit is contained in:
@@ -39,13 +39,13 @@ void CommandManager::ShowWhichKey() {
|
|||||||
|
|
||||||
if (ImGui::BeginTable("CommandsTable", commands_.size(),
|
if (ImGui::BeginTable("CommandsTable", commands_.size(),
|
||||||
ImGuiTableFlags_SizingStretchProp)) {
|
ImGuiTableFlags_SizingStretchProp)) {
|
||||||
for (const auto &[shortcut, info] : commands_) {
|
for (const auto &[shortcut, group] : commands_) {
|
||||||
ImGui::TableNextColumn();
|
ImGui::TableNextColumn();
|
||||||
ImGui::TextColored(colors[colorIndex], "%c: %s",
|
ImGui::TextColored(colors[colorIndex], "%c: %s",
|
||||||
info.command_info.mnemonic,
|
group.main_command.mnemonic,
|
||||||
info.command_info.name.c_str());
|
group.main_command.name.c_str());
|
||||||
colorIndex = (colorIndex + 1) % numColors;
|
colorIndex = (colorIndex + 1) % numColors;
|
||||||
}
|
}
|
||||||
ImGui::EndTable();
|
ImGui::EndTable();
|
||||||
}
|
}
|
||||||
ImGui::EndPopup();
|
ImGui::EndPopup();
|
||||||
@@ -55,9 +55,9 @@ void CommandManager::ShowWhichKey() {
|
|||||||
void CommandManager::SaveKeybindings(const std::string &filepath) {
|
void CommandManager::SaveKeybindings(const std::string &filepath) {
|
||||||
std::ofstream out(filepath);
|
std::ofstream out(filepath);
|
||||||
if (out.is_open()) {
|
if (out.is_open()) {
|
||||||
for (const auto &[shortcut, info] : commands_) {
|
for (const auto &[shortcut, group] : commands_) {
|
||||||
out << shortcut << " " << info.command_info.mnemonic << " "
|
out << shortcut << " " << group.main_command.mnemonic << " "
|
||||||
<< info.command_info.name << " " << info.command_info.desc << "\n";
|
<< group.main_command.name << " " << group.main_command.desc << "\n";
|
||||||
}
|
}
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
@@ -70,7 +70,7 @@ void CommandManager::LoadKeybindings(const std::string &filepath) {
|
|||||||
std::string shortcut, name, desc;
|
std::string shortcut, name, desc;
|
||||||
char mnemonic;
|
char mnemonic;
|
||||||
while (in >> shortcut >> mnemonic >> name >> desc) {
|
while (in >> shortcut >> mnemonic >> name >> desc) {
|
||||||
commands_[shortcut].command_info = {nullptr, mnemonic, name, desc};
|
commands_[shortcut].main_command = {nullptr, mnemonic, name, desc};
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define YAZE_APP_EDITOR_SYSTEM_COMMAND_MANAGER_H
|
#define YAZE_APP_EDITOR_SYSTEM_COMMAND_MANAGER_H
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
@@ -31,38 +32,36 @@ class CommandManager {
|
|||||||
CommandInfo() = default;
|
CommandInfo() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
// New command info which supports subsections of commands
|
// Simplified command structure without recursive types
|
||||||
struct CommandInfoOrPrefix {
|
struct CommandGroup {
|
||||||
CommandInfo command_info;
|
CommandInfo main_command;
|
||||||
std::unordered_map<std::string, CommandInfoOrPrefix> subcommands;
|
std::unordered_map<std::string, CommandInfo> subcommands;
|
||||||
CommandInfoOrPrefix(CommandInfo command_info)
|
|
||||||
: command_info(std::move(command_info)) {}
|
CommandGroup() = default;
|
||||||
CommandInfoOrPrefix() = default;
|
CommandGroup(CommandInfo main) : main_command(std::move(main)) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
void RegisterPrefix(const std::string &group_name, const char prefix,
|
void RegisterPrefix(const std::string &group_name, const char prefix,
|
||||||
const std::string &name, const std::string &desc) {
|
const std::string &name, const std::string &desc) {
|
||||||
commands_[group_name].command_info = {nullptr, prefix, name, desc};
|
commands_[group_name].main_command = {nullptr, prefix, name, desc};
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegisterSubcommand(const std::string &group_name,
|
void RegisterSubcommand(const std::string &group_name,
|
||||||
const std::string &shortcut, const char mnemonic,
|
const std::string &shortcut, const char mnemonic,
|
||||||
const std::string &name, const std::string &desc,
|
const std::string &name, const std::string &desc,
|
||||||
Command command) {
|
Command command) {
|
||||||
commands_[group_name].subcommands[shortcut].command_info = {
|
commands_[group_name].subcommands[shortcut] = {command, mnemonic, name, desc};
|
||||||
command, mnemonic, name, desc};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegisterCommand(const std::string &shortcut, Command command,
|
void RegisterCommand(const std::string &shortcut, Command command,
|
||||||
char mnemonic, const std::string &name,
|
char mnemonic, const std::string &name,
|
||||||
const std::string &desc) {
|
const std::string &desc) {
|
||||||
commands_[shortcut].command_info = {std::move(command), mnemonic, name,
|
commands_[shortcut].main_command = {std::move(command), mnemonic, name, desc};
|
||||||
desc};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExecuteCommand(const std::string &shortcut) {
|
void ExecuteCommand(const std::string &shortcut) {
|
||||||
if (commands_.find(shortcut) != commands_.end()) {
|
if (commands_.find(shortcut) != commands_.end()) {
|
||||||
commands_[shortcut].command_info.command();
|
commands_[shortcut].main_command.command();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +71,7 @@ class CommandManager {
|
|||||||
void LoadKeybindings(const std::string &filepath);
|
void LoadKeybindings(const std::string &filepath);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<std::string, CommandInfoOrPrefix> commands_;
|
std::unordered_map<std::string, CommandGroup> commands_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace editor
|
} // namespace editor
|
||||||
|
|||||||
Reference in New Issue
Block a user