Demo: Property Editor: rearrange code + replace use of bool to proper ImGuiChildFlags.

Amend 46691d1
This commit is contained in:
ocornut
2024-07-17 13:52:30 +02:00
parent 9c1f922b02
commit 4247f190c2
3 changed files with 92 additions and 87 deletions
+32 -27
View File
@@ -83,6 +83,7 @@ Index of this file:
// [SECTION] Example App: Debug Console / ShowExampleAppConsole() // [SECTION] Example App: Debug Console / ShowExampleAppConsole()
// [SECTION] Example App: Debug Log / ShowExampleAppLog() // [SECTION] Example App: Debug Log / ShowExampleAppLog()
// [SECTION] Example App: Simple Layout / ShowExampleAppLayout() // [SECTION] Example App: Simple Layout / ShowExampleAppLayout()
// [SECTION] Helpers: ExampleTreeNode, ExampleMemberInfo (for use by Property Editor etc.)
// [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor() // [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor()
// [SECTION] Example App: Long Text / ShowExampleAppLongText() // [SECTION] Example App: Long Text / ShowExampleAppLongText()
// [SECTION] Example App: Auto Resize / ShowExampleAppAutoResize() // [SECTION] Example App: Auto Resize / ShowExampleAppAutoResize()
@@ -7744,7 +7745,7 @@ static void ShowExampleAppLayout(bool* p_open)
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor() // [SECTION] Helpers: ExampleTreeNode, ExampleMemberInfo (for use by Property Editor etc.)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Simple representation for a tree // Simple representation for a tree
@@ -7819,8 +7820,31 @@ static ExampleTreeNode* ExampleTree_CreateDemoTree()
return node_L0; return node_L0;
} }
static void PropertyEditor_ShowTreeNode(ExampleTreeNode* node) //-----------------------------------------------------------------------------
// [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor()
//-----------------------------------------------------------------------------
struct ExampleAppPropertyEditor
{ {
void Draw(ExampleTreeNode* root_node)
{
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2));
ImGuiTableFlags table_flags = ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable | ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg;
if (ImGui::BeginTable("##split", 2, table_flags))
{
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("Object", ImGuiTableColumnFlags_WidthStretch, 1.0f);
ImGui::TableSetupColumn("Contents", ImGuiTableColumnFlags_WidthStretch, 2.0f); // Default twice larger
ImGui::TableHeadersRow();
for (ExampleTreeNode* node : root_node->Childs)
DrawTreeNode(node);
ImGui::EndTable();
}
ImGui::PopStyleVar();
}
void DrawTreeNode(ExampleTreeNode* node)
{
// Object tree node // Object tree node
ImGui::PushID((int)node->UID); ImGui::PushID((int)node->UID);
ImGui::TableNextRow(); ImGui::TableNextRow();
@@ -7837,7 +7861,7 @@ static void PropertyEditor_ShowTreeNode(ExampleTreeNode* node)
// Display child and data // Display child and data
if (node_open) if (node_open)
for (ExampleTreeNode* child : node->Childs) for (ExampleTreeNode* child : node->Childs)
PropertyEditor_ShowTreeNode(child); DrawTreeNode(child);
if (node_open && node->HasData) if (node_open && node->HasData)
{ {
// In a typical application, the structure description would be derived from a data-driven system. // In a typical application, the structure description would be derived from a data-driven system.
@@ -7884,27 +7908,10 @@ static void PropertyEditor_ShowTreeNode(ExampleTreeNode* node)
if (node_open) if (node_open)
ImGui::TreePop(); ImGui::TreePop();
ImGui::PopID(); ImGui::PopID();
}
static void PropertyEditor_ShowTree(ExampleTreeNode* root_node)
{
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2));
ImGuiTableFlags table_flags = ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable | ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg;
if (ImGui::BeginTable("##split", 2, table_flags))
{
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("Object", ImGuiTableColumnFlags_WidthStretch, 1.0f);
ImGui::TableSetupColumn("Contents", ImGuiTableColumnFlags_WidthStretch, 2.0f); // Default twice larger
ImGui::TableHeadersRow();
for (ExampleTreeNode* node : root_node->Childs)
PropertyEditor_ShowTreeNode(node);
ImGui::EndTable();
} }
ImGui::PopStyleVar(); };
}
// Demonstrate create a simple property editor. // Demonstrate creating a simple property editor.
// This demo is a bit lackluster nowadays, would be nice to improve.
static void ShowExampleAppPropertyEditor(bool* p_open) static void ShowExampleAppPropertyEditor(bool* p_open)
{ {
ImGui::SetNextWindowSize(ImVec2(430, 450), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(430, 450), ImGuiCond_FirstUseEver);
@@ -7915,11 +7922,9 @@ static void ShowExampleAppPropertyEditor(bool* p_open)
} }
IMGUI_DEMO_MARKER("Examples/Property Editor"); IMGUI_DEMO_MARKER("Examples/Property Editor");
static ExampleTreeNode* tree_data = NULL; static ExampleAppPropertyEditor property_editor;
if (tree_data == NULL) static ExampleTreeNode* tree_data = ExampleTree_CreateDemoTree();
tree_data = ExampleTree_CreateDemoTree(); property_editor.Draw(tree_data);
PropertyEditor_ShowTree(tree_data);
ImGui::End(); ImGui::End();
} }
+2 -2
View File
@@ -412,8 +412,8 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
SetNextWindowScroll(ImVec2(0.0f, 0.0f)); SetNextWindowScroll(ImVec2(0.0f, 0.0f));
// Create scrolling region (without border and zero window padding) // Create scrolling region (without border and zero window padding)
ImGuiWindowFlags child_flags = (flags & ImGuiTableFlags_ScrollX) ? ImGuiWindowFlags_HorizontalScrollbar : ImGuiWindowFlags_None; ImGuiWindowFlags child_window_flags = (flags & ImGuiTableFlags_ScrollX) ? ImGuiWindowFlags_HorizontalScrollbar : ImGuiWindowFlags_None;
BeginChildEx(name, instance_id, outer_rect.GetSize(), false, child_flags); BeginChildEx(name, instance_id, outer_rect.GetSize(), ImGuiChildFlags_None, child_window_flags);
table->InnerWindow = g.CurrentWindow; table->InnerWindow = g.CurrentWindow;
table->WorkRect = table->InnerWindow->WorkRect; table->WorkRect = table->InnerWindow->WorkRect;
table->OuterRect = table->InnerWindow->Rect(); table->OuterRect = table->InnerWindow->Rect();
+1 -1
View File
@@ -4284,7 +4284,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding);
PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize);
PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Ensure no clip rect so mouse hover can reach FramePadding edges PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Ensure no clip rect so mouse hover can reach FramePadding edges
bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), true, ImGuiWindowFlags_NoMove); bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), ImGuiChildFlags_Border, ImGuiWindowFlags_NoMove);
g.NavActivateId = backup_activate_id; g.NavActivateId = backup_activate_id;
PopStyleVar(3); PopStyleVar(3);
PopStyleColor(); PopStyleColor();