-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
298 lines (252 loc) · 9.39 KB
/
Menu.cpp
File metadata and controls
298 lines (252 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "Menu.h"
#include <SDL3_image/SDL_image.h>
#include "./imgui/imgui.h"
#include "./imgui/imgui_internal.h"
#include "imguifilediag/ImGuiFileDialog.h"
#ifndef VERSION
#define VERSION "Version 1.0.3"
#endif
#include <regex>
std::string _last_encounter_file = "";
bool _is_opening_file = false;
SDL_Texture* logo = nullptr;
void openEncounter(ToolState* state)
{
_is_opening_file = true;
IGFD::FileDialogConfig config;
config.path = ".";
ImGuiFileDialog::Instance()->OpenDialog("ChooseFileDlgKey", "Choose File",
".encounter", config);
}
void saveAsEncounter(ToolState* state)
{
_is_opening_file = false;
IGFD::FileDialogConfig config;
config.path = ".";
config.flags = ImGuiFileDialogFlags_ConfirmOverwrite;
ImGuiFileDialog::Instance()->OpenDialog("SaveFileDlgKey", "Save File",
".encounter", config);
}
bool edit_focused = false;
void saveEncounter(ToolState* state) {
if (edit_focused)
{
saveCreature(state);
return;
}
if (_last_encounter_file.empty()) {
saveAsEncounter(state);
} else {
save_encounter_to_file(state->current_encounter, _last_encounter_file);
}
}
void newEncounter(ToolState* state)
{
state->current_encounter = dnd::Encounter();
}
bool show_about = false;
void menuDraw(ToolState* state) {
if (logo == nullptr) {
logo = loadTextureFromFile(state->renderer, "assets/logo_encounter.png");
}
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize);
std::string title = "EncounterMaster";
if (state->encounter_battler)
title += " - Battler";
else if (state->encounter_planner)
title += " - Planner";
ImGui::Begin(title.c_str(), nullptr,
ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDocking);
ImGuiID dockspace_id = ImGui::GetID("encounter_master_dockspace");
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f));
static bool first_time = true;
if (first_time) {
first_time = false;
// Remove any previous layout
ImGui::DockBuilderRemoveNode(dockspace_id);
ImGui::DockBuilderAddNode(dockspace_id, ImGuiDockNodeFlags_DockSpace);
ImGui::DockBuilderSetNodeSize(dockspace_id, ImGui::GetIO().DisplaySize);
// Split dockspace into middle and right
ImGuiID dockMiddle, dockRight;
dockRight = ImGui::DockBuilderSplitNode(dockspace_id, ImGuiDir_Right, 0.33f,
nullptr, &dockMiddle);
// Dock windows
ImGui::DockBuilderDockWindow("Creature List", dockRight);
ImGui::DockBuilderDockWindow("Creature Edit", dockRight);
ImGui::DockBuilderDockWindow("Creature View", dockRight);
ImGui::DockBuilderDockWindow("Battle", dockMiddle);
ImGui::DockBuilderDockWindow("Encounter Edit", dockMiddle);
ImGui::DockBuilderFinish(dockspace_id);
}
if (ImGui::IsKeyChordPressed(ImGuiKey_N | ImGuiMod_Ctrl)) newEncounter(state);
if (ImGui::IsKeyChordPressed(ImGuiKey_O | ImGuiMod_Ctrl))
openEncounter(state);
if (ImGui::IsKeyChordPressed(ImGuiKey_S | ImGuiMod_Ctrl))
saveEncounter(state);
if (ImGui::IsKeyChordPressed(ImGuiKey_S | ImGuiMod_Ctrl | ImGuiMod_Shift))
saveAsEncounter(state);
if (ImGui::IsKeyChordPressed(ImGuiKey_R | ImGuiMod_Ctrl)) {
state->creatures = parse_creatures_from_directory("creatures");
state->filtered_creatures.clear();
state->creature_page = 0;
state->creature_selected_index = -1;
state->page.creatures.clear();
}
ImGui::BeginMenuBar();
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("New", "Ctrl+N")) {
newEncounter(state);
}
if (ImGui::MenuItem("Open...", "Ctrl+O")) {
openEncounter(state);
}
if (ImGui::MenuItem("Save", "Ctrl+S")) {
saveEncounter(state);
}
if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S")) {
saveAsEncounter(state);
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Creature"))
{
if (ImGui::MenuItem("Download Creatures")) {
state->is_downloading_creatures = true;
state->should_download_creatures = true;
downloadCreatures([state]() {
state->is_downloading_creatures = false;
state->should_download_creatures = false;
state->creatures = parse_creatures_from_directory("creatures");
if (state->creatures.empty()) {
state->should_download_creatures = true;
}
});
}
if (ImGui::MenuItem("Reload Creatures", "Ctrl+R")) {
state->creatures = parse_creatures_from_directory("creatures");
state->filtered_creatures.clear();
state->creature_page = 0;
state->creature_selected_index = -1;
state->page.creatures.clear();
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Help"))
{
if (ImGui::MenuItem("About")) {
show_about = true;
}
ImGui::EndMenu();
}
ImGui::EndMenuBar();
if (ImGuiFileDialog::Instance()->Display("ChooseFileDlgKey", 32 | ImGuiWindowFlags_NoDocking, ImVec2(320,320)) ||
ImGuiFileDialog::Instance()->Display("SaveFileDlgKey",
32 | ImGuiWindowFlags_NoDocking,
ImVec2(320, 320))) {
if (ImGuiFileDialog::Instance()->IsOk()) { // action if OK
std::string filePathName = ImGuiFileDialog::Instance()->GetFilePathName();
std::string filePath = ImGuiFileDialog::Instance()->GetCurrentPath();
if (_is_opening_file) {
state->current_encounter = parse_encounter_from_file(state, filePathName);
} else {
save_encounter_to_file(state->current_encounter, filePathName);
}
_last_encounter_file = filePathName;
}
ImGuiFileDialog::Instance()->Close();
}
ImGui::End();
ImGui::SetNextWindowSizeConstraints(ImVec2(300, 500),
ImVec2(FLT_MAX, FLT_MAX));
ImGui::Begin("Creature List");
ImGui::Text("Number of creatures: %zu", state->creatures.size());
if (state->filtered_creatures.size() > 0) {
ImGui::Text("Number of filtered creatures: %zu",
state->filtered_creatures.size());
}
ImGui::Text("Filter:");
ImGui::SameLine();
if (ImGui::InputText("##Search", state->creature_filter, 256) ||
state->reload_creatures)
{
state->filtered_creatures.clear();
state->creature_page = 0;
state->creature_selected_index = -1;
// Filter creatures based on search
state->filtered_creatures.clear();
for (auto& creature : state->creatures) {
if (!creature.get_name().has_value()) continue;
// Regex search, case insensitive
std::regex filter_regex(state->creature_filter,
std::regex_constants::icase);
if (std::regex_search(
creature.get_name().value() + " (" + creature.original_list + ")",
filter_regex)) {
state->filtered_creatures.push_back(creature);
}
else if (creature.get_creature_type().has_value()) {
std::string type_str = creature.type_to_string();
if (std::regex_search(type_str, filter_regex)) {
state->filtered_creatures.push_back(creature);
}
}
}
if (state->filtered_creatures.size() == state->creatures.size()) {
state->filtered_creatures.clear();
}
state->page.creatures.clear();
}
displayCreatures(state);
ImGui::End();
if (state->reload_creatures) state->reload_creatures = false;
ImGui::SetNextWindowSizeConstraints(ImVec2(300, 500),
ImVec2(FLT_MAX, FLT_MAX));
if (state->encounter_planner)
ImGui::Begin("Creature Edit");
else
ImGui::Begin("Creature View");
edit_focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows);
displayCreatureEdit(state);
ImGui::End();
ImGui::SetNextWindowSizeConstraints(ImVec2(300, 500),
ImVec2(FLT_MAX, FLT_MAX));
if (state->encounter_planner) {
ImGui::Begin("Encounter Edit");
displayEncounterEdit(state);
ImGui::End();
}
if (show_about) {
ImGui::SetNextWindowSizeConstraints(ImVec2(500, 535), ImVec2(500, 535));
ImGui::Begin("About EncounterMaster", &show_about,
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoCollapse);
ImGui::Text("EncounterMaster");
if (logo) {
ImGui::SameLine(ImGui::GetWindowWidth() - 160);
ImGui::Image((ImTextureID)(intptr_t)logo, ImVec2(134, 184.8));
}
ImGui::Text(VERSION);
ImGui::Separator();
ImGui::TextWrapped(
"A tool for managing Dungeons & Dragons encounters, creating and "
"editing creatures, and saving them for later use.");
ImGui::Separator();
ImGui::TextWrapped(
"Creatures are downloaded from creatures_list.txt, which contains "
"mirrors for creature data. You are encouraged to make your own lists with the save feature - and to host them as well.");
ImGui::Separator();
ImGui::TextWrapped("Created by Kade");
ImGui::Separator();
// Links
ImGui::TextLinkOpenURL("Github",
"https://github.com/Kade-github/EncounterMaster");
ImGui::SameLine();
ImGui::TextLinkOpenURL("efortner (Default Creature List)",
"https://gist.github.com/efortner/");
ImGui::End();
}
}