Skip to content

Commit

Permalink
Allow to detach and move the windows
Browse files Browse the repository at this point in the history
Still a bit experimental.
  • Loading branch information
guillaumechereau committed Jan 5, 2024
1 parent 689eecd commit 75a9200
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
11 changes: 8 additions & 3 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,22 +656,27 @@ 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;
ImGui::SetNextWindowSizeConstraints(
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)
Expand Down
4 changes: 3 additions & 1 deletion src/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 25 additions & 6 deletions src/gui/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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;

Expand All @@ -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();
}

Expand Down

0 comments on commit 75a9200

Please sign in to comment.