Enzo
Loading...
Searching...
No Matches
NetworkManager.h
1#pragma once
2#include "Engine/Network/UpdateLock.h"
3#include "Engine/Operator/GeometryOperator.h"
4#include "Engine/UndoRedo/UndoStack.h"
5#include "Engine/Types.h"
6#include <memory>
7#include <unordered_map>
8#include <QObject>
9
10namespace enzo::nt {
26: public QObject
27{
28 // TODO: make not Q_OBJECT
29 Q_OBJECT
30public:
31
34 using Map = std::unordered_map<OpId, std::unique_ptr<GeometryOperator>>;
35 Map& map_;
36 public:
37 class Iterator {
38 Map::iterator it_;
39 public:
40 using value_type = std::pair<const OpId, GeometryOperator&>;
41 using reference = value_type;
42 using difference_type = std::ptrdiff_t;
43 using iterator_category = std::forward_iterator_tag;
44
45 Iterator(Map::iterator it) : it_(it) {}
46 reference operator*() const { return {it_->first, *it_->second}; }
47 Iterator& operator++() { ++it_; return *this; }
48 Iterator operator++(int) { Iterator tmp = *this; ++it_; return tmp; }
49 bool operator==(const Iterator& other) const { return it_ == other.it_; }
50 bool operator!=(const Iterator& other) const { return it_ != other.it_; }
51 };
52
53 OperatorRange(Map& map) : map_(map) {}
54 Iterator begin() { return Iterator(map_.begin()); }
55 Iterator end() { return Iterator(map_.end()); }
56 std::size_t size() const { return map_.size(); }
57 };
58
60 OperatorRange operators() { return OperatorRange(gopStore_); }
61
63 NetworkManager(const NetworkManager& obj) = delete;
64
67
77 OpId createOperator(op::OpInfo opInfo, bt::Vector2f position = {0.f, 0.f});
78
83 std::optional<OpId> getDisplayOp();
84
88
93 void update();
94
99 bool isValidOp(nt::OpId opId);
100
105
109 void setDisplayOp(OpId opId);
110
114 void clearDisplayFlag();
115
124 void setSelectedNode(OpId opId, bool selected, bool add=false);
125
129 const std::vector<enzo::nt::OpId>& getSelectedNodes();
130
134 void setSelectedNodes(std::vector<enzo::nt::OpId> opIds);
135
143 void moveNode(OpId opId, bt::Vector2f newPos, bool skipUndo=false);
144
149 void deleteNode(OpId opId);
150
155 void removeOperator(OpId opId);
156
167 void restoreOperator(OpId opId, op::OpInfo opInfo);
168
172 void clear();
173
178 void cookOp(enzo::nt::OpId opId);
179
183 // @brief A signal emitted when the display node is changed
184 boost::signals2::signal<void (std::optional<nt::OpId>)> displayNodeChanged;
185
186 // @brief A signal emitted when the geometry to be displayed is changed
187 // This is different to #displayNodeChanged because the state of geometry
188 // in a node can change based on parameters or other factors.
189 boost::signals2::signal<void (std::shared_ptr<const enzo::NodePacket>)> displayGeoChanged;
190
191 // @brief A signal emitted when the selected node's geometry is changed
192 boost::signals2::signal<void (std::shared_ptr<const enzo::NodePacket>)> selectedGeoChanged;
193
194 // @brief A signal emitted when the selection of nodes changes
195 boost::signals2::signal<void (std::vector<nt::OpId> selectedNodeIds)> selectedNodesChanged;
196
197 // @brief A signal emitted when a new operator is created in the network
198 boost::signals2::signal<void (nt::OpId)> operatorCreated;
199
200 // @brief A signal emitted when an operator is about to be removed from the network
201 boost::signals2::signal<void (nt::OpId)> operatorRemoved;
202
203 // @brief A signal emitted when a connection is created between two operators
204 boost::signals2::signal<void (std::weak_ptr<nt::GeometryConnection>)> connectionCreated;
205
206 // @brief A signal emitted when the network is cleared
207 boost::signals2::signal<void ()> networkCleared;
208
209 // @brief A signal emitted when a node's position changes programmatically (e.g. undo/redo)
210 boost::signals2::signal<void (nt::OpId, bt::Vector2f)> nodePositionChanged;
213 UndoStack& undoStack() { return undoStack_; }
214
215 #ifdef UNIT_TEST
217 void _reset();
218 #endif
219
220private:
221 NetworkManager() {};
222
223 // functions
224 std::vector<enzo::nt::OpId> getDependencyGraph(enzo::nt::OpId opId);
225 std::vector<enzo::nt::OpId> getDependentsGraph(enzo::nt::OpId opId);
226
230 void onNodeDirtied(nt::OpId opId, bool dirtyDependents);
231
232 // variables
233 // store for geometry operators
234 std::vector<enzo::nt::OpId> selectedNodes_;
235 std::unordered_map<enzo::nt::OpId, std::unique_ptr<enzo::nt::GeometryOperator>> gopStore_;
236 // the highest operator id currently stored
237 enzo::nt::OpId maxOpId_=0;
238 // operator selected for displaying in the viewport
239 std::optional<OpId> displayOp_=std::nullopt;
240
241 UndoStack undoStack_;
242
243};
244
245inline enzo::nt::NetworkManager& nm() {
247}
248}
Basic attribute, parameter, and node types for Enzo.
uint64_t OpId
The unique ID assigned to each node in the network.
Definition Types.h:87
The unique runtime representation of a node.
Definition GeometryOperator.h:20
Iterable range over operators, yields {OpId, GeometryOperator&} pairs.
Definition NetworkManager.h:33
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:69
void removeOperator(OpId opId)
Removes an operator and all its connections from the network.
Definition NetworkManager.cpp:84
void setSelectedNodes(std::vector< enzo::nt::OpId > opIds)
Replaces the entire selection with the given set of nodes.
Definition NetworkManager.cpp:247
NetworkManager(const NetworkManager &obj)=delete
Deleted the copy constructor for singleton.
bool isValidOp(nt::OpId opId)
Returns whether the node exists in the network and is valid.
Definition NetworkManager.cpp:150
void clearDisplayFlag()
Clears the display flag so no node is displayed.
Definition NetworkManager.cpp:171
void setSelectedNode(OpId opId, bool selected, bool add=false)
Set the selection state for the given node.
Definition NetworkManager.cpp:178
void setDisplayOp(OpId opId)
Sets given OpId to be displayed, releasing previous display Node.
Definition NetworkManager.cpp:160
void cookOp(enzo::nt::OpId opId)
Cooks the given operator.
Definition NetworkManager.cpp:272
void deleteNode(OpId opId)
Deletes a node, pushing an undo command.
Definition NetworkManager.cpp:59
OperatorRange operators()
Returns an iterable range over all operators in the network.
Definition NetworkManager.h:60
const std::vector< enzo::nt::OpId > & getSelectedNodes()
Returns the OpIds for all selected nodes.
Definition NetworkManager.cpp:242
static NetworkManager & getInstance()
Returns a reference to the singleton instance.
Definition NetworkManager.cpp:134
OpId createOperator(op::OpInfo opInfo, bt::Vector2f position={0.f, 0.f})
Creates a new node in the network.
Definition NetworkManager.cpp:20
enzo::nt::UpdateLock lockUpdates()
Creates a lock object that prevents cooking until destroyed.
Definition NetworkManager.cpp:214
void clear()
Clears all operators and resets the network to its initial state.
Definition NetworkManager.cpp:261
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:340
void update()
Cooks dirtied nodes, is called automatically.
Definition NetworkManager.cpp:219
GeometryOperator & getGeoOperator(nt::OpId opId)
Returns a reference to the GeometryOperator with the given OpId.
Definition NetworkManager.cpp:140
void moveNode(OpId opId, bt::Vector2f newPos, bool skipUndo=false)
Moves a node to a new position, pushing an undo command.
Definition NetworkManager.cpp:45
Definition UndoStack.h:10
Prevents updates on the network manager while in scope.
Definition UpdateLock.h:16
Definition OpInfo.h:20