yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
editor_activator.cc
Go to the documentation of this file.
1#include "editor_activator.h"
2
16#include "app/gui/core/icons.h"
17#include "imgui/imgui.h"
18#include "imgui/imgui_internal.h"
19#include "util/log.h"
20#include "zelda3/common.h"
21
22namespace yaze {
23namespace editor {
24
26 deps_ = deps;
27 initialized_ = true;
28
29 // Subscribe to navigation events via EventBus
30 if (deps_.event_bus) {
32 [this](const JumpToRoomRequestEvent& event) {
33 JumpToDungeonRoom(event.room_id);
34 });
35
37 [this](const JumpToMapRequestEvent& event) {
38 JumpToOverworldMap(event.map_id);
39 });
40
42 [this](const JumpToMessageRequestEvent& event) {
43 JumpToMessage(event.message_id);
44 });
45
47 [this](const JumpToAssemblySymbolRequestEvent& event) {
48 JumpToAssemblySymbol(event.symbol);
49 });
50
51 LOG_INFO("EditorActivator", "Subscribed to navigation events");
52 }
53}
54
55void EditorActivator::SwitchToEditor(EditorType editor_type, bool force_visible,
56 bool from_dialog) {
57 if (!initialized_) {
58 LOG_WARN("EditorActivator", "Not initialized, cannot switch editor");
59 return;
60 }
61
62 // Avoid touching ImGui docking state when outside a frame
63 ImGuiContext* imgui_ctx = ImGui::GetCurrentContext();
64 const bool frame_active = imgui_ctx != nullptr && imgui_ctx->WithinFrameScope;
65 if (!frame_active && deps_.queue_deferred_action) {
66 const bool validate_session =
67 static_cast<bool>(deps_.get_current_session_id);
68 const size_t expected_session_id =
69 validate_session ? deps_.get_current_session_id() : 0;
70 deps_.queue_deferred_action([this, editor_type, force_visible, from_dialog,
71 validate_session, expected_session_id]() {
72 if (validate_session &&
74 deps_.get_current_session_id() != expected_session_id)) {
75 return;
76 }
77 SwitchToEditor(editor_type, force_visible, from_dialog);
78 });
79 return;
80 }
81
82 // If NOT coming from dialog, close editor selection UI
83 if (!from_dialog && deps_.ui_coordinator) {
85 }
86
87 auto* editor_set =
89 if (!editor_set) {
90 return;
91 }
92
94 switch (editor_type) {
103 case EditorType::kSprite: {
104 const auto status = deps_.ensure_editor_assets_loaded(editor_type);
105 if (!status.ok()) {
106 if (deps_.toast_manager) {
108 "Failed to prepare editor: " + std::string(status.message()),
110 }
111 return;
112 }
113 break;
114 }
115 default:
116 break;
117 }
118 }
119
120 // Toggle the editor in the active editors list
121 for (auto* editor : editor_set->active_editors_) {
122 if (editor->type() == editor_type) {
123 if (force_visible) {
124 editor->set_active(true);
125 } else {
126 editor->toggle_active();
127 }
128
129 if (EditorRegistry::IsPanelBasedEditor(editor_type)) {
130 if (*editor->active()) {
131 // Smooth transition: fade out old, then fade in new
132 // Panel refresh handled by OnEditorSwitch below
133 ActivatePanelBasedEditor(editor_type, editor);
134 } else {
135 DeactivatePanelBasedEditor(editor_type, editor, editor_set);
136 }
137 }
138 return;
139 }
140 }
141
142 // Handle non-editor-class cases (Assembly, Emulator, Hex, Settings, Agent)
143 HandleNonEditorClassSwitch(editor_type, force_visible);
144}
145
147 Editor* editor) {
149 return;
150
151 std::string old_category = deps_.window_manager->GetActiveCategory();
152 std::string new_category = EditorRegistry::GetEditorCategory(type);
153
154 // Only trigger OnEditorSwitch if category actually changes
155 if (old_category != new_category) {
156 // Kill any in-flight panel transitions from the previous editor so the
157 // new editor's windows start their fade from a clean state. Without this,
158 // stale alpha values leave old bitmap surfaces ghosted during the switch.
160 deps_.window_manager->OnEditorSwitch(old_category, new_category);
161 }
162
163 // Activate the layout context on every editor activation. LayoutManager
164 // builds the default tree only once, but repeated calls refresh the live
165 // editor/session context used by lazy first-open panel docking.
166 if (deps_.layout_manager) {
168 }
169}
170
172 Editor* editor,
173 EditorSet* editor_set) {
174 if (!deps_.window_manager || !editor_set)
175 return;
176
177 // Switch to another active panel-based editor
178 for (auto* other : editor_set->active_editors_) {
179 if (*other->active() && EditorRegistry::IsPanelBasedEditor(other->type()) &&
180 other != editor) {
181 std::string old_category = deps_.window_manager->GetActiveCategory();
182 std::string new_category =
184 if (old_category != new_category) {
186 deps_.window_manager->OnEditorSwitch(old_category, new_category);
187 }
188
189 // The fallback editor was already active, so it does not pass through
190 // ActivatePanelBasedEditor again. Refresh its live docking context here
191 // before any of its hidden-by-default panels are opened.
192 const EditorType fallback_type = other->type();
193 if (deps_.layout_manager) {
194 QueueEditorLayoutInitialization(fallback_type);
195 }
196 break;
197 }
198 }
199}
200
202 if (!deps_.layout_manager) {
203 return;
204 }
207 return;
208 }
209
210 const bool validate_session = static_cast<bool>(deps_.get_current_session_id);
211 const size_t expected_session_id =
212 validate_session ? deps_.get_current_session_id() : 0;
214 [this, type, validate_session, expected_session_id]() {
215 if (validate_session &&
217 deps_.get_current_session_id() != expected_session_id)) {
218 return;
219 }
221 });
222}
223
225 bool force_visible) {
226 switch (type) {
228 if (deps_.ui_coordinator) {
229 bool is_visible = !deps_.ui_coordinator->IsAsmEditorVisible();
230 if (force_visible) {
231 is_visible = true;
232 }
233
235
236 if (is_visible && deps_.window_manager) {
239 }
240 }
241 break;
242
244 if (deps_.ui_coordinator) {
245 bool is_visible = !deps_.ui_coordinator->IsEmulatorVisible();
246 if (force_visible)
247 is_visible = true;
248
250
251 if (is_visible && deps_.window_manager) {
253
256 ImGuiContext* ctx = ImGui::GetCurrentContext();
257 if (deps_.layout_manager && ctx && ctx->WithinFrameScope) {
258 ImGuiID dockspace_id = ImGui::GetID("MainDockSpace");
262 EditorType::kEmulator, dockspace_id);
263 LOG_INFO("EditorActivator", "Initialized emulator layout");
264 }
265 }
266 });
267 }
268 }
269 }
270 break;
271
272 case EditorType::kHex:
275 "Hex Editor");
276 }
277 break;
278
283 !force_visible) {
285 } else {
288 }
289 }
290 break;
291
292 default:
293 // Other editor types not handled here
294 break;
295 }
296}
297
300 return;
301
302 ImGuiContext* ctx = ImGui::GetCurrentContext();
303 if (!ctx || !ctx->WithinFrameScope) {
306 }
307 return;
308 }
309
310 const bool was_initialized = deps_.layout_manager->IsLayoutInitialized(type);
311 ImGuiID dockspace_id = ImGui::GetID("MainDockSpace");
312 deps_.layout_manager->InitializeEditorLayout(type, dockspace_id);
313 if (!was_initialized && deps_.layout_manager->IsLayoutInitialized(type)) {
314 LOG_INFO("EditorActivator", "Initialized layout for editor type %d",
315 static_cast<int>(type));
316 }
317}
318
320 auto* editor_set =
322 if (!editor_set)
323 return;
324
327 if (!status.ok()) {
328 if (deps_.toast_manager) {
329 deps_.toast_manager->Show("Failed to prepare Dungeon editor: " +
330 std::string(status.message()),
332 }
333 return;
334 }
335 }
336
337 // Switch to dungeon editor
339
340 // Open the room in the dungeon editor
341 editor_set->GetDungeonEditor()->add_room(room_id);
342}
343
345 if (map_id < 0 || map_id >= zelda3::kNumOverworldMaps) {
346 LOG_WARN("EditorActivator", "Rejected invalid overworld map jump target %d",
347 map_id);
348 if (deps_.toast_manager) {
350 "Invalid overworld map ID: " + std::to_string(map_id),
352 }
353 return;
354 }
355
356 auto* editor_set =
358 if (!editor_set)
359 return;
360
362 const auto status =
364 if (!status.ok()) {
365 if (deps_.toast_manager) {
366 deps_.toast_manager->Show("Failed to prepare Overworld editor: " +
367 std::string(status.message()),
369 }
370 return;
371 }
372 }
373
374 // Switch to overworld editor
376
377 // Set the current map in the overworld editor
378 editor_set->GetOverworldEditor()->set_current_map(map_id);
379}
380
381void EditorActivator::JumpToMessage(int message_id) {
382 if (message_id < 0)
383 return;
384
385 auto* editor_set =
387 if (!editor_set)
388 return;
389
392 if (!status.ok()) {
393 if (deps_.toast_manager) {
394 deps_.toast_manager->Show("Failed to prepare Message editor: " +
395 std::string(status.message()),
397 }
398 return;
399 }
400 }
401
402 // Switch to message editor (force visible so this behaves like navigation).
403 SwitchToEditor(EditorType::kMessage, /*force_visible=*/true);
404
405 if (auto* message_editor = editor_set->GetMessageEditor()) {
406 if (!message_editor->OpenMessageById(message_id)) {
407 if (deps_.toast_manager) {
409 "Message ID not found: " + std::to_string(message_id),
411 }
412 }
413 }
414}
415
416void EditorActivator::JumpToAssemblySymbol(const std::string& symbol) {
417 if (symbol.empty())
418 return;
419
420 auto* editor_set =
422 if (!editor_set)
423 return;
424
426 const auto status =
428 if (!status.ok()) {
429 if (deps_.toast_manager) {
430 deps_.toast_manager->Show("Failed to prepare Assembly editor: " +
431 std::string(status.message()),
433 }
434 return;
435 }
436 }
437
438 // Switch to assembly editor (force visible so this behaves like navigation).
439 SwitchToEditor(EditorType::kAssembly, /*force_visible=*/true);
440
441 if (auto* asm_editor = editor_set->GetAssemblyEditor()) {
442 const auto status = asm_editor->JumpToReference(symbol);
443 if (!status.ok()) {
444 if (deps_.toast_manager) {
446 "Assembly jump failed: " + std::string(status.message()),
448 }
449 return;
450 }
451 }
452}
453
454} // namespace editor
455} // namespace yaze
HandlerId Subscribe(std::function< void(const T &)> handler)
Definition event_bus.h:22
void HandleNonEditorClassSwitch(EditorType type, bool force_visible)
void ActivatePanelBasedEditor(EditorType type, Editor *editor)
void JumpToAssemblySymbol(const std::string &symbol)
Jump to an assembly symbol definition in the Assembly editor.
void SwitchToEditor(EditorType type, bool force_visible=false, bool from_dialog=false)
Switch to an editor, optionally forcing visibility.
void Initialize(const Dependencies &deps)
void JumpToMessage(int message_id)
Jump to a specific message ID in the Message editor.
void InitializeEditorLayout(EditorType type)
Initialize the DockBuilder layout for an editor.
void JumpToDungeonRoom(int room_id)
Jump to a specific dungeon room.
void JumpToOverworldMap(int map_id)
Jump to a specific overworld map.
void QueueEditorLayoutInitialization(EditorType type)
void DeactivatePanelBasedEditor(EditorType type, Editor *editor, EditorSet *editor_set)
static bool IsPanelBasedEditor(EditorType type)
static std::string GetEditorCategory(EditorType type)
Contains a complete set of editors for a single ROM instance.
std::vector< Editor * > active_editors_
Interface for editor classes.
Definition editor.h:245
bool IsLayoutInitialized(EditorType type) const
Check if a layout has been initialized for an editor.
void InitializeEditorLayout(EditorType type, ImGuiID dockspace_id)
Initialize the default layout for a specific editor type.
void OpenDrawer(DrawerType type)
Open a specific drawer.
DrawerType GetActiveDrawer() const
Get the currently active drawer type.
void CloseDrawer()
Close the currently active drawer.
void Show(const std::string &message, ToastType type=ToastType::kInfo, float ttl_seconds=3.0f)
void SetEmulatorVisible(bool visible)
void SetAsmEditorVisible(bool visible)
void SetEditorSelectionVisible(bool visible)
void SetActiveCategory(const std::string &category, bool notify=true)
void OnEditorSwitch(const std::string &from_category, const std::string &to_category)
Handle editor/category switching for panel visibility.
bool OpenWindow(size_t session_id, const std::string &base_window_id)
void ClearWorkspaceTransitionState()
Definition animator.cc:89
#define LOG_WARN(category, format,...)
Definition log.h:108
#define LOG_INFO(category, format,...)
Definition log.h:106
Animator & GetAnimator()
Definition animator.cc:318
constexpr int kNumOverworldMaps
Definition common.h:85
std::function< EditorSet *()> get_current_editor_set
std::function< void(std::function< void()>)> queue_deferred_action
std::function< absl::Status(EditorType)> ensure_editor_assets_loaded
Request to navigate to an assembly symbol definition.
Request to navigate to a specific overworld map.
Request to navigate to a specific message ID.
Request to navigate to a specific dungeon room.