yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
editor_registry.cc
Go to the documentation of this file.
1#include "editor_registry.h"
2
3#include <unordered_set>
4#include <utility>
5
6#include "absl/strings/str_format.h"
7#include "app/editor/editor.h"
8
9namespace yaze {
10namespace editor {
11
12// Static mappings for editor types
13const std::unordered_map<EditorType, std::string>
15 {EditorType::kOverworld, "Overworld"},
16 {EditorType::kGraphics, "Graphics"},
17 {EditorType::kPalette, "Palette"},
18 {EditorType::kSprite, "Sprite"},
19 {EditorType::kScreen, "Screen"},
20 {EditorType::kMessage, "Message"},
21 {EditorType::kMusic, "Music"},
22 {EditorType::kAssembly, "Assembly"},
23 {EditorType::kEmulator, "Emulator"},
24 {EditorType::kHex, "Memory"},
25 {EditorType::kAgent, "Agent"},
26 {EditorType::kSettings, "System"}};
27
28const std::unordered_map<EditorType, std::string> EditorRegistry::kEditorNames =
29 {{EditorType::kDungeon, "Dungeon Editor"},
30 {EditorType::kOverworld, "Overworld Editor"},
31 {EditorType::kGraphics, "Graphics Editor"},
32 {EditorType::kPalette, "Palette Editor"},
33 {EditorType::kSprite, "Sprite Editor"},
34 {EditorType::kScreen, "Screen Editor"},
35 {EditorType::kMessage, "Message Editor"},
36 {EditorType::kMusic, "Music Editor"},
37 {EditorType::kAssembly, "Assembly Editor"},
38 {EditorType::kEmulator, "Emulator Editor"},
39 {EditorType::kHex, "Hex Editor"},
40 {EditorType::kAgent, "Agent Editor"},
41 {EditorType::kSettings, "Settings Editor"}};
42
43const std::unordered_map<EditorType, bool> EditorRegistry::kPanelBasedEditors =
44 {
49 {EditorType::kSprite, true},
50 {EditorType::kScreen, true},
52 {EditorType::kMusic, true},
55 {EditorType::kHex, true},
56 {EditorType::kAgent, true}, // Agent: Panel-based UI
57 {EditorType::kSettings, false} // Settings: Sidebar panel
58};
59
61 auto it = kPanelBasedEditors.find(type);
62 return it != kPanelBasedEditors.end() && it->second;
63}
64
66 switch (type) {
69 return true;
70 default:
71 return false;
72 }
73}
74
76 switch (type) {
80 return true;
81 default:
82 return false;
83 }
84}
85
87 auto it = kEditorNames.find(type);
88 if (it != kEditorNames.end()) {
89 return it->second;
90 }
91 return "Unknown Editor";
92}
93
95 auto it = kEditorCategories.find(type);
96 if (it != kEditorCategories.end()) {
97 return it->second;
98 }
99 return "Unknown";
100}
101
103 const std::string& category) {
104 if (category == "Hex") {
105 return EditorType::kHex;
106 }
107 for (const auto& [type, cat] : kEditorCategories) {
108 if (cat == category) {
109 return type; // Return first match
110 }
111 }
112 return EditorType::kSettings; // Default fallback
113}
114
115std::vector<std::string> EditorRegistry::GetAllEditorCategories() {
116 // Returns all editor categories in preferred display order for the sidebar
117 // This is a fixed list to ensure consistent ordering
118 return {
119 "Overworld", // Primary map editing
120 "Dungeon", // Dungeon/room editing
121 "Graphics", // Graphics sheet editing
122 "Palette", // Color palette editing
123 "Sprite", // Sprite editing
124 "Message", // Text/dialogue editing
125 "Screen", // Screen/title editing
126 "Music", // Music/SPC editing
127 "Assembly", // Code editing
128 "Memory", // Hex editor (uses "Memory" category for cards)
129 "Emulator", // Game testing
130 "Agent" // AI Agent
131 };
132}
133
135 const std::string& category) const {
136 std::vector<EditorType> editors;
137
138 for (const auto& [type, cat] : kEditorCategories) {
139 if (cat == category) {
140 editors.push_back(type);
141 }
142 }
143
144 return editors;
145}
146
147std::vector<std::string> EditorRegistry::GetAvailableCategories() const {
148 std::vector<std::string> categories;
149 std::unordered_set<std::string> seen;
150
151 for (const auto& [type, category] : kEditorCategories) {
152 if (seen.find(category) == seen.end()) {
153 categories.push_back(category);
154 seen.insert(category);
155 }
156 }
157
158 return categories;
159}
160
162 auto it = kEditorNames.find(type);
163 if (it != kEditorNames.end()) {
164 return it->second;
165 }
166 return "Unknown Editor";
167}
168
170 ValidateEditorType(type);
171
172 if (!editor) {
173 throw std::invalid_argument("Editor pointer cannot be null");
174 }
175
176 registered_editors_[type] = editor;
177 printf("[EditorRegistry] Registered %s\n",
178 GetEditorDisplayName(type).c_str());
179}
180
182 ValidateEditorType(type);
183 if (!factory) {
184 throw std::invalid_argument("Editor factory cannot be null");
185 }
186 editor_factories_[type] = std::move(factory);
187}
188
193
195 ValidateEditorType(type);
196 return editor_factories_.find(type) != editor_factories_.end();
197}
198
199std::unique_ptr<Editor> EditorRegistry::CreateEditor(EditorType type,
200 Rom* rom) const {
201 ValidateEditorType(type);
202 auto it = editor_factories_.find(type);
203 if (it == editor_factories_.end()) {
204 return nullptr;
205 }
206 return it->second(rom);
207}
208
210 ValidateEditorType(type);
211
212 auto it = registered_editors_.find(type);
213 if (it != registered_editors_.end()) {
214 registered_editors_.erase(it);
215 printf("[EditorRegistry] Unregistered %s\n",
216 GetEditorDisplayName(type).c_str());
217 }
218}
219
221 ValidateEditorType(type);
222
223 auto it = registered_editors_.find(type);
224 if (it != registered_editors_.end()) {
225 return it->second;
226 }
227 return nullptr;
228}
229
231 ValidateEditorType(type);
232
233 auto it = registered_editors_.find(type);
234 if (it != registered_editors_.end() && it->second) {
235 return *it->second->active();
236 }
237 return false;
238}
239
241 ValidateEditorType(type);
242
243 auto it = registered_editors_.find(type);
244 if (it != registered_editors_.end() && it->second) {
245 return *it->second->active();
246 }
247 return false;
248}
249
251 ValidateEditorType(type);
252
253 auto it = registered_editors_.find(type);
254 if (it != registered_editors_.end() && it->second) {
255 it->second->set_active(active);
256 }
257}
258
260 return kEditorCategories.find(type) != kEditorCategories.end();
261}
262
264 if (!IsValidEditorType(type)) {
265 throw std::invalid_argument(
266 absl::StrFormat("Invalid editor type: %d", static_cast<int>(type)));
267 }
268}
269
270} // namespace editor
271} // namespace yaze
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:28
void RegisterEditor(EditorType type, Editor *editor)
static const std::unordered_map< EditorType, std::string > kEditorNames
static bool UpdateAllowedWithoutLoadedRom(EditorType type)
static EditorType GetEditorTypeFromCategory(const std::string &category)
static const std::unordered_map< EditorType, std::string > kEditorCategories
void ValidateEditorType(EditorType type) const
static std::vector< std::string > GetAllEditorCategories()
Get all editor categories in display order for sidebar.
std::vector< EditorType > GetEditorsInCategory(const std::string &category) const
static bool IsPanelBasedEditor(EditorType type)
std::unique_ptr< Editor > CreateEditor(EditorType type, Rom *rom) const
static bool IsExperimentalEditor(EditorType type)
std::function< std::unique_ptr< Editor >(Rom *)> EditorFactory
void SetEditorActive(EditorType type, bool active)
std::string GetEditorDisplayName(EditorType type) const
bool HasFactory(EditorType type) const
void UnregisterEditor(EditorType type)
bool IsEditorActive(EditorType type) const
std::unordered_map< EditorType, EditorFactory > editor_factories_
bool IsValidEditorType(EditorType type) const
void RegisterFactory(EditorType type, EditorFactory factory)
bool IsEditorVisible(EditorType type) const
void UnregisterFactory(EditorType type)
std::vector< std::string > GetAvailableCategories() const
static std::string GetEditorName(EditorType type)
static const std::unordered_map< EditorType, bool > kPanelBasedEditors
Editor * GetEditor(EditorType type) const
static std::string GetEditorCategory(EditorType type)
std::unordered_map< EditorType, Editor * > registered_editors_
Interface for editor classes.
Definition editor.h:245