Skip to content

Commit

Permalink
Created quick and dirty demo of layout saving/restoring using the set…
Browse files Browse the repository at this point in the history
…tings API.

Related: ocornut#4033
  • Loading branch information
PathogenDavid committed Apr 14, 2021
1 parent 3f16a52 commit 8de138d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions examples/example_win32_directx11/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "imgui_impl_win32.h"
#include "imgui_impl_dx11.h"
#include <d3d11.h>
#include <stdio.h>
#include <tchar.h>

// Data
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8de138d

Please sign in to comment.