From 75a920041208f09c3565006012378c5677a32468 Mon Sep 17 00:00:00 2001 From: Guillaume Chereau Date: Fri, 5 Jan 2024 15:22:20 +0800 Subject: [PATCH] Allow to detach and move the windows Still a bit experimental. --- src/gui.cpp | 11 ++++++++--- src/gui.h | 4 +++- src/gui/app.c | 31 +++++++++++++++++++++++++------ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/gui.cpp b/src/gui.cpp index 97468809a..a061f8da2 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -656,15 +656,16 @@ void gui_row_end(void) gui->item_size = 0; } -void gui_window_begin(const char *label, float x, float y, float w, float h) +void gui_window_begin(const char *label, float x, float y, float w, float h, + bool *moved) { ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | - ImGuiWindowFlags_NoMove | // ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoDecoration; float max_h; - ImGui::SetNextWindowPos(ImVec2(x, y)); + ImGui::SetNextWindowPos(ImVec2(x, y), + moved == NULL ? ImGuiCond_Always : ImGuiCond_Appearing); ImGui::SetNextWindowSize(ImVec2(w, h)); if (h == 0) { max_h = ImGui::GetMainViewport()->Size.y - y; @@ -672,6 +673,10 @@ void gui_window_begin(const char *label, float x, float y, float w, float h) ImVec2(0, 0), ImVec2(FLT_MAX, max_h)); } ImGui::Begin(label, NULL, flags); + + if (moved != NULL) { + *moved = ImGui::GetWindowPos() != ImVec2(x, y); + } } void gui_window_end(void) diff --git a/src/gui.h b/src/gui.h index f65028715..b972cdfcf 100644 --- a/src/gui.h +++ b/src/gui.h @@ -35,7 +35,9 @@ # define GUI_PANEL_WIDTH_LARGE 400 #endif -void gui_window_begin(const char *label, float x, float y, float w, float h); +void gui_window_begin(const char *label, float x, float y, float w, float h, + bool *moved); + void gui_window_end(void); bool gui_want_capture_mouse(void); diff --git a/src/gui/app.c b/src/gui/app.c index 333ff3a13..971cfc9d2 100644 --- a/src/gui/app.c +++ b/src/gui/app.c @@ -53,10 +53,11 @@ void gui_debug_panel(void); void gui_export_panel(void); bool gui_rotation_bar(void); -static const struct { +static struct { const char *name; int icon; void (*fn)(void); + bool detached; } PANELS[] = { {NULL}, {"Tools", ICON_TOOLS, gui_tools_panel}, @@ -99,6 +100,9 @@ static void render_left_panel(void) void gui_app(void) { float x = 0, y = 0; + bool moved; + const char *name; + int i; goxel.show_export_viewport = false; @@ -117,24 +121,39 @@ void gui_app(void) y = ITEM_HEIGHT + 2; } - gui_window_begin("Top Bar", x, y, 0, 0); + gui_window_begin("Top Bar", x, y, 0, 0, NULL); gui_top_bar(); gui_window_end(); y += ICON_HEIGHT + 28; - gui_window_begin("Left Bar", x, y, 0, 0); + gui_window_begin("Left Bar", x, y, 0, 0, NULL); render_left_panel(); gui_window_end(); if (goxel.gui.current_panel) { x += ICON_HEIGHT + 28; - gui_window_begin("Controls", x, y, goxel.gui.panel_width, 0); - - if (gui_panel_header(PANELS[goxel.gui.current_panel].name)) + name = PANELS[goxel.gui.current_panel].name; + gui_window_begin(name, x, y, goxel.gui.panel_width, 0, &moved); + if (gui_panel_header(name)) goxel.gui.current_panel = 0; else PANELS[goxel.gui.current_panel].fn(); + gui_window_end(); + if (moved) { + PANELS[goxel.gui.current_panel].detached = true; + goxel.gui.current_panel = 0; + } + } + + for (i = 0; i < ARRAY_SIZE(PANELS); i++) { + if (!PANELS[i].detached) continue; + name = PANELS[i].name; + gui_window_begin(name, 0, 0, goxel.gui.panel_width, 0, &moved); + if (gui_panel_header(name)) { + PANELS[i].detached = false; + } + PANELS[i].fn(); gui_window_end(); }