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
|
// 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.
|
// ImGui window with a list of the available key commands which can be used.
|
||||||
void CommandManager::ShowWhichKey() {
|
void CommandManager::ShowWhichKey() {
|
||||||
|
if (commands_.empty()) {
|
||||||
|
InitializeDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Space))) {
|
if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Space))) {
|
||||||
ImGui::OpenPopup("WhichKey");
|
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")) {
|
if (ImGui::BeginPopup("WhichKey")) {
|
||||||
for (const auto& [shortcut, command] : commands_) {
|
// ESC to close the popup
|
||||||
ImGui::Text("%s: %s", shortcut.c_str(),
|
if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape))) {
|
||||||
command->GetDescription().c_str());
|
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();
|
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 editor
|
||||||
} // namespace app
|
} // namespace app
|
||||||
} // namespace yaze
|
} // namespace yaze
|
||||||
|
|||||||
@@ -9,47 +9,42 @@ namespace yaze {
|
|||||||
namespace app {
|
namespace app {
|
||||||
namespace editor {
|
namespace editor {
|
||||||
|
|
||||||
class Command {
|
|
||||||
public:
|
|
||||||
virtual ~Command() = default;
|
|
||||||
virtual void Execute() = 0;
|
|
||||||
virtual std::string GetDescription() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class CommandManager {
|
class CommandManager {
|
||||||
public:
|
public:
|
||||||
void ShowWhichKey();
|
using Command = std::function<void()>;
|
||||||
|
|
||||||
void RegisterCommand(const std::string& shortcut, Command* command) {
|
struct CommandInfo {
|
||||||
commands_[shortcut] = command;
|
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) {
|
void ExecuteCommand(const std::string& shortcut) {
|
||||||
if (commands_.find(shortcut) != commands_.end()) {
|
if (commands_.find(shortcut) != commands_.end()) {
|
||||||
commands_[shortcut]->Execute();
|
commands_[shortcut].command();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Undo() {
|
void ShowWhichKey();
|
||||||
if (!undo_stack_.empty()) {
|
|
||||||
undo_stack_.top()->Execute();
|
|
||||||
redo_stack_.push(undo_stack_.top());
|
|
||||||
undo_stack_.pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Redo() {
|
void InitializeDefaults();
|
||||||
if (!redo_stack_.empty()) {
|
|
||||||
redo_stack_.top()->Execute();
|
|
||||||
undo_stack_.push(redo_stack_.top());
|
|
||||||
redo_stack_.pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::stack<Command*> undo_stack_;
|
std::unordered_map<std::string, CommandInfo> commands_;
|
||||||
std::stack<Command*> redo_stack_;
|
|
||||||
std::unordered_map<std::string, Command*> commands_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace editor
|
} // namespace editor
|
||||||
|
|||||||
Submodule src/lib/imgui updated: 8db126188d...f3d242a90d
Reference in New Issue
Block a user