TazGraph Project v0.1.0
Loading...
Searching...
No Matches
DataManager.h
1#pragma once
2
3#include "../../pch.h"
4
5#include "../GECS/Core/SimulationStep.h"
6#include "../GECS/UtilComponents.h"
7
8namespace fs = std::filesystem;
9
10namespace Taz {
12 std::vector<Entity*> entities;
13 };
14
16 std::vector<GECSRenderBatch> batches;
17 glm::vec4 backgroundColor;
18 bool renderDebug = false;
19
20 PlaneModelRenderer planeModelRenderer;
21 PlaneColorRenderer planeColorRenderer;
22 LineRenderer lineRenderer;
23 LightRenderer lightRenderer;
24 };
25}
26
27
29private:
30 std::string requestedMap;
31 std::atomic<bool> managerChangeRequested{ false };
32
33public:
34 // Gets the single instance of CameraManager (singleton)
35 static DataManager& getInstance() {
36 static DataManager instance; // Guaranteed to be destroyed. Instantiated on first use.
37 return instance;
38 }
39
40 DataManager() {}
41
42 ImGui::ComboAutoSelectData data;
43 ImGui::ComboAutoSelectData pathData;
44
45
46 std::vector<std::string> fileNames;
47 std::vector<std::string> pollingFileNames;
48 std::vector<std::string> pathsFileNames;
49 std::string pathLoading;
50
51 bool filesLoaded = false;
52
53 bool saving = false;
54 bool startingNew = false;
55 bool loading = false;
56 bool loadingPath = false;
57 bool goingBack = false;
58
59 std::map<int, NodeEntity*> mapSimToGraphNodes;
60 std::map<int, LinkEntity*> mapSimToGraphLinks;
61 std::map<int, EmptyEntity*> mapSimToGraphPaths;
62
63 void setMapToLoad(const std::string& name) {
64 if (managerChangeRequested.load() == true) return;
65
66 requestedMap = name;
67 managerChangeRequested.store(true);
68 }
69
70 std::string getMapToLoad() {
71 return requestedMap;
72 }
73
74 bool getManagerChangeRequested() {
75 return managerChangeRequested.load();
76 }
77
78 void setManagerChangeRequested(bool m_request) {
79 managerChangeRequested.store(m_request);
80 }
81
82 void setPathLoading(bool loading)
83 {
84 loadingPath = loading;
85 }
86
87 bool isSaving() {
88 return saving;
89 }
90
91 void setNewMap(bool m_startingNew)
92 {
93 startingNew = m_startingNew;
94 }
95
96 void setLoading(bool m_loading)
97 {
98 loading = m_loading;
99 }
100
101 bool isStartingNew()
102 {
103 return startingNew;
104 }
105
106 bool isLoading()
107 {
108 return loading;
109 }
110
111 bool isLoadingPath()
112 {
113 return loadingPath;
114 }
115
116 bool isGoingBack()
117 {
118 return goingBack;
119 }
120
121 std::string getPathLoading() {
122 return pathLoading;
123 }
124
125 void SetGoingBack(bool m_goingBack) {
126 goingBack = m_goingBack;
127 }
128
129 void updateFileNamesInAssets() {
130 fileNames.clear();
131 const std::string path = "assets/Maps"; // Directory path
132 for (const auto& entry : fs::directory_iterator(path)) {
133 if (entry.is_regular_file()) {
134 fileNames.push_back(entry.path().filename().string()); // Add file name to vector
135 }
136 }
137 }
138
139 void updatePathFileNamesInAssets() {
140 pathsFileNames.clear();
141 const std::string path = "assets/Paths"; // Directory path
142 for (const auto& entry : fs::directory_iterator(path)) {
143 if (entry.is_regular_file()) {
144 pathsFileNames.push_back(entry.path().filename().string()); // Add file name to vector
145 }
146 }
147 pathsFileNames.push_back(">Reset");
148 }
149
150 void ReloadAccessibleFiles() {
151 if (!filesLoaded) {
152 updateFileNamesInAssets();
153 updatePathFileNamesInAssets();
154
155 filesLoaded = true; // Set to true so we don't reload unnecessarily
156 }
157 }
158
159 void applyStep(Manager& manager, int transitionToStep)
160 {
161 // Nodes
162
163 auto stepIt = manager.steps.begin();
164 std::advance(stepIt, transitionToStep);
165
166 for (auto& node : stepIt->nodes) {
167 NodeEntity* t_node = node.first;
168
169 if (t_node->hasComponent<TransformComponent>() &&
170 //t_node->hasComponent<MovingAnimatorComponent>() &&
171 t_node->hasComponent<RectangleFlashAnimatorComponent>()
172 ) {
173
174 auto& tc = t_node->GetComponent<TransformComponent>();
175
176 /*t_node->GetComponent<MovingAnimatorComponent>().Play("StepMove",*/
177 tc.position = node.second.position;
178
179 tc.size = glm::vec3(node.second.size);
180
181 t_node->GetComponent<RectangleFlashAnimatorComponent>().Play("RectInterpolation", node.second.color);
182
183 }
184 }
185
186 // Links
187 for (auto& link : stepIt->links) {
188 LinkEntity* t_link = link.first;
189
190 if (t_link->hasComponent<Line_w_Color>()) {
191 auto& lwc = t_link->GetComponent<Line_w_Color>();
192 lwc.src_color = link.second.color;
193 lwc.dest_color = link.second.color;
194 lwc.width = link.second.width;
195 }
196 }
197
198 for (auto& simPaths : DataManager::getInstance().mapSimToGraphPaths) {
199 auto& pathLinks = simPaths.second->GetComponent<PathLinkerComponent>().pathLinks;
200 auto toRemove = pathLinks;
201
202 for (auto pathLinkId : toRemove) {
203 simPaths.second->GetComponent<PathLinkerComponent>().removeLink(pathLinkId);
204 Entity* pathLink = manager.getEntityFromId(pathLinkId);
205 pathLink->destroy();
206 }
207 }
208
209 // Paths
210 for (size_t i = 0; i < stepIt->paths.size(); i++) {
211
212 auto stepIt = manager.steps.begin();
213 std::advance(stepIt, transitionToStep);
214
215 auto& path = stepIt->paths[i];
216
217 auto& plc = path.first->GetComponent<PathLinkerComponent>();
218
219 plc.color = path.second.color;
220 plc.width = path.second.width;
221
222 for (auto linkId : path.second.link_ids) {
223 // Find the link entity by ID
224 LinkEntity* linkEntity = nullptr;
225 for (auto& link : DataManager::getInstance().mapSimToGraphLinks) {
226 if (link.second->getId() == linkId) {
227 linkEntity = link.second;
228 break;
229 }
230 }
231
232 if (!linkEntity) break;
233
234 int idA = std::get<int>(linkEntity->getFromNode());
235 int idB = std::get<int>(linkEntity->getToNode());
236
237 // create ECS link
238 auto& link = manager.addEntity<Link>((int)idA, (int)idB);
239 link.addToGroup(Manager::groupPathLinks);
240 link.addComponent<Line_w_Color>();
241
242 link.addComponent<LineFlashAnimatorComponent>();
243
244 manager.grid->addLink(&link, manager.grid->getGridLevel());
245
246 // associate link with path linker
247 path.first->GetComponent<PathLinkerComponent>().addLink(link.getId());
248
249 }
250 }
251
252 // PATH LINKS UPDATE
253 if (manager.arrowheadsEnabled) {
254 for (auto* link : manager.getGroup<LinkEntity>(Manager::groupPathLinks))
255 {
256 auto* link_entity = dynamic_cast<LinkEntity*>(link);
257
258 link_entity->setConnectionType(LinkEntity::ConnectionType::PORT_TO_PORT);
259 link_entity->updateConnection();
260 }
261
262 for (auto* pathLinker : manager.getGroup<EmptyEntity>(Manager::groupPathLinksHolder))
263 {
264 pathLinker->GetComponent<PathLinkerComponent>().createInnerLinks();
265 }
266 }
267 }
268
269 SimulationStep deepCopySimulationStepTo(
270 Manager& manager,
271 sim_dump::UInt32 sourceStepIndex
272 )
273 {
274 auto src = std::find_if(
275 manager.steps.begin(),
276 manager.steps.end(),
277 [&](const SimulationStep& s) {
278 return s.step_index == sourceStepIndex;
279 }
280 );
281
282 if (src == manager.steps.end()) {
283 TAZ_ERROR("Source SimulationStep not found");
284 return SimulationStep();
285 }
286
287 SimulationStep copied = SimulationStep(*src); // deep copy of vectors
288
289 return copied;
290 }
291
292
293 void reindexSteps(Manager& manager)
294 {
295 sim_dump::UInt32 idx = 0;
296 for (auto& step : manager.steps) {
297 step.step_index = idx++;
298 }
299 }
300
301 void addSimulationStep(Manager& manager) {
302 SimulationStep new_step = SimulationStep();
303
304 new_step.step_index = manager.steps.back().step_index + 1;
305 new_step.timestamp = manager.steps.back().timestamp + 0.01f;
306
307 manager.steps.push_back(new_step);
308
309 reindexSteps(manager);
310 }
311
312
313 void addSimulationStep(Manager& manager, sim_dump::UInt32 step, double timestamp, sim_dump::UInt32 copyStep) {
314 SimulationStep new_step;
315
316 if (copyStep != -1) {
317 new_step = deepCopySimulationStepTo(manager, copyStep);
318 }
319 else {
320 new_step = SimulationStep();
321 }
322
323 new_step.step_index = step;
324 new_step.timestamp = timestamp;
325
326 if (step >= manager.steps.size()) {
327 manager.steps.push_back(new_step);
328 return;
329 }
330
331 auto it = manager.steps.begin();
332 std::advance(it, step);
333
334 manager.steps.insert(it, new_step); // there are 2 steps with same index but
335 //newly insterted is before the other
336
337 reindexSteps(manager);
338 }
339
340 void removeSimulationStep(Manager& manager, sim_dump::UInt32 step) {
341 if (step >= manager.steps.size()) {
342 TAZ_ERROR("SimulationStep to remove not found");
343 return;
344 }
345 auto it = manager.steps.begin();
346 std::advance(it, step);
347
348 manager.steps.erase(it);
349
350 reindexSteps(manager);
351 }
352
353
354 std::string simulationStepToString(const SimulationStep& step)
355 {
356 std::ostringstream out;
357
358 out << "=== Simulation Step ===\n";
359 out << "Index: " << step.step_index << "\n";
360 out << "Timestamp: " << step.timestamp << "\n";
361
362 // ---- Nodes ----
363 out << "Nodes (" << step.nodes.size() << ")\n";
364 for (const auto& [node, simNode] : step.nodes) {
365 out << " Node ptr: " << node
366 << " pos: (" << simNode.position.x
367 << ", " << simNode.position.y
368 << ", " << simNode.position.z << ")"
369 << " size: " << simNode.size.value
370 << "\n";
371 }
372
373 // ---- Links ----
374 out << "Links (" << step.links.size() << ")\n";
375 for (const auto& [link, simLink] : step.links) {
376 out << " Link ptr: " << link
377 << " width: " << simLink.width
378 << "\n";
379 }
380
381 // ---- Paths ----
382 out << "Paths (" << step.paths.size() << ")\n";
383 for (const auto& [pathEntity, simPath] : step.paths) {
384 out << " Path ptr: " << pathEntity
385 << " width: " << simPath.width
386 << " links: ";
387
388 for (EntityID id : simPath.link_ids) {
389 out << EntityIDUtils::toString(id) << " ";
390 }
391 out << "\n";
392 }
393
394 out << "=======================\n";
395
396 return out.str();
397 }
398
399 std::string simulationStepToString(const Manager& manager, int stepIndex)
400 {
401 std::ostringstream out;
402
403 auto it = std::find_if(
404 manager.steps.begin(),
405 manager.steps.end(),
406 [&](const SimulationStep& s) { return s.step_index == stepIndex; }
407 );
408
409 if (it != manager.steps.end()) {
410 out << simulationStepToString(*it);
411 }
412 else {
413 out << "SimulationStep " << stepIndex << " not found.\n";
414 }
415
416 return out.str();
417 }
418
419 void connectClient(int port) {
420#ifdef _WIN32
421 WSADATA wsa;
422 WSAStartup(MAKEWORD(2, 2), &wsa);
423#endif
424
425 socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
426 if (sock < 0) {
427 perror("socket error");
428 return;
429 }
430
431 sockaddr_in addr{};
432 addr.sin_family = AF_INET;
433 addr.sin_port = htons((u_short)port);
434 inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
435
436 if (connect(sock, (sockaddr*)&addr, sizeof(addr)) < 0) {
437 perror("connect error");
438 close_socket(sock);
439 return;
440 }
441
442 char input[1024];
443 while (fgets(input, sizeof(input), stdin)) {
444 send(sock, input, (int)strlen(input), 0);
445 }
446
447 close_socket(sock);
448
449#ifdef _WIN32
450 WSACleanup();
451#endif
452 }
453};
Definition DataManager.h:28
Definition GECSEntity.h:7
Definition GECS.h:224
Definition LightRenderer.h:15
Definition LineFlashAnimatorComponent.h:14
Definition LineRenderer.h:6
Definition Line_w_Color.h:6
Definition GECSEntity.h:108
Definition GECSManager.h:20
Definition GECSEntity.h:40
Definition PathLinkerComponent.h:6
Definition PlaneColorRenderer.h:14
Definition PlaneModelRenderer.h:15
Definition RectangleFlashAnimatorComponent.h:14
Definition TransformComponent.h:7
Definition SimulationStep.h:6
Definition DataManager.h:15
Definition DataManager.h:11
Definition Renderer.h:19