yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
content_registry.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <memory>
5#include <mutex>
6#include <string>
7#include <vector>
8
12#include "app/editor/editor.h"
13#include "core/project.h"
14#include "rom/rom.h"
15#include "zelda3/game_data.h"
16
17namespace yaze::editor {
18
19namespace {
20
21// Singleton storage for ContentRegistry state
22// Uses lazy initialization to avoid static initialization order issues
24 std::mutex mutex;
25
26 // When global_context is set, Context delegates to it.
27 // The raw pointers below are fallback storage used only before
28 // GlobalEditorContext is wired in (early startup / tests).
29 GlobalEditorContext* global_context = nullptr;
30
31 Rom* current_rom = nullptr;
32 ::yaze::EventBus* event_bus = nullptr;
33 Editor* current_editor = nullptr;
34 ::yaze::zelda3::GameData* game_data = nullptr;
35 ::yaze::project::YazeProject* current_project = nullptr;
36 std::vector<std::unique_ptr<EditorPanel>> panels;
37 std::vector<ContentRegistry::Panels::PanelFactory> factories;
38 std::vector<ContentRegistry::Editors::EditorFactory> editor_factories;
39 std::vector<ContentRegistry::Shortcuts::ShortcutDef> shortcuts;
40 std::vector<ContentRegistry::Settings::SettingDef> settings;
41
42 static RegistryState& Get() {
43 static RegistryState instance;
44 return instance;
45 }
46};
47
48} // namespace
49
50// =============================================================================
51// Context Implementation
52// =============================================================================
53
54namespace ContentRegistry {
55
56namespace Context {
57
59 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
60 RegistryState::Get().global_context = ctx;
61}
62
63Rom* rom() {
64 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
65 auto& state = RegistryState::Get();
66 if (state.global_context) return state.global_context->GetCurrentRom();
67 return state.current_rom;
68}
69
70void SetRom(Rom* rom) {
71 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
72 auto& state = RegistryState::Get();
73 if (state.global_context) {
74 state.global_context->SetCurrentRom(rom);
75 }
76 state.current_rom = rom; // keep fallback in sync
77}
78
80 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
81 auto& state = RegistryState::Get();
82 if (state.global_context) return &state.global_context->GetEventBus();
83 return state.event_bus;
84}
85
87 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
88 auto& state = RegistryState::Get();
89 // EventBus in GlobalEditorContext is a reference set at construction,
90 // so we don't update it here. Just keep fallback in sync.
91 state.event_bus = bus;
92}
93
95 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
96 auto& state = RegistryState::Get();
97 if (state.global_context) return state.global_context->GetCurrentEditor();
98 return state.current_editor;
99}
100
102 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
103 auto& state = RegistryState::Get();
104 if (state.global_context) {
105 state.global_context->SetCurrentEditor(editor);
106 }
107 state.current_editor = editor;
108}
109
111 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
112 auto& state = RegistryState::Get();
113 if (state.global_context) return state.global_context->GetGameData();
114 return state.game_data;
115}
116
118 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
119 auto& state = RegistryState::Get();
120 if (state.global_context) {
121 state.global_context->SetGameData(data);
122 }
123 state.game_data = data;
124}
125
127 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
128 auto& state = RegistryState::Get();
129 if (state.global_context) return state.global_context->GetCurrentProject();
130 return state.current_project;
131}
132
134 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
135 auto& state = RegistryState::Get();
136 if (state.global_context) {
137 state.global_context->SetCurrentProject(project);
138 }
139 state.current_project = project;
140}
141
142void Clear() {
143 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
144 auto& state = RegistryState::Get();
145 if (state.global_context) {
146 state.global_context->Clear();
147 }
148 state.current_rom = nullptr;
149 state.event_bus = nullptr;
150 state.current_editor = nullptr;
151 state.game_data = nullptr;
152 state.current_project = nullptr;
153}
154
155} // namespace Context
156
157// =============================================================================
158// Panels Implementation
159// =============================================================================
160
161namespace Panels {
162
164 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
165 RegistryState::Get().factories.push_back(std::move(factory));
166}
167
168std::vector<std::unique_ptr<EditorPanel>> CreateAll() {
169 std::vector<PanelFactory> factories;
170 {
171 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
172 factories = RegistryState::Get().factories;
173 }
174
175 std::vector<std::unique_ptr<EditorPanel>> result;
176 result.reserve(factories.size());
177
178 for (const auto& factory : factories) {
179 if (auto panel = factory()) {
180 result.push_back(std::move(panel));
181 }
182 }
183 return result;
184}
185
186void Register(std::unique_ptr<EditorPanel> panel) {
187 if (!panel) return;
188
189 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
190 RegistryState::Get().panels.push_back(std::move(panel));
191}
192
193std::vector<EditorPanel*> GetAll() {
194 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
195
196 std::vector<EditorPanel*> result;
197 result.reserve(RegistryState::Get().panels.size());
198
199 for (const auto& panel : RegistryState::Get().panels) {
200 result.push_back(panel.get());
201 }
202
203 return result;
204}
205
206EditorPanel* Get(const std::string& id) {
207 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
208
209 for (const auto& panel : RegistryState::Get().panels) {
210 if (panel->GetId() == id) {
211 return panel.get();
212 }
213 }
214
215 return nullptr;
216}
217
218void Clear() {
219 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
220 RegistryState::Get().panels.clear();
221}
222
223} // namespace Panels
224
225// =============================================================================
226// Editors Implementation
227// =============================================================================
228
229namespace Editors {
230
232 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
233 RegistryState::Get().editor_factories.push_back(std::move(factory));
234}
235
236std::vector<std::unique_ptr<Editor>> CreateAll(const EditorDependencies& deps) {
237 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
238 std::vector<std::unique_ptr<Editor>> result;
239 result.reserve(RegistryState::Get().editor_factories.size());
240
241 for (const auto& factory : RegistryState::Get().editor_factories) {
242 if (auto editor = factory(deps)) {
243 result.push_back(std::move(editor));
244 }
245 }
246 return result;
247}
248
249} // namespace Editors
250
251// =============================================================================
252// Shortcuts Implementation
253// =============================================================================
254
255namespace Shortcuts {
256
257void Register(const ShortcutDef& shortcut) {
258 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
259 RegistryState::Get().shortcuts.push_back(shortcut);
260}
261
262void add(const std::string& id, const std::string& key, const std::string& desc) {
263 Register({id, key, desc});
264}
265
266std::vector<ShortcutDef> GetAll() {
267 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
268 return RegistryState::Get().shortcuts;
269}
270
271} // namespace Shortcuts
272
273// =============================================================================
274// Settings Implementation
275// =============================================================================
276
277namespace Settings {
278
279void Register(const SettingDef& setting) {
280 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
281 RegistryState::Get().settings.push_back(setting);
282}
283
284void add(const std::string& section, const std::string& key, const std::string& default_val, const std::string& desc) {
285 Register({key, section, default_val, desc});
286}
287
288std::vector<SettingDef> GetAll() {
289 std::lock_guard<std::mutex> lock(RegistryState::Get().mutex);
290 return RegistryState::Get().settings;
291}
292
293} // namespace Settings
294
295} // namespace ContentRegistry
296
297} // namespace yaze::editor
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
Base interface for all logical panel components.
Interface for editor classes.
Definition editor.h:236
Instance-based runtime context replacing ContentRegistry::Context.
void SetGlobalContext(GlobalEditorContext *ctx)
Rom * rom()
Get the current ROM instance.
void SetEventBus(::yaze::EventBus *bus)
Set the current EventBus instance.
::yaze::EventBus * event_bus()
Get the current EventBus instance.
void SetRom(Rom *rom)
Set the current ROM instance.
void SetGameData(::yaze::zelda3::GameData *data)
Set the current game data instance.
Editor * current_editor()
Get the currently active editor.
void SetCurrentEditor(Editor *editor)
Set the currently active editor.
::yaze::zelda3::GameData * game_data()
Get the current game data instance.
::yaze::project::YazeProject * current_project()
Get the current project instance.
void Clear()
Clear all context state.
void SetCurrentProject(::yaze::project::YazeProject *project)
Set the current project instance.
void RegisterFactory(EditorFactory factory)
std::function< std::unique_ptr< Editor >(const EditorDependencies &)> EditorFactory
std::vector< std::unique_ptr< Editor > > CreateAll(const EditorDependencies &deps)
std::vector< std::unique_ptr< EditorPanel > > CreateAll()
Create new instances of all registered panels.
std::vector< EditorPanel * > GetAll()
Get all registered panels.
void Clear()
Clear all registered panels.
void Register(std::unique_ptr< EditorPanel > panel)
Register a panel instance (Legacy/Global).
std::function< std::unique_ptr< EditorPanel >()> PanelFactory
void RegisterFactory(PanelFactory factory)
Register a panel factory.
EditorPanel * Get(const std::string &id)
Get a specific panel by its ID.
void Register(const SettingDef &setting)
void add(const std::string &section, const std::string &key, const std::string &default_val, const std::string &desc)
void add(const std::string &id, const std::string &key, const std::string &desc)
void Register(const ShortcutDef &shortcut)
Editors are the view controllers for the application.
Unified dependency container for all editor types.
Definition editor.h:163
std::vector< ContentRegistry::Shortcuts::ShortcutDef > shortcuts
std::vector< ContentRegistry::Panels::PanelFactory > factories
std::vector< ContentRegistry::Editors::EditorFactory > editor_factories
std::vector< ContentRegistry::Settings::SettingDef > settings
Modern project structure with comprehensive settings consolidation.
Definition project.h:120