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
66 const nt::NodeType& nodeType,
67 const Path& parent = Path("/"),
68 const std::string& name = "",
69 Vector2 position = {0.f, 0.f}
70 );
71
74 Node* getNodeAtPath(const Path& path) { return network_.getNodeAtPath(path); }
75
78 Scope* getScope(const Path& path) { return network_.getScope(path); }
79
82 std::vector<NodeId> getChildNodeIds(const Path& scope)
83 {
84 return network_.getChildNodeIds(scope);
85 }
86
91 std::optional<NodeId> getDisplayNode();
92
96
101 void update();
102
107 bool isValidNode(nt::NodeId nodeId);
108
112 Node& getNode(nt::NodeId nodeId);
113
117 void setDisplayNode(NodeId nodeId);
118
122 void clearDisplayFlag();
123
130 std::optional<NodeId> getPrimaryNode();
131
135 void setPrimaryNode(NodeId nodeId);
136
140 void clearPrimaryNode();
141
144 floatT getFrame() const { return frame_; }
145
147 void setFrame(floatT frame);
148
150 intT getStartFrame() const { return startFrame_; }
151
153 intT getEndFrame() const { return endFrame_; }
154
157 void setStartFrame(intT frame);
158
161 void setEndFrame(intT frame);
162
164 floatT getFps() const { return fps_; }
165
168 void setFps(floatT fps);
169
172 floatT getTime() const { return (frame_ - 1) / fps_; }
173
182 void setSelectedNode(NodeId nodeId, bool selected, bool add = false);
183
187 const std::vector<enzo::nt::NodeId>& getSelectedNodes();
188
192 void setSelectedNodes(std::vector<enzo::nt::NodeId> nodeIds);
193
201 void moveNode(NodeId nodeId, Vector2 newPos, bool skipUndo = false);
202
211 void deleteNode(NodeId nodeId);
212
230 void createNodeWithId(
231 NodeId nodeId,
232 const nt::NodeType& nodeType,
233 const Path& path,
234 Vector2 position
235 );
236
240 void clear();
241
246 void cook(enzo::nt::NodeId nodeId);
247
257 enzo::NodePacket cookOutput(enzo::nt::NodeId nodeId, unsigned int outputIndex);
258
260 nt::NetworkGraph& graph() { return network_.graph(); }
261
268 unsigned int getInputCount(NodeId nodeId);
269
277 NodeId inputNodeId,
278 unsigned int inputIndex,
279 NodeId outputNodeId,
280 unsigned int outputIndex
281 );
282
286 void disconnectNodes(const nt::Connection& connection);
287
290 Node* findNode(const NetworkPath& path, NodeId fromNode = nullNode)
291 {
292 return network_.findNode(path, fromNode);
293 }
294
297 std::weak_ptr<prm::NodeParameter>
298 findParameter(const NetworkPath& path, NodeId fromNode = nullNode)
299 {
300 return network_.findParameter(path, fromNode);
301 }
302
306 // @brief A signal emitted when the display node is changed
307 boost::signals2::signal<void(std::optional<nt::NodeId>)> displayNodeChanged;
308
309 // @brief A signal emitted when the geometry to be displayed is changed
310 // This is different to #displayNodeChanged because the state of geometry
311 // in a node can change based on parameters or other factors.
312 boost::signals2::signal<void(std::shared_ptr<const enzo::NodePacket>)> displayGeoChanged;
313
314 // @brief A signal emitted when the selected node's geometry is changed
315 boost::signals2::signal<void(std::shared_ptr<const enzo::NodePacket>)> selectedGeoChanged;
316
317 // @brief A signal emitted when the primary node changes
318 boost::signals2::signal<void(std::optional<nt::NodeId>)> primaryNodeChanged;
319
320 // @brief A signal emitted when the primary node's geometry changes
321 boost::signals2::signal<void(std::shared_ptr<const enzo::NodePacket>)> primaryGeoChanged;
322
323 // @brief A signal emitted when the selection of nodes changes
324 boost::signals2::signal<void(std::vector<nt::NodeId> selectedNodeIds)> selectedNodesChanged;
325
326 // @brief A signal emitted when a new node is created in the network
327 boost::signals2::signal<void(nt::NodeId)> nodeCreated;
328
329 // @brief A signal emitted when a node is about to be removed from the network
330 boost::signals2::signal<void(nt::NodeId)> nodeRemoved;
331
332 // @brief A signal emitted when a connection is created between two nodes
333 boost::signals2::signal<void(nt::Connection)> connectionCreated;
334
335 // @brief A signal emitted when a connection is removed between two nodes
336 boost::signals2::signal<void(nt::Connection)> connectionRemoved;
337
338 // @brief A signal emitted when the network is cleared
339 boost::signals2::signal<void()> networkCleared;
340
341 // @brief A signal emitted when a node's position changes programmatically (e.g. undo/redo)
342 boost::signals2::signal<void(nt::NodeId, Vector2)> nodePositionChanged;
343
344 // @brief A signal emitted when the scene moves to a different frame
345 boost::signals2::signal<void(floatT frame)> frameChanged;
346
347 // @brief A signal emitted when the playback range changes
348 boost::signals2::signal<void(intT startFrame, intT endFrame)> frameRangeChanged;
349
350 // @brief A signal emitted when the playback rate changes
351 boost::signals2::signal<void(floatT fps)> fpsChanged;
354 UndoStack& undoStack() { return undoStack_; }
355
359 void _reset();
360
361 private:
362 NetworkManager() {};
363
364 // functions
366 void disconnectNode(NodeId nodeId);
367
369 void openInputGap(NodeId nodeId, unsigned int fromIndex);
370
372 void closeInputGap(NodeId nodeId, unsigned int fromIndex);
373
375 void moveInput(const nt::Connection& connection, unsigned int inputIndex);
376
380 void onNodeDirtied(nt::NodeId nodeId, bool dirtyDependents);
381
384 void dirtyUnits_(const std::vector<nt::Unit>& units);
385
387 void dirtyTimeDependents_();
388
390 void resetTime_();
391
392 // variables
393 // every node, its wiring, and the scopes they live in
394 nt::Network network_;
395 std::vector<enzo::nt::NodeId> selectedNodes_;
396 // node selected for displaying in the viewport
397 std::optional<NodeId> displayNode_ = std::nullopt;
398 // the primary node that drives the parameter and geometry panes
399 std::optional<NodeId> primaryNode_ = std::nullopt;
400
401 // the frame the scene sits on, and the range playback runs over
402 floatT frame_ = 1;
403 intT startFrame_ = 1;
404 intT endFrame_ = 240;
405 floatT fps_ = 24;
406
407 UndoStack undoStack_;
408};
409
411} // 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:306
A path that references a network entity, either a node or a parameter on a node.
Definition NetworkPath.h:20
Definition NodePacket.h:9
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:193
std::optional< NodeId > getPrimaryNode()
Returns the node ID of the primary node, or nullopt when none.
Definition NetworkManager.cpp:152
void setDisplayNode(NodeId nodeId)
Sets given NodeId to be displayed, releasing previous display Node.
Definition NetworkManager.cpp:134
bool isValidNode(nt::NodeId nodeId)
Returns whether the node exists in the network and is valid.
Definition NetworkManager.cpp:132
intT getStartFrame() const
Returns the first frame of the playback range.
Definition NetworkManager.h:150
Node * findNode(const NetworkPath &path, NodeId fromNode=nullNode)
Resolves a node reference such as "grid1" or "../grid1" to its node.
Definition NetworkManager.h:290
auto nodes()
Returns an iterable range over all nodes in the network.
Definition NetworkManager.h:37
void clearPrimaryNode()
Clears the primary node so none is primary.
Definition NetworkManager.cpp:165
Node & getNode(nt::NodeId nodeId)
Returns a reference to the Node with the given NodeId.
Definition NetworkManager.cpp:130
void cook(enzo::nt::NodeId nodeId)
Cooks the given node.
Definition NetworkManager.cpp:319
Node * getNodeAtPath(const Path &path)
Returns the node at an exact path, or null when no node is there.
Definition NetworkManager.h:74
floatT getTime() const
Returns the current frame as seconds, where the start of frame 1 is zero.
Definition NetworkManager.h:172
void setStartFrame(intT frame)
Sets the frame the playback range starts on.
Definition NetworkManager.cpp:183
enzo::nt::UpdateLock lockUpdates()
Creates a lock object that prevents cooking until destroyed.
Definition NetworkManager.cpp:257
const std::vector< enzo::nt::NodeId > & getSelectedNodes()
Returns the NodeIds for all selected nodes.
Definition NetworkManager.cpp:291
void setSelectedNode(NodeId nodeId, bool selected, bool add=false)
Set the selection state for the given node.
Definition NetworkManager.cpp:222
enzo::NodePacket cookOutput(enzo::nt::NodeId nodeId, unsigned int outputIndex)
Returns a copy of one of a node's outputs.
Definition NetworkManager.cpp:337
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:38
void clearDisplayFlag()
Clears the display flag so no node is displayed.
Definition NetworkManager.cpp:145
floatT getFrame() const
Returns the frame the scene sits on.
Definition NetworkManager.h:144
unsigned int getInputCount(NodeId nodeId)
Returns how many inputs a node currently takes.
Definition NetworkManager.cpp:343
void setSelectedNodes(std::vector< enzo::nt::NodeId > nodeIds)
Replaces the entire selection with the given set of nodes.
Definition NetworkManager.cpp:293
void deleteNode(NodeId nodeId)
Deletes a node, pushing an undo command.
Definition NetworkManager.cpp:52
intT getEndFrame() const
Returns the last frame of the playback range.
Definition NetworkManager.h:153
void _reset()
For use in unit tests, resets the state of the node.
Definition NetworkManager.cpp:493
NodeId createNode(const nt::NodeType &nodeType, const Path &parent=Path("/"), const std::string &name="", Vector2 position={0.f, 0.f})
Creates a new node inside a scope.
Definition NetworkManager.cpp:20
void disconnectNodes(const nt::Connection &connection)
Removes a wired connection between two nodes.
Definition NetworkManager.cpp:397
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:83
Scope * getScope(const Path &path)
Returns the scope at a path, or null when no scope sits there.
Definition NetworkManager.h:78
void setFrame(floatT frame)
Moves the scene to a frame, clamped to the playback range.
Definition NetworkManager.cpp:172
void setFps(floatT fps)
Sets how many frames make up a second of playback.
Definition NetworkManager.cpp:202
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:307
nt::NetworkGraph & graph()
Returns the graph that owns the network's wiring and dependencies.
Definition NetworkManager.h:260
floatT getFps() const
Returns how many frames make up a second of playback.
Definition NetworkManager.h:164
void update()
Cooks dirtied nodes, is called automatically.
Definition NetworkManager.cpp:259
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:298
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:82
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:461
nt::Connection connectNodes(NodeId inputNodeId, unsigned int inputIndex, NodeId outputNodeId, unsigned int outputIndex)
Wires one node's output into another node's input.
Definition NetworkManager.cpp:356
void setPrimaryNode(NodeId nodeId)
Sets the given NodeId as the primary node, releasing the previous one.
Definition NetworkManager.cpp:154
static NetworkManager & getInstance()
Returns a reference to the singleton instance.
Definition NetworkManager.cpp:124
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
One wired link between two nodes, the ground truth of the network's wiring.
Definition Connection.h:20
Everything every node of one kind shares.
Definition NodeType.h:39