diff --git a/imgui.cpp b/imgui.cpp index 89af25e42..1ac9c729f 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -5304,24 +5304,13 @@ void ImGui::LogButtons() LogToClipboard(g.LogAutoExpandMaxDepth); } -bool ImGui::CollapsingHeader(const char* label, const char* str_id, bool display_frame, bool default_open) +bool ImGui::TreeNodeBehaviorIsOpened(ImGuiID id, ImGuiTreeNodeFlags flags) { - ImGuiWindow* window = GetCurrentWindow(); - if (window->SkipItems) - return false; - - ImGuiState& g = *GImGui; - const ImGuiStyle& style = g.Style; - - IM_ASSERT(str_id != NULL || label != NULL); - if (str_id == NULL) - str_id = label; - if (label == NULL) - label = str_id; - const ImGuiID id = window->GetID(str_id); - // We only write to the tree storage if the user clicks (or explicitely use SetNextTreeNode*** functions) + ImGuiState& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; ImGuiStorage* storage = window->DC.StateStorage; + bool opened; if (g.SetNextTreeNodeOpenedCond != 0) { @@ -5348,9 +5337,33 @@ bool ImGui::CollapsingHeader(const char* label, const char* str_id, bool display } else { - opened = storage->GetInt(id, default_open) != 0; + opened = storage->GetInt(id, (flags & ImGuiTreeNodeFlags_DefaultOpen) ? 1 : 0) != 0; } + // When logging is enabled, we automatically expand tree nodes (but *NOT* collapsing headers.. seems like sensible behavior). + // NB- If we are above max depth we still allow manually opened nodes to be logged. + if (g.LogEnabled && !(flags & ImGuiTreeNodeFlags_NoAutoExpandOnLog) && window->DC.TreeDepth < g.LogAutoExpandMaxDepth) + opened = true; + + return opened; +} + +bool ImGui::CollapsingHeader(const char* label, const char* str_id, bool display_frame, bool default_open) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiState& g = *GImGui; + const ImGuiStyle& style = g.Style; + + IM_ASSERT(str_id != NULL || label != NULL); + if (str_id == NULL) + str_id = label; + if (label == NULL) + label = str_id; + const ImGuiID id = window->GetID(str_id); + // Framed header expand a little outside the default padding const ImVec2 window_padding = window->WindowPadding; const ImVec2 label_size = CalcTextSize(label, NULL, true); @@ -5368,12 +5381,9 @@ bool ImGui::CollapsingHeader(const char* label, const char* str_id, bool display const ImRect text_bb(bb.Min, bb.Min + ImVec2(collapser_width + style.FramePadding.x*2*0 + (label_size.x > 0.0f ? label_size.x : 0.0f), label_size.y)); ItemSize(ImVec2(text_bb.GetSize().x, bb.GetSize().y), display_frame ? style.FramePadding.y : 0.0f); - // When logging is enabled, if automatically expand tree nodes (but *NOT* collapsing headers.. seems like sensible behavior). - // NB- If we are above max depth we still allow manually opened nodes to be logged. - if (g.LogEnabled && !display_frame && window->DC.TreeDepth < g.LogAutoExpandMaxDepth) - opened = true; - const ImRect interact_bb = display_frame ? bb : ImRect(text_bb.Min, text_bb.Max + ImVec2(style.FramePadding.x*2,0.0f)); // FIXME + bool opened = TreeNodeBehaviorIsOpened(id, (default_open ? ImGuiTreeNodeFlags_DefaultOpen : 0) | (display_frame ? ImGuiTreeNodeFlags_NoAutoExpandOnLog : 0)); + if (!ItemAdd(bb, &id)) //if (!ItemAdd(interact_bb, &id)) // Correct but would prevent user from accessing rendered bb which may be of use return opened; @@ -5383,7 +5393,7 @@ bool ImGui::CollapsingHeader(const char* label, const char* str_id, bool display if (pressed) { opened = !opened; - storage->SetInt(id, opened); + window->DC.StateStorage->SetInt(id, opened); } // Render diff --git a/imgui.h b/imgui.h index 8b9d74c64..f71d0e201 100644 --- a/imgui.h +++ b/imgui.h @@ -294,7 +294,7 @@ namespace ImGui IMGUI_API bool VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* display_format = "%.0f"); // Widgets: Trees - IMGUI_API bool TreeNode(const char* str_label_id); // if returning 'true' the node is open and the user is responsible for calling TreePop + IMGUI_API bool TreeNode(const char* str_label_id); // if returning 'true' the node is open and the user is responsible for calling TreePop() IMGUI_API bool TreeNode(const char* str_id, const char* fmt, ...) IM_PRINTFARGS(2); // " IMGUI_API bool TreeNode(const void* ptr_id, const char* fmt, ...) IM_PRINTFARGS(2); // " IMGUI_API bool TreeNodeV(const char* str_id, const char* fmt, va_list args); // " diff --git a/imgui_internal.h b/imgui_internal.h index 73255bcc8..7d45ec955 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -33,6 +33,7 @@ struct ImGuiWindow; typedef int ImGuiLayoutType; // enum ImGuiLayoutType_ typedef int ImGuiButtonFlags; // enum ImGuiButtonFlags_ +typedef int ImGuiTreeNodeFlags; // enum ImGuiTreeNodeFlags_ //------------------------------------------------------------------------- // STB libraries @@ -144,6 +145,12 @@ enum ImGuiButtonFlags_ ImGuiButtonFlags_AlignTextBaseLine = 1 << 6 }; +enum ImGuiTreeNodeFlags_ +{ + ImGuiTreeNodeFlags_DefaultOpen = 1 << 0, + ImGuiTreeNodeFlags_NoAutoExpandOnLog = 1 << 1 +}; + enum ImGuiSelectableFlagsPrivate_ { // NB: need to be in sync with last value of ImGuiSelectableFlags_ @@ -636,6 +643,7 @@ namespace ImGui IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_x, float default_y); IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x); + // NB: All position are in absolute pixels coordinates (not window coordinates) IMGUI_API void RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, bool hide_text_after_hash = true); IMGUI_API void RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width); IMGUI_API void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, ImGuiAlign align = ImGuiAlign_Default, const ImVec2* clip_min = NULL, const ImVec2* clip_max = NULL); @@ -660,6 +668,8 @@ namespace ImGui IMGUI_API bool InputScalarEx(const char* label, ImGuiDataType data_type, void* data_ptr, void* step_ptr, void* step_fast_ptr, const char* scalar_format, ImGuiInputTextFlags extra_flags); IMGUI_API bool InputScalarAsWidgetReplacement(const ImRect& aabb, const char* label, ImGuiDataType data_type, void* data_ptr, ImGuiID id, int decimal_precision); + IMGUI_API bool TreeNodeBehaviorIsOpened(ImGuiID id, ImGuiTreeNodeFlags flags = 0); // Consume previous SetNextTreeNodeOpened() data, if any. May return true when logging + IMGUI_API void PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size); IMGUI_API int ParseFormatPrecision(const char* fmt, int default_value);