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 {EditorType::kPlanning, "Planning"}};
28
29const std::unordered_map<EditorType, std::string> EditorRegistry::kEditorNames =
30 {{EditorType::kDungeon, "Dungeon Editor"},
31 {EditorType::kOverworld, "Overworld Editor"},
32 {EditorType::kGraphics, "Graphics Editor"},
33 {EditorType::kPalette, "Palette Editor"},
34 {EditorType::kSprite, "Sprite Editor"},
35 {EditorType::kScreen, "Screen Editor"},
36 {EditorType::kMessage, "Message Editor"},
37 {EditorType::kMusic, "Music Editor"},
38 {EditorType::kAssembly, "Assembly Editor"},
39 {EditorType::kEmulator, "Emulator Editor"},
40 {EditorType::kHex, "Hex Editor"},
41 {EditorType::kAgent, "Agent Editor"},
42 {EditorType::kSettings, "Settings Editor"},
43 {EditorType::kPlanning, "Planning Editor"}};
44
45const std::unordered_map<EditorType, bool> EditorRegistry::kPanelBasedEditors =
46 {
51 {EditorType::kSprite, true},
52 {EditorType::kScreen, true},
54 {EditorType::kMusic, true},
57 {EditorType::kHex, true},
58 {EditorType::kAgent, true}, // Agent: Panel-based UI
59 {EditorType::kSettings, false}, // Settings: Sidebar panel
61};
62
64 auto it = kPanelBasedEditors.find(type);
65 return it != kPanelBasedEditors.end() && it->second;
66}
67
69 switch (type) {
73 return true;
74 default:
75 return false;
76 }
77}
78
82
84 switch (type) {
86 return {
87 .experimental = true,
88 .disabled_save_posture = ExperimentalSavePosture::kReadOnly,
89 };
91 return {
92 .experimental = true,
93 .disabled_save_posture = ExperimentalSavePosture::kDefensiveBackup,
94 };
96 return {
97 .experimental = true,
98 .disabled_save_posture = ExperimentalSavePosture::kNormal,
99 };
100 default:
101 return {};
102 }
103}
104
106 EditorType type, bool experiments_enabled) {
107 return !experiments_enabled && GetExperimentPolicy(type).experimental;
108}
109
111 auto it = kEditorNames.find(type);
112 if (it != kEditorNames.end()) {
113 return it->second;
114 }
115 return "Unknown Editor";
116}
117
119 auto it = kEditorCategories.find(type);
120 if (it != kEditorCategories.end()) {
121 return it->second;
122 }
123 return "Unknown";
124}
125
127 const std::string& category) {
128 if (category == "Hex") {
129 return EditorType::kHex;
130 }
131 for (const auto& [type, cat] : kEditorCategories) {
132 if (cat == category) {
133 return type; // Return first match
134 }
135 }
136 return EditorType::kSettings; // Default fallback
137}
138
139std::vector<std::string> EditorRegistry::GetAllEditorCategories() {
140 // Returns all editor categories in preferred display order for the sidebar
141 // This is a fixed list to ensure consistent ordering
142 return {
143 "Overworld", // Primary map editing
144 "Dungeon", // Dungeon/room editing
145 "Graphics", // Graphics sheet editing
146 "Palette", // Color palette editing
147 "Sprite", // Sprite editing
148 "Message", // Text/dialogue editing
149 "Screen", // Screen/title editing
150 "Music", // Music/SPC editing
151 "Assembly", // Code editing
152 "Memory", // Hex editor (uses "Memory" category for cards)
153 "Emulator", // Game testing
154 "Agent", // AI Agent
155 "Planning" // Goals, route and decisions ([planning] sidecar)
156 };
157}
158
160 const std::string& category) const {
161 std::vector<EditorType> editors;
162
163 for (const auto& [type, cat] : kEditorCategories) {
164 if (cat == category) {
165 editors.push_back(type);
166 }
167 }
168
169 return editors;
170}
171
172std::vector<std::string> EditorRegistry::GetAvailableCategories() const {
173 std::vector<std::string> categories;
174 std::unordered_set<std::string> seen;
175
176 for (const auto& [type, category] : kEditorCategories) {
177 if (seen.find(category) == seen.end()) {
178 categories.push_back(category);
179 seen.insert(category);
180 }
181 }
182
183 return categories;
184}
185
187 auto it = kEditorNames.find(type);
188 if (it != kEditorNames.end()) {
189 return it->second;
190 }
191 return "Unknown Editor";
192}
193
195 ValidateEditorType(type);
196
197 if (!editor) {
198 throw std::invalid_argument("Editor pointer cannot be null");
199 }
200
201 registered_editors_[type] = editor;
202 printf("[EditorRegistry] Registered %s\n",
203 GetEditorDisplayName(type).c_str());
204}
205
207 ValidateEditorType(type);
208 if (!factory) {
209 throw std::invalid_argument("Editor factory cannot be null");
210 }
211 editor_factories_[type] = std::move(factory);
212}
213
218
220 ValidateEditorType(type);
221 return editor_factories_.find(type) != editor_factories_.end();
222}
223
224std::unique_ptr<Editor> EditorRegistry::CreateEditor(EditorType type,
225 Rom* rom) const {
226 ValidateEditorType(type);
227 auto it = editor_factories_.find(type);
228 if (it == editor_factories_.end()) {
229 return nullptr;
230 }
231 return it->second(rom);
232}
233
235 ValidateEditorType(type);
236
237 auto it = registered_editors_.find(type);
238 if (it != registered_editors_.end()) {
239 registered_editors_.erase(it);
240 printf("[EditorRegistry] Unregistered %s\n",
241 GetEditorDisplayName(type).c_str());
242 }
243}
244
246 ValidateEditorType(type);
247
248 auto it = registered_editors_.find(type);
249 if (it != registered_editors_.end()) {
250 return it->second;
251 }
252 return nullptr;
253}
254
256 ValidateEditorType(type);
257
258 auto it = registered_editors_.find(type);
259 if (it != registered_editors_.end() && it->second) {
260 return *it->second->active();
261 }
262 return false;
263}
264
266 ValidateEditorType(type);
267
268 auto it = registered_editors_.find(type);
269 if (it != registered_editors_.end() && it->second) {
270 return *it->second->active();
271 }
272 return false;
273}
274
276 ValidateEditorType(type);
277
278 auto it = registered_editors_.find(type);
279 if (it != registered_editors_.end() && it->second) {
280 it->second->set_active(active);
281 }
282}
283
285 return kEditorCategories.find(type) != kEditorCategories.end();
286}
287
289 if (!IsValidEditorType(type)) {
290 throw std::invalid_argument(
291 absl::StrFormat("Invalid editor type: %d", static_cast<int>(type)));
292 }
293}
294
295} // namespace editor
296} // 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:49
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)
static bool ShouldWarnAboutExperimentalEditor(EditorType type, bool experiments_enabled)
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)
static EditorExperimentPolicy GetExperimentPolicy(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:249