Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
GraphComment.cpp
Go to the documentation of this file.
1/**
2 * @file GraphComment.cpp
3 * @brief CommentManager and GraphComment serialisation (Phase 9).
4 * @author Olympe Engine
5 * @date 2026-03-09
6 */
7
8#include "GraphComment.h"
9
10#include "../system/system_utils.h"
11
13
14namespace Olympe {
15
16// ============================================================================
17// GraphComment serialisation
18// ============================================================================
19
21{
22 json j = json::object();
23 j["id"] = id;
24 j["text"] = text;
25 j["posX"] = posX;
26 j["posY"] = posY;
27 j["w"] = width;
28 j["h"] = height;
29 j["color"] = static_cast<int>(color);
30 j["font"] = fontSize;
31 j["vis"] = isVisible;
32 return j;
33}
34
36{
38
39 if (j.contains("id") && j["id"].is_number_integer())
40 c.id = j["id"].get<int>();
41
42 if (j.contains("text") && j["text"].is_string())
43 c.text = j["text"].get<std::string>();
44
45 if (j.contains("posX") && j["posX"].is_number())
46 c.posX = j["posX"].get<float>();
47
48 if (j.contains("posY") && j["posY"].is_number())
49 c.posY = j["posY"].get<float>();
50
51 if (j.contains("w") && j["w"].is_number())
52 c.width = j["w"].get<float>();
53
54 if (j.contains("h") && j["h"].is_number())
55 c.height = j["h"].get<float>();
56
57 if (j.contains("color") && j["color"].is_number_integer())
58 c.color = j["color"].get<unsigned int>();
59
60 if (j.contains("font") && j["font"].is_number())
61 c.fontSize = j["font"].get<float>();
62
63 if (j.contains("vis") && j["vis"].is_boolean())
64 c.isVisible = j["vis"].get<bool>();
65
66 return c;
67}
68
69// ============================================================================
70// CommentManager — Singleton
71// ============================================================================
72
74{
75 static CommentManager s_Instance;
76 return s_Instance;
77}
78
80 : m_NextCommentId(1)
81{
82}
83
84// ============================================================================
85// CRUD
86// ============================================================================
87
89{
90 GraphComment c = comment;
92 m_Comments.push_back(c);
93 return c.id;
94}
95
97{
98 for (size_t i = 0; i < m_Comments.size(); ++i)
99 {
100 if (m_Comments[i].id == commentId)
101 {
102 m_Comments.erase(m_Comments.begin() + static_cast<int>(i));
103 return;
104 }
105 }
106}
107
109{
110 for (size_t i = 0; i < m_Comments.size(); ++i)
111 {
112 if (m_Comments[i].id == commentId)
113 return &m_Comments[i];
114 }
115 return nullptr;
116}
117
119{
120 for (size_t i = 0; i < m_Comments.size(); ++i)
121 {
122 if (m_Comments[i].id == commentId)
123 {
124 GraphComment updated = comment;
125 updated.id = commentId; // preserve ID
127 return;
128 }
129 }
130 SYSTEM_LOG << "[CommentManager] UpdateComment: ID " << commentId
131 << " not found." << std::endl;
132}
133
135{
136 return static_cast<int>(m_Comments.size());
137}
138
140{
141 m_Comments.clear();
142 m_NextCommentId = 1;
143}
144
145// ============================================================================
146// Serialisation
147// ============================================================================
148
150{
151 json arr = json::array();
152 for (size_t i = 0; i < m_Comments.size(); ++i)
153 arr.push_back(m_Comments[i].ToJson());
154 j["comments"] = arr;
155}
156
158{
159 Clear();
160 if (!j.contains("comments") || !j["comments"].is_array())
161 return;
162
163 const json& arr = j["comments"];
164 for (auto it = arr.begin(); it != arr.end(); ++it)
165 {
167 // Re-insert preserving original ID but updating the counter
168 if (c.id >= m_NextCommentId)
169 m_NextCommentId = c.id + 1;
170 m_Comments.push_back(c);
171 }
172}
173
174} // namespace Olympe
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Visual comment/annotation system for node graphs (Phase 9).
Singleton that owns all GraphComment instances for the active graph.
void SaveToJson(nlohmann::json &j) const
static CommentManager & Get()
Returns the single shared instance.
void LoadFromJson(const nlohmann::json &j)
void Clear()
Removes all comments and resets the ID counter.
GraphComment * GetComment(int commentId)
Returns a pointer to the comment with the given ID.
std::vector< GraphComment > m_Comments
int AddComment(const GraphComment &comment)
Adds a comment and assigns it a unique ID.
int GetCommentCount() const
Returns the total number of comments managed.
void UpdateComment(int commentId, const GraphComment &comment)
Replaces the stored comment data for the given ID.
void RemoveComment(int commentId)
Removes the comment with the given ID.
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
nlohmann::json json
A rectangular comment box placed on the graph canvas.
static GraphComment FromJson(const nlohmann::json &j)
float posX
Left edge in graph space.
int id
Unique comment ID (assigned by CommentManager)
nlohmann::json ToJson() const
float fontSize
Font size in points.
float height
Box height in graph space.
unsigned int color
RGBA packed colour (default: amber)
std::string text
Text content displayed inside the box.
float width
Box width in graph space.
bool isVisible
Whether the comment is drawn.
float posY
Top edge in graph space.
#define SYSTEM_LOG