Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
CommandSystem.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 using json = nlohmann::json;
21
22 /**
23 * EditorCommand - Base class for all undoable editor commands
24 * Implements command pattern for undo/redo support
25 */
27 {
28 public:
29 virtual ~EditorCommand() = default;
30
31 // Execute the command (perform the operation)
32 virtual void Execute() = 0;
33
34 // Undo the command (revert the operation)
35 virtual void Undo() = 0;
36
37 // Get a human-readable description of the command
38 virtual std::string GetDescription() const = 0;
39 };
40
41 /**
42 * CommandStack - Manages undo/redo command history
43 * Maintains two stacks for undo and redo operations
44 */
46 {
47 public:
50
51 // Execute a new command and add to undo stack
52 void ExecuteCommand(std::unique_ptr<EditorCommand> cmd);
53
54 // Undo the last command
55 void Undo();
56
57 // Redo the last undone command
58 void Redo();
59
60 // Query state
61 bool CanUndo() const { return !m_UndoStack.empty(); }
62 bool CanRedo() const { return !m_RedoStack.empty(); }
63
64 // Get command information
65 const EditorCommand* GetLastCommand() const;
66 std::string GetLastCommandDescription() const;
67 std::string GetNextRedoDescription() const;
68
69 // Get stack contents for UI display
70 std::vector<std::string> GetUndoStackDescriptions() const;
71 std::vector<std::string> GetRedoStackDescriptions() const;
72
73 // Clear all history
74 void Clear();
75
76 // Get stack sizes
77 size_t GetUndoStackSize() const { return m_UndoStack.size(); }
78 size_t GetRedoStackSize() const { return m_RedoStack.size(); }
79
80 private:
81 std::vector<std::unique_ptr<EditorCommand>> m_UndoStack;
82 std::vector<std::unique_ptr<EditorCommand>> m_RedoStack;
83 size_t m_MaxStackSize; // Limit memory usage
84 };
85
86 // ========================================================================
87 // Concrete Command Classes
88 // ========================================================================
89
90 /**
91 * CreateNodeCommand - Command to create a new node in graph
92 */
94 {
95 public:
96 CreateNodeCommand(const std::string& graphId, const std::string& nodeType,
97 float posX, float posY, const std::string& nodeName = "");
98
99 void Execute() override;
100 void Undo() override;
101 std::string GetDescription() const override;
102
103 private:
104 std::string m_GraphId;
105 std::string m_NodeType;
106 std::string m_NodeName;
107 float m_PosX;
108 float m_PosY;
109 int m_CreatedNodeId; // Set during Execute
110 };
111
112 /**
113 * DeleteNodeCommand - Command to delete a node from graph
114 */
115 class DeleteNodeCommand : public EditorCommand
116 {
117 public:
118 DeleteNodeCommand(const std::string& graphId, int nodeId);
119
120 void Execute() override;
121 void Undo() override;
122 std::string GetDescription() const override;
123
124 private:
125 std::string m_GraphId;
127 json m_NodeData; // Saved node data for undo
128 };
129
130 /**
131 * MoveNodeCommand - Command to move a node's position
132 */
133 class MoveNodeCommand : public EditorCommand
134 {
135 public:
136 MoveNodeCommand(const std::string& graphId, int nodeId,
137 float oldX, float oldY, float newX, float newY);
138
139 void Execute() override;
140 void Undo() override;
141 std::string GetDescription() const override;
142
143 private:
144 std::string m_GraphId;
148 };
149
150 /**
151 * LinkNodesCommand - Command to create a link between nodes
152 */
154 {
155 public:
156 LinkNodesCommand(const std::string& graphId, int parentId, int childId);
157
158 void Execute() override;
159 void Undo() override;
160 std::string GetDescription() const override;
161
162 private:
163 std::string m_GraphId;
166 };
167
168 /**
169 * UnlinkNodesCommand - Command to remove a link between nodes
170 */
172 {
173 public:
174 UnlinkNodesCommand(const std::string& graphId, int parentId, int childId);
175
176 void Execute() override;
177 void Undo() override;
178 std::string GetDescription() const override;
179
180 private:
181 std::string m_GraphId;
184 };
185
186 /**
187 * SetParameterCommand - Command to set a node parameter
188 */
190 {
191 public:
192 SetParameterCommand(const std::string& graphId, int nodeId,
193 const std::string& paramName,
194 const std::string& oldValue,
195 const std::string& newValue);
196
197 void Execute() override;
198 void Undo() override;
199 std::string GetDescription() const override;
200
201 private:
202 std::string m_GraphId;
204 std::string m_ParamName;
205 std::string m_OldValue;
206 std::string m_NewValue;
207 };
208
209 /**
210 * DuplicateNodeCommand - Command to duplicate a node
211 */
213 {
214 public:
215 DuplicateNodeCommand(const std::string& graphId, int sourceNodeId);
216
217 void Execute() override;
218 void Undo() override;
219 std::string GetDescription() const override;
220
221 private:
222 std::string m_GraphId;
224 int m_CreatedNodeId; // Set during Execute
225 json m_NodeData; // Backup for undo
226 };
227
228 /**
229 * EditNodeCommand - Command to edit node properties (name, type-specific fields)
230 */
232 {
233 public:
234 EditNodeCommand(const std::string& graphId, int nodeId,
235 const std::string& oldName, const std::string& newName,
236 const std::string& oldSubtype, const std::string& newSubtype);
237
238 void Execute() override;
239 void Undo() override;
240 std::string GetDescription() const override;
241
242 private:
243 std::string m_GraphId;
245 std::string m_OldName;
246 std::string m_NewName;
247 std::string m_OldSubtype; // actionType, conditionType, or decoratorType
248 std::string m_NewSubtype;
249 };
250}
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
CommandStack - Manages undo/redo command history Maintains two stacks for undo and redo operations.
std::string GetNextRedoDescription() const
size_t GetRedoStackSize() const
std::vector< std::unique_ptr< EditorCommand > > m_UndoStack
std::vector< std::string > GetRedoStackDescriptions() const
std::vector< std::unique_ptr< EditorCommand > > m_RedoStack
const EditorCommand * GetLastCommand() const
void ExecuteCommand(std::unique_ptr< EditorCommand > cmd)
std::string GetLastCommandDescription() const
std::vector< std::string > GetUndoStackDescriptions() const
size_t GetUndoStackSize() const
CreateNodeCommand - Command to create a new node in graph.
std::string GetDescription() const override
DeleteNodeCommand - Command to delete a node from graph.
void Undo() override
Undo the command.
void Execute() override
Execute the command.
std::string GetDescription() const override
Get a human-readable description of the command.
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 ~EditorCommand()=default
virtual std::string GetDescription() const =0
virtual void Execute()=0
virtual void Undo()=0
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
Get a human-readable description of the command.
void Undo() override
Undo the command.
void Execute() override
Execute the command.
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
nlohmann::json json