From 8de138d48c966af5dbe513ae8d968cafb9684418 Mon Sep 17 00:00:00 2001 From: David Maas Date: Wed, 14 Apr 2021 07:18:33 -0500 Subject: [PATCH] Created quick and dirty demo of layout saving/restoring using the settings API. Related: https://github.com/ocornut/imgui/issues/4033 --- examples/example_win32_directx11/main.cpp | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/examples/example_win32_directx11/main.cpp b/examples/example_win32_directx11/main.cpp index d6378d804101..a6459db1c875 100644 --- a/examples/example_win32_directx11/main.cpp +++ b/examples/example_win32_directx11/main.cpp @@ -6,6 +6,7 @@ #include "imgui_impl_win32.h" #include "imgui_impl_dx11.h" #include +#include #include // Data @@ -14,6 +15,11 @@ static ID3D11DeviceContext* g_pd3dDeviceContext = NULL; static IDXGISwapChain* g_pSwapChain = NULL; static ID3D11RenderTargetView* g_mainRenderTargetView = NULL; +#define SAVED_LATYOUT_COUNT 12 +static char* saved_layouts[SAVED_LATYOUT_COUNT]; +static size_t saved_layout_sizes[SAVED_LATYOUT_COUNT]; +static int current_layout = 0; + // Forward declarations of helper functions bool CreateDeviceD3D(HWND hWnd); void CleanupDeviceD3D(); @@ -114,6 +120,41 @@ int main(int, char**) if (done) break; + // Check if the user requested a layout change + unsigned int desired_layout = current_layout; + for (unsigned int i = 0; i < IM_ARRAYSIZE(saved_layouts); i++) + { + unsigned int key_code = VK_F1 + i; + + if (io.KeysDown[key_code]) + { + desired_layout = i; + } + } + + if (desired_layout != current_layout) + { + printf("Changing layout F%d -> F%d\n", current_layout + 1, desired_layout + 1); + + // Save the current layout + ImGui::MemFree((void*)saved_layouts[current_layout]); + + size_t settings_size = 0; + const char* settings = ImGui::SaveIniSettingsToMemory(&settings_size); + + saved_layouts[current_layout] = (char*)ImGui::MemAlloc(settings_size); + memcpy(saved_layouts[current_layout], settings, settings_size); + saved_layout_sizes[current_layout] = settings_size; + + // Load the requested layout + // (Or if there is no layout in that slot, use the current layout for that slot.) + current_layout = desired_layout; + if (saved_layouts[current_layout] != nullptr) + { + ImGui::LoadIniSettingsFromMemory(saved_layouts[current_layout]); + } + } + // Start the Dear ImGui frame ImGui_ImplDX11_NewFrame(); ImGui_ImplWin32_NewFrame(); @@ -175,6 +216,13 @@ int main(int, char**) } // Cleanup + for (unsigned int i = 0; i < IM_ARRAYSIZE(saved_layouts); i++) + { + ImGui::MemFree(saved_layouts[i]); + saved_layouts[i] = nullptr; + saved_layout_sizes[i] = 0; + } + ImGui_ImplDX11_Shutdown(); ImGui_ImplWin32_Shutdown(); ImGui::DestroyContext();