Expose a method to get hovered Control in Viewport

This commit is contained in:
Claire Blackshaw 2023-12-18 14:50:21 +01:00 committed by Yuri Sizov
parent 2d0ee20ff3
commit fe7725239e
3 changed files with 14 additions and 0 deletions

View file

@ -128,6 +128,13 @@
Returns the [Control] having the focus within this viewport. If no [Control] has the focus, returns null.
</description>
</method>
<method name="gui_get_hovered_control" qualifiers="const">
<return type="Control" />
<description>
Returns the [Control] that the mouse is currently hovering over in this viewport. If no [Control] has the cursor, returns null.
Typically the leaf [Control] node or deepest level of the subtree which claims hover. This is very useful when used together with [method Node.is_ancestor_of] to find if the mouse is within a control tree.
</description>
</method>
<method name="gui_is_drag_successful" qualifiers="const">
<return type="bool" />
<description>

View file

@ -3518,6 +3518,11 @@ Control *Viewport::gui_get_focus_owner() const {
return gui.key_focus;
}
Control *Viewport::gui_get_hovered_control() const {
ERR_READ_THREAD_GUARD_V(nullptr);
return gui.mouse_over;
}
void Viewport::set_msaa_2d(MSAA p_msaa) {
ERR_MAIN_THREAD_GUARD;
ERR_FAIL_INDEX(p_msaa, MSAA_MAX);
@ -4557,6 +4562,7 @@ void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("gui_release_focus"), &Viewport::gui_release_focus);
ClassDB::bind_method(D_METHOD("gui_get_focus_owner"), &Viewport::gui_get_focus_owner);
ClassDB::bind_method(D_METHOD("gui_get_hovered_control"), &Viewport::gui_get_hovered_control);
ClassDB::bind_method(D_METHOD("set_disable_input", "disable"), &Viewport::set_disable_input);
ClassDB::bind_method(D_METHOD("is_input_disabled"), &Viewport::is_input_disabled);

View file

@ -610,6 +610,7 @@ public:
void gui_release_focus();
Control *gui_get_focus_owner() const;
Control *gui_get_hovered_control() const;
PackedStringArray get_configuration_warnings() const override;