Refactor CommandManager::ShowWhichKey to initialize defaults if commands are empty
This commit is contained in:
@@ -9,19 +9,76 @@ namespace editor {
|
||||
// When the player presses Space, a popup will appear fixed to the bottom of the
|
||||
// ImGui window with a list of the available key commands which can be used.
|
||||
void CommandManager::ShowWhichKey() {
|
||||
if (commands_.empty()) {
|
||||
InitializeDefaults();
|
||||
}
|
||||
|
||||
if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Space))) {
|
||||
ImGui::OpenPopup("WhichKey");
|
||||
}
|
||||
|
||||
ImGui::SetNextWindowPos(ImVec2(0, ImGui::GetIO().DisplaySize.y - 100),
|
||||
ImGuiCond_Always);
|
||||
ImGui::SetNextWindowSize(ImVec2(ImGui::GetIO().DisplaySize.x, 100),
|
||||
ImGuiCond_Always);
|
||||
if (ImGui::BeginPopup("WhichKey")) {
|
||||
for (const auto& [shortcut, command] : commands_) {
|
||||
ImGui::Text("%s: %s", shortcut.c_str(),
|
||||
command->GetDescription().c_str());
|
||||
// ESC to close the popup
|
||||
if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape))) {
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
|
||||
const ImVec4 colors[] = {
|
||||
ImVec4(0.8f, 0.2f, 0.2f, 1.0f), // Soft Red
|
||||
ImVec4(0.2f, 0.8f, 0.2f, 1.0f), // Soft Green
|
||||
ImVec4(0.2f, 0.2f, 0.8f, 1.0f), // Soft Blue
|
||||
ImVec4(0.8f, 0.8f, 0.2f, 1.0f), // Soft Yellow
|
||||
ImVec4(0.8f, 0.2f, 0.8f, 1.0f), // Soft Magenta
|
||||
ImVec4(0.2f, 0.8f, 0.8f, 1.0f) // Soft Cyan
|
||||
};
|
||||
const int numColors = sizeof(colors) / sizeof(colors[0]);
|
||||
int colorIndex = 0;
|
||||
|
||||
if (ImGui::BeginTable("CommandsTable", commands_.size(),
|
||||
ImGuiTableFlags_SizingStretchProp)) {
|
||||
for (const auto& [shortcut, info] : commands_) {
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::TextColored(colors[colorIndex], "%c: %s", info.mnemonic,
|
||||
info.name.c_str());
|
||||
colorIndex = (colorIndex + 1) % numColors;
|
||||
}
|
||||
ImGui::EndTable();
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
}
|
||||
|
||||
void CommandManager::InitializeDefaults() {
|
||||
commands_ = {
|
||||
{"O",
|
||||
{[]() { /* Open ROM logic */ }, 'O', "Open ROM",
|
||||
"Open a ROM file for editing"}},
|
||||
{"S",
|
||||
{[]() { /* Save ROM logic */ }, 'S', "Save ROM",
|
||||
"Save the current ROM"}},
|
||||
{"I",
|
||||
{[]() { /* Import Data logic */ }, 'I', "Import Data",
|
||||
"Import data into the ROM"}},
|
||||
{"E",
|
||||
{[]() { /* Export Data logic */ }, 'E', "Export Data",
|
||||
"Export data from the ROM"}},
|
||||
{"P",
|
||||
{[]() { /* Patch ROM logic */ }, 'P', "Patch ROM",
|
||||
"Apply a patch to the ROM"}},
|
||||
{"U", {[]() { /* Undo logic */ }, 'U', "Undo", "Undo the last action"}},
|
||||
{"R",
|
||||
{[]() { /* Redo logic */ }, 'R', "Redo", "Redo the last undone action"}},
|
||||
{"F", {[]() { /* Find logic */ }, 'F', "Find", "Find data in the ROM"}},
|
||||
{"G",
|
||||
{[]() { /* Goto logic */ }, 'G', "Goto",
|
||||
"Go to a specific address in the ROM"}},
|
||||
{"H", {[]() { /* Help logic */ }, 'H', "Help", "Show help information"}}};
|
||||
}
|
||||
|
||||
} // namespace editor
|
||||
} // namespace app
|
||||
} // namespace yaze
|
||||
|
||||
@@ -9,47 +9,42 @@ namespace yaze {
|
||||
namespace app {
|
||||
namespace editor {
|
||||
|
||||
class Command {
|
||||
public:
|
||||
virtual ~Command() = default;
|
||||
virtual void Execute() = 0;
|
||||
virtual std::string GetDescription() const = 0;
|
||||
};
|
||||
|
||||
class CommandManager {
|
||||
public:
|
||||
void ShowWhichKey();
|
||||
using Command = std::function<void()>;
|
||||
|
||||
void RegisterCommand(const std::string& shortcut, Command* command) {
|
||||
commands_[shortcut] = command;
|
||||
struct CommandInfo {
|
||||
Command command;
|
||||
char mnemonic;
|
||||
std::string name;
|
||||
std::string desc;
|
||||
CommandInfo(Command command, char mnemonic, const std::string& name,
|
||||
const std::string& desc)
|
||||
: command(std::move(command)),
|
||||
mnemonic(mnemonic),
|
||||
name(name),
|
||||
desc(desc) {}
|
||||
CommandInfo() = default;
|
||||
};
|
||||
|
||||
void RegisterCommand(const std::string& shortcut, Command command,
|
||||
char mnemonic, const std::string& name,
|
||||
const std::string& desc) {
|
||||
commands_[shortcut] = {std::move(command), mnemonic, name, desc};
|
||||
}
|
||||
|
||||
void ExecuteCommand(const std::string& shortcut) {
|
||||
if (commands_.find(shortcut) != commands_.end()) {
|
||||
commands_[shortcut]->Execute();
|
||||
commands_[shortcut].command();
|
||||
}
|
||||
}
|
||||
|
||||
void Undo() {
|
||||
if (!undo_stack_.empty()) {
|
||||
undo_stack_.top()->Execute();
|
||||
redo_stack_.push(undo_stack_.top());
|
||||
undo_stack_.pop();
|
||||
}
|
||||
}
|
||||
void ShowWhichKey();
|
||||
|
||||
void Redo() {
|
||||
if (!redo_stack_.empty()) {
|
||||
redo_stack_.top()->Execute();
|
||||
undo_stack_.push(redo_stack_.top());
|
||||
redo_stack_.pop();
|
||||
}
|
||||
}
|
||||
void InitializeDefaults();
|
||||
|
||||
private:
|
||||
std::stack<Command*> undo_stack_;
|
||||
std::stack<Command*> redo_stack_;
|
||||
std::unordered_map<std::string, Command*> commands_;
|
||||
std::unordered_map<std::string, CommandInfo> commands_;
|
||||
};
|
||||
|
||||
} // namespace editor
|
||||
|
||||
Submodule src/lib/imgui updated: 8db126188d...f3d242a90d
Reference in New Issue
Block a user