From d11b4160aac28aa118d7481dd92878408edefa7c Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 13 Apr 2015 21:52:38 +0100 Subject: [PATCH] Added IsItemVisible(). Made IsClipped() also return false when log is enabled. --- imgui.cpp | 26 ++++++++++++++++++-------- imgui.h | 3 ++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 1330943e0..e5e8713f6 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -491,7 +491,7 @@ static bool ItemAdd(const ImRect& bb, const ImGuiID* id); static void ItemSize(ImVec2 size, float text_offset_y = 0.0f); static void ItemSize(const ImRect& bb, float text_offset_y = 0.0f); static void PushColumnClipRect(int column_index = -1); -static bool IsClipped(const ImRect& bb); +static bool IsClippedEx(const ImRect& bb, bool clip_even_when_logged); static bool IsMouseHoveringRect(const ImRect& bb); static bool IsKeyPressedMap(ImGuiKey key, bool repeat = true); @@ -2746,6 +2746,13 @@ bool ImGui::IsAnyItemActive() return g.ActiveId != 0; } +bool ImGui::IsItemVisible() +{ + ImGuiWindow* window = GetCurrentWindow(); + ImRect r(window->ClipRectStack.back()); + return r.Overlaps(window->DC.LastItemRect); +} + ImVec2 ImGui::GetItemRectMin() { ImGuiWindow* window = GetCurrentWindow(); @@ -4245,7 +4252,7 @@ void ImGui::TextUnformatted(const char* text, const char* text_end) while (line < text_end) { const char* line_end = strchr(line, '\n'); - if (IsClipped(line_rect)) + if (IsClippedEx(line_rect, false)) break; const ImVec2 line_size = CalcTextSize(line, line_end, false); @@ -7242,20 +7249,23 @@ static inline void ItemSize(const ImRect& bb, float text_offset_y) ItemSize(bb.GetSize(), text_offset_y); } -static bool IsClipped(const ImRect& bb) +static bool IsClippedEx(const ImRect& bb, bool clip_even_when_logged) { ImGuiState& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); - if (!bb.Overlaps(ImRect(window->ClipRectStack.back())) && !g.LogEnabled) - return true; + if (!bb.Overlaps(ImRect(window->ClipRectStack.back()))) + { + if (clip_even_when_logged || !g.LogEnabled) + return true; + } return false; } bool ImGui::IsClipped(const ImVec2& item_size) { ImGuiWindow* window = GetCurrentWindow(); - return IsClipped(ImRect(window->DC.CursorPos, window->DC.CursorPos + item_size)); + return IsClippedEx(ImRect(window->DC.CursorPos, window->DC.CursorPos + item_size), true); } static bool ItemAdd(const ImRect& bb, const ImGuiID* id) @@ -7263,7 +7273,7 @@ static bool ItemAdd(const ImRect& bb, const ImGuiID* id) ImGuiWindow* window = GetCurrentWindow(); window->DC.LastItemID = id ? *id : 0; window->DC.LastItemRect = bb; - if (IsClipped(bb)) + if (IsClippedEx(bb, false)) { if (!id || *id != GImGui->ActiveId) { @@ -7498,7 +7508,7 @@ void ImGui::Columns(int columns_count, const char* id, bool border) const ImGuiID column_id = window->DC.ColumnsSetID + ImGuiID(i); const ImRect column_rect(ImVec2(x-4,y1),ImVec2(x+4,y2)); - if (IsClipped(column_rect)) + if (IsClippedEx(column_rect, false)) continue; bool hovered, held; diff --git a/imgui.h b/imgui.h index e2707d583..3009b83f3 100644 --- a/imgui.h +++ b/imgui.h @@ -373,7 +373,8 @@ namespace ImGui IMGUI_API bool IsItemHoveredRect(); // was the last item hovered by mouse? even if another item is active while we are hovering this IMGUI_API bool IsItemActive(); // was the last item active? (e.g. button being held, text field being edited- items that don't interact will always return false) IMGUI_API bool IsAnyItemActive(); // - IMGUI_API ImVec2 GetItemRectMin(); // get bounding rect of last item + IMGUI_API bool IsItemVisible(); + IMGUI_API ImVec2 GetItemRectMin(); // get bounding rect of last item in screen space IMGUI_API ImVec2 GetItemRectMax(); // " IMGUI_API ImVec2 GetItemRectSize(); // " IMGUI_API bool IsWindowFocused(); // is current window focused (differentiate child windows from each others)