Skip to content

Commit 654b599

Browse files
committed
Merge pull request #99890 from Sauermann/fix-vp-mouse-notifications
Introduce `Viewport` functions for keeping the mouse over state consistent
2 parents 2675997 + 8e00035 commit 654b599

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

doc/classes/Viewport.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@
188188
If [member handle_input_locally] is set to [code]false[/code], this method will try finding the first parent viewport that is set to handle input locally, and return its value for [method is_input_handled] instead.
189189
</description>
190190
</method>
191+
<method name="notify_mouse_entered">
192+
<return type="void" />
193+
<description>
194+
Inform the Viewport that the mouse has entered its area. Use this function before sending an [InputEventMouseButton] or [InputEventMouseMotion] to the [Viewport] with [method Viewport.push_input]. See also [method notify_mouse_exited].
195+
[b]Note:[/b] In most cases, it is not necessary to call this function because [SubViewport] nodes that are children of [SubViewportContainer] are notified automatically. This is only necessary when interacting with viewports in non-default ways, for example as textures in [TextureRect] or with an [Area3D] that forwards input events.
196+
</description>
197+
</method>
198+
<method name="notify_mouse_exited">
199+
<return type="void" />
200+
<description>
201+
Inform the Viewport that the mouse has left its area. Use this function when the node that displays the viewport notices the mouse has left the area of the displayed viewport. See also [method notify_mouse_entered].
202+
[b]Note:[/b] In most cases, it is not necessary to call this function because [SubViewport] nodes that are children of [SubViewportContainer] are notified automatically. This is only necessary when interacting with viewports in non-default ways, for example as textures in [TextureRect] or with an [Area3D] that forwards input events.
203+
</description>
204+
</method>
191205
<method name="push_input">
192206
<return type="void" />
193207
<param index="0" name="event" type="InputEvent" />

scene/main/viewport.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3352,6 +3352,22 @@ void Viewport::_push_unhandled_input_internal(const Ref<InputEvent> &p_event) {
33523352
}
33533353
}
33543354

3355+
void Viewport::notify_mouse_entered() {
3356+
if (gui.mouse_in_viewport) {
3357+
WARN_PRINT_ED("The Viewport was previously notified that the mouse is in its area. There is no need to notify it at this time.");
3358+
return;
3359+
}
3360+
notification(NOTIFICATION_VP_MOUSE_ENTER);
3361+
}
3362+
3363+
void Viewport::notify_mouse_exited() {
3364+
if (!gui.mouse_in_viewport) {
3365+
WARN_PRINT_ED("The Viewport was previously notified that the mouse has left its area. There is no need to notify it at this time.");
3366+
return;
3367+
}
3368+
_mouse_leave_viewport();
3369+
}
3370+
33553371
void Viewport::set_physics_object_picking(bool p_enable) {
33563372
ERR_MAIN_THREAD_GUARD;
33573373
physics_object_picking = p_enable;
@@ -4799,6 +4815,8 @@ void Viewport::_bind_methods() {
47994815
#ifndef DISABLE_DEPRECATED
48004816
ClassDB::bind_method(D_METHOD("push_unhandled_input", "event", "in_local_coords"), &Viewport::push_unhandled_input, DEFVAL(false));
48014817
#endif // DISABLE_DEPRECATED
4818+
ClassDB::bind_method(D_METHOD("notify_mouse_entered"), &Viewport::notify_mouse_entered);
4819+
ClassDB::bind_method(D_METHOD("notify_mouse_exited"), &Viewport::notify_mouse_exited);
48024820

48034821
ClassDB::bind_method(D_METHOD("get_mouse_position"), &Viewport::get_mouse_position);
48044822
ClassDB::bind_method(D_METHOD("warp_mouse", "position"), &Viewport::warp_mouse);

scene/main/viewport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ class Viewport : public Node {
596596
#ifndef DISABLE_DEPRECATED
597597
void push_unhandled_input(const Ref<InputEvent> &p_event, bool p_local_coords = false);
598598
#endif // DISABLE_DEPRECATED
599+
void notify_mouse_entered();
600+
void notify_mouse_exited();
599601

600602
void set_disable_input(bool p_disable);
601603
bool is_input_disabled() const;

0 commit comments

Comments
 (0)