Enzo
Loading...
Searching...
No Matches
DeleteNodeCommand.h
1#pragma once
2
3#include "Engine/Core/Types.h"
4#include "Engine/Network/GeometryOperator.h"
5#include "Engine/Network/NetworkManager.h"
6#include "Engine/Network/OperatorTable.h"
7#include "Engine/UndoRedo/UndoCommand.h"
8#include <string>
9#include <vector>
10
11namespace enzo::nt {
12
14{
15
16 struct SavedParameter
17 {
18 std::string name;
19 prm::PrmValues values;
20 };
21
22 public:
23 DeleteNodeCommand(OpId opId) : opId_(opId)
24 {
25 GeometryOperator& op = nm().getGeoOperator(opId_);
26 typeName_ = op.getType().getName();
27 position_ = op.getPosition();
28
29 // Save parms
30 savedParms_ = std::vector<SavedParameter>();
31 for (auto weakPrm : op.getParameters())
32 {
33 if (auto prm = weakPrm.lock())
34 {
35 savedParms_.push_back({prm->getName(), prm->getValues()});
36 }
37 }
38 }
39
40 void undo() override
41 {
42 // Restore operator
43 auto opInfo = op::OperatorTable::getOpInfo(typeName_);
44 nm().restoreOperator(opId_, opInfo.value());
45
46 GeometryOperator& op = nm().getGeoOperator(opId_);
47
48 // Restore position
49 nm().moveNode(opId_, position_, true);
50
51 // Restore parms
52 for (const auto& saved : savedParms_)
53 {
54 if (auto prm = op.getParameter(saved.name).lock())
55 {
56 prm->setValues(saved.values);
57 }
58 }
59 }
60
61 void redo() override { nm().removeOperator(opId_, false); }
62
63 UndoCommandType type() const override { return UndoCommandType::DeleteNode; }
64
65 private:
66 OpId opId_;
67 std::string typeName_;
68 Vector2 position_;
69 std::vector<SavedParameter> savedParms_;
70};
71
72} // 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
Definition DeleteNodeCommand.h:14
The unique runtime representation of a node.
Definition GeometryOperator.h:16
const op::OpInfo & getType() const
Returns the static type definition this node was created from.
Definition GeometryOperator.cpp:163
std::vector< std::weak_ptr< prm::NodeParameter > > getParameters()
Returns all parameters belonging to this node.
Definition GeometryOperator.cpp:151
Vector2 getPosition() const
Returns the node's position in the network graph.
Definition GeometryOperator.h:116
std::weak_ptr< prm::NodeParameter > getParameter(std::string_view parameterName)
Returns a parameter with the given name belonging to this node.
Definition GeometryOperator.cpp:107
void restoreOperator(OpId opId, op::OpInfo opInfo)
Restores a previously removed operator with a specific OpId.
Definition NetworkManager.cpp:74
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 & getGeoOperator(nt::OpId opId)
Returns a reference to the GeometryOperator with the given OpId.
Definition NetworkManager.cpp:141
Definition UndoCommand.h:8
const std::string & getName() const
Returns the internal type name shared by all nodes of this type (eg. "copy_to_points")
Definition OpInfo.h:30