Demo: re-use tree data. Fix property editor vertical alignment. Tweak recent memory allocation monitor.

This commit is contained in:
ocornut 2024-07-30 11:44:13 +02:00
parent 1f634f1d94
commit 1b44e40444
2 changed files with 57 additions and 43 deletions

View File

@ -15070,7 +15070,12 @@ void ImGui::ShowMetricsWindow(bool* p_open)
for (int n = buf_size - 1; n >= 0; n--) for (int n = buf_size - 1; n >= 0; n--)
{ {
ImGuiDebugAllocEntry* entry = &info->LastEntriesBuf[(info->LastEntriesIdx - n + buf_size) % buf_size]; ImGuiDebugAllocEntry* entry = &info->LastEntriesBuf[(info->LastEntriesIdx - n + buf_size) % buf_size];
BulletText("Frame %06d: %+3d ( %2d malloc, %2d free )%s", entry->FrameCount, entry->AllocCount - entry->FreeCount, entry->AllocCount, entry->FreeCount, (n == 0) ? " (most recent)" : ""); BulletText("Frame %06d: %+3d ( %2d alloc, %2d free )", entry->FrameCount, entry->AllocCount - entry->FreeCount, entry->AllocCount, entry->FreeCount);
if (n == 0)
{
SameLine();
Text("<- %d frames ago", g.FrameCount - entry->FrameCount);
}
} }
TreePop(); TreePop();
} }

View File

