From 99c7622a1bd747b068db97a6e1c8e2cd06e39037 Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 27 Sep 2017 16:20:53 +0200 Subject: [PATCH] ItemAdd(), not performing computation for IsItemRectHovered() which does them itself, allowing us in the next commit to optimize ItemAdd() and make its logic more consistent with IsHovered(). --- imgui.cpp | 15 ++++++++------- imgui_demo.cpp | 4 ++-- imgui_internal.h | 3 +-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 99abdf97..555cea14 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1942,19 +1942,17 @@ bool ImGui::ItemAdd(const ImRect& bb, const ImGuiID* id) ImGuiWindow* window = g.CurrentWindow; window->DC.LastItemId = id ? *id : 0; window->DC.LastItemRect = bb; - window->DC.LastItemHoveredAndUsable = window->DC.LastItemHoveredRect = false; + window->DC.LastItemHoveredAndUsable = false; if (IsClippedEx(bb, id, false)) return false; //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] // Setting LastItemHoveredAndUsable for public facing IsItemHovered(). This is a sensible default, but widgets are free to override it. - // FIXME: Consider moving this code to IsItemHovered() so it's only evaluated if users needs it. + // FIXME-OPT: Consider moving this code to IsItemHovered() so it's only evaluated if users needs it. if (IsMouseHoveringRect(bb.Min, bb.Max)) { // Matching the behavior of internal IsHovered() but: - // - we are always setting LastItemHoveredRect, which is returned by IsItemRectHovered(), so the order of tests is tweaked to be different. // - we allow hovering to be true when ActiveId==window->MoveID, so that clicking on non-interactive items such as a Text() item still returns true with IsItemHovered()) - window->DC.LastItemHoveredRect = true; if (g.HoveredRootWindow == window->RootWindow) if (g.ActiveId == 0 || (id && g.ActiveId == *id) || g.ActiveIdAllowOverlap || (g.ActiveId == window->MoveId)) if (IsWindowContentHoverable(window)) @@ -1973,7 +1971,7 @@ bool ImGui::IsItemHovered() bool ImGui::IsItemRectHovered() { ImGuiWindow* window = GetCurrentWindowRead(); - return window->DC.LastItemHoveredRect; + return IsMouseHoveringRect(window->DC.LastItemRect.Min, window->DC.LastItemRect.Max); } // [Internal] The user-facing IsItemHovered() is using data emitted from ItemAdd(), with a slightly different logic. @@ -10020,13 +10018,16 @@ void ImGui::EndGroup() ItemAdd(group_bb, NULL); } - // If the current ActiveId was declared within the boundary of our group, we copy it to LastItemId so IsItemActive() will function on the entire group. + // If the current ActiveId was declared within the boundary of our group, we copy it to LastItemId so IsItemActive() will be functional on the entire group. // It would be be neater if we replaced window.DC.LastItemId by e.g. 'bool LastItemIsActive', but if you search for LastItemId you'll notice it is only used in that context. const bool active_id_within_group = (!group_data.BackupActiveIdIsAlive && g.ActiveIdIsAlive && g.ActiveId && g.ActiveIdWindow->RootWindow == window->RootWindow); if (active_id_within_group) window->DC.LastItemId = g.ActiveId; if (active_id_within_group && g.HoveredId == g.ActiveId) - window->DC.LastItemHoveredAndUsable = window->DC.LastItemHoveredRect = true; + { + window->DC.LastItemHoveredAndUsable = true; + window->DC.LastItemRect = group_bb; + } window->DC.GroupStack.pop_back(); diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 81e89db5..d846faa1 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -1126,11 +1126,11 @@ void ImGui::ShowTestWindow(bool* p_open) ImGui::Button("CCC"); ImGui::Button("DDD"); ImGui::EndGroup(); - if (ImGui::IsItemHovered()) - ImGui::SetTooltip("Group hovered"); ImGui::SameLine(); ImGui::Button("EEE"); ImGui::EndGroup(); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("First group hovered"); } // Capture the group size and create widgets using the same size ImVec2 size = ImGui::GetItemRectSize(); diff --git a/imgui_internal.h b/imgui_internal.h index b12b3c05..b204b602 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -601,7 +601,6 @@ struct IMGUI_API ImGuiDrawContext ImGuiID LastItemId; ImRect LastItemRect; bool LastItemHoveredAndUsable; // Item rectangle is hovered, and its window is currently interactable with (not blocked by a popup preventing access to the window) - bool LastItemHoveredRect; // Item rectangle is hovered, but its window may or not be currently interactable with (might be blocked by a popup preventing access to the window) bool MenuBarAppending; float MenuBarOffsetX; ImVector ChildWindows; @@ -642,7 +641,7 @@ struct IMGUI_API ImGuiDrawContext TreeDepth = 0; LastItemId = 0; LastItemRect = ImRect(0.0f,0.0f,0.0f,0.0f); - LastItemHoveredAndUsable = LastItemHoveredRect = false; + LastItemHoveredAndUsable = false; MenuBarAppending = false; MenuBarOffsetX = 0.0f; StateStorage = NULL;