Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
EditorState.h
Go to the documentation of this file.
1/*
2 * Olympe Tilemap Editor - Editor State & Command Pattern
3 *
4 * Manages editor history with Undo/Redo functionality using the Command pattern.
5 */
6
7#pragma once
8
9#include "LevelManager.h"
10#include <memory>
11#include <vector>
12#include <string>
13#include <deque>
14
15namespace Olympe {
16namespace Editor {
17
18 // Forward declaration
19 class LevelManager;
20
21 // ========================================================================
22 // Command Pattern - Abstract Base Class
23 // ========================================================================
24
25 class Command
26 {
27 public:
28 virtual ~Command() = default;
29
30 // Execute the command
31 virtual void Execute(LevelManager& levelManager) = 0;
32
33 // Undo the command
34 virtual void Undo(LevelManager& levelManager) = 0;
35
36 // Get a human-readable description of the command
37 virtual std::string GetDescription() const = 0;
38 };
39
40 // ========================================================================
41 // Concrete Commands
42 // ========================================================================
43
44 // Command to place/create a new entity
46 {
47 public:
48 PlaceEntityCommand(const std::string& prefabPath, const Vector& position);
49 ~PlaceEntityCommand() override = default;
50
51 void Execute(LevelManager& levelManager) override;
52 void Undo(LevelManager& levelManager) override;
53 std::string GetDescription() const override;
54
55 private:
56 std::string m_prefabPath;
58 std::string m_entityId; // Stored after execution
59 };
60
61 // Command to set a tile at a specific position
62 class SetTileCommand : public Command
63 {
64 public:
65 SetTileCommand(int x, int y, int newTileId);
66 ~SetTileCommand() override = default;
67
68 void Execute(LevelManager& levelManager) override;
69 void Undo(LevelManager& levelManager) override;
70 std::string GetDescription() const override;
71
72 private:
73 int m_x;
74 int m_y;
77 };
78
79 // Command to move an existing entity
81 {
82 public:
83 MoveEntityCommand(const std::string& entityId, const Vector& newPosition);
84 ~MoveEntityCommand() override = default;
85
86 void Execute(LevelManager& levelManager) override;
87 void Undo(LevelManager& levelManager) override;
88 std::string GetDescription() const override;
89
90 private:
91 std::string m_entityId;
94 };
95
96 // Command to delete an entity
98 {
99 public:
100 DeleteEntityCommand(const std::string& entityId);
101 ~DeleteEntityCommand() override = default;
102
103 void Execute(LevelManager& levelManager) override;
104 void Undo(LevelManager& levelManager) override;
105 std::string GetDescription() const override;
106
107 private:
108 std::string m_entityId;
109 std::unique_ptr<EntityInstance> m_savedEntity;
110 };
111
112 // Command to set collision at a specific position
114 {
115 public:
116 SetCollisionCommand(int x, int y, uint8_t newMask);
117 ~SetCollisionCommand() override = default;
118
119 void Execute(LevelManager& levelManager) override;
120 void Undo(LevelManager& levelManager) override;
121 std::string GetDescription() const override;
122
123 private:
124 int m_x;
125 int m_y;
128 };
129
130 // ========================================================================
131 // EditorState - Manages Command History and Undo/Redo
132 // ========================================================================
133
135 {
136 public:
137 EditorState();
138 ~EditorState();
139
140 // Execute a command and add it to history
141 void ExecuteCommand(std::unique_ptr<Command> command, LevelManager& levelManager);
142
143 // Undo the last command
145
146 // Redo a previously undone command
148
149 // Check if undo/redo is available
150 bool CanUndo() const;
151 bool CanRedo() const;
152
153 // Get description of the next undo/redo operation
154 std::string GetUndoDescription() const;
155 std::string GetRedoDescription() const;
156
157 // Clear all history
158 void ClearHistory();
159
160 // Get history information
161 size_t GetHistorySize() const { return m_commandHistory.size(); }
162 size_t GetHistoryIndex() const { return m_historyIndex; }
163
164 // Set maximum history size (default: 100)
166
167 private:
168 void TrimHistory();
169
170 std::deque<std::unique_ptr<Command>> m_commandHistory;
171 size_t m_historyIndex; // Current position in history
173 };
174
175} // namespace Editor
176} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
virtual void Execute(LevelManager &levelManager)=0
virtual ~Command()=default
virtual std::string GetDescription() const =0
virtual void Undo(LevelManager &levelManager)=0
std::string GetDescription() const override
std::unique_ptr< EntityInstance > m_savedEntity
void Undo(LevelManager &levelManager) override
void Execute(LevelManager &levelManager) override
~DeleteEntityCommand() override=default
bool Redo(LevelManager &levelManager)
void SetMaxHistorySize(size_t maxSize)
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
size_t GetHistoryIndex() const
size_t GetHistorySize() const
std::string GetUndoDescription() const
~MoveEntityCommand() override=default
std::string GetDescription() const override
void Execute(LevelManager &levelManager) override
void Undo(LevelManager &levelManager) override
void Undo(LevelManager &levelManager) override
std::string GetDescription() const override
~PlaceEntityCommand() override=default
void Execute(LevelManager &levelManager) override
~SetCollisionCommand() override=default
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() override=default