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().

docking
omar 7 years ago
parent e87ad328e4
commit 99c7622a1b

@ -1942,19 +1942,17 @@ bool ImGui::ItemAdd(const ImRect& bb, const ImGuiID* id)
ImGuiWindow* window = g.CurrentWindow; ImGuiWindow* window = g.CurrentWindow;
window->DC.LastItemId = id ? *id : 0; window->DC.LastItemId = id ? *id : 0;
window->DC.LastItemRect = bb; window->DC.LastItemRect = bb;
window->DC.LastItemHoveredAndUsable = window->DC.LastItemHoveredRect = false; window->DC.LastItemHoveredAndUsable = false;
if (IsClippedEx(bb, id, false)) if (IsClippedEx(bb, id, false))
return false; return false;
//if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] //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. // 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)) if (IsMouseHoveringRect(bb.Min, bb.Max))
{ {
// Matching the behavior of internal IsHovered() but: // 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()) // - 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.HoveredRootWindow == window->RootWindow)
if (g.ActiveId == 0 || (id && g.ActiveId == *id) || g.ActiveIdAllowOverlap || (g.ActiveId == window->MoveId)) if (g.ActiveId == 0 || (id && g.ActiveId == *id) || g.ActiveIdAllowOverlap || (g.ActiveId == window->MoveId))
if (IsWindowContentHoverable(window)) if (IsWindowContentHoverable(window))
@ -1973,7 +1971,7 @@ bool ImGui::IsItemHovered()
bool ImGui::IsItemRectHovered() bool ImGui::IsItemRectHovered()
{ {
ImGuiWindow* window = GetCurrentWindowRead(); 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. // [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); 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. // 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); const bool active_id_within_group = (!group_data.BackupActiveIdIsAlive && g.ActiveIdIsAlive && g.ActiveId && g.ActiveIdWindow->RootWindow == window->RootWindow);
if (active_id_within_group) if (active_id_within_group)
window->DC.LastItemId = g.ActiveId; window->DC.LastItemId = g.ActiveId;
if (active_id_within_group && g.HoveredId == 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(); window->DC.GroupStack.pop_back();

@ -1126,11 +1126,11 @@ void ImGui::ShowTestWindow(bool* p_open)
ImGui::Button("CCC"); ImGui::Button("CCC");
ImGui::Button("DDD"); ImGui::Button("DDD");
ImGui::EndGroup(); ImGui::EndGroup();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("Group hovered");
ImGui::SameLine(); ImGui::SameLine();
ImGui::Button("EEE"); ImGui::Button("EEE");
ImGui::EndGroup(); ImGui::EndGroup();
if (ImGui::IsItemHovered())
ImGui::SetTooltip("First group hovered");
} }
// Capture the group size and create widgets using the same size // Capture the group size and create widgets using the same size
ImVec2 size = ImGui::GetItemRectSize(); ImVec2 size = ImGui::GetItemRectSize();

@ -601,7 +601,6 @@ struct IMGUI_API ImGuiDrawContext
ImGuiID LastItemId; ImGuiID LastItemId;
ImRect LastItemRect; 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 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; bool MenuBarAppending;
float MenuBarOffsetX; float MenuBarOffsetX;
ImVector<ImGuiWindow*> ChildWindows; ImVector<ImGuiWindow*> ChildWindows;
@ -642,7 +641,7 @@ struct IMGUI_API ImGuiDrawContext
TreeDepth = 0; TreeDepth = 0;
LastItemId = 0; LastItemId = 0;
LastItemRect = ImRect(0.0f,0.0f,0.0f,0.0f); LastItemRect = ImRect(0.0f,0.0f,0.0f,0.0f);
LastItemHoveredAndUsable = LastItemHoveredRect = false; LastItemHoveredAndUsable = false;
MenuBarAppending = false; MenuBarAppending = false;
MenuBarOffsetX = 0.0f; MenuBarOffsetX = 0.0f;
StateStorage = NULL; StateStorage = NULL;

Loading…
Cancel
Save