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