monogame-ui-interaction
MonoGame UI Interaction — Hit Testing & Event Bubbling
This skill covers how pointer input (mouse, touch) connects to UI elements. For input polling fundamentals (prev/curr state), see monogame-input. For keyboard/gamepad focus, see monogame-ui-focus. For complete code templates, read references/ui-interaction.md.
Assumed Contract
UIElement.Bounds must be an up-to-date absolute screen Rectangle before hit testing runs. If Bounds is empty (
Rectangle.Emptyor default), no element will be hit. Always run the layout Arrange pass (frommonogame-ui-layout) before processing pointer input.Hit testing reads
MouseStatefrommonogame-input's prev/curr state pattern — do not pollMouse.GetState()a second time inside UI update code.
Hit Testing: DFS Reverse Traversal
The visual tree is painted parent → children (painter's algorithm). The child drawn last appears on top. Therefore, hit testing must check children in reverse order (last drawn = first tested):