Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
EditorState.cpp
Go to the documentation of this file.
1/*
2 * Olympe Tilemap Editor - Editor State Implementation
3 */
4
5#include "../include/EditorState.h"
6#include <iostream>
7
8namespace Olympe {
9namespace Editor {
10
11 // ========================================================================
12 // PlaceEntityCommand Implementation
13 // ========================================================================
14
15 PlaceEntityCommand::PlaceEntityCommand(const std::string& prefabPath, const Vector& position)
16 : m_prefabPath(prefabPath), m_position(position), m_entityId("")
17 {
18 }
19
21 {
22 EntityInstance* entity = levelManager.CreateEntity(m_prefabPath);
23 if (entity)
24 {
25 entity->position = m_position;
26 m_entityId = entity->id;
27 levelManager.MarkDirty();
28 }
29 }
30
32 {
33 if (!m_entityId.empty())
34 {
35 levelManager.DeleteEntity(m_entityId);
36 levelManager.MarkDirty();
37 }
38 }
39
41 {
42 return "Place Entity: " + m_prefabPath;
43 }
44
45 // ========================================================================
46 // SetTileCommand Implementation
47 // ========================================================================
48
50 : m_x(x), m_y(y), m_newTileId(newTileId), m_oldTileId(-1)
51 {
52 }
53
60
62 {
63 if (m_oldTileId != -1)
64 {
66 levelManager.MarkDirty();
67 }
68 }
69
71 {
72 return "Set Tile at (" + std::to_string(m_x) + ", " + std::to_string(m_y) + ")";
73 }
74
75 // ========================================================================
76 // MoveEntityCommand Implementation
77 // ========================================================================
78
79 MoveEntityCommand::MoveEntityCommand(const std::string& entityId, const Vector& newPosition)
80 : m_entityId(entityId), m_newPosition(newPosition), m_oldPosition(0, 0, 0)
81 {
82 }
83
85 {
86 EntityInstance* entity = levelManager.GetEntity(m_entityId);
87 if (entity)
88 {
89 m_oldPosition = entity->position;
90 levelManager.UpdateEntityPosition(m_entityId, m_newPosition);
91 levelManager.MarkDirty();
92 }
93 }
94
96 {
97 levelManager.UpdateEntityPosition(m_entityId, m_oldPosition);
98 levelManager.MarkDirty();
99 }
100
102 {
103 return "Move Entity: " + m_entityId;
104 }
105
106 // ========================================================================
107 // DeleteEntityCommand Implementation
108 // ========================================================================
109
110 DeleteEntityCommand::DeleteEntityCommand(const std::string& entityId)
111 : m_entityId(entityId), m_savedEntity(nullptr)
112 {
113 }
114
116 {
117 EntityInstance* entity = levelManager.GetEntity(m_entityId);
118 if (entity)
119 {
120 // Save a copy of the entity before deleting
121 m_savedEntity = std::make_unique<EntityInstance>();
122 m_savedEntity->id = entity->id;
123 m_savedEntity->prefabPath = entity->prefabPath;
124 m_savedEntity->name = entity->name;
125 m_savedEntity->position = entity->position;
126 m_savedEntity->overrides = entity->overrides;
127
128 levelManager.DeleteEntity(m_entityId);
129 levelManager.MarkDirty();
130 }
131 }
132
134 {
135 if (m_savedEntity)
136 {
137 // Recreate the entity
138 EntityInstance* entity = levelManager.CreateEntity(m_savedEntity->prefabPath);
139 if (entity)
140 {
141 // Restore saved properties
142 entity->id = m_savedEntity->id;
143 entity->name = m_savedEntity->name;
144 entity->position = m_savedEntity->position;
145 entity->overrides = m_savedEntity->overrides;
146 levelManager.MarkDirty();
147 }
148 }
149 }
150
152 {
153 return "Delete Entity: " + m_entityId;
154 }
155
156 // ========================================================================
157 // SetCollisionCommand Implementation
158 // ========================================================================
159
161 : m_x(x), m_y(y), m_newMask(newMask), m_oldMask(0)
162 {
163 }
164
166 {
167 m_oldMask = levelManager.GetCollision(m_x, m_y);
168 levelManager.SetCollision(m_x, m_y, m_newMask);
169 levelManager.MarkDirty();
170 }
171
173 {
174 levelManager.SetCollision(m_x, m_y, m_oldMask);
175 levelManager.MarkDirty();
176 }
177
179 {
180 return "Set Collision at (" + std::to_string(m_x) + ", " + std::to_string(m_y) + ")";
181 }
182
183 // ========================================================================
184 // EditorState Implementation
185 // ========================================================================
186
188 : m_historyIndex(0), m_maxHistorySize(100)
189 {
190 }
191
195
197 {
198 if (!command)
199 {
200 std::cerr << "[EditorState] Attempted to execute null command" << std::endl;
201 return;
202 }
203
204 // Execute the command
205 command->Execute(levelManager);
206
207 // Remove any commands after the current position (redo history)
208 if (m_historyIndex < m_commandHistory.size())
209 {
210 m_commandHistory.erase(
212 m_commandHistory.end()
213 );
214 }
215
216 // Add the command to history
217 m_commandHistory.push_back(std::move(command));
219
220 // Trim history if it exceeds max size
221 TrimHistory();
222
223 std::cout << "[EditorState] Command executed: " << m_commandHistory.back()->GetDescription() << std::endl;
224 }
225
227 {
228 if (!CanUndo())
229 {
230 return false;
231 }
232
235
236 std::cout << "[EditorState] Undo: " << m_commandHistory[m_historyIndex]->GetDescription() << std::endl;
237 return true;
238 }
239
241 {
242 if (!CanRedo())
243 {
244 return false;
245 }
246
249
250 std::cout << "[EditorState] Redo: " << m_commandHistory[m_historyIndex - 1]->GetDescription() << std::endl;
251 return true;
252 }
253
255 {
256 return m_historyIndex > 0;
257 }
258
260 {
261 return m_historyIndex < m_commandHistory.size();
262 }
263
265 {
266 if (CanUndo())
267 {
268 return m_commandHistory[m_historyIndex - 1]->GetDescription();
269 }
270 return "";
271 }
272
274 {
275 if (CanRedo())
276 {
277 return m_commandHistory[m_historyIndex]->GetDescription();
278 }
279 return "";
280 }
281
283 {
284 m_commandHistory.clear();
285 m_historyIndex = 0;
286 std::cout << "[EditorState] History cleared" << std::endl;
287 }
288
290 {
291 // Remove oldest commands if we exceed max size
292 while (m_commandHistory.size() > m_maxHistorySize)
293 {
294 m_commandHistory.pop_front();
295 if (m_historyIndex > 0)
296 {
298 }
299 }
300 }
301
302} // namespace Editor
303} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::string GetDescription() const override
DeleteEntityCommand(const std::string &entityId)
std::unique_ptr< EntityInstance > m_savedEntity
void Undo(LevelManager &levelManager) override
void Execute(LevelManager &levelManager) override
bool Redo(LevelManager &levelManager)
bool Undo(LevelManager &levelManager)
std::string GetRedoDescription() const
void ExecuteCommand(std::unique_ptr< Command > command, LevelManager &levelManager)
std::deque< std::unique_ptr< Command > > m_commandHistory
std::string GetUndoDescription() const
MoveEntityCommand(const std::string &entityId, const Vector &newPosition)
std::string GetDescription() const override
void Execute(LevelManager &levelManager) override
void Undo(LevelManager &levelManager) override
void Undo(LevelManager &levelManager) override
PlaceEntityCommand(const std::string &prefabPath, const Vector &position)
std::string GetDescription() const override
void Execute(LevelManager &levelManager) override
SetCollisionCommand(int x, int y, uint8_t newMask)
void Execute(LevelManager &levelManager) override
void Undo(LevelManager &levelManager) override
std::string GetDescription() const override
void Execute(LevelManager &levelManager) override
std::string GetDescription() const override
void Undo(LevelManager &levelManager) override
SetTileCommand(int x, int y, int newTileId)