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#endif
11
12// C++14 compatible directory traversal (no std::filesystem)
13#include <cstring>
14#ifdef _WIN32
15#include <windows.h>
16#else
17#include <dirent.h>
18#include <sys/stat.h>
19#endif
20
21
23{
24 if (m_active) return;
25 m_active = true;
26
27 // Pause the game when the menu is activated
29
30 SYSTEM_LOG << "GameMenu: activated\n";
31}
32
34{
35 if (!m_active) return;
36 m_active = false;
37
38 // Resume the game when the menu is closed
40
41 SYSTEM_LOG << "GameMenu: deactivated\n";
42}
43
48
53
55{
56 switch (m_selected)
57 {
59 Deactivate(); // Resume game
60 break;
61
63 // Restart current level
64 // For now, just log - full implementation depends on level loading system
65 SYSTEM_LOG << "GameMenu: Restart selected (not yet implemented)\n";
66 Deactivate();
67 break;
68
70 // Quit to main menu or exit
72 break;
73 }
74}
75
77{
78 if (!m_active) return;
79 SYSTEM_LOG << "--- GameMenu ---\n";
80 for (size_t i = 0; i < m_entries.size(); ++i)
81 {
82 if (static_cast<int>(i) == m_selected) SYSTEM_LOG << "> "; else SYSTEM_LOG << " ";
83 SYSTEM_LOG << m_entries[i] << "\n";
84 }
85}
86
88{
89 // Check for F2 key press to toggle F2 menu
90 // This will be called from the game loop
91 // For now, this is a placeholder - actual key handling is done elsewhere
92}
93
98
100{
101 if (m_f2MenuOpen == open) return;
104 {
106 }
107}
108
109void GameMenu::ScanForTiledMaps(const std::string& directory)
110{
111 #ifdef _WIN32
112 // Windows implementation
113 std::string searchPath = directory + "\\*";
116
118 {
119 do
120 {
121 std::string fileName = findData.cFileName;
122 if (fileName == "." || fileName == "..") continue;
123
124 std::string fullPath = directory + "\\" + fileName;
125
126 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
127 {
128 // Recursively scan subdirectories
129 ScanForTiledMaps(fullPath);
130 }
131
132 else
133 {
134 // Check if file has .tmj extension
135 if (fileName.size() > 4 && fileName.substr(fileName.size() - 4) == ".tmj")
136 {
137 m_tiledMapPaths.push_back(fullPath);
138 }
139 }
140 } while (FindNextFileA(hFind, &findData));
141
143 }
144 #else
145 // Unix/Linux implementation
146 DIR* dir = opendir(directory.c_str());
147 if (dir != nullptr)
148 {
149 struct dirent* entry;
150 while ((entry = readdir(dir)) != nullptr)
151 {
152 std::string fileName = entry->d_name;
153 if (fileName == "." || fileName == "..") continue;
154
155 std::string fullPath = directory + "/" + fileName;
156
157 struct stat statbuf;
158 if (stat(fullPath.c_str(), &statbuf) == 0)
159 {
160 if (S_ISDIR(statbuf.st_mode))
161 {
162 // Recursively scan subdirectories
163 ScanForTiledMaps(fullPath);
164 }
165 else if (S_ISREG(statbuf.st_mode))
166 {
167 // Check if file has .tmj extension
168 if (fileName.size() > 4 && fileName.substr(fileName.size() - 4) == ".tmj")
169 {
170 m_tiledMapPaths.push_back(fullPath);
171 }
172 }
173 }
174 }
175 closedir(dir);
176 }
177 #endif
178}
179
181{
182 m_tiledMapPaths.clear();
184
185 //ScanForTiledMaps("Blueprints");
186 //ScanForTiledMaps("Levels");
187 //ScanForTiledMaps("gamedata");
188 //ScanForTiledMaps("gamedata\\levels");
189 ScanForTiledMaps(".");
191
192 if (!m_tiledMapPaths.empty())
193 {
195 }
196
197 SYSTEM_LOG << "GameMenu: Found " << m_tiledMapPaths.size() << " Tiled maps\n";
198}
199
201{
202#ifdef OLYMPE_BLUEPRINT_EDITOR_ENABLED
203 if (!m_f2MenuOpen) return;
204
205 ImGui::SetNextWindowSize(ImVec2(600, 400), ImGuiCond_FirstUseEver);
206 if (ImGui::Begin("Load Tiled Level (F2)", &m_f2MenuOpen))
207 {
208 ImGui::Text("Select a Tiled map (.tmj) to load:");
209 ImGui::Separator();
210
211 // Scan button
212 if (ImGui::Button("Refresh List"))
213 {
215 }
216
217 ImGui::SameLine();
218 ImGui::Text("Found %d maps", static_cast<int>(m_tiledMapPaths.size()));
219
220 ImGui::Separator();
221
222 // Map list
223 ImGui::BeginChild("MapList", ImVec2(0, -30), true);
224
225 for (size_t i = 0; i < m_tiledMapPaths.size(); ++i)
226 {
227 const std::string& mapPath = m_tiledMapPaths[i];
228
229 // Extract just the filename for display
230 std::string displayName = mapPath;
231 size_t lastSlash = displayName.find_last_of("/\\");
232 if (lastSlash != std::string::npos)
233 {
234 displayName = displayName.substr(lastSlash + 1);
235 }
236
237 bool isSelected = (static_cast<int>(i) == m_selectedMapIndex);
238 if (ImGui::Selectable(displayName.c_str(), isSelected, ImGuiSelectableFlags_AllowDoubleClick))
239 {
240 m_selectedMapIndex = static_cast<int>(i);
241
242 // Double-click to load
243 if (ImGui::IsMouseDoubleClicked(0))
244 {
245 SYSTEM_LOG << "GameMenu: Loading map: " << mapPath << "\n";
247 m_f2MenuOpen = false;
248 }
249 }
250
251 // Show full path as tooltip
252 if (ImGui::IsItemHovered())
253 {
254 ImGui::SetTooltip("%s", mapPath.c_str());
255 }
256 }
257
258 ImGui::EndChild();
259
260 // Load button
261 ImGui::Separator();
262 if (ImGui::Button("Load Selected", ImVec2(120, 0)))
263 {
264 if (m_selectedMapIndex >= 0 && m_selectedMapIndex < static_cast<int>(m_tiledMapPaths.size()))
265 {
266 const std::string& mapPath = m_tiledMapPaths[m_selectedMapIndex];
267 SYSTEM_LOG << "GameMenu: Loading map: " << mapPath << "\n";
269 m_f2MenuOpen = false;
270 }
271 }
272
273 ImGui::SameLine();
274 if (ImGui::Button("Cancel", ImVec2(120, 0)))
275 {
276 m_f2MenuOpen = false;
277 }
278 }
279 ImGui::End();
280#endif
281}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
void Activate()
Definition GameMenu.cpp:22
virtual void Render()
Definition GameMenu.cpp:76
void SelectPrevious()
Definition GameMenu.cpp:44
void SetF2MenuOpen(bool open)
Definition GameMenu.cpp:99
void RefreshTiledMapList()
Definition GameMenu.cpp:180
void Update()
Definition GameMenu.cpp:87
bool m_hasScannedTiledMaps
Definition GameMenu.h:76
bool m_active
Definition GameMenu.h:68
std::vector< std::string > m_entries
Definition GameMenu.h:69
void ToggleF2Menu()
Definition GameMenu.cpp:94
void SelectNext()
Definition GameMenu.cpp:49
std::vector< std::string > m_tiledMapPaths
Definition GameMenu.h:74
void Deactivate()
Definition GameMenu.cpp:33
void ValidateSelection()
Definition GameMenu.cpp:54
int m_selectedMapIndex
Definition GameMenu.h:75
int m_selected
Definition GameMenu.h:70
void ScanForTiledMaps(const std::string &directory)
Definition GameMenu.cpp:109
void RenderF2Menu()
Definition GameMenu.cpp:200
bool m_f2MenuOpen
Definition GameMenu.h:73
@ Restart
Definition GameMenu.h:17
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