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