Fix: widgets in a back node steal hover/click from the front node#337
Open
pthom wants to merge 1 commit into
Open
Fix: widgets in a back node steal hover/click from the front node#337pthom wants to merge 1 commit into
pthom wants to merge 1 commit into
Conversation
When two overlapping nodes contain interactive widgets, ImGui's "first hovered wins" rule causes the back node's widgets to claim hover even when a front node visually covers them. Detect the case in NodeBuilder::Begin() by checking whether any later-drawn node in the Z-sorted m_Nodes covers the cursor; if so, set HoveredId to a sentinel for the duration of the user's node body so subsequent ItemHoverable() calls bail out. End() restores the previous HoveredId. ActiveId is left untouched so any in-progress interaction keeps working.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi @thedmd,
Here is a proposed fix for a bug when nodes overlap: the back node might sometimes steal the focus (if its ui code comes first).
Bug description
When two nodes overlap and both contain interactive widgets, the back node might react to hover instead of the front node.
This happens when the C++ rendering code for the back node is executed before the front node.
With the example below, AAA is executed before BBB
And AAA will "win" the click, even in the zone where AAA and BBB overlap below:
Analysis of the cause:
It seems like ImGui resolves overlapping items with "first hovered wins": the first
ItemHoverable()call whose rect contains the cursor setsg.HoveredId, and later calls bail out.Items inside
BeginNode/EndNodeare submitted in user code order, regardless of node Z-order, so the node defined firstalways wins. This conflicts with the editor's drawing order, which puts later-defined (or higher-Z) nodes on top.
Node-frame interaction in
BuildControlalready works correctly because it iteratesm_Nodesin reverse and submits its ownInvisibleButtonper node: the front node's button is submitted first and wins. But, the same guarantee does not extend to widgets the user submits betweenBeginNodeandEndNode.Fix
NodeBuilder::Begin, check whether any later-drawn node in the Z-sortedm_Nodescovers the cursor. If so, saveg.HoveredIdand set it to a sentinel value before user code runs, so anyItemHoverable()call inside this node's body bails out.NodeBuilder::Endrestoresg.HoveredIdfirst thing.g.ActiveIdis intentionally left alone so any interaction already in progress (e.g. a drag started before the front node moved on top) keeps working until mouse-up.