Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
NodeGraphManager.cpp
Go to the documentation of this file.
1/**
2 * @file NodeGraphManager.cpp
3 * @brief Implementation of NodeGraphManager
4 * @author Olympe Engine
5 * @date 2026-02-18
6 */
7
8#include "NodeGraphManager.h"
9#include "GraphMigrator.h"
10#include "../system/system_utils.h"
11#include <fstream>
12
14
15namespace Olympe {
16namespace NodeGraph {
17
18// ============================================================================
19// Singleton
20// ============================================================================
21
27
29 : m_nextGraphId(1)
30{
31}
32
36
37// ============================================================================
38// Graph Lifecycle
39// ============================================================================
40
41GraphId NodeGraphManager::CreateGraph(const std::string& graphType, const std::string& graphKind)
42{
45
46 auto doc = std::unique_ptr<GraphDocument>(new GraphDocument());
47 doc->type = graphType;
48 doc->graphKind = graphKind;
49
50 std::string name = graphKind + " " + std::to_string(newId.value);
51
52 m_graphs[newId] = std::move(doc);
53 m_graphNames[newId] = name;
54 m_graphOrder.push_back(newId);
56
57 SYSTEM_LOG << "[NodeGraphManager] Created graph " << newId.value << " (" << graphKind << ")" << std::endl;
58
59 return newId;
60}
61
62GraphId NodeGraphManager::LoadGraph(const std::string& filepath)
63{
64 json j;
65 if (!JsonHelper::LoadJsonFromFile(filepath, j))
66 {
67 SYSTEM_LOG << "[NodeGraphManager] Failed to load graph from " << filepath << std::endl;
68 return GraphId{0};
69 }
70
71 // Use migrator to handle version detection and migration
73
76
77 // Extract name from filepath
78 std::string name = filepath;
79 size_t lastSlash = filepath.find_last_of("/\\");
80 if (lastSlash != std::string::npos)
81 {
82 name = filepath.substr(lastSlash + 1);
83 }
84
85 m_graphs[newId] = std::unique_ptr<GraphDocument>(new GraphDocument(doc));
86 m_graphNames[newId] = name;
87 m_graphOrder.push_back(newId);
89
90 SYSTEM_LOG << "[NodeGraphManager] Loaded graph " << newId.value << " from " << filepath << std::endl;
91
92 return newId;
93}
94
95bool NodeGraphManager::SaveGraph(GraphId id, const std::string& filepath)
96{
97 auto it = m_graphs.find(id);
98 if (it == m_graphs.end())
99 {
100 SYSTEM_LOG << "[NodeGraphManager] Cannot save: graph " << id.value << " not found" << std::endl;
101 return false;
102 }
103
104 json j = it->second->ToJson();
105
106 if (!JsonHelper::SaveJsonToFile(filepath, j, 2))
107 {
108 SYSTEM_LOG << "[NodeGraphManager] Failed to save graph to " << filepath << std::endl;
109 return false;
110 }
111
112 it->second->SetDirty(false);
113
114 SYSTEM_LOG << "[NodeGraphManager] Saved graph " << id.value << " to " << filepath << std::endl;
115
116 return true;
117}
118
120{
121 auto it = m_graphs.find(id);
122 if (it == m_graphs.end())
123 {
124 return false;
125 }
126
127 m_graphs.erase(it);
128 m_graphNames.erase(id);
129
130 // Remove from order
131 auto orderIt = std::find(m_graphOrder.begin(), m_graphOrder.end(), id);
132 if (orderIt != m_graphOrder.end())
133 {
134 m_graphOrder.erase(orderIt);
135 }
136
137 // Update active graph if closed
138 if (m_activeGraphId == id)
139 {
140 if (!m_graphOrder.empty())
141 {
143 }
144 else
145 {
147 }
148 }
149
150 SYSTEM_LOG << "[NodeGraphManager] Closed graph " << id.value << std::endl;
151
152 return true;
153}
154
155// ========================================================================
156// Active Graph Management
157// ========================================================================
158
160{
161 auto it = m_graphs.find(id);
162 if (it != m_graphs.end())
163 {
164 m_activeGraphId = id;
165 }
166}
167
169{
170 if (m_activeGraphId.value == 0)
171 return nullptr;
172
174}
175
176// ========================================================================
177// Queries
178// ========================================================================
179
181{
182 auto it = m_graphs.find(id);
183 if (it != m_graphs.end())
184 {
185 return it->second.get();
186 }
187 return nullptr;
188}
189
190std::vector<GraphId> NodeGraphManager::GetAllGraphIds() const
191{
192 std::vector<GraphId> ids;
193 for (auto it = m_graphs.begin(); it != m_graphs.end(); ++it)
194 {
195 ids.push_back(it->first);
196 }
197 return ids;
198}
199
201{
202 auto it = m_graphNames.find(id);
203 if (it != m_graphNames.end())
204 {
205 return it->second;
206 }
207 return "";
208}
209
210} // namespace NodeGraph
211} // namespace Olympe
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Migration system for JSON versions.
Singleton manager for multiple node graphs.
Main document class for a node graph.
static GraphDocument LoadWithMigration(const json &j)
Load graph with automatic migration.
Singleton manager for multiple node graphs.
std::string GetGraphName(GraphId id) const
Get graph name (for tab display)
std::map< GraphId, std::string > m_graphNames
GraphDocument * GetGraph(GraphId id)
Get graph by ID.
static NodeGraphManager & Get()
Get singleton instance.
void SetActiveGraph(GraphId id)
Set active graph.
GraphDocument * GetActiveGraph()
Get active graph.
GraphId CreateGraph(const std::string &graphType, const std::string &graphKind)
Create a new graph.
bool SaveGraph(GraphId id, const std::string &filepath)
Save a graph to file.
std::vector< GraphId > GetAllGraphIds() const
Get all graph IDs.
GraphId LoadGraph(const std::string &filepath)
Load a graph from file.
std::map< GraphId, std::unique_ptr< GraphDocument > > m_graphs
bool CloseGraph(GraphId id)
Close a graph.
bool LoadJsonFromFile(const std::string &filepath, json &j)
Load and parse a JSON file.
Definition json_helper.h:42
bool SaveJsonToFile(const std::string &filepath, const json &j, int indent=4)
Save a JSON object to a file with formatting.
Definition json_helper.h:73
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
nlohmann::json json
#define SYSTEM_LOG