Skip to content

Commit

Permalink
Fix window event forwarding
Browse files Browse the repository at this point in the history
* Rearrange push_input/push_unhandled_input as push_input/push_local_input/
  push_local_gui_input/push_local_unhandled_input
* Fix extra window event forwarding in push_gui_input
* Fix code format of the last commit.
  • Loading branch information
zhehangd committed Feb 23, 2022
1 parent 62070f0 commit bece838
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 40 deletions.
24 changes: 15 additions & 9 deletions doc/classes/Viewport.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,41 @@
<description>
</description>
</method>
<method name="push_gui_input">
<method name="push_input">
<return type="void" />
<argument index="0" name="event" type="InputEvent" />
<argument index="1" name="in_local_coords" type="bool" default="false" />
<description>
Pushes an input event to [method Control._gui_input]
Pushes an input event.
</description>
</method>
<method name="push_input">
<method name="push_local_gui_input">
<return type="void" />
<argument index="0" name="event" type="InputEvent" />
<argument index="1" name="in_local_coords" type="bool" default="false" />
<description>
Pushes an input event to [method Node._input]
</description>
</method>
<method name="push_text_input">
<method name="push_local_input">
<return type="void" />
<argument index="0" name="text" type="String" />
<argument index="0" name="event" type="InputEvent" />
<argument index="1" name="in_local_coords" type="bool" default="false" />
<description>
Returns [code]true[/code] if the viewport is currently embedding windows.
The three push_local_*_input functions push an event to the three event passes respectively. Unlike push_input, they do not forward events to subwindows.
</description>
</method>
<method name="push_unhandled_input">
<method name="push_local_unhandled_input">
<return type="void" />
<argument index="0" name="event" type="InputEvent" />
<argument index="1" name="in_local_coords" type="bool" default="false" />
<description>
Pushes an input event to [method Node._unhandled_input]
</description>
</method>
<method name="push_text_input">
<return type="void" />
<argument index="0" name="text" type="String" />
<description>
Returns [code]true[/code] if the viewport is currently embedding windows.
</description>
</method>
<method name="set_input_as_handled">
Expand Down
4 changes: 2 additions & 2 deletions editor/editor_command_palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ void EditorCommandPalette::register_shortcuts_as_command() {
ev.instantiate();
ev->set_shortcut(shortcut);
String shortcut_text = String(shortcut->get_as_text());
add_command(command_name, *key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_unhandled_input), varray(ev, false), shortcut_text);
add_command(command_name, *key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_local_unhandled_input), varray(ev, false), shortcut_text);
key = unregistered_shortcuts.next(key);
}
unregistered_shortcuts.clear();
Expand All @@ -260,7 +260,7 @@ Ref<Shortcut> EditorCommandPalette::add_shortcut_command(const String &p_command
ev.instantiate();
ev->set_shortcut(p_shortcut);
String shortcut_text = String(p_shortcut->get_as_text());
add_command(p_command, p_key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_unhandled_input), varray(ev, false), shortcut_text);
add_command(p_command, p_key, callable_mp(EditorNode::get_singleton()->get_viewport(), &Viewport::push_local_unhandled_input), varray(ev, false), shortcut_text);
} else {
const String key_name = String(p_key);
const String command_name = String(p_command);
Expand Down
2 changes: 0 additions & 2 deletions scene/2d/touch_screen_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ void TouchScreenButton::_press(int p_finger_pressed) {
iea->set_action(action);
iea->set_pressed(true);
get_viewport()->push_input(iea, true);
get_viewport()->push_gui_input(iea, true);
}

emit_signal(SNAME("pressed"));
Expand All @@ -307,7 +306,6 @@ void TouchScreenButton::_release(bool p_exiting_tree) {
iea->set_action(action);
iea->set_pressed(false);
get_viewport()->push_input(iea, true);
get_viewport()->push_gui_input(iea, true);
}
}

