(De-)serialization of dock state works

simple_math_single_header
Martin Felis 2018-03-04 21:10:57 +01:00
parent 5cacee03c1
commit d6ac057ce9
3 changed files with 125 additions and 11 deletions

View File

@ -821,6 +821,7 @@ struct DockContext
if (!root) return; if (!root) return;
ImVec2 min_size = root->getMinSize(); ImVec2 min_size = root->getMinSize();
root->label = ImStrdup("ROOT");
ImVec2 requested_size = size; ImVec2 requested_size = size;
root->setPosSize(pos, ImMax(min_size, requested_size)); root->setPosSize(pos, ImMax(min_size, requested_size));
@ -905,12 +906,14 @@ struct DockContext
void tryDockToRoot(Dock& dock) void tryDockToRoot(Dock& dock)
{ {
if(dock.parent != nullptr)
return;
fprintf(stderr, "Trying to dock to root\n"); fprintf(stderr, "Trying to dock to root\n");
Dock* tmp = getRootDock(); Dock* tmp = getRootDock();
doDock(dock, getRootDock(), Slot_Tab); doDock(dock, getRootDock(), Slot_Tab);
} }
void cleanDocks() void cleanDocks()
{ {
restart: restart:
@ -1059,6 +1062,102 @@ struct DockContext
return -1; 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) // 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) // void SaveDock(Lumix::FS::OsFile& file)
// { // {
// g_dock.save(file); // g_dock.save(file);

View File

@ -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 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 EndDock();
IMGUI_API void SetDockActive(); IMGUI_API void SetDockActive();
//IMGUI_API void SaveDock(Lumix::FS::OsFile& file); IMGUI_API void SaveDock();
//IMGUI_API void LoadDock(lua_State* L); IMGUI_API void LoadDock();
} // namespace ImGui } // namespace ImGui

View File

@ -141,6 +141,7 @@ int main(void)
GuiInputState gui_input_state; GuiInputState gui_input_state;
gGuiInputState = &gui_input_state; gGuiInputState = &gui_input_state;
ImGui_ImplGlfwGL3_Init(gWindow, true); ImGui_ImplGlfwGL3_Init(gWindow, true);
ImGui::LoadDock();
// Timer // Timer
Timer timer; Timer timer;
@ -217,6 +218,7 @@ int main(void)
if (draw_imgui_demo) if (draw_imgui_demo)
ImGui::ShowDemoWindow(); 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); module_manager.Update(gTimer->mDeltaTime);
@ -228,6 +230,7 @@ int main(void)
glfwSwapBuffers(gWindow); glfwSwapBuffers(gWindow);
} }
ImGui::SaveDock();
module_manager.UnregisterModules(); module_manager.UnregisterModules();
gRenderer = nullptr; gRenderer = nullptr;