Enzo
Loading...
Searching...
No Matches
NetworkManager.h
1#pragma once
2#include "Engine/Core/Types.h"
3#include "Engine/Network/GeometryOperator.h"
4#include "Engine/Network/UpdateLock.h"
5#include "Engine/NetworkGraph/NetworkGraph.h"
6#include "Engine/UndoRedo/UndoStack.h"
7#include <memory>
8#include <unordered_map>
9
10namespace enzo {
11class NetworkPath;
12}
13
14namespace enzo::nt {
27{
28 public:
31 {
32 using Map = std::unordered_map<OpId, std::unique_ptr<GeometryOperator>>;
33 Map& map_;
34
35 public:
37 {
38 Map::iterator it_;
39
40 public:
41 using value_type = std::pair<const OpId, GeometryOperator&>;
42 using reference = value_type;
43 using difference_type = std::ptrdiff_t;
44 using iterator_category = std::forward_iterator_tag;
45
46 Iterator(Map::iterator it) : it_(it) {}
47 reference operator*() const { return {it_->first, *it_->second}; }
48 Iterator& operator++()
49 {
50 ++it_;
51 return *this;
52 }
53 Iterator operator++(int)
54 {
55 Iterator tmp = *this;
56 ++it_;
57 return tmp;
58 }
59 bool operator==(const Iterator& other) const { return it_ == other.it_; }
60 bool operator!=(const Iterator& other) const { return it_ != other.it_; }
61 };
62
63 OperatorRange(Map& map) : map_(map) {}
64 Iterator begin() { return Iterator(map_.begin()); }
65 Iterator end() { return Iterator(map_.end()); }
66 std::size_t size() const { return map_.size(); }
67 };
68
70 OperatorRange operators() { return OperatorRange(gopStore_); }
71
73 NetworkManager(const NetworkManager& obj) = delete;
74
77
92 OpId
93 createOperator(op::OpInfo opInfo, const std::string& path = "", Vector2 position = {0.f, 0.f});
94
99 std::optional<OpId> getDisplayOp();
100
104
109 void update();
110
115 bool isValidOp(nt::OpId opId);
116
121
125 void setDisplayOp(OpId opId);
126
130 void clearDisplayFlag();
131
140 void setSelectedNode(OpId opId, bool selected, bool add = false);
141
145 const std::vector<enzo::nt::OpId>& getSelectedNodes();
146
150 void setSelectedNodes(std::vector<enzo::nt::OpId> opIds);
151
159 void moveNode(OpId opId, Vector2 newPos, bool skipUndo = false);
160
165 void deleteNode(OpId opId);
166
176 void removeOperator(OpId opId, bool removeConnections = true);
177
189 void restoreOperator(OpId opId, op::OpInfo opInfo);
190
194 void clear();
195
200 void cookOp(enzo::nt::OpId opId);
201
203 nt::NetworkGraph& graph() { return graph_; }
204
209 OpId inputOpId,
210 unsigned int inputIndex,
211 OpId outputOpId,
212 unsigned int outputIndex
213 );
214
216 void disconnectNodes(const nt::Connection& connection);
217
222 GeometryOperator* findOperator(const NetworkPath& path, OpId fromOp = nullOp);
223
228 std::weak_ptr<prm::NodeParameter> findParameter(const NetworkPath& path, OpId fromOp = nullOp);
229
233 // @brief A signal emitted when the display node is changed
234 boost::signals2::signal<void(std::optional<nt::OpId>)> displayNodeChanged;
235
236 // @brief A signal emitted when the geometry to be displayed is changed
237 // This is different to #displayNodeChanged because the state of geometry
238 // in a node can change based on parameters or other factors.
239 boost::signals2::signal<void(std::shared_ptr<const enzo::NodePacket>)> displayGeoChanged;
240
241 // @brief A signal emitted when the selected node's geometry is changed
242 boost::signals2::signal<void(std::shared_ptr<const enzo::NodePacket>)> selectedGeoChanged;
243
244 // @brief A signal emitted when the selection of nodes changes
245 boost::signals2::signal<void(std::vector<nt::OpId> selectedNodeIds)> selectedNodesChanged;
246
247 // @brief A signal emitted when a new operator is created in the network
248 boost::signals2::signal<void(nt::OpId)> operatorCreated;
249
250 // @brief A signal emitted when an operator is about to be removed from the network
251 boost::signals2::signal<void(nt::OpId)> operatorRemoved;
252
253 // @brief A signal emitted when a connection is created between two operators
254 boost::signals2::signal<void(nt::Connection)> connectionCreated;
255
256 // @brief A signal emitted when a connection is removed between two operators
257 boost::signals2::signal<void(nt::Connection)> connectionRemoved;
258
259 // @brief A signal emitted when the network is cleared
260 boost::signals2::signal<void()> networkCleared;
261
262 // @brief A signal emitted when a node's position changes programmatically (e.g. undo/redo)
263 boost::signals2::signal<void(nt::OpId, Vector2)> nodePositionChanged;
266 UndoStack& undoStack() { return undoStack_; }
267
271 void _reset();
272
273 private:
274 NetworkManager() {};
275
276 // functions
278 void disconnectOperator(OpId opId);
279
283 void onNodeDirtied(nt::OpId opId, bool dirtyDependents);
284
285 // variables
286 // store for geometry operators
287 std::vector<enzo::nt::OpId> selectedNodes_;
288 std::unordered_map<enzo::nt::OpId, std::unique_ptr<enzo::nt::GeometryOperator>> gopStore_;
289 // the highest operator id currently stored
290 enzo::nt::OpId maxOpId_ = 0;
291 // operator selected for displaying in the viewport
292 std::optional<OpId> displayOp_ = std::nullopt;
293 // owns the network's wiring and dependencies
294 nt::NetworkGraph graph_;
295
296 UndoStack undoStack_;
297};
298
300} // namespace enzo::nt
Basic attribute, parameter, and node types for Enzo.
uint64_t OpId
The unique ID assigned to each node in the network.
Definition Types.h:137
A path that references a network entity, either a node or a parameter on a node.
Definition NetworkPath.h:20
The unique runtime representation of a node.
Definition GeometryOperator.h:16
The single owner of the network's wiring and dependencies.
Definition NetworkGraph.h:24
Iterable range over operators, yields {OpId, GeometryOperator&} pairs.
Definition NetworkManager.h:31
The central coordinator of the engine's node system.
Definition NetworkManager.h:27
void restoreOperator(OpId opId, op::OpInfo opInfo)
Restores a previously removed operator with a specific OpId.
Definition NetworkManager.cpp:74
nt::Connection connectNodes(OpId inputOpId, unsigned int inputIndex, OpId outputOpId, unsigned int outputIndex)
Wires one node's output into another node's input.
Definition NetworkManager.cpp:312
std::weak_ptr< prm::NodeParameter > findParameter(const NetworkPath &path, OpId fromOp=nullOp)
Resolves a parameter reference to its parameter.
Definition NetworkManager.cpp:172
OpId createOperator(op::OpInfo opInfo, const std::string &path="", Vector2 position={0.f, 0.f})
Creates a new node in the network.
Definition NetworkManager.cpp:20
void setSelectedNodes(std::vector< enzo::nt::OpId > opIds)
Replaces the entire selection with the given set of nodes.
Definition NetworkManager.cpp:271
std::optional< OpId > getDisplayOp()
Returns the operator ID for the node with its display flag set. There can only be only be one operato...
Definition NetworkManager.cpp:367
void removeOperator(OpId opId, bool removeConnections=true)
Removes an operator from the network.
Definition NetworkManager.cpp:87
void moveNode(OpId opId, Vector2 newPos, bool skipUndo=false)
Moves a node to a new position, pushing an undo command.
Definition NetworkManager.cpp:42
GeometryOperator * findOperator(const NetworkPath &path, OpId fromOp=nullOp)
Resolves a node reference to its operator.
Definition NetworkManager.cpp:153
enzo::nt::UpdateLock lockUpdates()
Creates a lock object that prevents cooking until destroyed.
Definition NetworkManager.cpp:245
NetworkManager(const NetworkManager &obj)=delete
Deleted the copy constructor for singleton.
const std::vector< enzo::nt::OpId > & getSelectedNodes()
Returns the OpIds for all selected nodes.
Definition NetworkManager.cpp:269
bool isValidOp(nt::OpId opId)
Returns whether the node exists in the network and is valid.
Definition NetworkManager.cpp:182
void clearDisplayFlag()
Clears the display flag so no node is displayed.
Definition NetworkManager.cpp:203
void setSelectedNode(OpId opId, bool selected, bool add=false)
Set the selection state for the given node.
Definition NetworkManager.cpp:210
void setDisplayOp(OpId opId)
Sets given OpId to be displayed, releasing previous display Node.
Definition NetworkManager.cpp:192
void _reset()
For use in unit tests, resets the state of the operator.
Definition NetworkManager.cpp:386
void cookOp(enzo::nt::OpId opId)
Cooks the given operator.
Definition NetworkManager.cpp:297
void disconnectNodes(const nt::Connection &connection)
Removes a wired connection between two nodes.
Definition NetworkManager.cpp:345
void deleteNode(OpId opId)
Deletes a node, pushing an undo command.
Definition NetworkManager.cpp:56
OperatorRange operators()
Returns an iterable range over all operators in the network.
Definition NetworkManager.h:70
GeometryOperator & getGeoOperator(nt::OpId opId)
Returns a reference to the GeometryOperator with the given OpId.
Definition NetworkManager.cpp:141
void clear()
Clears all operators and resets the network to its initial state.
Definition NetworkManager.cpp:285
nt::NetworkGraph & graph()
Returns the graph that owns the network's wiring and dependencies.
Definition NetworkManager.h:203
void update()
Cooks dirtied nodes, is called automatically.
Definition NetworkManager.cpp:247
static NetworkManager & getInstance()
Returns a reference to the singleton instance.
Definition NetworkManager.cpp:135
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
Definition OpInfo.h:19