Enzo
Loading...
Searching...
No Matches
NetworkManager.h
1#pragma once
2#include "Engine/Core/Types.h"
3#include "Engine/Network/Network.h"
4#include "Engine/Network/Node.h"
5#include "Engine/Network/Scope.h"
6#include "Engine/Network/UpdateLock.h"
7#include "Engine/NetworkGraph/NetworkGraph.h"
8#include "Engine/UndoRedo/UndoStack.h"
9#include <optional>
10#include <vector>
11
12namespace enzo {
13class NetworkPath;
14}
15
16namespace enzo::nt {
31{
32 public:
34 nt::Network& network() { return network_; }
35
37 auto nodes() { return network_.nodes(); }
38
40 NetworkManager(const NetworkManager& obj) = delete;
41
44
65 const std::string& typeName,
66 const Path& parent = Path("/"),
67 const std::string& name = "",
68 Vector2 position = {0.f, 0.f}
69 );
70
73 Node* getNodeAtPath(const Path& path) { return network_.getNodeAtPath(path); }
74
77 Scope* getScope(const Path& path) { return network_.getScope(path); }
78
81 std::vector<NodeId> getChildNodeIds(const Path& scope)
82 {
83 return network_.getChildNodeIds(scope);
84 }
85
90 std::optional<NodeId> getDisplayNode();
91
95
100 void update();
101
106 bool isValidNode(nt::NodeId nodeId);
107
111 Node& getNode(nt::NodeId nodeId);
112
116 void setDisplayNode(NodeId nodeId);
117
121 void clearDisplayFlag();
122
129 std::optional<NodeId> getPrimaryNode();
130
134 void setPrimaryNode(NodeId nodeId);
135
139 void clearPrimaryNode();
140
143 floatT getFrame() const { return frame_; }
144
146 void setFrame(floatT frame);
147
149 intT getStartFrame() const { return startFrame_; }
150
152 intT getEndFrame() const { return endFrame_; }
153
156 void setStartFrame(intT frame);
157
160 void setEndFrame(intT frame);
161
163 floatT getFps() const { return fps_; }
164
167 void setFps(floatT fps);
168
171 floatT getTime() const { return (frame_ - 1) / fps_; }
172
181 void setSelectedNode(NodeId nodeId, bool selected, bool add = false);
182
186 const std::vector<enzo::nt::NodeId>& getSelectedNodes();
187
191 void setSelectedNodes(std::vector<enzo::nt::NodeId> nodeIds);
192
200 void moveNode(NodeId nodeId, Vector2 newPos, bool skipUndo = false);
201
210 void deleteNode(NodeId nodeId);
211
229 void createNodeWithId(
230 NodeId nodeId,
231 const nt::NodeType& nodeType,
232 const Path& path,
233 Vector2 position
234 );
235
239 void clear();
240
245 void cook(enzo::nt::NodeId nodeId);
246
248 nt::NetworkGraph& graph() { return network_.graph(); }
249
256 unsigned int getInputCount(NodeId nodeId);
257
265 NodeId inputNodeId,
266 unsigned int inputIndex,
267 NodeId outputNodeId,
268 unsigned int outputIndex
269 );
270
274 void disconnectNodes(const nt::NodeLink& nodeLink);
275
278 Node* findNode(const NetworkPath& path, NodeId fromNode = nullNode)
279 {
280 return network_.findNode(path, fromNode);
281 }
282
285 std::weak_ptr<prm::NodeParameter>
286 findParameter(const NetworkPath& path, NodeId fromNode = nullNode)
287 {
288 return network_.findParameter(path, fromNode);
289 }
290
294 // @brief A signal emitted when the display node is changed
295 boost::signals2::signal<void(std::optional<nt::NodeId>)> displayNodeChanged;
296
297 // @brief A signal emitted when the geometry to be displayed is changed
298 // This is different to #displayNodeChanged because the state of geometry
299 // in a node can change based on parameters or other factors.
300 boost::signals2::signal<void(std::shared_ptr<const enzo::NodePacket>)> displayGeoChanged;
301
302 // @brief A signal emitted when the selected node's geometry is changed
303 boost::signals2::signal<void(std::shared_ptr<const enzo::NodePacket>)> selectedGeoChanged;
304
305 // @brief A signal emitted when the primary node changes
306 boost::signals2::signal<void(std::optional<nt::NodeId>)> primaryNodeChanged;
307
308 // @brief A signal emitted when the primary node's geometry changes
309 boost::signals2::signal<void(std::shared_ptr<const enzo::NodePacket>)> primaryGeoChanged;
310
311 // @brief A signal emitted when the selection of nodes changes
312 boost::signals2::signal<void(std::vector<nt::NodeId> selectedNodeIds)> selectedNodesChanged;
313
314 // @brief A signal emitted when a new node is created in the network
315 boost::signals2::signal<void(nt::NodeId)> nodeCreated;
316
317 // @brief A signal emitted when a node is about to be removed from the network
318 boost::signals2::signal<void(nt::NodeId)> nodeRemoved;
319
320 // @brief A signal emitted when a node link is created between two nodes
321 boost::signals2::signal<void(nt::NodeLink)> nodeLinkCreated;
322
323 // @brief A signal emitted when a node link is removed between two nodes
324 boost::signals2::signal<void(nt::NodeLink)> nodeLinkRemoved;
325
326 // @brief A signal emitted when the network is cleared
327 boost::signals2::signal<void()> networkCleared;
328
329 // @brief A signal emitted when a node's position changes programmatically (e.g. undo/redo)
330 boost::signals2::signal<void(nt::NodeId, Vector2)> nodePositionChanged;
331
332 // @brief A signal emitted when the scene moves to a different frame
333 boost::signals2::signal<void(floatT frame)> frameChanged;
334
335 // @brief A signal emitted when the playback range changes
336 boost::signals2::signal<void(intT startFrame, intT endFrame)> frameRangeChanged;
337
338 // @brief A signal emitted when the playback rate changes
339 boost::signals2::signal<void(floatT fps)> fpsChanged;
342 UndoStack& undoStack() { return undoStack_; }
343
347 void _reset();
348
349 private:
350 NetworkManager() {};
351
352 // functions
354 void disconnectNode(NodeId nodeId);
355
357 void openInputGap(NodeId nodeId, unsigned int fromIndex);
358
360 void closeInputGap(NodeId nodeId, unsigned int fromIndex);
361
363 void moveInput(const nt::NodeLink& nodeLink, unsigned int inputIndex);
364
368 void onNodeDirtied(nt::NodeId nodeId, bool dirtyDependents);
369
372 void dirtyUnits_(const std::vector<nt::Unit>& units);
373
375 void dirtyTimeDependents_();
376
378 void resetTime_();
379
380 // variables
381 // every node, its wiring, and the scopes they live in
382 nt::Network network_;
383 std::vector<enzo::nt::NodeId> selectedNodes_;
384 // node selected for displaying in the viewport
385 std::optional<NodeId> displayNode_ = std::nullopt;
386 // the primary node that drives the parameter and geometry panes
387 std::optional<NodeId> primaryNode_ = std::nullopt;
388
389 // the frame the scene sits on, and the range playback runs over
390 floatT frame_ = 1;
391 intT startFrame_ = 1;
392 intT endFrame_ = 240;
393 floatT fps_ = 24;
394
395 UndoStack undoStack_;
396};
397
399} // namespace enzo::nt
Basic attribute, parameter, and node types for Enzo.
uint64_t NodeId
The unique ID assigned to each node in the network.
Definition Types.h:335
A path that references a network entity, either a node or a parameter on a node.
Definition NetworkPath.h:20
A default path class for creation and manipulation of tree-based paths.
Definition Path.h:14
The single owner of the network's wiring and dependencies.
Definition NetworkGraph.h:24
The central coordinator of the engine's node system.
Definition NetworkManager.h:31
void setEndFrame(intT frame)
Sets the frame the playback range ends on.
Definition NetworkManager.cpp:201
std::optional< NodeId > getPrimaryNode()
Returns the node ID of the primary node, or nullopt when none.
Definition NetworkManager.cpp:160
void setDisplayNode(NodeId nodeId)
Sets given NodeId to be displayed, releasing previous display Node.
Definition NetworkManager.cpp:142
bool isValidNode(nt::NodeId nodeId)
Returns whether the node exists in the network and is valid.
Definition NetworkManager.cpp:140
intT getStartFrame() const
Returns the first frame of the playback range.
Definition NetworkManager.h:149
Node * findNode(const NetworkPath &path, NodeId fromNode=nullNode)
Resolves a node reference such as "grid1" or "../grid1" to its node.
Definition NetworkManager.h:278
auto nodes()
Returns an iterable range over all nodes in the network.
Definition NetworkManager.h:37
NodeId createNode(const std::string &typeName, const Path &parent=Path("/"), const std::string &name="", Vector2 position={0.f, 0.f})
Creates a new node inside a scope.
Definition NetworkManager.cpp:22
void clearPrimaryNode()
Clears the primary node so none is primary.
Definition NetworkManager.cpp:173
Node & getNode(nt::NodeId nodeId)
Returns a reference to the Node with the given NodeId.
Definition NetworkManager.cpp:138
void cook(enzo::nt::NodeId nodeId)
Cooks the given node.
Definition NetworkManager.cpp:327
Node * getNodeAtPath(const Path &path)
Returns the node at an exact path, or null when no node is there.
Definition NetworkManager.h:73
floatT getTime() const
Returns the current frame as seconds, where the start of frame 1 is zero.
Definition NetworkManager.h:171
void setStartFrame(intT frame)
Sets the frame the playback range starts on.
Definition NetworkManager.cpp:191
enzo::nt::UpdateLock lockUpdates()
Creates a lock object that prevents cooking until destroyed.
Definition NetworkManager.cpp:265
const std::vector< enzo::nt::NodeId > & getSelectedNodes()
Returns the NodeIds for all selected nodes.
Definition NetworkManager.cpp:299
void setSelectedNode(NodeId nodeId, bool selected, bool add=false)
Set the selection state for the given node.
Definition NetworkManager.cpp:230
NetworkManager(const NetworkManager &obj)=delete
Deleted the copy constructor for singleton.
void moveNode(NodeId nodeId, Vector2 newPos, bool skipUndo=false)
Moves a node to a new position, pushing an undo command.
Definition NetworkManager.cpp:46
nt::NodeLink connectNodes(NodeId inputNodeId, unsigned int inputIndex, NodeId outputNodeId, unsigned int outputIndex)
Wires one node's output into another node's input.
Definition NetworkManager.cpp:358
void clearDisplayFlag()
Clears the display flag so no node is displayed.
Definition NetworkManager.cpp:153
floatT getFrame() const
Returns the frame the scene sits on.
Definition NetworkManager.h:143
unsigned int getInputCount(NodeId nodeId)
Returns how many inputs a node currently takes.
Definition NetworkManager.cpp:345
void setSelectedNodes(std::vector< enzo::nt::NodeId > nodeIds)
Replaces the entire selection with the given set of nodes.
Definition NetworkManager.cpp:301
void deleteNode(NodeId nodeId)
Deletes a node, pushing an undo command.
Definition NetworkManager.cpp:60
intT getEndFrame() const
Returns the last frame of the playback range.
Definition NetworkManager.h:152
void _reset()
For use in unit tests, resets the state of the node.
Definition NetworkManager.cpp:495
void createNodeWithId(NodeId nodeId, const nt::NodeType &nodeType, const Path &path, Vector2 position)
Creates a node with an identity the caller dictates rather than one picked here.
Definition NetworkManager.cpp:91
Scope * getScope(const Path &path)
Returns the scope at a path, or null when no scope sits there.
Definition NetworkManager.h:77
void setFrame(floatT frame)
Moves the scene to a frame, clamped to the playback range.
Definition NetworkManager.cpp:180
void setFps(floatT fps)
Sets how many frames make up a second of playback.
Definition NetworkManager.cpp:210
nt::Network & network()
Returns the network holding every node, its wiring, and its scopes.
Definition NetworkManager.h:34
void clear()
Clears all nodes and resets the network to its initial state.
Definition NetworkManager.cpp:315
nt::NetworkGraph & graph()
Returns the graph that owns the network's wiring and dependencies.
Definition NetworkManager.h:248
floatT getFps() const
Returns how many frames make up a second of playback.
Definition NetworkManager.h:163
void update()
Cooks dirtied nodes, is called automatically.
Definition NetworkManager.cpp:267
std::weak_ptr< prm::NodeParameter > findParameter(const NetworkPath &path, NodeId fromNode=nullNode)
Resolves a parameter reference such as "grid1.tx" to its parameter.
Definition NetworkManager.h:286
std::vector< NodeId > getChildNodeIds(const Path &scope)
Returns the ids of the nodes living directly inside a scope, in no particular order.
Definition NetworkManager.h:81
std::optional< NodeId > getDisplayNode()
Returns the node ID for the node with its display flag set. There can only be only be one node displa...
Definition NetworkManager.cpp:463
void disconnectNodes(const nt::NodeLink &nodeLink)
Removes a node link between two nodes.
Definition NetworkManager.cpp:399
void setPrimaryNode(NodeId nodeId)
Sets the given NodeId as the primary node, releasing the previous one.
Definition NetworkManager.cpp:162
static NetworkManager & getInstance()
Returns a reference to the singleton instance.
Definition NetworkManager.cpp:132
Every node in a scene, their wiring, and the scopes they live in.
Definition Network.h:30
Node * getNodeAtPath(const Path &path)
Returns the node at an exact path, or null when no node is there.
Definition Network.cpp:26
std::vector< NodeId > getChildNodeIds(const Path &scope)
Returns the ids of the nodes living directly inside a scope, in no particular order.
Definition Network.cpp:35
NetworkGraph & graph()
Returns the wiring and dependencies between the nodes.
Definition Network.h:103
std::weak_ptr< prm::NodeParameter > findParameter(const NetworkPath &path, NodeId fromNode=nullNode)
Resolves a parameter reference to its parameter.
Definition Network.cpp:102
Node * findNode(const NetworkPath &path, NodeId fromNode=nullNode)
Resolves a node reference to its node.
Definition Network.cpp:88
Scope * getScope(const Path &path)
Returns the scope at a path, or null when no scope sits there.
Definition Network.cpp:64
auto nodes()
Returns an iterable range over every node, yielding {NodeId, Node&} pairs.
Definition Network.h:43
The unique runtime representation of a node.
Definition Node.h:18
One place nodes live, either the root or the inside of a node that holds one.
Definition Scope.h:26
Definition UndoStack.h:11
Prevents updates on the network manager while in scope.
Definition UpdateLock.h:15
Everything every node of one kind shares.
Definition NodeType.h:40