Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
NodeGroup.cpp
Go to the documentation of this file.
1/**
2 * @file NodeGroup.cpp
3 * @brief GroupManager implementation (Phase 9).
4 * @author Olympe Engine
5 * @date 2026-03-09
6 */
7
8#include "NodeGroup.h"
9
10#include "../system/system_utils.h"
11
13
14namespace Olympe {
15
16// ============================================================================
17// NodeGroup serialisation
18// ============================================================================
19
21{
22 json j = json::object();
23 j["id"] = id;
24 j["name"] = name;
25 j["isCollapsed"] = isCollapsed;
26 j["collapsedPosX"] = collapsedPosX;
27 j["collapsedPosY"] = collapsedPosY;
28 j["headerColor"] = static_cast<int>(headerColor);
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("id") && j["id"].is_number_integer())
43 g.id = j["id"].get<int>();
44
45 if (j.contains("name") && j["name"].is_string())
46 g.name = j["name"].get<std::string>();
47
48 if (j.contains("isCollapsed") && j["isCollapsed"].is_boolean())
49 g.isCollapsed = j["isCollapsed"].get<bool>();
50
51 if (j.contains("collapsedPosX") && j["collapsedPosX"].is_number())
52 g.collapsedPosX = j["collapsedPosX"].get<float>();
53
54 if (j.contains("collapsedPosY") && j["collapsedPosY"].is_number())
55 g.collapsedPosY = j["collapsedPosY"].get<float>();
56
57 if (j.contains("headerColor") && j["headerColor"].is_number_integer())
58 g.headerColor = j["headerColor"].get<unsigned int>();
59
60 if (j.contains("nodeIds") && j["nodeIds"].is_array())
61 {
62 const json& arr = j["nodeIds"];
63 for (auto it = arr.begin(); it != arr.end(); ++it)
64 {
65 if (it->is_number_integer())
66 g.nodeIds.push_back(it->get<int>());
67 }
68 }
69
70 return g;
71}
72
73// ============================================================================
74// GroupManager — Singleton
75// ============================================================================
76
78{
79 static GroupManager s_Instance;
80 return s_Instance;
81}
82
84 : m_NextGroupId(1)
85{
86}
87
88// ============================================================================
89// Lifecycle
90// ============================================================================
91
92int GroupManager::CreateGroup(const std::string& name, const std::vector<int>& nodeIds)
93{
95 g.id = m_NextGroupId++;
96 g.name = name;
97 g.nodeIds = nodeIds;
98 m_Groups.push_back(g);
99 return g.id;
100}
101
103{
104 for (size_t i = 0; i < m_Groups.size(); ++i)
105 {
106 if (m_Groups[i].id == groupId)
107 {
108 m_Groups.erase(m_Groups.begin() + static_cast<int>(i));
109 return;
110 }
111 }
112}
113
115{
117 if (g)
118 g->isCollapsed = true;
119}
120
122{
124 if (g)
125 g->isCollapsed = false;
126}
127
129{
130 for (size_t i = 0; i < m_Groups.size(); ++i)
131 {
132 if (m_Groups[i].id == groupId)
133 return &m_Groups[i];
134 }
135 return nullptr;
136}
137
139{
140 return static_cast<int>(m_Groups.size());
141}
142
144{
145 m_Groups.clear();
146 m_NextGroupId = 1;
147}
148
149// ============================================================================
150// Serialisation
151// ============================================================================
152
154{
155 json arr = json::array();
156 for (size_t i = 0; i < m_Groups.size(); ++i)
157 arr.push_back(m_Groups[i].ToJson());
158 j["groups"] = arr;
159}
160
162{
163 Clear();
164 if (!j.contains("groups") || !j["groups"].is_array())
165 return;
166
167 const json& arr = j["groups"];
168 for (auto it = arr.begin(); it != arr.end(); ++it)
169 {
171 if (g.id >= m_NextGroupId)
172 m_NextGroupId = g.id + 1;
173 m_Groups.push_back(g);
174 }
175}
176
177} // namespace Olympe
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Node grouping and collapse system for node graphs (Phase 9).
Singleton that owns all NodeGroup instances for the active graph.
Definition NodeGroup.h:64
static GroupManager & Get()
Returns the single shared instance.
Definition NodeGroup.cpp:77
void LoadFromJson(const nlohmann::json &j)
void ExpandGroup(int groupId)
Marks the group as expanded.
void Clear()
Removes all groups and resets the ID counter.
void DeleteGroup(int groupId)
Deletes the group with the given ID.
NodeGroup * GetGroup(int groupId)
Returns a pointer to the group with the given ID.
int CreateGroup(const std::string &name, const std::vector< int > &nodeIds)
Creates a new group from the given node IDs.
Definition NodeGroup.cpp:92
int GetGroupCount() const
Returns the total number of groups.
std::vector< NodeGroup > m_Groups
Definition NodeGroup.h:116
void CollapseGroup(int groupId)
Marks the group as collapsed.
void SaveToJson(nlohmann::json &j) const
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
nlohmann::json json
A named set of node IDs that can be collapsed into a single header.
Definition NodeGroup.h:35
unsigned int headerColor
RGBA packed (default: blue)
Definition NodeGroup.h:42
int id
Unique group ID.
Definition NodeGroup.h:36
bool isCollapsed
True = render as header only.
Definition NodeGroup.h:39
float collapsedPosX
Position used when collapsed.
Definition NodeGroup.h:40
static NodeGroup FromJson(const nlohmann::json &j)
Definition NodeGroup.cpp:38
std::string name
Display name for the group.
Definition NodeGroup.h:37
nlohmann::json ToJson() const
Definition NodeGroup.cpp:20
std::vector< int > nodeIds
IDs of member nodes.
Definition NodeGroup.h:38