1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-11-12 02:00:58 +01:00

IsItemHovered(), ItemAdd(): finishing cleaning up, moved the code to IsItemHovered() so ItemAdd() is more lightweight and the two IsXXXHovered functions are now very similar, making their differences less confusing.

This commit is contained in:
omar 2017-09-27 17:21:50 +02:00
parent 19e22baa06
commit 0adcddac39
2 changed files with 15 additions and 23 deletions

View File

@ -1942,29 +1942,27 @@ 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 = 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]
// Set up for public-facing IsItemHovered(). We store the result in DC.LastItemHoveredAndUsable.
// This is roughly matching the behavior of internal-facing IsHovered()
// - 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 don't expose the flatten_child feature that IsHovered() has, which is only used by the window resizing widget (may rework this)
// FIXME-OPT: Consider moving this code to IsItemHovered() so it's only evaluated if users needs it.
if (g.HoveredWindow == window)
if (g.ActiveId == 0 || (id && g.ActiveId == *id) || g.ActiveIdAllowOverlap || (g.ActiveId == window->MoveId))
if (IsMouseHoveringRect(bb.Min, bb.Max))
if (IsWindowContentHoverable(window))
window->DC.LastItemHoveredAndUsable = true;
return true; return true;
} }
// This is roughly matching the behavior of internal-facing IsHovered()
// - 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 don't expose the flatten_child feature that IsHovered() has, which is only used by the window resizing widget (may rework this)
bool ImGui::IsItemHovered() bool ImGui::IsItemHovered()
{ {
ImGuiWindow* window = GetCurrentWindowRead(); ImGuiContext& g = *GImGui;
return window->DC.LastItemHoveredAndUsable; ImGuiWindow* window = g.CurrentWindow;
if (g.HoveredWindow == window)
if (g.ActiveId == 0 || g.ActiveId == window->DC.LastItemId || g.ActiveIdAllowOverlap || g.ActiveId == window->MoveId)
if (IsMouseHoveringRect(window->DC.LastItemRect.Min, window->DC.LastItemRect.Max))
if (IsWindowContentHoverable(window))
return true;
return false;
} }
bool ImGui::IsItemRectHovered() bool ImGui::IsItemRectHovered()
@ -1973,13 +1971,13 @@ bool ImGui::IsItemRectHovered()
return IsMouseHoveringRect(window->DC.LastItemRect.Min, window->DC.LastItemRect.Max); 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 facing IsHovered() differs slightly from IsItemHovered().
bool ImGui::IsHovered(const ImRect& bb, ImGuiID id, bool flatten_childs) bool ImGui::IsHovered(const ImRect& bb, ImGuiID id, bool flatten_childs)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
if (g.HoveredId == 0 || g.HoveredId == id || g.HoveredIdAllowOverlap) if (g.HoveredId == 0 || g.HoveredId == id || g.HoveredIdAllowOverlap)
{ {
ImGuiWindow* window = GetCurrentWindowRead(); ImGuiWindow* window = g.CurrentWindow;
if (g.HoveredWindow == window || (flatten_childs && g.HoveredRootWindow == window->RootWindow)) if (g.HoveredWindow == window || (flatten_childs && g.HoveredRootWindow == window->RootWindow))
if (g.ActiveId == 0 || g.ActiveId == id || g.ActiveIdAllowOverlap) if (g.ActiveId == 0 || g.ActiveId == id || g.ActiveIdAllowOverlap)
if (IsMouseHoveringRect(bb.Min, bb.Max)) if (IsMouseHoveringRect(bb.Min, bb.Max))
@ -10023,11 +10021,7 @@ void ImGui::EndGroup()
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) window->DC.LastItemRect = group_bb;
{
window->DC.LastItemHoveredAndUsable = true;
window->DC.LastItemRect = group_bb;
}
window->DC.GroupStack.pop_back(); window->DC.GroupStack.pop_back();

View File

@ -600,7 +600,6 @@ struct IMGUI_API ImGuiDrawContext
int TreeDepth; int TreeDepth;
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 MenuBarAppending; bool MenuBarAppending;
float MenuBarOffsetX; float MenuBarOffsetX;
ImVector<ImGuiWindow*> ChildWindows; ImVector<ImGuiWindow*> ChildWindows;
@ -641,7 +640,6 @@ 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 = false;
MenuBarAppending = false; MenuBarAppending = false;
MenuBarOffsetX = 0.0f; MenuBarOffsetX = 0.0f;
StateStorage = NULL; StateStorage = NULL;