Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
GameMenu.cpp
Go to the documentation of this file.
1#include "GameMenu.h"
2#include "../VideoGame.h"
3#include "../World.h"
4#include <iostream>
5#include <fstream>
6#include "system_utils.h"
7
8#ifdef OLYMPE_BLUEPRINT_EDITOR_ENABLED
9#include "../third_party/imgui/imgui.h"
10#include "../BlueprintEditor/blueprinteditor.h"
11#endif
12
13// C++14 compatible directory traversal (no std::filesystem)
14#include <cstring>
15#ifdef _WIN32
16#include <windows.h>
17#else
18#include <dirent.h>
19#include <sys/stat.h>
20#endif
21
22
24{
25 if (m_active) return;
26 m_active = true;
27
28 // Pause the game when the menu is activated
30
31 SYSTEM_LOG << "GameMenu: activated\n";
32}
33
35{
36 if (!m_active) return;
37 m_active = false;
38
39 // Resume the game when the menu is closed
41
42 SYSTEM_LOG << "GameMenu: deactivated\n";
43}
44
49
54
56{
57 switch (m_selected)
58 {
60 Deactivate(); // Resume game
61 break;
62
64 // Restart current level
65 // For now, just log - full implementation depends on level loading system
66 SYSTEM_LOG << "GameMenu: Restart selected (not yet implemented)\n";
67 Deactivate();
68 break;
69
71 // Quit to main menu or exit
73 break;
74 }
75}
76
78{
79 if (!m_active) return;
80 SYSTEM_LOG << "--- GameMenu ---\n";
81 for (size_t i = 0; i < m_entries.size(); ++i)
82 {
83 if (static_cast<int>(i) == m_selected) SYSTEM_LOG << "> "; else SYSTEM_LOG << " ";
84 SYSTEM_LOG << m_entries[i] << "\n";
85 }
86}
87
89{
90 // Check for F2 key press to toggle F2 menu
91 // This will be called from the game loop
92 // For now, this is a placeholder - actual key handling is done elsewhere
93}
94
99
101{
102 if (m_f2MenuOpen == open) return;
105 {
107 }
108}
109
111{
113 SYSTEM_LOG << "GameMenu: Runtime Blueprint Panel "
114 << (m_runtimeBlueprintOpen ? "opened" : "closed") << "\n";
115}
116
118{
119#ifdef OLYMPE_BLUEPRINT_EDITOR_ENABLED
120 if (!m_runtimeBlueprintOpen) return;
121
122 ImGui::SetNextWindowSize(ImVec2(480, 320), ImGuiCond_FirstUseEver);
123 if (ImGui::Begin("Blueprints (F2)", &m_runtimeBlueprintOpen))
124 {
125 ImGui::TextDisabled("Loaded blueprint assets");
126 ImGui::Separator();
127
128 // List all blueprint assets from the backend
130 const auto assets = editor.GetAllAssets();
131
132 if (assets.empty())
133 {
134 ImGui::TextColored(ImVec4(0.6f, 0.6f, 0.6f, 1.0f), "(no blueprints found in asset root)");
135 }
136 else
137 {
138 ImGui::BeginChild("BPList", ImVec2(0, -40), true);
139 for (const auto& asset : assets)
140 {
141 if (asset.isDirectory) continue;
142 ImGui::BulletText("[%s] %s", asset.type.c_str(), asset.name.c_str());
143 if (ImGui::IsItemHovered())
144 ImGui::SetTooltip("%s", asset.filepath.c_str());
145 }
146 ImGui::EndChild();
147 }
148
149 ImGui::Separator();
150 // Provide an explicit button to open the full node-graph editor
151 if (ImGui::Button("Open in Blueprint Editor", ImVec2(-1, 0)))
152 {
153 editor.SetActive(true);
155 SYSTEM_LOG << "GameMenu: opened full Blueprint Editor from runtime panel\n";
156 }
157 if (ImGui::IsItemHovered())
158 ImGui::SetTooltip("Opens the full node-graph Blueprint Editor");
159 }
160 ImGui::End();
161#endif
162}
163
164void GameMenu::ScanForTiledMaps(const std::string& directory)
165{
166 #ifdef _WIN32
167 // Windows implementation
168 std::string searchPath = directory + "\\*";
171
173 {
174 do
175 {
176 std::string fileName = findData.cFileName;
177 if (fileName == "." || fileName == "..") continue;
178
179 std::string fullPath = directory + "\\" + fileName;
180
181 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
182 {
183 // Recursively scan subdirectories
184 ScanForTiledMaps(fullPath);
185 }
186
187 else
188 {
189 // Check if file has .tmj extension
190 if (fileName.size() > 4 && (fileName.substr(fileName.size() - 4) == ".tmj" || fileName.substr(fileName.size() - 4) == ".tmx"))
191 {
192 m_tiledMapPaths.push_back(fullPath);
193 }
194 }
195 } while (FindNextFileA(hFind, &findData));
196
198 }
199 #else
200 // Unix/Linux implementation
201 DIR* dir = opendir(directory.c_str());
202 if (dir != nullptr)
203 {
204 struct dirent* entry;
205 while ((entry = readdir(dir)) != nullptr)
206 {
207 std::string fileName = entry->d_name;
208 if (fileName == "." || fileName == "..") continue;
209
210 std::string fullPath = directory + "/" + fileName;
211
212 struct stat statbuf;
213 if (stat(fullPath.c_str(), &statbuf) == 0)
214 {
215 if (S_ISDIR(statbuf.st_mode))
216 {
217 // Recursively scan subdirectories
218 ScanForTiledMaps(fullPath);
219 }
220 else if (S_ISREG(statbuf.st_mode))
221 {
222 // Check if file has .tmj extension
223 if (fileName.size() > 4 && fileName.substr(fileName.size() - 4) == ".tmj")
224 {
225 m_tiledMapPaths.push_back(fullPath);
226 }
227 }
228 }
229 }
230 closedir(dir);
231 }
232 #endif
233}
234
236{
237 m_tiledMapPaths.clear();
239
240 //ScanForTiledMaps("Blueprints");
241 //ScanForTiledMaps("Levels");
242 //ScanForTiledMaps("gamedata");
243 //ScanForTiledMaps("gamedata\\levels");
244 ScanForTiledMaps(".");
246
247 if (!m_tiledMapPaths.empty())
248 {
250 }
251
252 SYSTEM_LOG << "GameMenu: Found " << m_tiledMapPaths.size() << " Tiled maps\n";
253}
254
256{
257#ifdef OLYMPE_BLUEPRINT_EDITOR_ENABLED
258 if (!m_f2MenuOpen) return;
259
260 ImGui::SetNextWindowSize(ImVec2(600, 400), ImGuiCond_FirstUseEver);
261 if (ImGui::Begin("Load Tiled Level (F2)", &m_f2MenuOpen))
262 {
263 ImGui::Text("Select a Tiled map (.tmj) to load:");
264 ImGui::Separator();
265
266 // Scan button
267 if (ImGui::Button("Refresh List"))
268 {
270 }
271
272 ImGui::SameLine();
273 ImGui::Text("Found %d maps", static_cast<int>(m_tiledMapPaths.size()));
274
275 ImGui::Separator();
276
277 // Map list
278 ImGui::BeginChild("MapList", ImVec2(0, -30), true);
279
280 for (size_t i = 0; i < m_tiledMapPaths.size(); ++i)
281 {
282 const std::string& mapPath = m_tiledMapPaths[i];
283
284 // Extract just the filename for display
285 std::string displayName = mapPath;
286 size_t lastSlash = displayName.find_last_of("/\\");
287 if (lastSlash != std::string::npos)
288 {
289 displayName = displayName.substr(lastSlash + 1);
290 }
291
292 bool isSelected = (static_cast<int>(i) == m_selectedMapIndex);
293 if (ImGui::Selectable(displayName.c_str(), isSelected, ImGuiSelectableFlags_AllowDoubleClick))
294 {
295 m_selectedMapIndex = static_cast<int>(i);
296
297 // Double-click to load
298 if (ImGui::IsMouseDoubleClicked(0))
299 {
300 SYSTEM_LOG << "GameMenu: Loading map: " << mapPath << "\n";
302 m_f2MenuOpen = false;
303 }
304 }
305
306 // Show full path as tooltip
307 if (ImGui::IsItemHovered())
308 {
309 ImGui::SetTooltip("%s", mapPath.c_str());
310 }
311 }
312
313 ImGui::EndChild();
314
315 // Load button
316 ImGui::Separator();
317 if (ImGui::Button("Load Selected", ImVec2(120, 0)))
318 {
319 if (m_selectedMapIndex >= 0 && m_selectedMapIndex < static_cast<int>(m_tiledMapPaths.size()))
320 {
321 const std::string& mapPath = m_tiledMapPaths[m_selectedMapIndex];
322 SYSTEM_LOG << "GameMenu: Loading map: " << mapPath << "\n";
324 m_f2MenuOpen = false;
325 }
326 }
327
328 ImGui::SameLine();
329 if (ImGui::Button("Cancel", ImVec2(120, 0)))
330 {
331 m_f2MenuOpen = false;
332 }
333 }
334 ImGui::End();
335#endif
336}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
void Activate()
Definition GameMenu.cpp:23
virtual void Render()
Definition GameMenu.cpp:77
void SelectPrevious()
Definition GameMenu.cpp:45
void SetF2MenuOpen(bool open)
Definition GameMenu.cpp:100
void RefreshTiledMapList()
Definition GameMenu.cpp:235
bool m_runtimeBlueprintOpen
Definition GameMenu.h:82
void Update()
Definition GameMenu.cpp:88
bool m_hasScannedTiledMaps
Definition GameMenu.h:85
bool m_active
Definition GameMenu.h:74
std::vector< std::string > m_entries
Definition GameMenu.h:75
void ToggleF2Menu()
Definition GameMenu.cpp:95
void SelectNext()
Definition GameMenu.cpp:50
std::vector< std::string > m_tiledMapPaths
Definition GameMenu.h:83
void Deactivate()
Definition GameMenu.cpp:34
void RenderRuntimeBlueprintPanel()
Definition GameMenu.cpp:117
void ToggleRuntimeBlueprintPanel()
Definition GameMenu.cpp:110
void ValidateSelection()
Definition GameMenu.cpp:55
int m_selectedMapIndex
Definition GameMenu.h:84
int m_selected
Definition GameMenu.h:76
void ScanForTiledMaps(const std::string &directory)
Definition GameMenu.cpp:164
void RenderF2Menu()
Definition GameMenu.cpp:255
bool m_f2MenuOpen
Definition GameMenu.h:79
@ Restart
Definition GameMenu.h:17
static BlueprintEditor & Get()
static VideoGame & Get()
Definition VideoGame.h:34
void Pause()
Definition VideoGame.h:46
void RequestQuit()
Definition VideoGame.h:48
void Resume()
Definition VideoGame.h:47
bool LoadLevelFromTiled(const std::string &tiledMapPath)
Definition World.cpp:1006
static World & Get()
Get singleton instance (short form)
Definition World.h:232
#define SYSTEM_LOG