Skip to content

Commit

Permalink
Expose a method to get hovered Control in Viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimau authored and YuriSizov committed Dec 18, 2023
1 parent 2d0ee20 commit fe77252
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/classes/Viewport.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 6 additions & 0 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ class Viewport : public Node {

void gui_release_focus();
Control *gui_get_focus_owner() const;
Control *gui_get_hovered_control() const;

PackedStringArray get_configuration_warnings() const override;

Expand Down

0 comments on commit fe77252

Please sign in to comment.