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

[3.x] Expose some helper methods on Viewport #92573

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions doc/classes/Viewport.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@
Returns the drag data from the GUI, that was previously returned by [method Control.get_drag_data].
</description>
</method>
<method name="gui_get_focus_owner" qualifiers="const">
<return type="Control" />
<description>
Returns the [Control] having the focus within this viewport. If no [Control] has the focus, returns [code]null[/code].
</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 [code]null[/code].
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_a_parent_of] to find if the mouse is within a control tree.
</description>
</method>
<method name="gui_has_modal_stack" qualifiers="const">
<return type="bool" />
<description>
Expand All @@ -127,6 +140,12 @@
Alternative to [constant Node.NOTIFICATION_DRAG_BEGIN] and [constant Node.NOTIFICATION_DRAG_END] when you prefer polling the value.
</description>
</method>
<method name="gui_release_focus">
<return type="void" />
<description>
Removes the focus from the currently focused [Control] within this viewport. If no [Control] has the focus, does nothing.
</description>
</method>
<method name="input">
<return type="void" />
<argument index="0" name="local_event" type="InputEvent" />
Expand Down
12 changes: 3 additions & 9 deletions scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@

#include "control.h"

#include "core/engine.h"
#include "core/message_queue.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "core/print_string.h"
#include "core/project_settings.h"
#include "scene/gui/label.h"
#include "scene/gui/panel.h"
#include "scene/main/canvas_layer.h"
#include "scene/main/viewport.h"
#include "scene/scene_string_names.h"
Expand Down Expand Up @@ -2045,8 +2040,7 @@ void Control::release_focus() {
return;
}

get_viewport()->_gui_remove_focus();
update();
get_viewport()->gui_release_focus();
}

bool Control::is_toplevel_control() const {
Expand Down Expand Up @@ -2474,7 +2468,7 @@ bool Control::get_pass_on_modal_close_click() const {

Control *Control::get_focus_owner() const {
ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
return get_viewport()->_gui_get_focus_owner();
return get_viewport()->gui_get_focus_owner();
}

void Control::warp_mouse(const Point2 &p_to_pos) {
Expand Down
22 changes: 14 additions & 8 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@
#include "scene/gui/control.h"
#include "scene/gui/label.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/panel.h"
#include "scene/gui/panel_container.h"
#include "scene/gui/popup_menu.h"
#include "scene/gui/viewport_container.h"
#include "scene/main/canvas_layer.h"
#include "scene/main/timer.h"
#include "scene/resources/mesh.h"
#include "scene/scene_string_names.h"
#include "servers/physics_2d_server.h"
Expand Down Expand Up @@ -2664,7 +2662,7 @@ void Viewport::_gui_hid_control(Control *p_control) {
}

if (gui.key_focus == p_control) {
_gui_remove_focus();
gui_release_focus();
}
if (gui.mouse_over == p_control) {
gui.mouse_over = nullptr;
Expand Down Expand Up @@ -2696,11 +2694,12 @@ void Viewport::_gui_remove_control(Control *p_control) {
}
}

void Viewport::_gui_remove_focus() {
void Viewport::gui_release_focus() {
if (gui.key_focus) {
Node *f = gui.key_focus;
Control *f = gui.key_focus;
gui.key_focus = nullptr;
f->notification(Control::NOTIFICATION_FOCUS_EXIT, true);
f->update();
}
}

Expand All @@ -2717,7 +2716,7 @@ void Viewport::_gui_control_grab_focus(Control *p_control) {
if (gui.key_focus && gui.key_focus == p_control) {
return;
}
get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, "_viewports", "_gui_remove_focus");
get_tree()->call_group_flags(SceneTree::GROUP_CALL_REALTIME, "_viewports", "gui_release_focus");
gui.key_focus = p_control;
emit_signal("gui_focus_changed", p_control);
p_control->notification(Control::NOTIFICATION_FOCUS_ENTER);
Expand Down Expand Up @@ -2815,10 +2814,14 @@ List<Control *>::Element *Viewport::_gui_show_modal(Control *p_control) {
return node;
}

Control *Viewport::_gui_get_focus_owner() {
Control *Viewport::gui_get_focus_owner() const {
return gui.key_focus;
}

Control *Viewport::gui_get_hovered_control() const {
return gui.mouse_over;
}

void Viewport::_gui_grab_click_focus(Control *p_control) {
gui.mouse_click_grabber = p_control;
call_deferred("_post_gui_grab_click_focus");
Expand Down Expand Up @@ -3454,6 +3457,10 @@ void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("gui_is_dragging"), &Viewport::gui_is_dragging);
ClassDB::bind_method(D_METHOD("gui_is_drag_successful"), &Viewport::gui_is_drag_successful);

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("get_modal_stack_top"), &Viewport::get_modal_stack_top);

ClassDB::bind_method(D_METHOD("set_disable_input", "disable"), &Viewport::set_disable_input);
Expand All @@ -3466,7 +3473,6 @@ void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_keep_3d_linear"), &Viewport::get_keep_3d_linear);

ClassDB::bind_method(D_METHOD("_gui_show_tooltip"), &Viewport::_gui_show_tooltip);
ClassDB::bind_method(D_METHOD("_gui_remove_focus"), &Viewport::_gui_remove_focus);
ClassDB::bind_method(D_METHOD("_post_gui_grab_click_focus"), &Viewport::_post_gui_grab_click_focus);

ClassDB::bind_method(D_METHOD("set_shadow_atlas_size", "size"), &Viewport::set_shadow_atlas_size);
Expand Down
8 changes: 4 additions & 4 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "scene/main/node.h"
#include "scene/resources/texture.h"
#include "scene/resources/world_2d.h"
#include "servers/visual_server.h"

class Camera;
class Camera2D;
Expand Down Expand Up @@ -388,16 +387,13 @@ class Viewport : public Node {
bool _gui_is_modal_on_top(const Control *p_control);
List<Control *>::Element *_gui_show_modal(Control *p_control);

void _gui_remove_focus();
void _gui_unfocus_control(Control *p_control);
bool _gui_control_has_focus(const Control *p_control);
void _gui_control_grab_focus(Control *p_control);
void _gui_grab_click_focus(Control *p_control);
void _post_gui_grab_click_focus();
void _gui_accept_event();

Control *_gui_get_focus_owner();

Vector2 _get_window_offset() const;

bool _gui_drop(Control *p_at_control, Point2 p_at_pos, bool p_just_check);
Expand Down Expand Up @@ -572,6 +568,10 @@ class Viewport : public Node {
void gui_reset_canvas_sort_index();
int gui_get_canvas_sort_index();

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

virtual String get_configuration_warning() const;

void set_usage(Usage p_usage);
Expand Down
Loading