Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Viewport methods to find Control nodes and subwindows at global position #103929

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add Viewport methods to find controls and subwindows at global position
HuntJSparra committed Mar 17, 2025
commit d09a900635929ae530d9fbf8c6fde5f9e8599cbe
16 changes: 16 additions & 0 deletions doc/classes/Viewport.xml
Original file line number Diff line number Diff line change
@@ -142,6 +142,15 @@
Cancels the drag operation that was previously started through [method Control._get_drag_data] or forced with [method Control.force_drag].
</description>
</method>
<method name="gui_get_control_at_position">
<return type="Control" />
<param index="0" name="position" type="Vector2" />
<param index="1" name="ignore_subwindows" type="bool" default="false" />
<description>
Returns the [Control] at the global position, ignoring [Control]s with [member Control.mouse_filter] set to [constant Control.MOUSE_FILTER_IGNORE]. If [param ignore_subwindows] is false, then this will return controls from embedded subwindows.
This behavior mirrors [method gui_get_hovered_control].
</description>
</method>
<method name="gui_get_drag_data" qualifiers="const">
<return type="Variant" />
<description>
@@ -161,6 +170,13 @@
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_get_window_at_position">
<return type="Window" />
<param index="0" name="position" type="Vector2" />
<description>
Returns the embedded subwindow at the global position, if any.
</description>
</method>
<method name="gui_is_drag_successful" qualifiers="const">
<return type="bool" />
<description>
24 changes: 24 additions & 0 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
@@ -1676,6 +1676,27 @@ void Viewport::_gui_call_notification(Control *p_control, int p_what) {
}
}

Control *Viewport::gui_get_control_at_position(const Vector2 &p_global, bool p_ignore_subwindows) {
Window *window = p_ignore_subwindows ? nullptr : gui_get_window_at_position(p_global);
if (window) {
return window->gui_get_control_at_position(window->get_final_transform().affine_inverse().xform(p_global - window->get_position()));
} else {
return gui_find_control(p_global);
}
}

Window *Viewport::gui_get_window_at_position(const Vector2 &p_global) {
for (int i = 0; i < gui.sub_windows.size(); i++) {
Viewport::SubWindow sw = gui.sub_windows[i];
Rect2i sw_rect = Rect2i(sw.window->get_position_with_decorations(), sw.window->get_size_with_decorations());
if (sw_rect.has_point(p_global)) {
return sw.window;
}
}

return nullptr;
}

// `gui_find_control` doesn't take embedded windows into account. So the caller of this function
// needs to make sure, that there is no embedded window at the specified position.
Control *Viewport::gui_find_control(const Point2 &p_global) {
@@ -4830,6 +4851,9 @@ void Viewport::_bind_methods() {
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("gui_get_control_at_position", "position", "ignore_subwindows"), &Viewport::gui_get_control_at_position, DEFVAL(false));
ClassDB::bind_method(D_METHOD("gui_get_window_at_position", "position"), &Viewport::gui_get_window_at_position);

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);

2 changes: 2 additions & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
@@ -650,6 +650,8 @@ class Viewport : public Node {
bool gui_is_drag_successful() const;
void gui_cancel_drag();

Control *gui_get_control_at_position(const Vector2 &p_global, bool ignore_subwindows = false);
Window *gui_get_window_at_position(const Vector2 &p_global);
Control *gui_find_control(const Point2 &p_global);

void set_sdf_oversize(SDFOversize p_sdf_oversize);