diff options
Diffstat (limited to 'src/ui')
| -rw-r--r-- | src/ui/ui_layer.cpp | 862 | ||||
| -rw-r--r-- | src/ui/ui_layer.h | 74 |
2 files changed, 682 insertions, 254 deletions
diff --git a/src/ui/ui_layer.cpp b/src/ui/ui_layer.cpp index 7d71ccc..69e2e41 100644 --- a/src/ui/ui_layer.cpp +++ b/src/ui/ui_layer.cpp @@ -1,6 +1,7 @@ #include "ui_layer.h" #include "scene/scene.h" +#include "core/log.h" #include "core/settings_manager.h" #include "core/hdri_manager.h" #include "rendering/render_api.h" @@ -11,57 +12,291 @@ #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <algorithm> +#include <cfloat> #include <cstdio> +#include <cstring> #include <string> namespace Donut { - 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"; + struct TabDesc { const char* label; const char* key; View view; }; + struct PanelDesc { const char* label; const char* key; }; + + static const TabDesc kTabs[kTabCount] = { + { "Simulation", "simulation", View::BlackHole }, + { "Scene Editor", "scene", View::Scene }, + { "Export", "export", View::BlackHole }, + }; + static const PanelDesc kPanels[kPanelCount] = { + { "Outliner", "outliner" }, + { "Properties", "properties" }, + { "Black Hole", "black_hole" }, + { "Settings", "settings" }, + { "Export", "export" }, + { "Diagnostics", "stats" }, + }; + + static const bool kAllowed[kTabCount][kPanelCount] = { + { true, true, true, true, false, true }, + { true, true, false, true, false, true }, + { true, true, true, true, true, true }, + }; + static const bool kDefaults[kTabCount][kPanelCount] = { + { false, false, true, false, false, true }, + { true, true, false, false, false, true }, + { false, false, true, false, true, true }, + }; + + static constexpr float kBodySize = 15.0f; + static constexpr float kCaptionSize = 13.0f; + static constexpr float kHeadSize = 18.0f; + static constexpr float kBigSize = 30.0f; + + static const ImVec4 kAmber (0.98f, 0.62f, 0.20f, 1.00f); + static const ImVec4 kAmberHi (1.00f, 0.73f, 0.36f, 1.00f); + static const ImVec4 kInk (0.09f, 0.07f, 0.03f, 1.00f); + static const ImVec4 kGreen (0.55f, 0.85f, 0.45f, 1.00f); + static const ImVec4 kRed (0.95f, 0.45f, 0.35f, 1.00f); + + struct Persisted + { + int active = 0; + bool show[kTabCount][kPanelCount] = {}; + bool loaded = false; + }; + static Persisted s_persisted; + + static auto settings_read_open(ImGuiContext*, ImGuiSettingsHandler*, const char* name) -> void* + { + return std::strcmp(name, "Workspaces") == 0 ? &s_persisted : nullptr; + } + + static auto settings_read_line(ImGuiContext*, ImGuiSettingsHandler*, void*, const char* line) -> void + { + const char* eq = std::strchr(line, '='); + if (!eq) return; + const std::string lhs(line, eq); + const char* rhs = eq + 1; + + if (lhs == "active") + { + for (int t = 0; t < kTabCount; ++t) + if (std::strcmp(rhs, kTabs[t].key) == 0) { s_persisted.active = t; s_persisted.loaded = true; } + return; + } + for (int t = 0; t < kTabCount; ++t) + { + if (lhs != kTabs[t].key) continue; + s_persisted.loaded = true; + for (bool& b : s_persisted.show[t]) b = false; + for (const char* tok = rhs; *tok; ) + { + const char* end = std::strchr(tok, ','); + const size_t len = end ? (size_t)(end - tok) : std::strlen(tok); + for (int p = 0; p < kPanelCount; ++p) + if (std::strlen(kPanels[p].key) == len && std::strncmp(tok, kPanels[p].key, len) == 0) + s_persisted.show[t][p] = true; + tok = end ? end + 1 : tok + len; + } + } + } + + static auto settings_write_all(ImGuiContext*, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out) -> void + { + out->appendf("[%s][Workspaces]\n", handler->TypeName); + out->appendf("active=%s\n", kTabs[s_persisted.active].key); + for (int t = 0; t < kTabCount; ++t) + { + out->appendf("%s=", kTabs[t].key); + bool first = true; + for (int p = 0; p < kPanelCount; ++p) + { + if (!s_persisted.show[t][p]) continue; + if (!first) out->append(","); + out->append(kPanels[p].key); + first = false; + } + out->append("\n"); + } + out->append("\n"); + } 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); + s.WindowRounding = 8.0f; s.ChildRounding = 6.0f; s.FrameRounding = 6.0f; + s.GrabRounding = 6.0f; s.PopupRounding = 8.0f; s.ScrollbarRounding = 8.0f; s.TabRounding = 6.0f; + s.WindowPadding = ImVec2(14.0f, 12.0f); s.FramePadding = ImVec2(10.0f, 6.0f); + s.ItemSpacing = ImVec2(8.0f, 7.0f); s.ItemInnerSpacing = ImVec2(8.0f, 5.0f); + s.IndentSpacing = 18.0f; s.ScrollbarSize = 10.0f; s.GrabMinSize = 10.0f; + s.WindowBorderSize = 1.0f; s.FrameBorderSize = 0.0f; s.PopupBorderSize = 1.0f; s.ChildBorderSize = 1.0f; + s.WindowTitleAlign = ImVec2(0.0f, 0.5f); + s.TabBarOverlineSize = 2.0f; + s.DockingSeparatorSize = 2.0f; + + const ImVec4 bg0 (0.055f, 0.062f, 0.080f, 1.00f); + const ImVec4 bg1 (0.095f, 0.105f, 0.130f, 1.00f); + const ImVec4 bg2 (0.140f, 0.155f, 0.190f, 1.00f); + const ImVec4 bg3 (0.185f, 0.205f, 0.250f, 1.00f); + const ImVec4 line(0.170f, 0.190f, 0.230f, 1.00f); + const ImVec4 text(0.920f, 0.930f, 0.950f, 1.00f); + const ImVec4 dim (0.550f, 0.580f, 0.640f, 1.00f); + const ImVec4 amber_dim(0.98f, 0.62f, 0.20f, 0.35f); - 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); + c[ImGuiCol_Text] = text; + c[ImGuiCol_TextDisabled] = dim; + c[ImGuiCol_WindowBg] = bg0; + c[ImGuiCol_ChildBg] = ImVec4(bg1.x, bg1.y, bg1.z, 0.60f); + c[ImGuiCol_PopupBg] = ImVec4(bg0.x, bg0.y, bg0.z, 0.98f); + c[ImGuiCol_Border] = line; + c[ImGuiCol_BorderShadow] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f); + c[ImGuiCol_FrameBg] = bg1; + c[ImGuiCol_FrameBgHovered] = bg2; + c[ImGuiCol_FrameBgActive] = bg3; + c[ImGuiCol_TitleBg] = bg0; + c[ImGuiCol_TitleBgActive] = bg1; + c[ImGuiCol_TitleBgCollapsed] = bg0; + c[ImGuiCol_MenuBarBg] = bg0; + c[ImGuiCol_ScrollbarBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f); + c[ImGuiCol_ScrollbarGrab] = bg2; + c[ImGuiCol_ScrollbarGrabHovered] = bg3; + c[ImGuiCol_ScrollbarGrabActive] = amber_dim; + c[ImGuiCol_CheckMark] = kAmberHi; + c[ImGuiCol_SliderGrab] = kAmber; + c[ImGuiCol_SliderGrabActive] = kAmberHi; + c[ImGuiCol_Button] = bg2; + c[ImGuiCol_ButtonHovered] = bg3; + c[ImGuiCol_ButtonActive] = kAmber; + c[ImGuiCol_Header] = ImVec4(0.110f, 0.120f, 0.150f, 1.00f); + c[ImGuiCol_HeaderHovered] = bg2; + c[ImGuiCol_HeaderActive] = bg3; + c[ImGuiCol_Separator] = line; + c[ImGuiCol_SeparatorHovered] = kAmber; + c[ImGuiCol_SeparatorActive] = kAmberHi; + c[ImGuiCol_ResizeGrip] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f); + c[ImGuiCol_ResizeGripHovered] = amber_dim; + c[ImGuiCol_ResizeGripActive] = kAmber; + c[ImGuiCol_Tab] = bg0; + c[ImGuiCol_TabHovered] = bg2; + c[ImGuiCol_TabSelected] = bg1; + c[ImGuiCol_TabSelectedOverline] = kAmber; + c[ImGuiCol_TabDimmed] = bg0; + c[ImGuiCol_TabDimmedSelected] = bg1; + c[ImGuiCol_TabDimmedSelectedOverline] = amber_dim; + c[ImGuiCol_DockingPreview] = ImVec4(kAmber.x, kAmber.y, kAmber.z, 0.50f); + c[ImGuiCol_DockingEmptyBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f); + c[ImGuiCol_PlotLines] = kAmber; + c[ImGuiCol_PlotLinesHovered] = kAmberHi; + c[ImGuiCol_PlotHistogram] = kAmber; + c[ImGuiCol_PlotHistogramHovered] = kAmberHi; + c[ImGuiCol_TableHeaderBg] = bg1; + c[ImGuiCol_TableBorderStrong] = line; + c[ImGuiCol_TableBorderLight] = ImVec4(line.x, line.y, line.z, 0.5f); + c[ImGuiCol_TableRowBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f); + c[ImGuiCol_TableRowBgAlt] = ImVec4(1.0f, 1.0f, 1.0f, 0.025f); + c[ImGuiCol_TextSelectedBg] = amber_dim; + c[ImGuiCol_DragDropTarget] = kAmberHi; + } + + static auto prop(const char* label, float column = 116.0f) -> void + { + ImGui::AlignTextToFramePadding(); + ImGui::TextUnformatted(label); + ImGui::SameLine(column); + ImGui::SetNextItemWidth(-FLT_MIN); + } + + static auto accent_button(const char* label, const ImVec2& size = ImVec2(0.0f, 0.0f)) -> bool + { + ImGui::PushStyleColor(ImGuiCol_Button, kAmber); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, kAmberHi); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, kAmberHi); + ImGui::PushStyleColor(ImGuiCol_Text, kInk); + const bool pressed = ImGui::Button(label, size); + ImGui::PopStyleColor(4); + return pressed; + } + + UILayer::UILayer() + { + for (int t = 0; t < kTabCount; ++t) + { + TabState& tab = m_tabs[t]; + tab.id = static_cast<Tab>(t); + tab.label = kTabs[t].label; + tab.key = kTabs[t].key; + tab.view = kTabs[t].view; + tab.dock_id = ImHashStr((std::string("donut.dock.") + tab.key).c_str()); + for (int p = 0; p < kPanelCount; ++p) + { + tab.name[p] = std::string(kPanels[p].label) + "###" + tab.key + "." + kPanels[p].key; + tab.allowed[p] = kAllowed[t][p]; + tab.defaults[p] = kDefaults[t][p]; + tab.show[p] = kDefaults[t][p]; + } + } + } + + auto UILayer::init() -> void + { + ImGuiIO& io = ImGui::GetIO(); + m_font_body = io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-Regular.ttf", kBodySize); + m_font_heading = io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-SemiBold.ttf", kHeadSize); + if (!m_font_body) m_font_body = io.Fonts->AddFontDefault(); + if (!m_font_heading) m_font_heading = m_font_body; + io.FontDefault = m_font_body; + + ImGuiSettingsHandler handler; + handler.TypeName = "Donut"; + handler.TypeHash = ImHashStr("Donut"); + handler.ReadOpenFn = settings_read_open; + handler.ReadLineFn = settings_read_line; + handler.WriteAllFn = settings_write_all; + ImGui::AddSettingsHandler(&handler); + } + + auto UILayer::section(const char* title, bool default_open) -> bool + { + ImGui::PushFont(m_font_heading, kHeadSize); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(8.0f, 6.0f)); + const bool open = ImGui::CollapsingHeader(title, default_open ? ImGuiTreeNodeFlags_DefaultOpen : 0); + ImGui::PopStyleVar(); + ImGui::PopFont(); + if (open) ImGui::Spacing(); + return open; + } + + auto UILayer::caption(const char* text) -> void + { + ImGui::PushFont(m_font_body, kCaptionSize); + ImGui::PushTextWrapPos(0.0f); + ImGui::TextDisabled("%s", text); + ImGui::PopTextWrapPos(); + ImGui::PopFont(); + } + + auto UILayer::segmented(const char* id, const char* const* labels, int count, int& value) -> bool + { + bool changed = false; + ImGui::PushID(id); + const float gap = ImGui::GetStyle().ItemInnerSpacing.x; + const float width = (ImGui::GetContentRegionAvail().x - gap * (float)(count - 1)) / (float)count; + for (int i = 0; i < count; ++i) + { + if (i > 0) ImGui::SameLine(0.0f, gap); + const bool active = (value == i); + if (active) + { + ImGui::PushStyleColor(ImGuiCol_Button, kAmber); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, kAmberHi); + ImGui::PushStyleColor(ImGuiCol_Text, kInk); + } + if (ImGui::Button(labels[i], ImVec2(width, 0.0f)) && !active) { value = i; changed = true; } + if (active) ImGui::PopStyleColor(3); + } + ImGui::PopID(); + return changed; } auto UILayer::draw(const UIContext& ctx) -> View @@ -71,180 +306,283 @@ namespace Donut ImGuizmo::BeginFrame(); - draw_menu_bar(ctx); - - const ImGuiID dockspace_id = ImGui::DockSpaceOverViewport( - 0, ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode); - 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 (s_persisted.loaded) + { + m_active_tab = std::clamp(s_persisted.active, 0, kTabCount - 1); + for (int t = 0; t < kTabCount; ++t) + for (int p = 0; p < kPanelCount; ++p) + m_tabs[t].show[p] = m_tabs[t].allowed[p] && s_persisted.show[t][p]; + } + for (TabState& tab : m_tabs) + { + if (ImGui::DockBuilderGetNode(tab.dock_id) != nullptr) continue; + bool known = false; + for (int p = 0; p < kPanelCount; ++p) + if (tab.allowed[p] && ImGui::FindWindowSettingsByID(ImHashStr(tab.name[p].c_str()))) known = true; + tab.reset = !known; + } + m_tab_request = m_active_tab; + m_tabs[m_active_tab].appearing = true; } - 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); + for (TabState& tab : m_tabs) + if (tab.reset) { build_layout(tab); tab.reset = false; } + + draw_menu_bar(ctx); + draw_tab_strip(); + draw_status_bar(ctx); + + TabState& tab = m_tabs[m_active_tab]; + ImGui::DockSpaceOverViewport(tab.dock_id, ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode); - return m_mode == Mode::Scene ? View::Scene : View::BlackHole; + for (int p = 0; p < kPanelCount; ++p) + if (tab.allowed[p] && tab.show[p]) draw_panel(ctx, tab, static_cast<Panel>(p)); + tab.appearing = false; + + sync_persisted(); + return tab.view; } auto UILayer::draw_menu_bar(const UIContext&) -> void { if (!ImGui::BeginMainMenuBar()) return; - if (ImGui::BeginMenu("View")) + ImGui::PushFont(m_font_heading, kBodySize); + ImGui::TextColored(kAmber, "Donut"); + ImGui::PopFont(); + ImGui::Separator(); + + TabState& tab = m_tabs[m_active_tab]; + + if (ImGui::BeginMenu("Workspace")) { - 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; + const char* mod = ImGui::GetIO().ConfigMacOSXBehaviors ? "Cmd" : "Ctrl"; + for (int i = 0; i < kTabCount; ++i) + { + char shortcut[16]; + std::snprintf(shortcut, sizeof(shortcut), "%s+%d", mod, i + 1); + if (ImGui::MenuItem(m_tabs[i].label, shortcut, m_active_tab == i)) request_tab(i); + } 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::TextDisabled("%s panels", tab.label); + ImGui::Separator(); + for (int p = 0; p < kPanelCount; ++p) + if (tab.allowed[p]) ImGui::MenuItem(kPanels[p].label, nullptr, &tab.show[p]); 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; + if (ImGui::MenuItem("Reset this workspace")) tab.reset = true; + if (ImGui::MenuItem("Reset all workspaces")) for (TabState& t : m_tabs) t.reset = true; ImGui::EndMenu(); } - 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(); } - auto UILayer::build_layout(unsigned int dockspace_id) -> void + auto UILayer::draw_tab_strip() -> void { - ImGui::DockBuilderRemoveNode(dockspace_id); - ImGui::DockBuilderAddNode(dockspace_id, - (int)ImGuiDockNodeFlags_DockSpace | (int)ImGuiDockNodeFlags_PassthruCentralNode); - ImGui::DockBuilderSetNodeSize(dockspace_id, ImGui::GetMainViewport()->WorkSize); + for (int i = 0; i < kTabCount; ++i) + if (ImGui::Shortcut(ImGuiMod_Ctrl | (ImGuiKey_1 + i), ImGuiInputFlags_RouteGlobal)) request_tab(i); - ImGuiID centre = dockspace_id; + ImGuiViewport* vp = ImGui::GetMainViewport(); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(18.0f, 6.0f)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8.0f, 2.0f)); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); + const float height = ImGui::GetFrameHeight() + 4.0f; + const ImGuiWindowFlags flags = ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse + | ImGuiWindowFlags_NoSavedSettings; + if (ImGui::BeginViewportSideBar("##donut_tabs", vp, ImGuiDir_Up, height, flags)) + { + ImGui::PushFont(m_font_heading, kBodySize); + if (ImGui::BeginTabBar("##workspaces", ImGuiTabBarFlags_NoTooltip | ImGuiTabBarFlags_DrawSelectedOverline + | ImGuiTabBarFlags_NoTabListScrollingButtons)) + { + const bool can_switch = ImGui::GetCurrentContext()->MovingWindow == nullptr && !ImGui::IsDragDropActive(); + for (int i = 0; i < kTabCount; ++i) + { + const ImGuiTabItemFlags item_flags = ImGuiTabItemFlags_NoReorder | ImGuiTabItemFlags_NoCloseWithMiddleMouseButton + | (m_tab_request == i ? ImGuiTabItemFlags_SetSelected : 0); + if (ImGui::BeginTabItem(m_tabs[i].label, nullptr, item_flags)) + { + if (i != m_active_tab && can_switch && m_tab_request < 0) { m_active_tab = i; m_tabs[i].appearing = true; } + ImGui::EndTabItem(); + } + } + ImGui::EndTabBar(); + } + ImGui::PopFont(); + } + for (int i = 0; i < kTabCount; ++i) + if (i != m_active_tab) + ImGui::DockSpace(m_tabs[i].dock_id, ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_KeepAliveOnly); + ImGui::End(); + ImGui::PopStyleVar(3); + m_tab_request = -1; + } - switch (m_layout) + auto UILayer::draw_status_bar(const UIContext& ctx) -> void + { + ImGuiViewport* vp = ImGui::GetMainViewport(); + const float height = ImGui::GetFrameHeight(); + const ImGuiWindowFlags flags = ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings + | ImGuiWindowFlags_MenuBar; + if (ImGui::BeginViewportSideBar("##donut_status", vp, ImGuiDir_Down, height, flags)) { - case Layout::SceneEditing: + if (ImGui::BeginMenuBar()) + { + ImGuiIO& io = ImGui::GetIO(); + ImGui::PushFont(m_font_body, kCaptionSize); + + ImGui::TextDisabled("%s", ctx.device_name.empty() ? "GPU" : ctx.device_name.c_str()); + ImGui::Separator(); + ImGui::TextColored(kAmber, "%s", m_tabs[m_active_tab].label); + ImGui::Separator(); + ImGui::TextDisabled("%.0f x %.0f", io.DisplaySize.x * io.DisplayFramebufferScale.x, + io.DisplaySize.y * io.DisplayFramebufferScale.y); + + if (SettingsManager::get_show_fps()) + { + ImGui::Separator(); + ImGui::Text("%.0f fps", io.Framerate); + } + if (SettingsManager::get_show_performance_metrics()) + { + ImGui::Separator(); + ImGui::TextDisabled("%.2f ms", io.Framerate > 0.0f ? 1000.0f / io.Framerate : 0.0f); + } + + if (m_exp_reported) + { + const char* msg = m_exp_written > 0 ? "export written" : "export failed"; + const float tw = ImGui::CalcTextSize(msg).x; + ImGui::SameLine(ImGui::GetWindowWidth() - tw - 12.0f); + ImGui::TextColored(m_exp_written > 0 ? kGreen : kRed, "%s", msg); + } + + ImGui::PopFont(); + ImGui::EndMenuBar(); + } + } + ImGui::End(); + } + + auto UILayer::build_layout(TabState& tab) -> void + { + DONUT_INFO("seeding the {} workspace layout", tab.label); + const ImGuiID root = tab.dock_id; + ImGui::DockBuilderRemoveNode(root); + ImGui::DockBuilderAddNode(root, (int)ImGuiDockNodeFlags_DockSpace | (int)ImGuiDockNodeFlags_PassthruCentralNode); + const ImGuiViewport* vp = ImGui::GetMainViewport(); + const ImVec2 seed_size = (vp->WorkSize.x > 0.0f && vp->WorkSize.y > 0.0f) ? vp->WorkSize : vp->Size; + ImGui::DockBuilderSetNodeSize(root, seed_size); + + auto dock = [&](Panel p, ImGuiID node) { ImGui::DockBuilderDockWindow(tab.name[(int)p].c_str(), node); }; + ImGuiID centre = root; + + switch (tab.id) + { + case Tab::SceneEditor: { ImGuiID left = 0, left_bottom = 0, right = 0; ImGui::DockBuilderSplitNode(centre, ImGuiDir_Left, 0.24f, &left, ¢re); ImGui::DockBuilderSplitNode(left, ImGuiDir_Down, 0.50f, &left_bottom, &left); ImGui::DockBuilderSplitNode(centre, ImGuiDir_Right, 0.24f, &right, ¢re); - 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); + dock(Panel::Outliner, left); + dock(Panel::Properties, left_bottom); + dock(Panel::Stats, right); + dock(Panel::Settings, right); break; } - case Layout::Export: + case Tab::Export: { ImGuiID right = 0, right_bottom = 0; ImGui::DockBuilderSplitNode(centre, ImGuiDir_Right, 0.28f, &right, ¢re); 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); + dock(Panel::Export, right); + dock(Panel::Settings, right); + dock(Panel::BlackHole, right_bottom); + dock(Panel::Stats, right_bottom); + dock(Panel::Outliner, right_bottom); + dock(Panel::Properties, right_bottom); break; } - case Layout::Simulation: - default: + case Tab::Simulation: { ImGuiID right = 0, right_bottom = 0; ImGui::DockBuilderSplitNode(centre, ImGuiDir_Right, 0.24f, &right, ¢re); - 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); + ImGui::DockBuilderSplitNode(right, ImGuiDir_Down, 0.34f, &right_bottom, &right); + dock(Panel::BlackHole, right); + dock(Panel::Settings, right); + dock(Panel::Outliner, right); + dock(Panel::Properties, right); + dock(Panel::Stats, right_bottom); break; } } - ImGui::DockBuilderFinish(dockspace_id); + ImGui::DockBuilderFinish(root); + std::copy(std::begin(tab.defaults), std::end(tab.defaults), tab.show); } - auto UILayer::apply_layout(Layout layout) -> void + auto UILayer::sync_persisted() -> void { - m_layout = layout; - m_rebuild_layout = true; - switch (layout) + bool changed = !s_persisted.loaded || s_persisted.active != m_active_tab; + for (int t = 0; t < kTabCount; ++t) + for (int p = 0; p < kPanelCount; ++p) + changed |= s_persisted.show[t][p] != m_tabs[t].show[p]; + if (!changed) return; + + s_persisted.active = m_active_tab; + for (int t = 0; t < kTabCount; ++t) + for (int p = 0; p < kPanelCount; ++p) + s_persisted.show[t][p] = m_tabs[t].show[p]; + s_persisted.loaded = true; + ImGui::MarkIniSettingsDirty(); + } + + auto UILayer::begin_panel(TabState& tab, Panel p) -> bool + { + const ImGuiWindowFlags flags = tab.appearing ? ImGuiWindowFlags_NoFocusOnAppearing : 0; + return ImGui::Begin(tab.name[(int)p].c_str(), &tab.show[(int)p], flags); + } + + auto UILayer::draw_panel(const UIContext& ctx, TabState& tab, Panel p) -> void + { + switch (p) { - 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; + case Panel::Outliner: panel_outliner(ctx, tab); break; + case Panel::Properties: panel_properties(ctx, tab); break; + case Panel::BlackHole: panel_black_hole(ctx, tab); break; + case Panel::Settings: panel_settings(ctx, tab); break; + case Panel::Export: panel_export(ctx, tab); break; + case Panel::Stats: panel_stats(ctx, tab); break; } } - auto UILayer::panel_outliner(const UIContext& ctx) -> void + auto UILayer::panel_outliner(const UIContext& ctx, TabState& tab) -> void { - if (ImGui::Begin(kOutliner, &m_show_outliner)) + if (begin_panel(tab, Panel::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) + if (accent_button("+ 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); + const int n = (int)objs.size(); + o.position = glm::vec3((float)(n % 5) * 5.0f - 10.0f, 2.0f, (float)(n / 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)); + o.color = glm::vec3(0.35f + 0.12f * (float)(n % 5), 0.55f, 0.9f - 0.12f * (float)(n % 4)); objs.push_back(o); sc.selected_object = (int)objs.size() - 1; } @@ -256,48 +594,65 @@ namespace Donut if (sc.selected_object >= (int)objs.size()) sc.selected_object = (int)objs.size() - 1; } ImGui::EndDisabled(); - ImGui::SameLine(); + ImGui::PushFont(m_font_body, kCaptionSize); ImGui::TextDisabled("%d / 64", (int)objs.size()); - ImGui::Separator(); + ImGui::PopFont(); + ImGui::Spacing(); ImGui::BeginChild("obj_list"); + const float row_h = ImGui::GetTextLineHeight(); + const float swatch = row_h * 0.72f; 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; + ImGui::PushID(i); + const ImVec2 p = ImGui::GetCursorScreenPos(); + char label[32]; + std::snprintf(label, sizeof(label), " Sphere %d", i); + if (ImGui::Selectable(label, sc.selected_object == i)) sc.selected_object = i; + const glm::vec3& col = objs[(size_t)i].color; + const ImVec2 a(p.x + 6.0f, p.y + (row_h - swatch) * 0.5f); + ImGui::GetWindowDrawList()->AddRectFilled(a, ImVec2(a.x + swatch, a.y + swatch), + ImGui::GetColorU32(ImVec4(col.r, col.g, col.b, 1.0f)), 3.0f); + ImGui::PopID(); } - if (objs.empty()) ImGui::TextDisabled("No spheres. Add one above."); + if (objs.empty()) caption("Nothing here yet. Add a sphere above."); ImGui::EndChild(); } ImGui::End(); } - auto UILayer::panel_properties(const UIContext& ctx) -> void + auto UILayer::panel_properties(const UIContext& ctx, TabState& tab) -> void { - if (ImGui::Begin(kProperties, &m_show_properties)) + if (begin_panel(tab, Panel::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."); + caption("Select a sphere in the Outliner to edit it."); } else { - SceneObject& o = objs[sc.selected_object]; + SceneObject& o = objs[(size_t)sc.selected_object]; + ImGui::PushFont(m_font_heading, kHeadSize); ImGui::Text("Sphere %d", sc.selected_object); + ImGui::PopFont(); 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(); + + prop("Position"); ImGui::DragFloat3("##pos", &o.position.x, 0.1f); + prop("Radius"); ImGui::DragFloat("##rad", &o.radius, 0.05f, 0.1f, 20.0f); + prop("Colour"); ImGui::ColorEdit3("##col", &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; + ImGui::PushFont(m_font_body, kCaptionSize); + ImGui::TextDisabled("GIZMO"); + ImGui::PopFont(); + static const char* const ops[] = { "Move", "Scale" }; + segmented("gizmo_op", ops, 2, m_gizmo_op); - if (m_mode == Mode::Scene) + if (tab.view == View::Scene) { ImGuizmo::SetOrthographic(false); ImGuizmo::SetDrawlist(ImGui::GetBackgroundDrawList()); @@ -315,64 +670,62 @@ namespace Donut 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."); + caption("Drag the handles in the viewport."); } else { - ImGui::TextDisabled("Switch View to Scene to drag the gizmo."); + caption("The gizmo lives in the Scene Editor's viewport; the numbers work anywhere."); } } } ImGui::End(); } - auto UILayer::panel_black_hole(const UIContext& ctx) -> void + auto UILayer::panel_black_hole(const UIContext& ctx, TabState& tab) -> void { - if (ImGui::Begin(kBlackHole, &m_show_black_hole)) + if (begin_panel(tab, Panel::BlackHole)) { Scene& sc = ctx.scene; BlackHoleParams& bh = sc.black_hole; - if (ImGui::CollapsingHeader("Camera", ImGuiTreeNodeFlags_DefaultOpen)) + if (section("Camera")) { - 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"); + static const char* const modes[] = { "Orbital", "Free-fly" }; + int mode = sc.is_free_fly() ? 1 : 0; + if (segmented("cam_mode", modes, 2, mode)) sc.set_free_fly(mode == 1); + if (ImGui::Button("Reset view", ImVec2(-FLT_MIN, 0.0f))) sc.reset_sim_camera(); + prop("Field of view"); ImGui::SliderFloat("##fov", &bh.fov_degrees, 20.0f, 90.0f, "%.0f\xc2\xb0"); + caption(sc.is_free_fly() ? "WASD to move \xc2\xb7 Q/E down and up \xc2\xb7 Shift to boost \xc2\xb7 drag to look" + : "Drag to orbit \xc2\xb7 scroll to zoom"); } - if (ImGui::CollapsingHeader("Accretion disk", ImGuiTreeNodeFlags_DefaultOpen)) + if (section("Accretion disk")) { - 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"); + prop("Temperature"); ImGui::SliderFloat("##temp", &bh.temperature, 2000.0f, 15000.0f, "%.0f K"); + prop("Brightness"); ImGui::SliderFloat("##bright", &bh.brightness, 0.0f, 3.0f, "%.2f"); + prop("Inner radius"); ImGui::SliderFloat("##rin", &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."); + prop("Outer radius"); ImGui::SliderFloat("##rout", &bh.disk_outer_rs, bh.disk_inner_rs + 0.5f, 25.0f, "%.1f r_s"); + prop("Turbulence"); ImGui::SliderFloat("##turb", &bh.turbulence, 0.0f, 2.0f, "%.2f"); + caption("The inner edge sits at the ISCO (3 r_s). Novikov-Thorne temperature profile."); } - if (ImGui::CollapsingHeader("Quality")) + if (section("Quality", false)) { - ImGui::SliderInt("Integration steps", &bh.quality_steps, 2000, 15000); - ImGui::TextDisabled("Deeper lensing at higher cost. Settled frame is 4x supersampled."); + prop("Steps"); ImGui::SliderInt("##steps", &bh.quality_steps, 2000, 15000); + caption("More steps resolve the disk at steep angles, at a cost. Settled frames are 4x supersampled."); } } ImGui::End(); } - auto UILayer::panel_settings(const UIContext& ctx) -> void + auto UILayer::panel_settings(const UIContext& ctx, TabState& tab) -> void { - if (ImGui::Begin(kSettings, &m_show_settings)) + if (begin_panel(tab, Panel::Settings)) { auto& s = SettingsManager::get_settings(); - if (ImGui::CollapsingHeader("Display", ImGuiTreeNodeFlags_DefaultOpen)) + if (section("Display")) { const bool fs = ctx.actions.is_fullscreen(); int cw = 0, ch = 0; ctx.actions.get_window_size(cw, ch); @@ -384,7 +737,8 @@ namespace Donut }; 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)) + prop("Resolution"); + if (ImGui::BeginCombo("##res", cur_label)) { for (const auto& r : kRes) if (ImGui::Selectable(r.label, cw == r.w && ch == r.h)) @@ -394,54 +748,63 @@ namespace Donut ImGui::EndDisabled(); bool fs_v = fs; - if (ImGui::Checkbox("Fullscreen", &fs_v)) ctx.actions.set_fullscreen(fs_v); + prop("Fullscreen"); if (ImGui::Checkbox("##fs", &fs_v)) ctx.actions.set_fullscreen(fs_v); - if (ImGui::Checkbox("V-Sync", &s.graphics.v_sync_enabled)) + prop("V-Sync"); + if (ImGui::Checkbox("##vsync", &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")) + prop("Frame cap"); + if (ImGui::SliderInt("##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 (s.graphics.v_sync_enabled) caption("The frame cap only applies with V-Sync off."); - if (ImGui::SliderFloat("UI scale", &m_ui_scale, 0.8f, 1.6f, "%.2fx")) + prop("UI scale"); + if (ImGui::SliderFloat("##scale", &m_ui_scale, 0.8f, 1.6f, "%.2fx")) ctx.actions.set_ui_scale(m_ui_scale); } - if (ImGui::CollapsingHeader("Renderer", ImGuiTreeNodeFlags_DefaultOpen)) + if (section("Renderer")) { const char* apis[] = { "OpenGL", "Vulkan" }; int cur = (s.graphics.render_api == "Vulkan") ? 1 : 0; - if (ImGui::Combo("Graphics API", &cur, apis, 2)) + prop("Graphics API"); + if (ImGui::Combo("##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); + ImGui::TextColored(kAmber, "Restart to apply (%s is running)", running); else - ImGui::TextDisabled("Active backend: %s (%s)", running, ctx.device_name.c_str()); + { + char note[160]; + std::snprintf(note, sizeof(note), "Running %s on %s.", running, ctx.device_name.c_str()); + caption(note); + } } - if (ImGui::CollapsingHeader("Overlay", ImGuiTreeNodeFlags_DefaultOpen)) + if (section("Overlay")) { bool dirty = false; - dirty |= ImGui::Checkbox("Show FPS", &s.graphics.show_fps); - dirty |= ImGui::Checkbox("Show frame time", &s.graphics.show_performance_metrics); + prop("Show fps"); dirty |= ImGui::Checkbox("##showfps", &s.graphics.show_fps); + prop("Show frame time"); dirty |= ImGui::Checkbox("##showms", &s.graphics.show_performance_metrics); if (dirty) SettingsManager::save_settings(); + caption("Shown in the status bar along the bottom."); } - if (ImGui::CollapsingHeader("Environment", ImGuiTreeNodeFlags_DefaultOpen)) + if (section("Environment")) { auto& hdri = HDRIManager::get(); std::string preview = hdri.get_hdri_name(ctx.scene.hdri_path); - if (ImGui::BeginCombo("Starfield", preview.c_str())) + prop("Starfield"); + if (ImGui::BeginCombo("##hdri", preview.c_str())) { for (const auto& path : hdri.get_available_hdri()) { @@ -452,38 +815,40 @@ namespace Donut } ImGui::EndCombo(); } - ImGui::TextDisabled("Switching rebuilds the cubemap (brief pause)."); + caption("Switching rebuilds the cubemap (a brief pause)."); } } ImGui::End(); } - auto UILayer::panel_export(const UIContext& ctx) -> void + auto UILayer::panel_export(const UIContext& ctx, TabState& tab) -> void { - if (ImGui::Begin(kExport, &m_show_export)) + if (begin_panel(tab, Panel::Export)) { - ImGui::TextDisabled("Render the black hole + observables to disk."); + caption("Render the black hole and its observables to disk."); ImGui::Spacing(); - if (ImGui::CollapsingHeader("Channels", ImGuiTreeNodeFlags_DefaultOpen)) + + if (section("Channels")) { - 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."); + 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); + caption("Redshift and temperature are disk-only; impact covers the whole frame."); } - if (ImGui::CollapsingHeader("Output", ImGuiTreeNodeFlags_DefaultOpen)) + + if (section("Output")) { - 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)."); + prop("Resolution"); ImGui::Combo("##res", &m_exp_resolution, "1280 x 720\0" "1920 x 1080\0" "2560 x 1440\0" "3840 x 2160\0"); + prop("Format"); ImGui::Combo("##fmt", &m_exp_format, "Display image (PNG)\0" "Raw float image (PFM)\0" "Raw values (CSV)\0"); + caption(m_exp_format == 0 ? "Tone-mapped colour, or false-coloured observables." + : "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))) + if (accent_button("Export frame", ImVec2(-FLT_MIN, 0.0f))) { static const int dims[][2] = { {1280,720}, {1920,1080}, {2560,1440}, {3840,2160} }; ExportConfig cfg; @@ -498,31 +863,62 @@ namespace Donut if (m_exp_reported) { + ImGui::Spacing(); 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); + ImGui::TextColored(kGreen, "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::TextColored(kRed, "Export failed (see log)"); } } ImGui::End(); } - auto UILayer::panel_stats(const UIContext& ctx) -> void + auto UILayer::panel_stats(const UIContext& ctx, TabState& tab) -> void { - if (ImGui::Begin(kStats, &m_show_stats)) + if (begin_panel(tab, Panel::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) + const float ms = io.DeltaTime * 1000.0f; + m_frame_hist[m_frame_hist_i] = ms; + m_frame_hist_i = (m_frame_hist_i + 1) % kFrameHist; + + ImGui::PushFont(m_font_heading, kBigSize); + ImGui::Text("%.0f", io.Framerate); + ImGui::PopFont(); + ImGui::SameLine(0.0f, 8.0f); + ImGui::PushFont(m_font_body, kCaptionSize); + ImGui::TextDisabled("fps \xc2\xb7 %.2f ms", ms); + ImGui::PopFont(); + + float worst = 0.0f; + for (float v : m_frame_hist) worst = std::max(worst, v); + ImGui::PlotLines("##frametime", m_frame_hist, kFrameHist, m_frame_hist_i, nullptr, + 0.0f, std::max(worst * 1.15f, 4.0f), ImVec2(-FLT_MIN, 64.0f)); + caption("Frame time over the last 120 frames."); + ImGui::Spacing(); + + if (ImGui::BeginTable("facts", 2, ImGuiTableFlags_RowBg)) { - if (show_fps) ImGui::SameLine(); - ImGui::TextDisabled("%.1f ms/frame", io.Framerate > 0 ? 1000.0f / io.Framerate : 0.0f); + ImGui::TableSetupColumn("key", ImGuiTableColumnFlags_WidthFixed, 118.0f); + ImGui::TableSetupColumn("value", ImGuiTableColumnFlags_WidthStretch); + auto row = [](const char* key, const char* value) + { + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); ImGui::TextDisabled("%s", key); + ImGui::TableSetColumnIndex(1); ImGui::TextUnformatted(value); + }; + char buf[64]; + row("Device", ctx.device_name.empty() ? "GPU" : ctx.device_name.c_str()); + row("API", RendererAPI::get_api() == RendererAPI::API::Vulkan ? "Vulkan (MoltenVK)" : "OpenGL 4.1"); + row("Workspace", tab.label); + std::snprintf(buf, sizeof(buf), "%.0f x %.0f", io.DisplaySize.x * io.DisplayFramebufferScale.x, + io.DisplaySize.y * io.DisplayFramebufferScale.y); + row("Framebuffer", buf); + std::snprintf(buf, sizeof(buf), "%d", ctx.scene.black_hole.quality_steps); row("Integration steps", buf); + std::snprintf(buf, sizeof(buf), "%.0f K", ctx.scene.black_hole.temperature); row("Disk temperature", buf); + std::snprintf(buf, sizeof(buf), "%d", (int)ctx.scene.objects.size()); row("Scene objects", buf); + ImGui::EndTable(); } - if (!show_fps && !show_ms) - ImGui::TextDisabled("Enable FPS / frame time in Settings > Overlay."); } ImGui::End(); } diff --git a/src/ui/ui_layer.h b/src/ui/ui_layer.h index 62b4104..eac5273 100644 --- a/src/ui/ui_layer.h +++ b/src/ui/ui_layer.h @@ -3,40 +3,65 @@ #include "workspace.h" #include "rendering/view.h" +#include <string> + +struct ImFont; + namespace Donut { + enum class Tab { Simulation, SceneEditor, Export }; + enum class Panel { Outliner, Properties, BlackHole, Settings, Export, Stats }; + inline constexpr int kTabCount = 3; + inline constexpr int kPanelCount = 6; + class UILayer { public: - UILayer() = default; + UILayer(); + + auto init() -> void; auto draw(const UIContext& ctx) -> View; private: - enum class Mode { Scene, Simulation }; - enum class Layout { Simulation, SceneEditing, Export }; + struct TabState + { + Tab id = Tab::Simulation; + const char* label = nullptr; + const char* key = nullptr; + View view = View::BlackHole; + unsigned int dock_id = 0; + std::string name[kPanelCount]; + bool allowed[kPanelCount] = {}; + bool defaults[kPanelCount] = {}; + bool show[kPanelCount] = {}; + bool reset = false; + bool appearing = false; + }; auto draw_menu_bar(const UIContext& ctx) -> void; - auto build_layout(unsigned int dockspace_id) -> void; - auto apply_layout(Layout layout) -> void; + auto draw_tab_strip() -> void; + auto draw_status_bar(const UIContext& ctx) -> void; + auto build_layout(TabState& tab) -> void; + auto request_tab(int index) -> void { m_tab_request = index; } + auto sync_persisted() -> void; - auto panel_outliner(const UIContext& ctx) -> void; - auto panel_properties(const UIContext& ctx) -> void; - auto panel_black_hole(const UIContext& ctx) -> void; - auto panel_settings(const UIContext& ctx) -> void; - auto panel_export(const UIContext& ctx) -> void; - auto panel_stats(const UIContext& ctx) -> void; + auto begin_panel(TabState& tab, Panel p) -> bool; + auto draw_panel(const UIContext& ctx, TabState& tab, Panel p) -> void; + auto panel_outliner(const UIContext& ctx, TabState& tab) -> void; + auto panel_properties(const UIContext& ctx, TabState& tab) -> void; + auto panel_black_hole(const UIContext& ctx, TabState& tab) -> void; + auto panel_settings(const UIContext& ctx, TabState& tab) -> void; + auto panel_export(const UIContext& ctx, TabState& tab) -> void; + auto panel_stats(const UIContext& ctx, TabState& tab) -> void; - Mode m_mode = Mode::Simulation; - Layout m_layout = Layout::Simulation; - bool m_rebuild_layout = false; - bool m_first_frame = true; + auto section(const char* title, bool default_open = true) -> bool; + auto caption(const char* text) -> void; + auto segmented(const char* id, const char* const* labels, int count, int& value) -> bool; - bool m_show_outliner = false; - bool m_show_properties = false; - bool m_show_black_hole = true; - bool m_show_settings = false; - bool m_show_export = false; - bool m_show_stats = true; + TabState m_tabs[kTabCount]; + int m_active_tab = 0; + int m_tab_request = -1; + bool m_first_frame = true; int m_gizmo_op = 0; @@ -45,5 +70,12 @@ namespace Donut bool m_exp_color = true, m_exp_redshift = false, m_exp_temperature = false, m_exp_impact = false; int m_exp_resolution = 1, m_exp_format = 0; int m_exp_written = 0; bool m_exp_reported = false; + + ImFont* m_font_body = nullptr; + ImFont* m_font_heading = nullptr; + + static constexpr int kFrameHist = 120; + float m_frame_hist[kFrameHist] = {}; + int m_frame_hist_i = 0; }; } |
