Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BPCommandSystem.h
Go to the documentation of this file.
1/*
2 * Olympe Blueprint Editor - Command System
3 *
4 * Command pattern infrastructure for undo/redo functionality
5 * All editing operations should be wrapped in commands
6 */
7
8#pragma once
9
10#include <string>
11#include <vector>
12#include <memory>
13#include "../../Source/third_party/nlohmann/json.hpp"
14
15// Forward declare ImVec2
16struct ImVec2;
17
18namespace Olympe
19{
20 namespace Blueprint
21 {
23
24 /**
25 * EditorCommand - Base class for all undoable editor commands
26 * Implements command pattern for undo/redo support
27 */
29 {
30 public:
31 virtual ~EditorCommand() = default;
32
33 // Execute the command (perform the operation)
34 virtual void Execute() = 0;
35
36 // Undo the command (revert the operation)
37 virtual void Undo() = 0;
38
39 // Get a human-readable description of the command
40 virtual std::string GetDescription() const = 0;
41 };
42
43 /**
44 * CommandStack - Manages undo/redo command history
45 * Maintains two stacks for undo and redo operations
46 */
48 {
49 public:
52
53 // Execute a new command and add to undo stack
54 void ExecuteCommand(std::unique_ptr<EditorCommand> cmd);
55
56 // Undo the last command
57 void Undo();
58
59 // Redo the last undone command
60 void Redo();
61
62 // Query state
63 bool CanUndo() const { return !m_UndoStack.empty(); }
64 bool CanRedo() const { return !m_RedoStack.empty(); }
65
66 // Get command information
67 const EditorCommand* GetLastCommand() const;
68 std::string GetLastCommandDescription() const;
69 std::string GetNextRedoDescription() const;
70
71 // Get stack contents for UI display
72 std::vector<std::string> GetUndoStackDescriptions() const;
73 std::vector<std::string> GetRedoStackDescriptions() const;
74
75 // Clear all history
76 void Clear();
77
78 // Get stack sizes
79 size_t GetUndoStackSize() const { return m_UndoStack.size(); }
80 size_t GetRedoStackSize() const { return m_RedoStack.size(); }
81
82 private:
83 std::vector<std::unique_ptr<EditorCommand>> m_UndoStack;
84 std::vector<std::unique_ptr<EditorCommand>> m_RedoStack;
85 size_t m_MaxStackSize; // Limit memory usage
86 };
87
88 // ========================================================================
89 // Concrete Command Classes
90 // ========================================================================
91
92 /**
93 * CreateNodeCommand - Command to create a new node in graph
94 */
96 {
97 public:
98 // Optional outCreatedId allows caller to receive the created node id
99 CreateNodeCommand(const std::string& graphId, const std::string& nodeType,
100 float posX, float posY, const std::string& nodeName = "", int* outCreatedId = nullptr);
101
102 void Execute() override;
103 void Undo() override;
104 std::string GetDescription() const override;
105
106 private:
107 std::string m_GraphId;
108 std::string m_NodeType;
109 std::string m_NodeName;
110 float m_PosX;
111 float m_PosY;
112 int m_CreatedNodeId; // Set during Execute
114 };
115
116 /**
117 * DeleteNodeCommand - Command to delete a node from graph
118 */
120 {
121 public:
122 DeleteNodeCommand(const std::string& graphId, int nodeId);
123
124 void Execute() override;
125 void Undo() override;
126 std::string GetDescription() const override;
127
128 private:
129 std::string m_GraphId;
131 json m_NodeData; // Saved node data for undo
132 };
133
134 /**
135 * MoveNodeCommand - Command to move a node's position
136 */
138 {
139 public:
140 MoveNodeCommand(const std::string& graphId, int nodeId,
141 float oldX, float oldY, float newX, float newY);
142
143 void Execute() override;
144 void Undo() override;
145 std::string GetDescription() const override;
146
147 private:
148 std::string m_GraphId;
152 };
153
154 /**
155 * LinkNodesCommand - Command to create a link between nodes
156 */
158 {
159 public:
160 LinkNodesCommand(const std::string& graphId, int parentId, int childId);
161
162 void Execute() override;
163 void Undo() override;
164 std::string GetDescription() const override;
165
166 private:
167 std::string m_GraphId;
170 };
171
172 /**
173 * UnlinkNodesCommand - Command to remove a link between nodes
174 */
176 {
177 public:
178 UnlinkNodesCommand(const std::string& graphId, int parentId, int childId);
179
180 void Execute() override;
181 void Undo() override;
182 std::string GetDescription() const override;
183
184 private:
185 std::string m_GraphId;
188 };
189
190 /**
191 * SetParameterCommand - Command to set a node parameter
192 */
194 {
195 public:
196 SetParameterCommand(const std::string& graphId, int nodeId,
197 const std::string& paramName,
198 const std::string& oldValue,
199 const std::string& newValue);
200
201 void Execute() override;
202 void Undo() override;
203 std::string GetDescription() const override;
204
205 private:
206 std::string m_GraphId;
208 std::string m_ParamName;
209 std::string m_OldValue;
210 std::string m_NewValue;
211 };
212
213 /**
214 * DuplicateNodeCommand - Command to duplicate a node
215 */
217 {
218 public:
219 DuplicateNodeCommand(const std::string& graphId, int sourceNodeId);
220
221 void Execute() override;
222 void Undo() override;
223 std::string GetDescription() const override;
224
225 private:
226 std::string m_GraphId;
228 int m_CreatedNodeId; // Set during Execute
229 json m_NodeData; // Backup for undo
230 };
231
232 /**
233 * EditNodeCommand - Command to edit node properties (name, type-specific fields)
234 */
236 {
237 public:
238 EditNodeCommand(const std::string& graphId, int nodeId,
239 const std::string& oldName, const std::string& newName,
240 const std::string& oldSubtype, const std::string& newSubtype);
241
242 void Execute() override;
243 void Undo() override;
244 std::string GetDescription() const override;
245
246 private:
247 std::string m_GraphId;
249 std::string m_OldName;
250 std::string m_NewName;
251 std::string m_OldSubtype; // actionType, conditionType, or decoratorType
252 std::string m_NewSubtype;
253 };
254 }
255}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
CommandStack - Manages undo/redo command history Maintains two stacks for undo and redo operations.
std::vector< std::string > GetUndoStackDescriptions() const
std::string GetNextRedoDescription() const
std::string GetLastCommandDescription() const
std::vector< std::unique_ptr< EditorCommand > > m_RedoStack
std::vector< std::unique_ptr< EditorCommand > > m_UndoStack
const EditorCommand * GetLastCommand() const
std::vector< std::string > GetRedoStackDescriptions() const
void ExecuteCommand(std::unique_ptr< EditorCommand > cmd)
CreateNodeCommand - Command to create a new node in graph.
std::string GetDescription() const override
DeleteNodeCommand - Command to delete a node from graph.
std::string GetDescription() const override
DuplicateNodeCommand - Command to duplicate a node.
std::string GetDescription() const override
EditNodeCommand - Command to edit node properties (name, type-specific fields)
std::string GetDescription() const override
EditorCommand - Base class for all undoable editor commands Implements command pattern for undo/redo ...
virtual std::string GetDescription() const =0
virtual ~EditorCommand()=default
LinkNodesCommand - Command to create a link between nodes.
std::string GetDescription() const override
MoveNodeCommand - Command to move a node's position.
std::string GetDescription() const override
SetParameterCommand - Command to set a node parameter.
std::string GetDescription() const override
UnlinkNodesCommand - Command to remove a link between nodes.
std::string GetDescription() const override
nlohmann::json json
< Provides AssetID and INVALID_ASSET_ID
@ Blueprint
.ats files (SubGraph/VisualScript)
nlohmann::json json