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
21namespace yaze {
22namespace editor {
23
25 deps_ = deps;
26 initialized_ = true;
27
28 // Subscribe to navigation events via EventBus
29 if (deps_.event_bus) {
31 [this](const JumpToRoomRequestEvent& event) {
32 JumpToDungeonRoom(event.room_id);
33 });
34
36 [this](const JumpToMapRequestEvent& event) {
37 JumpToOverworldMap(event.map_id);
38 });
39
41 [this](const JumpToMessageRequestEvent& event) {
42 JumpToMessage(event.message_id);
43 });
44
46 [this](const JumpToAssemblySymbolRequestEvent& event) {
47 JumpToAssemblySymbol(event.symbol);
48 });
49
50 LOG_INFO("EditorActivator", "Subscribed to navigation events");
51 }
52}
53
54void EditorActivator::SwitchToEditor(EditorType editor_type, bool force_visible,
55 bool from_dialog) {
56 if (!initialized_) {
57 LOG_WARN("EditorActivator", "Not initialized, cannot switch editor");
58 return;
59 }
60
61 // Avoid touching ImGui docking state when outside a frame
62 ImGuiContext* imgui_ctx = ImGui::GetCurrentContext();
63 const bool frame_active = imgui_ctx != nullptr && imgui_ctx->WithinFrameScope;
64 if (!frame_active && deps_.queue_deferred_action) {
66 [this, editor_type, force_visible, from_dialog]() {
67 SwitchToEditor(editor_type, force_visible, from_dialog);
68 });
69 return;
70 }
71
72 // If NOT coming from dialog, close editor selection UI
73 if (!from_dialog && deps_.ui_coordinator) {
75 }
76
77 auto* editor_set =
79 if (!editor_set) {
80 return;
81 }
82
84 switch (editor_type) {
94 const auto status = deps_.ensure_editor_assets_loaded(editor_type);
95 if (!status.ok()) {
96 if (deps_.toast_manager) {
98 "Failed to prepare editor: " + std::string(status.message()),
100 }
101 return;
102 }
103 break;
104 }
105 default:
106 break;
107 }
108 }
109
110 // Toggle the editor in the active editors list
111 for (auto* editor : editor_set->active_editors_) {
112 if (editor->type() == editor_type) {
113 if (force_visible) {
114 editor->set_active(true);
115 } else {
116 editor->toggle_active();
117 }
118
119 if (EditorRegistry::IsPanelBasedEditor(editor_type)) {
120 if (*editor->active()) {
121 // Smooth transition: fade out old, then fade in new
122 // Panel refresh handled by OnEditorSwitch below
123 ActivatePanelBasedEditor(editor_type, editor);
124 } else {
125 DeactivatePanelBasedEditor(editor_type, editor, editor_set);
126 }
127 }
128 return;
129 }
130 }
131
132 // Handle non-editor-class cases (Assembly, Emulator, Hex, Settings, Agent)
133 HandleNonEditorClassSwitch(editor_type, force_visible);
134}
135
137 Editor* editor) {
139 return;
140
141 std::string old_category = deps_.window_manager->GetActiveCategory();
142 std::string new_category = EditorRegistry::GetEditorCategory(type);
143
144 // Only trigger OnEditorSwitch if category actually changes
145 if (old_category != new_category) {
146 // Kill any in-flight panel transitions from the previous editor so the
147 // new editor's windows start their fade from a clean state. Without this,
148 // stale alpha values leave old bitmap surfaces ghosted during the switch.
150 deps_.window_manager->OnEditorSwitch(old_category, new_category);
151 }
152
153 // Initialize default layout on first activation
154 if (deps_.layout_manager &&
157 deps_.queue_deferred_action([this, type]() {
158 if (deps_.layout_manager &&
160 ImGuiID dockspace_id = ImGui::GetID("MainDockSpace");
161 deps_.layout_manager->InitializeEditorLayout(type, dockspace_id);
162 }
163 });
164 }
165 }
166}
167
169 Editor* editor,
170 EditorSet* editor_set) {
171 if (!deps_.window_manager || !editor_set)
172 return;
173
174 // Switch to another active panel-based editor
175 for (auto* other : editor_set->active_editors_) {
176 if (*other->active() && EditorRegistry::IsPanelBasedEditor(other->type()) &&
177 other != editor) {
178 std::string old_category = deps_.window_manager->GetActiveCategory();
179 std::string new_category =
181 if (old_category != new_category) {
183 deps_.window_manager->OnEditorSwitch(old_category, new_category);
184 }
185 break;
186 }
187 }
188}
189
191 bool force_visible) {
192 switch (type) {
194 if (deps_.ui_coordinator) {
195 if (force_visible) {
197 } else {
200 }
201 }
202 break;
203
205 if (deps_.ui_coordinator) {
206 bool is_visible = !deps_.ui_coordinator->IsEmulatorVisible();
207 if (force_visible)
208 is_visible = true;
209
211
212 if (is_visible && deps_.window_manager) {
214
217 ImGuiContext* ctx = ImGui::GetCurrentContext();
218 if (deps_.layout_manager && ctx && ctx->WithinFrameScope) {
219 ImGuiID dockspace_id = ImGui::GetID("MainDockSpace");
223 EditorType::kEmulator, dockspace_id);
224 LOG_INFO("EditorActivator", "Initialized emulator layout");
225 }
226 }
227 });
228 }
229 }
230 }
231 break;
232
233 case EditorType::kHex:
236 "Hex Editor");
237 }
238 break;
239
244 !force_visible) {
246 } else {
249 }
250 }
251 break;
252
253 default:
254 // Other editor types not handled here
255 break;
256 }
257}
258
261 return;
262
263 ImGuiContext* ctx = ImGui::GetCurrentContext();
264 if (!ctx || !ctx->WithinFrameScope) {
267 [this, type]() { InitializeEditorLayout(type); });
268 }
269 return;
270 }
271
273 ImGuiID dockspace_id = ImGui::GetID("MainDockSpace");
274 deps_.layout_manager->InitializeEditorLayout(type, dockspace_id);
275 LOG_INFO("EditorActivator", "Initialized layout for editor type %d",
276 static_cast<int>(type));
277 }
278}
279
281 auto* editor_set =
283 if (!editor_set)
284 return;
285
288 if (!status.ok()) {
289 if (deps_.toast_manager) {
290 deps_.toast_manager->Show("Failed to prepare Dungeon editor: " +
291 std::string(status.message()),
293 }
294 return;
295 }
296 }
297
298 // Switch to dungeon editor
300
301 // Open the room in the dungeon editor
302 editor_set->GetDungeonEditor()->add_room(room_id);
303}
304
306 auto* editor_set =
308 if (!editor_set)
309 return;
310
312 const auto status =
314 if (!status.ok()) {
315 if (deps_.toast_manager) {
316 deps_.toast_manager->Show("Failed to prepare Overworld editor: " +
317 std::string(status.message()),
319 }
320 return;
321 }
322 }
323
324 // Switch to overworld editor
326
327 // Set the current map in the overworld editor
328 editor_set->GetOverworldEditor()->set_current_map(map_id);
329}
330
331void EditorActivator::JumpToMessage(int message_id) {
332 if (message_id < 0)
333 return;
334
335 auto* editor_set =
337 if (!editor_set)
338 return;
339
342 if (!status.ok()) {
343 if (deps_.toast_manager) {
344 deps_.toast_manager->Show("Failed to prepare Message editor: " +
345 std::string(status.message()),
347 }
348 return;
349 }
350 }
351
352 // Switch to message editor (force visible so this behaves like navigation).
353 SwitchToEditor(EditorType::kMessage, /*force_visible=*/true);
354
355 if (auto* message_editor = editor_set->GetMessageEditor()) {
356 if (!message_editor->OpenMessageById(message_id)) {
357 if (deps_.toast_manager) {
359 "Message ID not found: " + std::to_string(message_id),
361 }
362 }
363 }
364}
365
366void EditorActivator::JumpToAssemblySymbol(const std::string& symbol) {
367 if (symbol.empty())
368 return;
369
370 auto* editor_set =
372 if (!editor_set)
373 return;
374
376 const auto status =
378 if (!status.ok()) {
379 if (deps_.toast_manager) {
380 deps_.toast_manager->Show("Failed to prepare Assembly editor: " +
381 std::string(status.message()),
383 }
384 return;
385 }
386 }
387
388 // Switch to assembly editor (force visible so this behaves like navigation).
389 SwitchToEditor(EditorType::kAssembly, /*force_visible=*/true);
390
391 if (auto* asm_editor = editor_set->GetAssemblyEditor()) {
392 const auto status = asm_editor->JumpToReference(symbol);
393 if (!status.ok()) {
394 if (deps_.toast_manager) {
396 "Assembly jump failed: " + std::string(status.message()),
398 }
399 return;
400 }
401 }
402}
403
404} // namespace editor
405} // 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 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:240
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:107
#define LOG_INFO(category, format,...)
Definition log.h:105
Animator & GetAnimator()
Definition animator.cc:318
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.