Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
NodePresets.cpp
Go to the documentation of this file.
1/**
2 * @file NodePresets.cpp
3 * @brief NodePresetManager implementation (Phase 9).
4 * @author Olympe Engine
5 * @date 2026-03-09
6 */
7
8#include "NodePresets.h"
9
10#include <fstream>
11
12#include "../system/system_utils.h"
13
15
16namespace Olympe {
17
18// ============================================================================
19// NodePreset serialisation
20// ============================================================================
21
23{
24 json j = json::object();
25 j["name"] = name;
26 j["description"]= description;
27 j["category"] = category;
28 j["nodeData"] = nodeData;
29
30 json ids = json::array();
31 for (size_t i = 0; i < nodeIds.size(); ++i)
32 ids.push_back(nodeIds[i]);
33 j["nodeIds"] = ids;
34
35 return j;
36}
37
39{
41
42 if (j.contains("name") && j["name"].is_string())
43 p.name = j["name"].get<std::string>();
44
45 if (j.contains("description") && j["description"].is_string())
46 p.description = j["description"].get<std::string>();
47
48 if (j.contains("category") && j["category"].is_string())
49 p.category = j["category"].get<std::string>();
50
51 if (j.contains("nodeData"))
52 p.nodeData = j["nodeData"];
53
54 if (j.contains("nodeIds") && j["nodeIds"].is_array())
55 {
56 const json& arr = j["nodeIds"];
57 for (auto it = arr.begin(); it != arr.end(); ++it)
58 {
59 if (it->is_number_integer())
60 p.nodeIds.push_back(it->get<int>());
61 }
62 }
63
64 return p;
65}
66
67// ============================================================================
68// NodePresetManager — Singleton
69// ============================================================================
70
72{
73 static NodePresetManager s_Instance;
74 return s_Instance;
75}
76
80
81// ============================================================================
82// Persistence
83// ============================================================================
84
85void NodePresetManager::LoadPresets(const std::string& path)
86{
87 Clear();
88
89 std::ifstream ifs(path.c_str());
90 if (!ifs.is_open())
91 {
92 SYSTEM_LOG << "[NodePresetManager] Cannot open '" << path << "' for reading."
93 << std::endl;
94 return;
95 }
96
97 json root;
98 try
99 {
100 ifs >> root;
101 }
102 catch (...)
103 {
104 SYSTEM_LOG << "[NodePresetManager] JSON parse error in '" << path << "'."
105 << std::endl;
106 return;
107 }
108
109 if (!root.contains("presets") || !root["presets"].is_array())
110 {
111 SYSTEM_LOG << "[NodePresetManager] '" << path << "' has no 'presets' array."
112 << std::endl;
113 return;
114 }
115
116 const json& arr = root["presets"];
117 for (auto it = arr.begin(); it != arr.end(); ++it)
118 m_Presets.push_back(NodePreset::FromJson(*it));
119
120 SYSTEM_LOG << "[NodePresetManager] Loaded " << static_cast<int>(m_Presets.size())
121 << " preset(s) from '" << path << "'." << std::endl;
122}
123
124void NodePresetManager::SavePresets(const std::string& path) const
125{
126 json root = json::object();
127 json arr = json::array();
128 for (size_t i = 0; i < m_Presets.size(); ++i)
129 arr.push_back(m_Presets[i].ToJson());
130 root["presets"] = arr;
131
132 std::ofstream ofs(path.c_str());
133 if (!ofs.is_open())
134 {
135 SYSTEM_LOG << "[NodePresetManager] Cannot open '" << path << "' for writing."
136 << std::endl;
137 return;
138 }
139
140 ofs << root.dump(2);
141 SYSTEM_LOG << "[NodePresetManager] Saved " << static_cast<int>(m_Presets.size())
142 << " preset(s) to '" << path << "'." << std::endl;
143}
144
145// ============================================================================
146// CRUD
147// ============================================================================
148
150{
151 // Replace if a preset with the same name already exists
152 for (size_t i = 0; i < m_Presets.size(); ++i)
153 {
154 if (m_Presets[i].name == preset.name)
155 {
156 m_Presets[i] = preset;
157 return;
158 }
159 }
160 m_Presets.push_back(preset);
161}
162
163void NodePresetManager::RemovePreset(const std::string& name)
164{
165 for (size_t i = 0; i < m_Presets.size(); ++i)
166 {
167 if (m_Presets[i].name == name)
168 {
169 m_Presets.erase(m_Presets.begin() + static_cast<int>(i));
170 return;
171 }
172 }
173}
174
176{
177 for (size_t i = 0; i < m_Presets.size(); ++i)
178 {
179 if (m_Presets[i].name == name)
180 return &m_Presets[i];
181 }
182 return nullptr;
183}
184
186 const std::string& category) const
187{
188 std::vector<NodePreset> result;
189 for (size_t i = 0; i < m_Presets.size(); ++i)
190 {
191 if (m_Presets[i].category == category)
192 result.push_back(m_Presets[i]);
193 }
194 return result;
195}
196
198{
199 return static_cast<int>(m_Presets.size());
200}
201
203{
204 m_Presets.clear();
205}
206
207} // namespace Olympe
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Node preset / template system for frequently used node configurations (Phase 9).
Singleton that stores, serialises, and retrieves NodePreset instances.
Definition NodePresets.h:69
std::vector< NodePreset > GetPresetsInCategory(const std::string &category) const
Returns all presets that belong to category.
NodePreset * GetPreset(const std::string &name)
Returns a pointer to the preset with the given name.
int GetPresetCount() const
Returns the total number of stored presets.
void AddPreset(const NodePreset &preset)
Adds or replaces a preset (matched by name).
void SavePresets(const std::string &path) const
Saves all current presets to a JSON file at path.
void LoadPresets(const std::string &path)
Loads presets from a JSON file at path.
std::vector< NodePreset > m_Presets
void Clear()
Removes all presets.
static NodePresetManager & Get()
Returns the single shared instance.
void RemovePreset(const std::string &name)
Removes the preset with the given name.
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
nlohmann::json json
A saved node configuration that can be applied to the active graph.
Definition NodePresets.h:37
nlohmann::json nodeData
JSON snapshot of the node(s)
Definition NodePresets.h:41
static NodePreset FromJson(const nlohmann::json &j)
std::string name
Unique preset identifier.
Definition NodePresets.h:38
nlohmann::json ToJson() const
std::vector< int > nodeIds
For multi-node presets: list of involved IDs.
Definition NodePresets.h:42
std::string description
Human-readable description.
Definition NodePresets.h:39
std::string category
Category for grouping in the UI.
Definition NodePresets.h:40
#define SYSTEM_LOG