From d6ac057ce97a4ac624c73cd8c20be24d755c8817 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 4 Mar 2018 21:10:57 +0100 Subject: [PATCH] (De-)serialization of dock state works --- 3rdparty/imgui_dock_lumix/imgui_dock.cpp | 125 +++++++++++++++++++++-- 3rdparty/imgui_dock_lumix/imgui_dock.h | 4 +- src/main.cc | 7 +- 3 files changed, 125 insertions(+), 11 deletions(-) diff --git a/3rdparty/imgui_dock_lumix/imgui_dock.cpp b/3rdparty/imgui_dock_lumix/imgui_dock.cpp index c5ba508..68c6f26 100644 --- a/3rdparty/imgui_dock_lumix/imgui_dock.cpp +++ b/3rdparty/imgui_dock_lumix/imgui_dock.cpp @@ -821,6 +821,7 @@ struct DockContext if (!root) return; ImVec2 min_size = root->getMinSize(); + root->label = ImStrdup("ROOT"); ImVec2 requested_size = size; root->setPosSize(pos, ImMax(min_size, requested_size)); @@ -903,13 +904,15 @@ struct DockContext doDock(dock, tmp ? tmp : prev, tmp && !tmp->children[0] ? Slot_Tab : getSlotFromLocationCode(*c)); } - void tryDockToRoot(Dock& dock) - { - fprintf(stderr, "Trying to dock to root\n"); - Dock* tmp = getRootDock(); - doDock(dock, getRootDock(), Slot_Tab); - } + void tryDockToRoot(Dock& dock) + { + if(dock.parent != nullptr) + return; + fprintf(stderr, "Trying to dock to root\n"); + Dock* tmp = getRootDock(); + doDock(dock, getRootDock(), Slot_Tab); + } void cleanDocks() { @@ -948,7 +951,7 @@ struct DockContext m_end_action = EndAction_None; if (dock.first && opened) *opened = dock.opened; - if (dock.first) tryDockToRoot(dock); + if (dock.first) tryDockToRoot(dock); dock.first = false; if (opened && !*opened) @@ -1059,6 +1062,102 @@ struct DockContext return -1; } + void save() + { + FILE *fp = fopen("imgui_dock.layout", "w"); + fprintf(fp, "docks %d\n\n", m_docks.size()); + for (int i = 0; i < m_docks.size(); ++i) { + Dock& dock = *m_docks[i]; + + fprintf(fp, "index %d\n", i); + fprintf(fp, "label %s\n", dock.label), + fprintf(fp, "x %d\n", (int)dock.pos.x); + fprintf(fp, "y %d\n", (int)dock.pos.y); + fprintf(fp, "size_x %d\n", (int)dock.size.x); + fprintf(fp, "size_y %d\n", (int)dock.size.y); + fprintf(fp, "status %d\n", (int)dock.status); + fprintf(fp, "active %d\n", dock.active ? 1 : 0); + fprintf(fp, "opened %d\n", dock.opened ? 1 : 0); + fillLocation(dock); + fprintf(fp, "location %s\n", strlen(dock.location) ? dock.location : "-1"); + fprintf(fp, "child0 %d\n", getDockIndex(dock.children[0])); + fprintf(fp, "child1 %d\n", getDockIndex(dock.children[1])); + fprintf(fp, "prev %d\n", getDockIndex(dock.prev_tab)); + fprintf(fp, "next %d\n", getDockIndex(dock.next_tab)); + fprintf(fp, "parent %d\n\n", getDockIndex(dock.parent)); + } + fclose(fp); + + } + + Dock* getDockByIndex(int idx) { return idx < 0 ? nullptr : m_docks[(int)idx]; } + + + void load() + { + for (int i = 0; i < m_docks.size(); ++i) + { + m_docks[i]->~Dock(); + MemFree(m_docks[i]); + } + m_docks.clear(); + + FILE *fp = fopen("imgui_dock.layout", "r"); + + if (fp) { + int ival; + char str2[64]; + fscanf(fp, "docks %d", &ival); + printf("%d docks\n", ival); + + for (int i = 0; i < ival; i++) { + Dock *new_dock = (Dock *) MemAlloc(sizeof(Dock)); + m_docks.push_back(new_dock); + } + + for (int i = 0; i < ival; i++) { + int id, id1, id2, id3, id4, id5; + int st; + int b1, b2; + char lab[32]; + + fscanf(fp, "%s %d", str2, &id); + fscanf(fp, "%s %[^\n]s", str2, &lab[0]); + fscanf(fp, "%s %f", str2, &m_docks[id]->pos.x); + fscanf(fp, "%s %f", str2, &m_docks[id]->pos.y); + fscanf(fp, "%s %f", str2, &m_docks[id]->size.x); + fscanf(fp, "%s %f", str2, &m_docks[id]->size.y); + fscanf(fp, "%s %d", str2, &st); + fscanf(fp, "%s %d", str2, &b1); + fscanf(fp, "%s %d", str2, &b2); + fscanf(fp, "%s %s", str2, &m_docks[id]->location[0]); + fscanf(fp, "%s %d", str2, &id1); + fscanf(fp, "%s %d", str2, &id2); + fscanf(fp, "%s %d", str2, &id3); + fscanf(fp, "%s %d", str2, &id4); + fscanf(fp, "%s %d", str2, &id5); + + fprintf(stderr, "Label: %s\n", lab); + m_docks[id]->label = strdup(lab); + m_docks[id]->id = ImHash(m_docks[id]->label,0); + + m_docks[id]->children[0] = getDockByIndex(id1); + m_docks[id]->children[1] = getDockByIndex(id2); + m_docks[id]->prev_tab = getDockByIndex(id3); + m_docks[id]->next_tab = getDockByIndex(id4); + m_docks[id]->parent = getDockByIndex(id5); + m_docks[id]->status = (Status_)st; + m_docks[id]->active = b1; + m_docks[id]->opened = b2; + + tryDockToStoredLocation(*m_docks[id]); + } + + fclose(fp); + } + printf("done\n"); fflush(stdout); + + } // void save(Lumix::FS::OsFile& file) // { @@ -1223,6 +1322,18 @@ void EndDock() } +void SaveDock() +{ + g_dock.save(); +} + + + + +void LoadDock() +{ + g_dock.load(); +} // void SaveDock(Lumix::FS::OsFile& file) // { // g_dock.save(file); diff --git a/3rdparty/imgui_dock_lumix/imgui_dock.h b/3rdparty/imgui_dock_lumix/imgui_dock.h index b9146a2..1d9a19b 100644 --- a/3rdparty/imgui_dock_lumix/imgui_dock.h +++ b/3rdparty/imgui_dock_lumix/imgui_dock.h @@ -12,8 +12,8 @@ IMGUI_API void RootDock(const ImVec2& pos, const ImVec2& size); IMGUI_API bool BeginDock(const char* label, bool* opened = nullptr, ImGuiWindowFlags extra_flags = 0, const ImVec2& default_size = ImVec2(-1, -1)); IMGUI_API void EndDock(); IMGUI_API void SetDockActive(); -//IMGUI_API void SaveDock(Lumix::FS::OsFile& file); -//IMGUI_API void LoadDock(lua_State* L); +IMGUI_API void SaveDock(); +IMGUI_API void LoadDock(); } // namespace ImGui diff --git a/src/main.cc b/src/main.cc index d42ef58..c084d00 100644 --- a/src/main.cc +++ b/src/main.cc @@ -141,6 +141,7 @@ int main(void) GuiInputState gui_input_state; gGuiInputState = &gui_input_state; ImGui_ImplGlfwGL3_Init(gWindow, true); + ImGui::LoadDock(); // Timer Timer timer; @@ -217,17 +218,19 @@ int main(void) if (draw_imgui_demo) ImGui::ShowDemoWindow(); - ImGui::RootDock(ImVec2(0.0f, menu_bar_height), ImVec2(width, height - menu_bar_height)); + + ImGui::RootDock(ImVec2(0.0f, menu_bar_height), ImVec2(width, height - menu_bar_height)); module_manager.Update(gTimer->mDeltaTime); - ImGui::Render(); + ImGui::Render(); usleep(16000); glfwSwapBuffers(gWindow); } + ImGui::SaveDock(); module_manager.UnregisterModules(); gRenderer = nullptr;