yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
command_manager.cc
Go to the documentation of this file.
1#include "command_manager.h"
2
3#include <fstream>
4
5#include "imgui/imgui.h"
6
7namespace yaze {
8namespace editor {
9
10ImGuiKey MapKeyToImGuiKey(char key) {
11 switch (key) {
12 case 'A':
13 return ImGuiKey_A;
14 case 'B':
15 return ImGuiKey_B;
16 case 'C':
17 return ImGuiKey_C;
18 case 'D':
19 return ImGuiKey_D;
20 case 'E':
21 return ImGuiKey_E;
22 case 'F':
23 return ImGuiKey_F;
24 case 'G':
25 return ImGuiKey_G;
26 case 'H':
27 return ImGuiKey_H;
28 case 'I':
29 return ImGuiKey_I;
30 case 'J':
31 return ImGuiKey_J;
32 case 'K':
33 return ImGuiKey_K;
34 case 'L':
35 return ImGuiKey_L;
36 case 'M':
37 return ImGuiKey_M;
38 case 'N':
39 return ImGuiKey_N;
40 case 'O':
41 return ImGuiKey_O;
42 case 'P':
43 return ImGuiKey_P;
44 case 'Q':
45 return ImGuiKey_Q;
46 case 'R':
47 return ImGuiKey_R;
48 case 'S':
49 return ImGuiKey_S;
50 case 'T':
51 return ImGuiKey_T;
52 case 'U':
53 return ImGuiKey_U;
54 case 'V':
55 return ImGuiKey_V;
56 case 'W':
57 return ImGuiKey_W;
58 case 'X':
59 return ImGuiKey_X;
60 case 'Y':
61 return ImGuiKey_Y;
62 case 'Z':
63 return ImGuiKey_Z;
64 case '/':
65 return ImGuiKey_Slash;
66 case '-':
67 return ImGuiKey_Minus;
68 default:
69 return ImGuiKey_COUNT;
70 }
71}
72
73// When the player presses Space, a popup will appear fixed to the bottom of the
74// ImGui window with a list of the available key commands which can be used.
76 if (ImGui::IsKeyPressed(ImGuiKey_Space)) {
77 ImGui::OpenPopup("WhichKey");
78 }
79
80 ImGui::SetNextWindowPos(ImVec2(0, ImGui::GetIO().DisplaySize.y - 100),
81 ImGuiCond_Always);
82 ImGui::SetNextWindowSize(ImVec2(ImGui::GetIO().DisplaySize.x, 100),
83 ImGuiCond_Always);
84 if (ImGui::BeginPopup("WhichKey")) {
85 // ESC to close the popup
86 if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
87 ImGui::CloseCurrentPopup();
88 }
89
90 const ImVec4 colors[] = {
91 ImVec4(0.8f, 0.2f, 0.2f, 1.0f), // Soft Red
92 ImVec4(0.2f, 0.8f, 0.2f, 1.0f), // Soft Green
93 ImVec4(0.2f, 0.2f, 0.8f, 1.0f), // Soft Blue
94 ImVec4(0.8f, 0.8f, 0.2f, 1.0f), // Soft Yellow
95 ImVec4(0.8f, 0.2f, 0.8f, 1.0f), // Soft Magenta
96 ImVec4(0.2f, 0.8f, 0.8f, 1.0f) // Soft Cyan
97 };
98 const int numColors = sizeof(colors) / sizeof(colors[0]);
99 int colorIndex = 0;
100
101 if (ImGui::BeginTable("CommandsTable", commands_.size(),
102 ImGuiTableFlags_SizingStretchProp)) {
103 for (const auto &[shortcut, info] : commands_) {
104 ImGui::TableNextColumn();
105 ImGui::TextColored(colors[colorIndex], "%c: %s",
106 info.command_info.mnemonic,
107 info.command_info.name.c_str());
108 colorIndex = (colorIndex + 1) % numColors;
109 }
110 ImGui::EndTable();
111 }
112 ImGui::EndPopup();
113 }
114}
115
116void CommandManager::SaveKeybindings(const std::string &filepath) {
117 std::ofstream out(filepath);
118 if (out.is_open()) {
119 for (const auto &[shortcut, info] : commands_) {
120 out << shortcut << " " << info.command_info.mnemonic << " "
121 << info.command_info.name << " " << info.command_info.desc << "\n";
122 }
123 out.close();
124 }
125}
126
127void CommandManager::LoadKeybindings(const std::string &filepath) {
128 std::ifstream in(filepath);
129 if (in.is_open()) {
130 commands_.clear();
131 std::string shortcut, name, desc;
132 char mnemonic;
133 while (in >> shortcut >> mnemonic >> name >> desc) {
134 commands_[shortcut].command_info = {nullptr, mnemonic, name, desc};
135 }
136 in.close();
137 }
138}
139
140} // namespace editor
141} // namespace yaze
std::unordered_map< std::string, CommandInfoOrPrefix > commands_
void LoadKeybindings(const std::string &filepath)
void SaveKeybindings(const std::string &filepath)
Editors are the view controllers for the application.
ImGuiKey MapKeyToImGuiKey(char key)
Main namespace for the application.
Definition controller.cc:18