From 52da6f1a44ff407fcb852a7e7124b7e652885d87 Mon Sep 17 00:00:00 2001 From: Markus Sauermann <6299227+Sauermann@users.noreply.github.com> Date: Thu, 10 Mar 2022 18:44:28 +0100 Subject: [PATCH] Update mouse cursor shape after changes This fixes some cases where the mouse cursor shape did not change automatically, but instead required a MouseMove to update. --- scene/gui/control.cpp | 12 ++++++++++++ scene/main/scene_tree.cpp | 1 + scene/main/viewport.cpp | 4 ++-- scene/main/window.cpp | 12 ++++++++++++ scene/main/window.h | 2 ++ 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 347fe9aa11e6..bd8ac93d8455 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -2225,7 +2225,19 @@ void Control::_window_find_focus_neighbor(const Vector2 &p_dir, Node *p_at, cons void Control::set_default_cursor_shape(CursorShape p_shape) { ERR_FAIL_INDEX(int(p_shape), CURSOR_MAX); + if (data.default_cursor == p_shape) { + return; + } data.default_cursor = p_shape; + + if (!is_inside_tree()) { + return; + } + if (!get_global_rect().has_point(get_global_mouse_position())) { + return; + } + + get_viewport()->get_base_window()->update_mouse_cursor_shape(); } Control::CursorShape Control::get_default_cursor_shape() const { diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp index c18aa5aaa299..a591c9dca260 100644 --- a/scene/main/scene_tree.cpp +++ b/scene/main/scene_tree.cpp @@ -1085,6 +1085,7 @@ void SceneTree::_change_scene(Node *p_to) { if (p_to) { current_scene = p_to; root->add_child(p_to); + root->update_mouse_cursor_shape(); } } diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 879d4949093f..b963607e80f0 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -1576,7 +1576,7 @@ void Viewport::_gui_input_event(Ref p_event) { gui.drag_preview_id = ObjectID(); } _propagate_viewport_notification(this, NOTIFICATION_DRAG_END); - // Change mouse accordingly. + get_base_window()->update_mouse_cursor_shape(); } _gui_cancel_tooltip(); @@ -1597,7 +1597,7 @@ void Viewport::_gui_input_event(Ref p_event) { gui.dragging = false; gui.drag_mouse_over = nullptr; _propagate_viewport_notification(this, NOTIFICATION_DRAG_END); - // Change mouse accordingly. + get_base_window()->update_mouse_cursor_shape(); } gui.mouse_focus_mask &= ~mouse_button_to_mask(mb->get_button_index()); // Remove from mask. diff --git a/scene/main/window.cpp b/scene/main/window.cpp index ebe9587b31d1..e6993e65225b 100644 --- a/scene/main/window.cpp +++ b/scene/main/window.cpp @@ -392,6 +392,18 @@ void Window::_event_callback(DisplayServer::WindowEvent p_event) { } } +void Window::update_mouse_cursor_shape() { + // The default shape is set in Viewport::_gui_input_event. To instantly + // see the shape in the viewport we need to trigger a mouse motion event. + Ref mm; + Vector2 pos = get_mouse_position(); + Transform2D xform = get_global_canvas_transform().affine_inverse(); + mm.instantiate(); + mm->set_position(pos); + mm->set_global_position(xform.xform(pos)); + push_input(mm); +} + void Window::show() { set_visible(true); } diff --git a/scene/main/window.h b/scene/main/window.h index 8c6ca6543685..786f0ada38c6 100644 --- a/scene/main/window.h +++ b/scene/main/window.h @@ -220,6 +220,8 @@ public: void set_visible(bool p_visible); bool is_visible() const; + void update_mouse_cursor_shape(); + void show(); void hide();