Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
GraphDocument.h
Go to the documentation of this file.
1/**
2 * @file GraphDocument.h
3 * @brief Document class for managing node graphs
4 * @author Olympe Engine
5 * @date 2026-02-18
6 *
7 * @details
8 * Manages a single node graph with CRUD operations, validation, and JSON serialization.
9 * Supports generic node graphs for any editor type.
10 */
11
12#pragma once
13
14#include "NodeGraphCore.h"
15#include "NodeAnnotations.h"
16#include "BlackboardSystem.h"
17#include "../json_helper.h"
18#include <memory>
19
20namespace Olympe {
21namespace NodeGraph {
22
23// Forward declaration
24class GraphDocument;
25
26/**
27 * @class GraphDocument
28 * @brief Main document class for a node graph
29 */
31public:
34
35 // ========================================================================
36 // Document Properties
37 // ========================================================================
38
39 std::string type;
40 std::string graphKind;
44
45 // ========================================================================
46 // CRUD Operations - Nodes
47 // ========================================================================
48
49 /**
50 * @brief Create a new node in the graph
51 * @param nodeType Type of the node (e.g., "BT_Selector", "BT_Action")
52 * @param pos Position of the node
53 * @return ID of the created node
54 */
55 NodeId CreateNode(const std::string& nodeType, Vector2 pos);
56
57 /**
58 * @brief Delete a node from the graph
59 * @param id ID of the node to delete
60 * @return true if deleted, false if not found
61 */
62 bool DeleteNode(NodeId id);
63
64 /**
65 * @brief Update node position
66 * @param id Node ID
67 * @param newPos New position
68 * @return true if updated, false if not found
69 */
71
72 /**
73 * @brief Update node parameters
74 * @param id Node ID
75 * @param params New parameters
76 * @return true if updated, false if not found
77 */
78 bool UpdateNodeParameters(NodeId id, const std::map<std::string, std::string>& params);
79
80 /**
81 * @brief Get a node by ID
82 * @param id Node ID
83 * @return Pointer to node data or nullptr if not found
84 */
86 const NodeData* GetNode(NodeId id) const;
87
88 /**
89 * @brief Update an existing node's data
90 * @param nodeId The node to update
91 * @param newData New node data (position, type, parameters)
92 * @return true if node was found and updated, false otherwise
93 */
94 bool UpdateNode(NodeId nodeId, const NodeData& newData);
95
96 // ========================================================================
97 // CRUD Operations - Links
98 // ========================================================================
99
100 /**
101 * @brief Connect two pins with a link
102 * @param fromPin Source pin
103 * @param toPin Target pin
104 * @return ID of created link
105 */
106 LinkId ConnectPins(PinId fromPin, PinId toPin);
107
108 /**
109 * @brief Disconnect a link
110 * @param id Link ID
111 * @return true if deleted, false if not found
112 */
113 bool DisconnectLink(LinkId id);
114
115 /**
116 * @brief Get a link by ID
117 * @param id Link ID
118 * @return Pointer to link data or nullptr if not found
119 */
121 const LinkData* GetLink(LinkId id) const;
122
123 // ========================================================================
124 // Validation
125 // ========================================================================
126
127 /**
128 * @brief Validate the graph structure
129 * @param errorMessage Output error message if validation fails
130 * @return true if valid, false otherwise
131 */
132 bool ValidateGraph(std::string& errorMessage) const;
133
134 /**
135 * @brief Check if the graph has cycles
136 * @return true if cycles detected, false otherwise
137 */
138 bool HasCycles() const;
139
140 // ========================================================================
141 // Serialization
142 // ========================================================================
143
144 /**
145 * @brief Convert graph to JSON format (v2 schema)
146 * @return JSON representation
147 */
148 json ToJson() const;
149
150 /**
151 * @brief Create graph from JSON
152 * @param j JSON object
153 * @return GraphDocument instance
154 */
155 static GraphDocument FromJson(const json& j);
156
157 /**
158 * @brief Automatically layout nodes in a hierarchical arrangement
159 * @param config Layout configuration (direction, spacing, etc.)
160 * @return true if layout was applied, false if graph is empty/invalid
161 */
163
164 // ========================================================================
165 // Data Access
166 // ========================================================================
167
168 const std::vector<NodeData>& GetNodes() const { return m_nodes; }
169 const std::vector<LinkData>& GetLinks() const { return m_links; }
170
171 std::vector<NodeData>& GetNodesRef() { return m_nodes; }
172 std::vector<LinkData>& GetLinksRef() { return m_links; }
173
174 bool IsDirty() const { return m_isDirty; }
175 void SetDirty(bool dirty) { m_isDirty = dirty; }
176
177 // ========================================================================
178 // Annotations (Phase 2.0)
179 // ========================================================================
180
181 /**
182 * @brief Get node annotations manager (non-const)
183 */
185
186 /**
187 * @brief Get node annotations manager (const)
188 */
190
191 // ========================================================================
192 // Blackboard (Phase 2.1)
193 // ========================================================================
194
195 /**
196 * @brief Get blackboard system (non-const)
197 */
199
200 /**
201 * @brief Get blackboard system (const)
202 */
203 const BlackboardSystem& GetBlackboard() const { return m_blackboard; }
204
205private:
206 // Data members
207 std::vector<NodeData> m_nodes;
208 std::vector<LinkData> m_links;
209
212 bool m_isDirty = false;
213
214 // Phase 2.0 - Node annotations
216
217 // Phase 2.1 - Blackboard system
219
220 // Helper methods
221 bool HasCyclesHelper(NodeId nodeId, std::vector<NodeId>& visited, std::vector<NodeId>& recursionStack) const;
222
223 /**
224 * @brief Helper to recursively layout a node and its children
225 * @param nodeId Node to layout
226 * @param config Layout configuration
227 * @param startX Starting X position
228 * @param startY Starting Y position
229 * @param depth Current depth in tree (0 = root)
230 * @param visited Map to track visited nodes (cycle detection)
231 * @return Total width consumed by this subtree
232 */
233 float AutoLayoutNode(
234 NodeId nodeId,
236 float startX,
237 float startY,
238 int depth,
239 std::map<NodeId, bool>& visited
240 );
241};
242
243} // namespace NodeGraph
244} // namespace Olympe
Blackboard system for shared graph variables (Phase 2.1)
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Per-node annotations manager for node graphs (Phase 2.0)
Core data structures for generic node graph system.
Manages named blackboard variables for a graph.
Main document class for a node graph.
bool UpdateNode(NodeId nodeId, const NodeData &newData)
Update an existing node's data.
bool DeleteNode(NodeId id)
Delete a node from the graph.
float AutoLayoutNode(NodeId nodeId, const AutoLayoutConfig &config, float startX, float startY, int depth, std::map< NodeId, bool > &visited)
Helper to recursively layout a node and its children.
bool AutoLayout(const AutoLayoutConfig &config)
Automatically layout nodes in a hierarchical arrangement.
bool UpdateNodeParameters(NodeId id, const std::map< std::string, std::string > &params)
Update node parameters.
LinkId ConnectPins(PinId fromPin, PinId toPin)
Connect two pins with a link.
const std::vector< LinkData > & GetLinks() const
std::vector< LinkData > & GetLinksRef()
bool HasCyclesHelper(NodeId nodeId, std::vector< NodeId > &visited, std::vector< NodeId > &recursionStack) const
std::vector< NodeData > & GetNodesRef()
LinkData * GetLink(LinkId id)
Get a link by ID.
json ToJson() const
Convert graph to JSON format (v2 schema)
bool DisconnectLink(LinkId id)
Disconnect a link.
std::vector< LinkData > m_links
static GraphDocument FromJson(const json &j)
Create graph from JSON.
const NodeAnnotationsManager & GetNodeAnnotations() const
Get node annotations manager (const)
NodeAnnotationsManager m_nodeAnnotations
NodeId CreateNode(const std::string &nodeType, Vector2 pos)
Create a new node in the graph.
NodeAnnotationsManager & GetNodeAnnotations()
Get node annotations manager (non-const)
NodeData * GetNode(NodeId id)
Get a node by ID.
bool UpdateNodePosition(NodeId id, Vector2 newPos)
Update node position.
const std::vector< NodeData > & GetNodes() const
bool HasCycles() const
Check if the graph has cycles.
bool ValidateGraph(std::string &errorMessage) const
Validate the graph structure.
std::vector< NodeData > m_nodes
const BlackboardSystem & GetBlackboard() const
Get blackboard system (const)
BlackboardSystem & GetBlackboard()
Get blackboard system (non-const)
Manages per-node annotations (breakpoints, comments, colors)
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json