How do I check if the mouse is over the HUD UI?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By NicTrib

I am trying to detect when the mouse is over the HUD UI, so that other objects don’t do anything when the user clicks on the UI.

:bust_in_silhouette: Reply From: ProXFox

there’s a signal for all control (UI) nodes that says “mouse_entered()”. use it.

:bust_in_silhouette: Reply From: VastView

I’ve also been struggling with this! I found that the only way is by checking for each indiviual UI element, or by seeing if the mouse is inside the area of a panel, e.g. an inventory panel.

Here’s the current documentation:

● mouse_entered()Emitted when the mouse enters the control's Rect area, provided its mouse_filter lets the event reach it.

Note: mouse_entered will not be emitted if the mouse enters a child Control node before entering the parent's Rect area, at least until the mouse is moved to reach the parent's Rect area.

● mouse_exited()Emitted when the mouse leaves the control's Rect area, provided its mouse_filter lets the event reach it.

Note: mouse_exited will be emitted if the mouse enters a child Control node, even if the mouse cursor is still inside the parent's Rect area.

If you want to check whether the mouse truly left the area, ignoring any top nodes, you can use code like this:

func _on_mouse_exited():
    if not Rect2(Vector2(), size).has_point(get_local_mouse_position()):
        # Not hovering over area.

Hope this helps!

Kind regards,
VastView