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 auto it = kEditorNames.find(type);
77 if (it != kEditorNames.end()) {
78 return it->second;
79 }
80 return "Unknown Editor";
81}
82
84 auto it = kEditorCategories.find(type);
85 if (it != kEditorCategories.end()) {
86 return it->second;
87 }
88 return "Unknown";
89}
90
92 const std::string& category) {
93 if (category == "Hex") {
94 return EditorType::kHex;
95 }
96 for (const auto& [type, cat] : kEditorCategories) {
97 if (cat == category) {
98 return type; // Return first match
99 }
100 }
101 return EditorType::kSettings; // Default fallback
102}
103
104std::vector<std::string> EditorRegistry::GetAllEditorCategories() {
105 // Returns all editor categories in preferred display order for the sidebar
106 // This is a fixed list to ensure consistent ordering
107 return {
108 "Overworld", // Primary map editing
109 "Dungeon", // Dungeon/room editing
110 "Graphics", // Graphics sheet editing
111 "Palette", // Color palette editing
112 "Sprite", // Sprite editing
113 "Message", // Text/dialogue editing
114 "Screen", // Screen/title editing
115 "Music", // Music/SPC editing
116 "Assembly", // Code editing
117 "Memory", // Hex editor (uses "Memory" category for cards)
118 "Emulator", // Game testing
119 "Agent" // AI Agent
120 };
121}
122
124 const std::string& category) const {
125 std::vector<EditorType> editors;
126
127 for (const auto& [type, cat] : kEditorCategories) {
128 if (cat == category) {
129 editors.push_back(type);
130 }
131 }
132
133 return editors;
134}
135
136std::vector<std::string> EditorRegistry::GetAvailableCategories() const {
137 std::vector<std::string> categories;
138 std::unordered_set<std::string> seen;
139
140 for (const auto& [type, category] : kEditorCategories) {
141 if (seen.find(category) == seen.end()) {
142 categories.push_back(category);
143 seen.insert(category);
144 }
145 }
146
147 return categories;
148}
149
151 auto it = kEditorNames.find(type);
152 if (it != kEditorNames.end()) {
153 return it->second;
154 }
155 return "Unknown Editor";
156}
157
159 ValidateEditorType(type);
160
161 if (!editor) {
162 throw std::invalid_argument("Editor pointer cannot be null");
163 }
164
165 registered_editors_[type] = editor;
166 printf("[EditorRegistry] Registered %s\n",
167 GetEditorDisplayName(type).c_str());
168}
169
171 ValidateEditorType(type);
172 if (!factory) {
173 throw std::invalid_argument("Editor factory cannot be null");
174 }
175 editor_factories_[type] = std::move(factory);
176}
177
182
184 ValidateEditorType(type);
185 return editor_factories_.find(type) != editor_factories_.end();
186}
187
188std::unique_ptr<Editor> EditorRegistry::CreateEditor(EditorType type,
189 Rom* rom) const {
190 ValidateEditorType(type);
191 auto it = editor_factories_.find(type);
192 if (it == editor_factories_.end()) {
193 return nullptr;
194 }
195 return it->second(rom);
196}
197
199 ValidateEditorType(type);
200
201 auto it = registered_editors_.find(type);
202 if (it != registered_editors_.end()) {
203 registered_editors_.erase(it);
204 printf("[EditorRegistry] Unregistered %s\n",
205 GetEditorDisplayName(type).c_str());
206 }
207}
208
210 ValidateEditorType(type);
211
212 auto it = registered_editors_.find(type);
213 if (it != registered_editors_.end()) {
214 return it->second;
215 }
216 return nullptr;
217}
218
220 ValidateEditorType(type);
221
222 auto it = registered_editors_.find(type);
223 if (it != registered_editors_.end() && it->second) {
224 return *it->second->active();
225 }
226 return false;
227}
228
230 ValidateEditorType(type);
231
232 auto it = registered_editors_.find(type);
233 if (it != registered_editors_.end() && it->second) {
234 return *it->second->active();
235 }
236 return false;
237}
238
240 ValidateEditorType(type);
241
242 auto it = registered_editors_.find(type);
243 if (it != registered_editors_.end() && it->second) {
244 it->second->set_active(active);
245 }
246}
247
249 return kEditorCategories.find(type) != kEditorCategories.end();
250}
251
253 if (!IsValidEditorType(type)) {
254 throw std::invalid_argument(
255 absl::StrFormat("Invalid editor type: %d", static_cast<int>(type)));
256 }
257}
258
259} // namespace editor
260} // 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
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:240