@ -199,6 +199,7 @@ Index of this file:
#if !defined(IMGUI_DISABLE_DEMO_WINDOWS) #if !defined(IMGUI_DISABLE_DEMO_WINDOWS)
// Forward Declarations // Forward Declarations
struct ImGuiDemoWindowData;
static void ShowExampleAppMainMenuBar(); static void ShowExampleAppMainMenuBar();
static void ShowExampleAppAssetsBrowser(bool* p_open); static void ShowExampleAppAssetsBrowser(bool* p_open);
static void ShowExampleAppConsole(bool* p_open); static void ShowExampleAppConsole(bool* p_open);
@ -206,7 +207,7 @@ static void ShowExampleAppCustomRendering(bool* p_open);
static void ShowExampleAppDocuments(bool* p_open); static void ShowExampleAppDocuments(bool* p_open);
static void ShowExampleAppLog(bool* p_open); static void ShowExampleAppLog(bool* p_open);
static void ShowExampleAppLayout(bool* p_open); static void ShowExampleAppLayout(bool* p_open);
static void ShowExampleAppPropertyEditor(bool* p_open); static void ShowExampleAppPropertyEditor(bool* p_open, ImGuiDemoWindowData* demo_data);
static void ShowExampleAppSimpleOverlay(bool* p_open); static void ShowExampleAppSimpleOverlay(bool* p_open);
static void ShowExampleAppAutoResize(bool* p_open); static void ShowExampleAppAutoResize(bool* p_open);
static void ShowExampleAppConstrainedResize(bool* p_open); static void ShowExampleAppConstrainedResize(bool* p_open);
@ -217,10 +218,9 @@ static void ShowExampleMenuFile();
// We split the contents of the big ShowDemoWindow() function into smaller functions // We split the contents of the big ShowDemoWindow() function into smaller functions
// (because the link time of very large functions tends to grow non-linearly) // (because the link time of very large functions tends to grow non-linearly)
struct DemoWindowData; static void ShowDemoWindowMenuBar(ImGuiDemoWindowData* demo_data);
static void ShowDemoWindowMenuBar(DemoWindowData* demo_data); static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data);
static void ShowDemoWindowWidgets(DemoWindowData* demo_data); static void ShowDemoWindowMultiSelect(ImGuiDemoWindowData* demo_data);
static void ShowDemoWindowMultiSelect(DemoWindowData* demo_data);
static void ShowDemoWindowLayout(); static void ShowDemoWindowLayout();
static void ShowDemoWindowPopups(); static void ShowDemoWindowPopups();
static void ShowDemoWindowTables(); static void ShowDemoWindowTables();
@ -309,13 +309,14 @@ static ExampleTreeNode* ExampleTree_CreateNode(const char* name, int uid, Exampl
// (this allocates _many_ more times than most other code in either Dear ImGui or others demo) // (this allocates _many_ more times than most other code in either Dear ImGui or others demo)
static ExampleTreeNode* ExampleTree_CreateDemoTree() static ExampleTreeNode* ExampleTree_CreateDemoTree()
{ {
static const char* root_names[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pineapple", "Strawberry", "Watermelon" }; static const char* root_names[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pear", "Pineapple", "Strawberry", "Watermelon" };
char name_buf[32]; char name_buf[32];
int uid = 0; int uid = 0;
ExampleTreeNode* node_L0 = ExampleTree_CreateNode("<ROOT>", ++uid, NULL); ExampleTreeNode* node_L0 = ExampleTree_CreateNode("<ROOT>", ++uid, NULL);
for (int idx_L0 = 0; idx_L0 < IM_ARRAYSIZE(root_names) * 2; idx_L0++) const int root_items_multiplier = 2;
for (int idx_L0 = 0; idx_L0 < IM_ARRAYSIZE(root_names) * root_items_multiplier; idx_L0++)
{ {
snprintf(name_buf, 32, "%s %d", root_names[idx_L0 / 2], idx_L0 % 2); snprintf(name_buf, 32, "%s %d", root_names[idx_L0 / root_items_multiplier], idx_L0 % root_items_multiplier);
ExampleTreeNode* node_L1 = ExampleTree_CreateNode(name_buf, ++uid, node_L0); ExampleTreeNode* node_L1 = ExampleTree_CreateNode(name_buf, ++uid, node_L0);
const int number_of_childs = (int)strlen(node_L1->Name); const int number_of_childs = (int)strlen(node_L1->Name);
for (int idx_L1 = 0; idx_L1 < number_of_childs; idx_L1++) for (int idx_L1 = 0; idx_L1 < number_of_childs; idx_L1++)
@ -338,7 +339,8 @@ static ExampleTreeNode* ExampleTree_CreateDemoTree()
// [SECTION] Demo Window / ShowDemoWindow() // [SECTION] Demo Window / ShowDemoWindow()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
struct DemoWindowData // Data to be shared accross different functions of the demo.
struct ImGuiDemoWindowData
{ {
// Examples Apps (accessible from the "Examples" menu) // Examples Apps (accessible from the "Examples" menu)
bool ShowMainMenuBar = false; bool ShowMainMenuBar = false;
@ -362,6 +364,9 @@ struct DemoWindowData
bool ShowIDStackTool = false; bool ShowIDStackTool = false;
bool ShowStyleEditor = false; bool ShowStyleEditor = false;
bool ShowAbout = false; bool ShowAbout = false;
// Other data
ExampleTreeNode* DemoTree = NULL;
}; };
// Demonstrate most Dear ImGui features (this is big function!) // Demonstrate most Dear ImGui features (this is big function!)
@ -377,32 +382,32 @@ void ImGui::ShowDemoWindow(bool* p_open)
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
// Stored data // Stored data
static DemoWindowData demo; static ImGuiDemoWindowData demo_data;
// Examples Apps (accessible from the "Examples" menu) // Examples Apps (accessible from the "Examples" menu)
if (demo.ShowMainMenuBar) { ShowExampleAppMainMenuBar(); } if (demo_data.ShowMainMenuBar) { ShowExampleAppMainMenuBar(); }
if (demo.ShowAppDocuments) { ShowExampleAppDocuments(&demo.ShowAppDocuments); } if (demo_data.ShowAppDocuments) { ShowExampleAppDocuments(&demo_data.ShowAppDocuments); }
if (demo.ShowAppAssetsBrowser) { ShowExampleAppAssetsBrowser(&demo.ShowAppAssetsBrowser); } if (demo_data.ShowAppAssetsBrowser) { ShowExampleAppAssetsBrowser(&demo_data.ShowAppAssetsBrowser); }
if (demo.ShowAppConsole) { ShowExampleAppConsole(&demo.ShowAppConsole); } if (demo_data.ShowAppConsole) { ShowExampleAppConsole(&demo_data.ShowAppConsole); }
if (demo.ShowAppCustomRendering) { ShowExampleAppCustomRendering(&demo.ShowAppCustomRendering); } if (demo_data.ShowAppCustomRendering) { ShowExampleAppCustomRendering(&demo_data.ShowAppCustomRendering); }
if (demo.ShowAppLog) { ShowExampleAppLog(&demo.ShowAppLog); } if (demo_data.ShowAppLog) { ShowExampleAppLog(&demo_data.ShowAppLog); }
if (demo.ShowAppLayout) { ShowExampleAppLayout(&demo.ShowAppLayout); } if (demo_data.ShowAppLayout) { ShowExampleAppLayout(&demo_data.ShowAppLayout); }
if (demo.ShowAppPropertyEditor) { ShowExampleAppPropertyEditor(&demo.ShowAppPropertyEditor); } if (demo_data.ShowAppPropertyEditor) { ShowExampleAppPropertyEditor(&demo_data.ShowAppPropertyEditor, &demo_data); }
if (demo.ShowAppSimpleOverlay) { ShowExampleAppSimpleOverlay(&demo.ShowAppSimpleOverlay); } if (demo_data.ShowAppSimpleOverlay) { ShowExampleAppSimpleOverlay(&demo_data.ShowAppSimpleOverlay); }
if (demo.ShowAppAutoResize) { ShowExampleAppAutoResize(&demo.ShowAppAutoResize); } if (demo_data.ShowAppAutoResize) { ShowExampleAppAutoResize(&demo_data.ShowAppAutoResize); }
if (demo.ShowAppConstrainedResize) { ShowExampleAppConstrainedResize(&demo.ShowAppConstrainedResize); } if (demo_data.ShowAppConstrainedResize) { ShowExampleAppConstrainedResize(&demo_data.ShowAppConstrainedResize); }
if (demo.ShowAppFullscreen) { ShowExampleAppFullscreen(&demo.ShowAppFullscreen); } if (demo_data.ShowAppFullscreen) { ShowExampleAppFullscreen(&demo_data.ShowAppFullscreen); }
if (demo.ShowAppLongText) { ShowExampleAppLongText(&demo.ShowAppLongText); } if (demo_data.ShowAppLongText) { ShowExampleAppLongText(&demo_data.ShowAppLongText); }
if (demo.ShowAppWindowTitles) { ShowExampleAppWindowTitles(&demo.ShowAppWindowTitles); } if (demo_data.ShowAppWindowTitles) { ShowExampleAppWindowTitles(&demo_data.ShowAppWindowTitles); }
// Dear ImGui Tools (accessible from the "Tools" menu) // Dear ImGui Tools (accessible from the "Tools" menu)
if (demo.ShowMetrics) { ImGui::ShowMetricsWindow(&demo.ShowMetrics); } if (demo_data.ShowMetrics) { ImGui::ShowMetricsWindow(&demo_data.ShowMetrics); }
if (demo.ShowDebugLog) { ImGui::ShowDebugLogWindow(&demo.ShowDebugLog); } if (demo_data.ShowDebugLog) { ImGui::ShowDebugLogWindow(&demo_data.ShowDebugLog); }
if (demo.ShowIDStackTool) { ImGui::ShowIDStackToolWindow(&demo.ShowIDStackTool); } if (demo_data.ShowIDStackTool) { ImGui::ShowIDStackToolWindow(&demo_data.ShowIDStackTool); }
if (demo.ShowAbout) { ImGui::ShowAboutWindow(&demo.ShowAbout); } if (demo_data.ShowAbout) { ImGui::ShowAboutWindow(&demo_data.ShowAbout); }
if (demo.ShowStyleEditor) if (demo_data.ShowStyleEditor)
{ {
ImGui::Begin("Dear ImGui Style Editor", &demo.ShowStyleEditor); ImGui::Begin("Dear ImGui Style Editor", &demo_data.ShowStyleEditor);
ImGui::ShowStyleEditor(); ImGui::ShowStyleEditor();
ImGui::End(); ImGui::End();
} }
@ -452,7 +457,7 @@ void ImGui::ShowDemoWindow(bool* p_open)
//ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f); // e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align) //ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f); // e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align)
// Menu Bar // Menu Bar
ShowDemoWindowMenuBar(&demo); ShowDemoWindowMenuBar(&demo_data);
ImGui::Text("dear imgui says hello! (%s) (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM); ImGui::Text("dear imgui says hello! (%s) (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM);
ImGui::Spacing(); ImGui::Spacing();
@ -573,7 +578,7 @@ void ImGui::ShowDemoWindow(bool* p_open)
IMGUI_DEMO_MARKER("Configuration/Style"); IMGUI_DEMO_MARKER("Configuration/Style");
if (ImGui::TreeNode("Style")) if (ImGui::TreeNode("Style"))
{ {
ImGui::Checkbox("Style Editor", &demo.ShowStyleEditor); ImGui::Checkbox("Style Editor", &demo_data.ShowStyleEditor);
ImGui::SameLine(); ImGui::SameLine();
HelpMarker("The same contents can be accessed in 'Tools->Style Editor' or by calling the ShowStyleEditor() function."); HelpMarker("The same contents can be accessed in 'Tools->Style Editor' or by calling the ShowStyleEditor() function.");
ImGui::TreePop(); ImGui::TreePop();
@ -621,7 +626,7 @@ void ImGui::ShowDemoWindow(bool* p_open)
} }
// All demo contents // All demo contents
ShowDemoWindowWidgets(&demo); ShowDemoWindowWidgets(&demo_data);
ShowDemoWindowLayout(); ShowDemoWindowLayout();
ShowDemoWindowPopups(); ShowDemoWindowPopups();
ShowDemoWindowTables(); ShowDemoWindowTables();
@ -636,7 +641,7 @@ void ImGui::ShowDemoWindow(bool* p_open)
// [SECTION] ShowDemoWindowMenuBar() // [SECTION] ShowDemoWindowMenuBar()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void ShowDemoWindowMenuBar(DemoWindowData* demo_data) static void ShowDemoWindowMenuBar(ImGuiDemoWindowData* demo_data)
{ {
IMGUI_DEMO_MARKER("Menu"); IMGUI_DEMO_MARKER("Menu");
if (ImGui::BeginMenuBar()) if (ImGui::BeginMenuBar())
@ -701,7 +706,7 @@ static void ShowDemoWindowMenuBar(DemoWindowData* demo_data)
// [SECTION] ShowDemoWindowWidgets() // [SECTION] ShowDemoWindowWidgets()
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void ShowDemoWindowWidgets(DemoWindowData* demo_data) static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data)
{ {
IMGUI_DEMO_MARKER("Widgets"); IMGUI_DEMO_MARKER("Widgets");
//ImGui::SetNextItemOpen(true, ImGuiCond_Once); //ImGui::SetNextItemOpen(true, ImGuiCond_Once);
@ -3098,7 +3103,7 @@ struct ExampleDualListBox
// Also read: https://github.com/ocornut/imgui/wiki/Multi-Select // Also read: https://github.com/ocornut/imgui/wiki/Multi-Select
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void ShowDemoWindowMultiSelect(DemoWindowData* demo_data) static void ShowDemoWindowMultiSelect(ImGuiDemoWindowData* demo_data)
{ {
IMGUI_DEMO_MARKER("Widgets/Selection State & Multi-Select"); IMGUI_DEMO_MARKER("Widgets/Selection State & Multi-Select");
if (ImGui::TreeNode("Selection State & Multi-Select")) if (ImGui::TreeNode("Selection State & Multi-Select"))
@ -3580,11 +3585,13 @@ static void ShowDemoWindowMultiSelect(DemoWindowData* demo_data)
}; // ExampleTreeFuncs }; // ExampleTreeFuncs
static ImGuiSelectionBasicStorage selection; static ImGuiSelectionBasicStorage selection;
static ExampleTreeNode* tree = ExampleTree_CreateDemoTree(); // Create tree once if (demo_data->DemoTree == NULL)
demo_data->DemoTree = ExampleTree_CreateDemoTree(); // Create tree once
ImGui::Text("Selection size: %d", selection.Size); ImGui::Text("Selection size: %d", selection.Size);
if (ImGui::BeginChild("##Tree", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY)) if (ImGui::BeginChild("##Tree", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY))
{ {
ExampleTreeNode* tree = demo_data->DemoTree;
ImGuiMultiSelectFlags ms_flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect2d; ImGuiMultiSelectFlags ms_flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect2d;
ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags, selection.Size, -1); ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags, selection.Size, -1);
ExampleTreeFuncs::ApplySelectionRequests(ms_io, tree, &selection); ExampleTreeFuncs::ApplySelectionRequests(ms_io, tree, &selection);
@ -4597,8 +4604,8 @@ static void ShowDemoWindowLayout()
ImGui::TreePop(); ImGui::TreePop();
} }
IMGUI_DEMO_MARKER("Layout/Clipping"); IMGUI_DEMO_MARKER("Layout/Text Clipping");
if (ImGui::TreeNode("Clipping")) if (ImGui::TreeNode("Text Clipping"))
{ {
static ImVec2 size(100.0f, 100.0f); static ImVec2 size(100.0f, 100.0f);
static ImVec2 offset(30.0f, 30.0f); static ImVec2 offset(30.0f, 30.0f);
@ -8868,6 +8875,7 @@ struct ExampleAppPropertyEditor
ImGui::TableNextRow(); ImGui::TableNextRow();
ImGui::PushID(field_desc.Name); ImGui::PushID(field_desc.Name);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::AlignTextToFramePadding();
ImGui::TextUnformatted(field_desc.Name); ImGui::TextUnformatted(field_desc.Name);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
void* field_ptr = (void*)(((unsigned char*)node) + field_desc.Offset); void* field_ptr = (void*)(((unsigned char*)node) + field_desc.Offset);
@ -8933,7 +8941,7 @@ struct ExampleAppPropertyEditor
}; };
// Demonstrate creating a simple property editor. // Demonstrate creating a simple property editor.
static void ShowExampleAppPropertyEditor(bool* p_open) static void ShowExampleAppPropertyEditor(bool* p_open, ImGuiDemoWindowData* demo_data)
{ {
ImGui::SetNextWindowSize(ImVec2(430, 450), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(430, 450), ImGuiCond_FirstUseEver);
if (!ImGui::Begin("Example: Property editor", p_open)) if (!ImGui::Begin("Example: Property editor", p_open))
@ -8944,8 +8952,9 @@ static void ShowExampleAppPropertyEditor(bool* p_open)
IMGUI_DEMO_MARKER("Examples/Property Editor"); IMGUI_DEMO_MARKER("Examples/Property Editor");
static ExampleAppPropertyEditor property_editor; static ExampleAppPropertyEditor property_editor;
static ExampleTreeNode* tree_data = ExampleTree_CreateDemoTree(); if (demo_data->DemoTree == NULL)
property_editor.Draw(tree_data); demo_data->DemoTree = ExampleTree_CreateDemoTree();
property_editor.Draw(demo_data->DemoTree);
ImGui::End(); ImGui::End();
} }