Skip to content

Commit

Permalink
Merge pull request #66625 from Sauermann/fix-move-child-update
Browse files Browse the repository at this point in the history
Create a virtual mouse move event after moving child nodes in tree
  • Loading branch information
YuriSizov authored May 30, 2023
2 parents f067e27 + ce10ca6 commit 6dd5ccd
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 7 deletions.
6 changes: 6 additions & 0 deletions doc/classes/Viewport.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@
Sets the number of subdivisions to use in the specified quadrant. A higher number of subdivisions allows you to have more shadows in the scene at once, but reduces the quality of the shadows. A good practice is to have quadrants with a varying number of subdivisions and to have as few subdivisions as possible.
</description>
</method>
<method name="update_mouse_cursor_state">
<return type="void" />
<description>
Force instantly updating the display based on the current mouse cursor position. This includes updating the mouse cursor shape and sending necessary [signal Control.mouse_entered], [signal CollisionObject2D.mouse_entered], [signal CollisionObject3D.mouse_entered] and [signal Window.mouse_entered] signals and their respective [code]mouse_exited[/code] counterparts.
</description>
</method>
<method name="warp_mouse">
<return type="void" />
<param index="0" name="position" type="Vector2" />
Expand Down
3 changes: 2 additions & 1 deletion scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,8 @@ void Control::set_default_cursor_shape(CursorShape p_shape) {
return;
}

get_viewport()->get_base_window()->update_mouse_cursor_shape();
// Display the new cursor shape instantly.
get_viewport()->update_mouse_cursor_state();
}

Control::CursorShape Control::get_default_cursor_shape() const {
Expand Down
8 changes: 8 additions & 0 deletions scene/main/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ void Node::_notification(int p_notification) {
memdelete(child);
}
} break;

case NOTIFICATION_CHILD_ORDER_CHANGED: {
// The order, in which canvas items are drawn gets rearranged.
// This makes it necessary to update mouse cursor and send according mouse_enter/mouse_exit signals for Control nodes.
if (get_viewport()) {
get_viewport()->update_mouse_cursor_state();
}
} break;
}
}

Expand Down
3 changes: 2 additions & 1 deletion scene/main/scene_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,8 @@ void SceneTree::_change_scene(Node *p_to) {
if (p_to) {
current_scene = p_to;
root->add_child(p_to);
root->update_mouse_cursor_shape();
// Update display for cursor instantly.
root->update_mouse_cursor_state();
}
}

Expand Down
12 changes: 11 additions & 1 deletion scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,8 @@ void Viewport::_perform_drop(Control *p_control, Point2 p_pos) {
gui.dragging = false;
gui.drag_mouse_over = nullptr;
_propagate_viewport_notification(this, NOTIFICATION_DRAG_END);
get_base_window()->update_mouse_cursor_shape();
// Display the new cursor shape instantly.
update_mouse_cursor_state();
}

void Viewport::_gui_cleanup_internal_state(Ref<InputEvent> p_event) {
Expand Down Expand Up @@ -3541,6 +3542,14 @@ Transform2D Viewport::get_screen_transform_internal(bool p_absolute_position) co
return get_final_transform();
}

void Viewport::update_mouse_cursor_state() {
// Updates need to happen in Window, because SubViewportContainers might be hidden behind other Controls.
Window *base_window = get_base_window();
if (base_window) {
base_window->update_mouse_cursor_state();
}
}

void Viewport::set_canvas_cull_mask(uint32_t p_canvas_cull_mask) {
ERR_MAIN_THREAD_GUARD;
canvas_cull_mask = p_canvas_cull_mask;
Expand Down Expand Up @@ -4143,6 +4152,7 @@ void Viewport::_bind_methods() {

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

ClassDB::bind_method(D_METHOD("gui_get_drag_data"), &Viewport::gui_get_drag_data);
ClassDB::bind_method(D_METHOD("gui_is_dragging"), &Viewport::gui_is_dragging);
Expand Down
1 change: 1 addition & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ class Viewport : public Node {

Vector2 get_mouse_position() const;
void warp_mouse(const Vector2 &p_position);
virtual void update_mouse_cursor_state();

void set_physics_object_picking(bool p_enable);
bool get_physics_object_picking();
Expand Down
9 changes: 6 additions & 3 deletions scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,13 @@ void Window::_event_callback(DisplayServer::WindowEvent p_event) {
}
}

void Window::update_mouse_cursor_shape() {
void Window::update_mouse_cursor_state() {
ERR_MAIN_THREAD_GUARD;
// 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.
// Update states based on mouse cursor position.
// This includes updated mouse_enter or mouse_exit signals or the current mouse cursor shape.
// These details are set in Viewport::_gui_input_event. To instantly
// see the changes in the viewport, we need to trigger a mouse motion event.
// This function should be called whenever scene tree changes affect the mouse cursor.
Ref<InputEventMouseMotion> mm;
Vector2 pos = get_mouse_position();
Transform2D xform = get_global_canvas_transform().affine_inverse();
Expand Down
2 changes: 1 addition & 1 deletion scene/main/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class Window : public Viewport {
void set_visible(bool p_visible);
bool is_visible() const;

void update_mouse_cursor_shape();
void update_mouse_cursor_state() override;

void show();
void hide();
Expand Down

0 comments on commit 6dd5ccd

Please sign in to comment.