aboutsummaryrefslogtreecommitdiff
path: root/src/ui/ui_layer.cpp
blob: 88e1b1d7d1a98f851f5946630135cbaa821cedfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include "ui_layer.h"

#include "scene/scene.h"
#include "core/settings_manager.h"
#include "core/hdri_manager.h"
#include "rendering/render_api.h"

#include <imgui.h>
#include <imgui_internal.h>     // DockBuilder*
#include <ImGuizmo.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <algorithm>
#include <cstdio>
#include <string>

namespace Donut
{
    // Panel window titles. These are the identity DockBuilder docks against, so
    // they must match the ImGui::Begin() titles exactly.
    static const char* kOutliner   = "Outliner";
    static const char* kProperties = "Properties";
    static const char* kBlackHole  = "Black Hole";
    static const char* kSettings   = "Settings";
    static const char* kExport     = "Export";
    static const char* kStats      = "Stats";

    // One-time ImGui theme: rounded, roomy, dark with a warm accretion-disk accent.
    static auto apply_donut_style() -> void
    {
        ImGuiStyle& s = ImGui::GetStyle();
        s.WindowRounding = 7.0f; s.ChildRounding = 5.0f; s.FrameRounding = 4.0f;
        s.GrabRounding = 4.0f; s.PopupRounding = 4.0f; s.ScrollbarRounding = 5.0f; s.TabRounding = 4.0f;
        s.WindowPadding = ImVec2(12, 12); s.FramePadding = ImVec2(9, 5);
        s.ItemSpacing = ImVec2(9, 8); s.ItemInnerSpacing = ImVec2(7, 5);
        s.WindowBorderSize = 0.0f; s.FrameBorderSize = 0.0f; s.WindowTitleAlign = ImVec2(0.02f, 0.5f);

        const ImVec4 amber   = ImVec4(0.98f, 0.62f, 0.20f, 1.00f);
        const ImVec4 amberHi = ImVec4(1.00f, 0.73f, 0.36f, 1.00f);
        ImVec4* c = s.Colors;
        c[ImGuiCol_WindowBg]        = ImVec4(0.07f, 0.08f, 0.10f, 0.97f);
        c[ImGuiCol_ChildBg]         = ImVec4(0.10f, 0.11f, 0.13f, 0.55f);
        c[ImGuiCol_PopupBg]         = ImVec4(0.09f, 0.10f, 0.12f, 0.98f);
        c[ImGuiCol_MenuBarBg]       = ImVec4(0.09f, 0.10f, 0.12f, 1.00f);
        c[ImGuiCol_TitleBg]         = ImVec4(0.09f, 0.10f, 0.12f, 1.00f);
        c[ImGuiCol_TitleBgActive]   = ImVec4(0.13f, 0.14f, 0.17f, 1.00f);
        c[ImGuiCol_Text]            = ImVec4(0.90f, 0.91f, 0.93f, 1.00f);
        c[ImGuiCol_TextDisabled]    = ImVec4(0.48f, 0.50f, 0.54f, 1.00f);
        c[ImGuiCol_FrameBg]         = ImVec4(0.16f, 0.17f, 0.20f, 1.00f);
        c[ImGuiCol_FrameBgHovered]  = ImVec4(0.22f, 0.24f, 0.28f, 1.00f);
        c[ImGuiCol_FrameBgActive]   = ImVec4(0.26f, 0.28f, 0.33f, 1.00f);
        c[ImGuiCol_Header]          = ImVec4(0.17f, 0.19f, 0.23f, 1.00f);
        c[ImGuiCol_HeaderHovered]   = ImVec4(0.24f, 0.27f, 0.32f, 1.00f);
        c[ImGuiCol_HeaderActive]    = ImVec4(0.28f, 0.31f, 0.37f, 1.00f);
        c[ImGuiCol_Button]          = ImVec4(0.20f, 0.22f, 0.26f, 1.00f);
        c[ImGuiCol_ButtonHovered]   = ImVec4(0.27f, 0.30f, 0.35f, 1.00f);
        c[ImGuiCol_ButtonActive]    = amber;
        c[ImGuiCol_SliderGrab]      = amber;
        c[ImGuiCol_SliderGrabActive]= amberHi;
        c[ImGuiCol_CheckMark]       = amberHi;
        c[ImGuiCol_Tab]             = ImVec4(0.13f, 0.14f, 0.17f, 1.00f);
        c[ImGuiCol_TabHovered]      = ImVec4(0.28f, 0.31f, 0.37f, 1.00f);
        c[ImGuiCol_TabActive]       = ImVec4(0.22f, 0.24f, 0.28f, 1.00f);
        c[ImGuiCol_Separator]       = ImVec4(0.20f, 0.22f, 0.26f, 1.00f);
        c[ImGuiCol_ScrollbarGrab]   = ImVec4(0.24f, 0.26f, 0.30f, 1.00f);
        c[ImGuiCol_DockingPreview]  = ImVec4(0.98f, 0.62f, 0.20f, 0.55f);
        c[ImGuiCol_DockingEmptyBg]  = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);   // pass-through
    }

    auto UILayer::draw(const UIContext& ctx) -> View
    {
        static bool styled = false;
        if (!styled) { apply_donut_style(); styled = true; }

        ImGuizmo::BeginFrame();   // the Properties panel draws the scene gizmo

        draw_menu_bar(ctx);

        // Full-viewport dock space with a pass-through centre: panels dock to the
        // edges while the render shows through the middle. Sits under the menu bar.
        // ImGui owns the id (stable per viewport) and persists the layout in imgui.ini.
        const ImGuiID dockspace_id = ImGui::DockSpaceOverViewport(
            0, ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode);

        // On the first frame, seed a default layout unless imgui.ini restored one
        // (a restored dock tree has splits/windows, so the root node isn't empty).
        if (m_first_frame)
        {
            m_first_frame = false;
            ImGuiDockNode* node = ImGui::DockBuilderGetNode(dockspace_id);
            if (node == nullptr || node->IsEmpty())
                m_rebuild_layout = true;
        }
        if (m_rebuild_layout)
        {
            build_layout(dockspace_id);
            m_rebuild_layout = false;
        }

        if (m_show_outliner)   panel_outliner(ctx);
        if (m_show_properties) panel_properties(ctx);
        if (m_show_black_hole) panel_black_hole(ctx);
        if (m_show_settings)   panel_settings(ctx);
        if (m_show_export)     panel_export(ctx);
        if (m_show_stats)      panel_stats(ctx);

        return m_mode == Mode::Scene ? View::Scene : View::BlackHole;
    }

    auto UILayer::draw_menu_bar(const UIContext& ctx) -> void
    {
        if (!ImGui::BeginMainMenuBar()) return;

        if (ImGui::BeginMenu("View"))
        {
            ImGui::TextDisabled("Centre viewport");
            ImGui::Separator();
            if (ImGui::MenuItem("Scene",      nullptr, m_mode == Mode::Scene))       m_mode = Mode::Scene;
            if (ImGui::MenuItem("Simulation", nullptr, m_mode == Mode::Simulation))  m_mode = Mode::Simulation;
            ImGui::EndMenu();
        }

        if (ImGui::BeginMenu("Window"))
        {
            ImGui::MenuItem(kOutliner,   nullptr, &m_show_outliner);
            ImGui::MenuItem(kProperties, nullptr, &m_show_properties);
            ImGui::MenuItem(kBlackHole,  nullptr, &m_show_black_hole);
            ImGui::MenuItem(kSettings,   nullptr, &m_show_settings);
            ImGui::MenuItem(kExport,     nullptr, &m_show_export);
            ImGui::MenuItem(kStats,      nullptr, &m_show_stats);
            ImGui::EndMenu();
        }

        if (ImGui::BeginMenu("Layout"))
        {
            ImGui::TextDisabled("Default layouts");
            ImGui::Separator();
            if (ImGui::MenuItem("Simulation",    nullptr, m_layout == Layout::Simulation))   apply_layout(Layout::Simulation);
            if (ImGui::MenuItem("Scene editing", nullptr, m_layout == Layout::SceneEditing))  apply_layout(Layout::SceneEditing);
            if (ImGui::MenuItem("Export",        nullptr, m_layout == Layout::Export))        apply_layout(Layout::Export);
            ImGui::Separator();
            if (ImGui::MenuItem("Reset current layout")) m_rebuild_layout = true;
            ImGui::EndMenu();
        }

        // Right-aligned mode + FPS readout.
        char status[64];
        std::snprintf(status, sizeof(status), "%s   %.0f FPS",
                      m_mode == Mode::Scene ? "Scene" : "Simulation", ImGui::GetIO().Framerate);
        float tw = ImGui::CalcTextSize(status).x;
        ImGui::SameLine(ImGui::GetWindowWidth() - tw - 16.0f);
        ImGui::TextDisabled("%s", status);

        ImGui::EndMainMenuBar();
    }

    // Rebuild the dock tree for the current layout. Windows named but not currently
    // visible still get a dock home here, so toggling them on lands them sensibly.
    auto UILayer::build_layout(unsigned int dockspace_id) -> void
    {
        ImGui::DockBuilderRemoveNode(dockspace_id);
        // DockSpace is a private flag, PassthruCentralNode a public one; combine as
        // int to avoid the C++20 mixed-enum bitwise-or warning.
        ImGui::DockBuilderAddNode(dockspace_id,
            (int)ImGuiDockNodeFlags_DockSpace | (int)ImGuiDockNodeFlags_PassthruCentralNode);
        ImGui::DockBuilderSetNodeSize(dockspace_id, ImGui::GetMainViewport()->WorkSize);

        ImGuiID centre = dockspace_id;

        switch (m_layout)
        {
        case Layout::SceneEditing:
        {
            ImGuiID left = 0, left_bottom = 0, right = 0;
            ImGui::DockBuilderSplitNode(centre, ImGuiDir_Left,  0.24f, &left,  &centre);
            ImGui::DockBuilderSplitNode(left,   ImGuiDir_Down,  0.50f, &left_bottom, &left);
            ImGui::DockBuilderSplitNode(centre, ImGuiDir_Right, 0.24f, &right, &centre);
            ImGui::DockBuilderDockWindow(kOutliner,   left);
            ImGui::DockBuilderDockWindow(kProperties, left_bottom);
            ImGui::DockBuilderDockWindow(kBlackHole,  right);
            ImGui::DockBuilderDockWindow(kSettings,   right);
            ImGui::DockBuilderDockWindow(kExport,     right);
            ImGui::DockBuilderDockWindow(kStats,      right);
            break;
        }
        case Layout::Export:
        {
            ImGuiID right = 0, right_bottom = 0;
            ImGui::DockBuilderSplitNode(centre, ImGuiDir_Right, 0.28f, &right, &centre);
            ImGui::DockBuilderSplitNode(right,  ImGuiDir_Down,  0.50f, &right_bottom, &right);
            ImGui::DockBuilderDockWindow(kExport,     right);
            ImGui::DockBuilderDockWindow(kOutliner,   right);
            ImGui::DockBuilderDockWindow(kProperties, right);
            ImGui::DockBuilderDockWindow(kBlackHole,  right_bottom);
            ImGui::DockBuilderDockWindow(kSettings,   right_bottom);
            ImGui::DockBuilderDockWindow(kStats,      right_bottom);
            break;
        }
        case Layout::Simulation:
        default:
        {
            ImGuiID right = 0, right_bottom = 0;
            ImGui::DockBuilderSplitNode(centre, ImGuiDir_Right, 0.24f, &right, &centre);
            ImGui::DockBuilderSplitNode(right,  ImGuiDir_Down,  0.30f, &right_bottom, &right);
            ImGui::DockBuilderDockWindow(kBlackHole,  right);
            ImGui::DockBuilderDockWindow(kSettings,   right);
            ImGui::DockBuilderDockWindow(kExport,     right);
            ImGui::DockBuilderDockWindow(kOutliner,   right);
            ImGui::DockBuilderDockWindow(kProperties, right);
            ImGui::DockBuilderDockWindow(kStats,      right_bottom);
            break;
        }
        }

        ImGui::DockBuilderFinish(dockspace_id);
    }

    // Apply a preset: set the centre view, choose which panels are visible, and
    // request a dock-tree rebuild next frame.
    auto UILayer::apply_layout(Layout layout) -> void
    {
        m_layout = layout;
        m_rebuild_layout = true;
        switch (layout)
        {
        case Layout::SceneEditing:
            m_mode = Mode::Scene;
            m_show_outliner = true;  m_show_properties = true;
            m_show_black_hole = false; m_show_settings = false; m_show_export = false;
            m_show_stats = true;
            break;
        case Layout::Export:
            m_mode = Mode::Simulation;
            m_show_export = true; m_show_black_hole = true;
            m_show_outliner = false; m_show_properties = false; m_show_settings = false;
            m_show_stats = true;
            break;
        case Layout::Simulation:
        default:
            m_mode = Mode::Simulation;
            m_show_black_hole = true;
            m_show_outliner = false; m_show_properties = false;
            m_show_settings = false; m_show_export = false;
            m_show_stats = true;
            break;
        }
    }

    auto UILayer::panel_outliner(const UIContext& ctx) -> void
    {
        if (ImGui::Begin(kOutliner, &m_show_outliner))
        {
            Scene& sc = ctx.scene;
            auto& objs = sc.objects;
            if (sc.selected_object >= (int)objs.size()) sc.selected_object = (int)objs.size() - 1;

            if (ImGui::Button("Add sphere") && objs.size() < 64)
            {
                SceneObject o;
                o.position = glm::vec3(((int)objs.size() % 5) * 5.0f - 10.0f, 2.0f, ((int)objs.size() / 5) * 5.0f);
                o.radius   = 1.5f;
                o.color    = glm::vec3(0.35f + 0.12f * (objs.size() % 5), 0.55f, 0.9f - 0.12f * (objs.size() % 4));
                objs.push_back(o);
                sc.selected_object = (int)objs.size() - 1;
            }
            ImGui::SameLine();
            ImGui::BeginDisabled(objs.empty());
            if (ImGui::Button("Delete") && !objs.empty())
            {
                objs.erase(objs.begin() + sc.selected_object);
                if (sc.selected_object >= (int)objs.size()) sc.selected_object = (int)objs.size() - 1;
            }
            ImGui::EndDisabled();

            ImGui::SameLine();
            ImGui::TextDisabled("%d / 64", (int)objs.size());
            ImGui::Separator();

            ImGui::BeginChild("obj_list");
            for (int i = 0; i < (int)objs.size(); ++i)
            {
                std::string label = "Sphere " + std::to_string(i);
                if (ImGui::Selectable(label.c_str(), sc.selected_object == i)) sc.selected_object = i;
            }
            if (objs.empty()) ImGui::TextDisabled("No spheres. Add one above.");
            ImGui::EndChild();
        }
        ImGui::End();
    }

    auto UILayer::panel_properties(const UIContext& ctx) -> void
    {
        if (ImGui::Begin(kProperties, &m_show_properties))
        {
            Scene& sc = ctx.scene;
            auto& objs = sc.objects;
            if (objs.empty() || sc.selected_object < 0 || sc.selected_object >= (int)objs.size())
            {
                ImGui::TextDisabled("Select a sphere in the Outliner.");
            }
            else
            {
                SceneObject& o = objs[sc.selected_object];
                ImGui::Text("Sphere %d", sc.selected_object);
                ImGui::Separator();
                ImGui::DragFloat3("Position", &o.position.x, 0.1f);
                ImGui::DragFloat("Radius", &o.radius, 0.05f, 0.1f, 20.0f);
                ImGui::ColorEdit3("Color", &o.color.x);

                ImGui::Spacing();
                if (ImGui::RadioButton("Move",  m_gizmo_op == 0)) m_gizmo_op = 0;
                ImGui::SameLine();
                if (ImGui::RadioButton("Scale", m_gizmo_op == 1)) m_gizmo_op = 1;

                if (m_mode == Mode::Scene)
                {
                    // Viewport gizmo for the selected sphere, using the scene camera
                    // (its projection was set for this frame by the Application). Drawn
                    // into the background list so it sits over the render, under panels.
                    ImGuizmo::SetOrthographic(false);
                    ImGuizmo::SetDrawlist(ImGui::GetBackgroundDrawList());
                    ImGuiIO& io = ImGui::GetIO();
                    ImGuizmo::SetRect(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y);
                    glm::mat4 view = sc.scene_camera.get_view_matrix();
                    glm::mat4 proj = sc.scene_camera.get_projection_matrix();
                    glm::mat4 xform = glm::translate(glm::mat4(1.0f), o.position)
                                    * glm::scale(glm::mat4(1.0f), glm::vec3(o.radius));
                    ImGuizmo::OPERATION op = (m_gizmo_op == 1) ? ImGuizmo::SCALE : ImGuizmo::TRANSLATE;
                    if (ImGuizmo::Manipulate(&view[0][0], &proj[0][0], op, ImGuizmo::WORLD, &xform[0][0]))
                    {
                        float t[3], r[3], s[3];
                        ImGuizmo::DecomposeMatrixToComponents(&xform[0][0], t, r, s);
                        o.position = glm::vec3(t[0], t[1], t[2]);
                        o.radius   = std::max(0.05f, (s[0] + s[1] + s[2]) / 3.0f);
                    }
                    ImGui::TextDisabled("Drag the handles in the viewport.");
                }
                else
                {
                    ImGui::TextDisabled("Switch View to Scene to drag the gizmo.");
                }
            }
        }
        ImGui::End();
    }

    auto UILayer::panel_black_hole(const UIContext& ctx) -> void
    {
        if (ImGui::Begin(kBlackHole, &m_show_black_hole))
        {
            Scene& sc = ctx.scene;
            BlackHoleParams& bh = sc.black_hole;

            if (ImGui::CollapsingHeader("Camera", ImGuiTreeNodeFlags_DefaultOpen))
            {
                bool ff = sc.is_free_fly();
                if (ImGui::RadioButton("Orbital", !ff)) sc.set_free_fly(false);
                ImGui::SameLine();
                if (ImGui::RadioButton("Free-fly", ff)) sc.set_free_fly(true);
                ImGui::SameLine();
                if (ImGui::Button("Reset view")) sc.reset_sim_camera();
                ImGui::SliderFloat("FOV", &bh.fov_degrees, 20.0f, 90.0f, "%.0f\xc2\xb0");
                ImGui::TextDisabled(ff ? "WASD move | Q/E down-up | Shift boost | drag look"
                                       : "Drag to orbit | scroll to zoom");
            }

            if (ImGui::CollapsingHeader("Accretion disk", ImGuiTreeNodeFlags_DefaultOpen))
            {
                ImGui::SliderFloat("Temperature", &bh.temperature, 2000.0f, 15000.0f, "%.0f K");
                ImGui::SliderFloat("Brightness",  &bh.brightness, 0.0f, 3.0f, "%.2f");
                ImGui::SliderFloat("Inner radius", &bh.disk_inner_rs, 3.0f, 12.0f, "%.1f r_s");
                if (bh.disk_outer_rs < bh.disk_inner_rs + 0.5f) bh.disk_outer_rs = bh.disk_inner_rs + 0.5f;
                ImGui::SliderFloat("Outer radius", &bh.disk_outer_rs, bh.disk_inner_rs + 0.5f, 25.0f, "%.1f r_s");
                ImGui::SliderFloat("Turbulence",  &bh.turbulence, 0.0f, 2.0f, "%.2f");
                ImGui::TextDisabled("Inner edge is the ISCO (3 r_s). Novikov-Thorne profile.");
            }

            if (ImGui::CollapsingHeader("Quality"))
            {
                ImGui::SliderInt("Integration steps", &bh.quality_steps, 2000, 15000);
                ImGui::TextDisabled("Deeper lensing at higher cost. Settled frame is 4x supersampled.");
            }
        }
        ImGui::End();
    }

    auto UILayer::panel_settings(const UIContext& ctx) -> void
    {
        if (ImGui::Begin(kSettings, &m_show_settings))
        {
            auto& s = SettingsManager::get_settings();

            if (ImGui::CollapsingHeader("Display", ImGuiTreeNodeFlags_DefaultOpen))
            {
                const bool fs = ctx.actions.is_fullscreen();
                int cw = 0, ch = 0; ctx.actions.get_window_size(cw, ch);

                struct Res { const char* label; int w, h; };
                static const Res kRes[] = {
                    { "1280 x 720",  1280, 720 }, { "1600 x 900",  1600, 900 },
                    { "1920 x 1080", 1920, 1080 }, { "2560 x 1440", 2560, 1440 },
                };
                char cur_label[32]; std::snprintf(cur_label, sizeof(cur_label), "%d x %d", cw, ch);
                ImGui::BeginDisabled(fs);
                if (ImGui::BeginCombo("Resolution", cur_label))
                {
                    for (const auto& r : kRes)
                        if (ImGui::Selectable(r.label, cw == r.w && ch == r.h))
                            ctx.actions.set_resolution(r.w, r.h);
                    ImGui::EndCombo();
                }
                ImGui::EndDisabled();

                bool fs_v = fs;
                if (ImGui::Checkbox("Fullscreen", &fs_v)) ctx.actions.set_fullscreen(fs_v);

                if (ImGui::Checkbox("V-Sync", &s.graphics.v_sync_enabled))
                {
                    ctx.actions.set_vsync(s.graphics.v_sync_enabled);
                    SettingsManager::save_settings();
                }

                ImGui::BeginDisabled(s.graphics.v_sync_enabled);
                if (ImGui::SliderInt("Frame cap", &s.simulation.target_fps, 30, 240, "%d FPS"))
                    SettingsManager::save_settings();
                ImGui::EndDisabled();
                if (s.graphics.v_sync_enabled)
                    ImGui::TextDisabled("Frame cap applies only with V-Sync off.");

                if (ImGui::SliderFloat("UI scale", &m_ui_scale, 0.8f, 1.6f, "%.2fx"))
                    ctx.actions.set_ui_scale(m_ui_scale);
            }

            if (ImGui::CollapsingHeader("Renderer", ImGuiTreeNodeFlags_DefaultOpen))
            {
                const char* apis[] = { "OpenGL", "Vulkan" };
                int cur = (s.graphics.render_api == "Vulkan") ? 1 : 0;
                if (ImGui::Combo("Graphics API", &cur, apis, 2))
                {
                    s.graphics.render_api = apis[cur];
                    SettingsManager::save_settings();
                }
                const char* running = (RendererAPI::get_api() == RendererAPI::API::Vulkan) ? "Vulkan" : "OpenGL";
                if (s.graphics.render_api != running)
                    ImGui::TextColored(ImVec4(0.98f, 0.62f, 0.20f, 1.0f), "Restart to apply (%s running)", running);
                else
                    ImGui::TextDisabled("Active backend: %s (%s)", running, ctx.device_name.c_str());
            }

            if (ImGui::CollapsingHeader("Overlay", ImGuiTreeNodeFlags_DefaultOpen))
            {
                bool dirty = false;
                dirty |= ImGui::Checkbox("Show FPS",        &s.graphics.show_fps);
                dirty |= ImGui::Checkbox("Show frame time", &s.graphics.show_performance_metrics);
                if (dirty) SettingsManager::save_settings();
            }

            if (ImGui::CollapsingHeader("Environment", ImGuiTreeNodeFlags_DefaultOpen))
            {
                auto& hdri = HDRIManager::get();
                std::string preview = hdri.get_hdri_name(ctx.scene.hdri_path);
                if (ImGui::BeginCombo("Starfield", preview.c_str()))
                {
                    for (const auto& path : hdri.get_available_hdri())
                    {
                        bool selected = (path == ctx.scene.hdri_path);
                        if (ImGui::Selectable(hdri.get_hdri_name(path).c_str(), selected))
                            ctx.scene.hdri_path = path;   // RenderPath rebuilds the cubemap next frame
                        if (selected) ImGui::SetItemDefaultFocus();
                    }
                    ImGui::EndCombo();
                }
                ImGui::TextDisabled("Switching rebuilds the cubemap (brief pause).");
            }
        }
        ImGui::End();
    }

    auto UILayer::panel_export(const UIContext& ctx) -> void
    {
        if (ImGui::Begin(kExport, &m_show_export))
        {
            ImGui::TextDisabled("Render the black hole + observables to disk.");
            ImGui::Spacing();
            if (ImGui::CollapsingHeader("Channels", ImGuiTreeNodeFlags_DefaultOpen))
            {
                ImGui::Checkbox("Colour (tone-mapped)",  &m_exp_color);
                ImGui::Checkbox("Redshift (g)",          &m_exp_redshift);
                ImGui::Checkbox("Emission temperature",  &m_exp_temperature);
                ImGui::Checkbox("Impact parameter",      &m_exp_impact);
                ImGui::TextDisabled("Redshift & temperature are disk-only; impact is whole-frame.");
            }
            if (ImGui::CollapsingHeader("Output", ImGuiTreeNodeFlags_DefaultOpen))
            {
                ImGui::Combo("Resolution", &m_exp_resolution, "1280 x 720\0" "1920 x 1080\0" "2560 x 1440\0" "3840 x 2160\0");
                ImGui::Combo("Format",     &m_exp_format,     "Display image (PNG)\0" "Raw float image (PFM)\0" "Raw values (CSV)\0");
                if (m_exp_format == 0) ImGui::TextDisabled("Tone-mapped colour / false-coloured observables.");
                else                   ImGui::TextDisabled("Actual physical values: g, temperature (K), impact (r_s).");
            }

            ImGui::Spacing();
            const bool any = m_exp_color || m_exp_redshift || m_exp_temperature || m_exp_impact;
            ImGui::BeginDisabled(!any);
            if (ImGui::Button("Export frame", ImVec2(-1.0f, 0.0f)))
            {
                static const int dims[][2] = { {1280,720}, {1920,1080}, {2560,1440}, {3840,2160} };
                ExportConfig cfg;
                cfg.color = m_exp_color; cfg.redshift = m_exp_redshift;
                cfg.temperature = m_exp_temperature; cfg.impact = m_exp_impact;
                cfg.width = dims[m_exp_resolution][0]; cfg.height = dims[m_exp_resolution][1];
                cfg.format = static_cast<ExportFormat>(m_exp_format);
                m_exp_written = ctx.actions.export_frame(cfg);
                m_exp_reported = true;
            }
            ImGui::EndDisabled();

            if (m_exp_reported)
            {
                if (m_exp_written > 0)
                    ImGui::TextColored(ImVec4(0.55f, 0.85f, 0.45f, 1.0f), "Wrote %d file(s) to exports/", m_exp_written);
                else
                    ImGui::TextColored(ImVec4(0.95f, 0.45f, 0.35f, 1.0f), "Export failed (see log)");
            }
        }
        ImGui::End();
    }

    auto UILayer::panel_stats(const UIContext& ctx) -> void
    {
        if (ImGui::Begin(kStats, &m_show_stats))
        {
            ImGuiIO& io = ImGui::GetIO();
            ImGui::TextDisabled("%s", ctx.device_name.empty() ? "GPU" : ctx.device_name.c_str());
            const bool show_fps = SettingsManager::get_show_fps();
            const bool show_ms  = SettingsManager::get_show_performance_metrics();
            if (show_fps) ImGui::Text("%.0f FPS", io.Framerate);
            if (show_ms)
            {
                if (show_fps) ImGui::SameLine();
                ImGui::TextDisabled("%.1f ms/frame", io.Framerate > 0 ? 1000.0f / io.Framerate : 0.0f);
            }
            if (!show_fps && !show_ms)
                ImGui::TextDisabled("Enable FPS / frame time in Settings > Overlay.");
        }
        ImGui::End();
    }
}