Expand Down
6 changes: 3 additions & 3 deletions scene/gui/subviewport_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void SubViewportContainer::input(const Ref<InputEvent> &p_event) {
continue;
}

c->push_input(ev);
c->push_local_input(ev);
}
}

Expand All @@ -189,7 +189,7 @@ void SubViewportContainer::gui_input(const Ref<InputEvent> &p_event) {
continue;
}

c->push_gui_input(ev);
c->push_local_gui_input(ev);
}
}

Expand All @@ -216,7 +216,7 @@ void SubViewportContainer::unhandled_input(const Ref<InputEvent> &p_event) {
continue;
}

c->push_unhandled_input(ev);
c->push_local_unhandled_input(ev);
}
}

Expand Down
62 changes: 47 additions & 15 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1980,21 +1980,27 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) {
get_tree()->get_nodes_in_group("_viewports", &vps);
for (Node *node : vps) {
Viewport *vp = Object::cast_to<Viewport>(node);
if (vp == nullptr) {continue;}
if (vp->gui.key_focus == nullptr) {continue;}
if (vp == nullptr) {
continue;
}
if (vp->gui.key_focus == nullptr) {
continue;
}

Viewport *vp_parent = vp;
while (vp_parent && (vp_parent != this)) {
vp_parent = vp_parent->get_parent_viewport();
}
if (vp_parent != this) {continue;}
if (vp_parent != this) {
continue;
}

target_vp = vp;
break;
}
if (target_vp->gui.key_focus) {
Viewport::GUI &target_gui = target_vp->gui;
Control* &target_key_focus = target_vp->gui.key_focus;
Control *&target_key_focus = target_vp->gui.key_focus;
if (target_key_focus && !target_key_focus->is_visible_in_tree()) {
target_key_focus->release_focus();
}
Expand Down Expand Up @@ -2705,18 +2711,19 @@ void Viewport::push_input(const Ref<InputEvent> &p_event, bool p_local_coords) {
return;
}

if (!_can_consume_input_events()) {
return;
if (!is_input_handled()) {
push_local_input(ev);
}

if (!is_input_handled()) {
get_tree()->_call_input_pause(input_group, SceneTree::CALL_INPUT_TYPE_INPUT, ev, this);
push_local_gui_input(ev);
}
if (!is_input_handled()) {
push_local_unhandled_input(ev);
}

event_count++;
}

void Viewport::push_gui_input(const Ref<InputEvent> &p_event, bool p_local_coords) {
void Viewport::push_local_input(const Ref<InputEvent> &p_event, bool p_local_coords) {
ERR_FAIL_COND(!is_inside_tree());

if (disable_input) {
Expand All @@ -2736,11 +2743,35 @@ void Viewport::push_gui_input(const Ref<InputEvent> &p_event, bool p_local_coord
ev = p_event;
}

if (is_embedding_subwindows() && _sub_windows_forward_input(p_event)) {
set_input_as_handled();
if (!_can_consume_input_events()) {
return;
}

if (!is_input_handled()) {
get_tree()->_call_input_pause(input_group, SceneTree::CALL_INPUT_TYPE_INPUT, ev, this);
}
}

void Viewport::push_local_gui_input(const Ref<InputEvent> &p_event, bool p_local_coords) {
ERR_FAIL_COND(!is_inside_tree());

if (disable_input) {
return;
}

if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && get_tree()->get_edited_scene_root()->is_ancestor_of(this)) {
return;
}

local_input_handled = false;

Ref<InputEvent> ev;
if (!p_local_coords) {
ev = _make_input_local(p_event);
} else {
ev = p_event;
}

if (!_can_consume_input_events()) {
return;
}
Expand All @@ -2750,7 +2781,7 @@ void Viewport::push_gui_input(const Ref<InputEvent> &p_event, bool p_local_coord
}
}

void Viewport::push_unhandled_input(const Ref<InputEvent> &p_event, bool p_local_coords) {
void Viewport::push_local_unhandled_input(const Ref<InputEvent> &p_event, bool p_local_coords) {
ERR_FAIL_COND(p_event.is_null());
ERR_FAIL_COND(!is_inside_tree());
local_input_handled = false;
Expand Down Expand Up @@ -3626,8 +3657,9 @@ void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_viewport_rid"), &Viewport::get_viewport_rid);
ClassDB::bind_method(D_METHOD("push_text_input", "text"), &Viewport::push_text_input);
ClassDB::bind_method(D_METHOD("push_input", "event", "in_local_coords"), &Viewport::push_input, DEFVAL(false));
ClassDB::bind_method(D_METHOD("push_gui_input", "event", "in_local_coords"), &Viewport::push_gui_input, DEFVAL(false));
ClassDB::bind_method(D_METHOD("push_unhandled_input", "event", "in_local_coords"), &Viewport::push_unhandled_input, DEFVAL(false));
ClassDB::bind_method(D_METHOD("push_local_input", "event", "in_local_coords"), &Viewport::push_local_gui_input, DEFVAL(false));
ClassDB::bind_method(D_METHOD("push_local_gui_input", "event", "in_local_coords"), &Viewport::push_local_gui_input, DEFVAL(false));
ClassDB::bind_method(D_METHOD("push_local_unhandled_input", "event", "in_local_coords"), &Viewport::push_local_unhandled_input, DEFVAL(false));

ClassDB::bind_method(D_METHOD("get_camera_2d"), &Viewport::get_camera_2d);
ClassDB::bind_method(D_METHOD("set_as_audio_listener_2d", "enable"), &Viewport::set_as_audio_listener_2d);
Expand Down
5 changes: 3 additions & 2 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,9 @@ class Viewport : public Node {

void push_text_input(const String &p_text);
void push_input(const Ref<InputEvent> &p_event, bool p_local_coords = false);
void push_gui_input(const Ref<InputEvent> &p_event, bool p_local_coords = false);
void push_unhandled_input(const Ref<InputEvent> &p_event, bool p_local_coords = false);
void push_local_input(const Ref<InputEvent> &p_event, bool p_local_coords = false);
void push_local_gui_input(const Ref<InputEvent> &p_event, bool p_local_coords = false);
void push_local_unhandled_input(const Ref<InputEvent> &p_event, bool p_local_coords = false);

void set_disable_input(bool p_disable);
bool is_input_disabled() const;
Expand Down
6 changes: 1 addition & 5 deletions scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,11 +920,7 @@ void Window::_window_input(const Ref<InputEvent> &p_ev) {

emit_signal(SceneStringNames::get_singleton()->window_input, p_ev);

push_input(p_ev);
push_gui_input(p_ev);
if (!is_input_handled()) {
push_unhandled_input(p_ev);
}
push_input(p_ev, false);
}

void Window::_window_input_text(const String &p_text) {
Expand Down
4 changes: 2 additions & 2 deletions tests/test_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ int register_test_command(String p_command, TestFunc p_function);
#define SEND_GUI_MOUSE_EVENT(m_object, m_local_pos, m_input, m_mask) \
{ \
_CREATE_GUI_MOUSE_EVENT(m_object, m_local_pos, m_input, m_mask); \
m_object->get_viewport()->push_gui_input(event); \
m_object->get_viewport()->push_local_gui_input(event); \
MessageQueue::get_singleton()->flush(); \
}

#define SEND_GUI_DOUBLE_CLICK(m_object, m_local_pos) \
{ \
_CREATE_GUI_MOUSE_EVENT(m_object, m_local_pos, MouseButton::LEFT, MouseButton::LEFT); \
event->set_double_click(true); \
m_object->get_viewport()->push_gui_input(event); \
m_object->get_viewport()->push_local_gui_input(event); \
MessageQueue::get_singleton()->flush(); \
}

Expand Down

0 comments on commit bece838

Please sign in to comment.