Create a virtual mouse move event after moving child nodes

This updates mouse cursor and mouse-over-states without the need
for additional mouse movements.
This commit is contained in:
Markus Sauermann 2022-09-29 23:45:51 +02:00
parent 72f7131be1
commit ce10ca6979
8 changed files with 37 additions and 7 deletions

View File

@ -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. 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> </description>
</method> </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"> <method name="warp_mouse">
<return type="void" /> <return type="void" />
<param index="0" name="position" type="Vector2" /> <param index="0" name="position" type="Vector2" />

View File

@ -2400,7 +2400,8 @@ void Control::set_default_cursor_shape(CursorShape p_shape) {
return; 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 { Control::CursorShape Control::get_default_cursor_shape() const {

View File

@ -216,6 +216,14 @@ void Node::_notification(int p_notification) {
memdelete(child); memdelete(child);
} }
} break; } 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;
} }
} }

View File

@ -1395,7 +1395,8 @@ void SceneTree::_change_scene(Node *p_to) {
if (p_to) { if (p_to) {
current_scene = p_to; current_scene = p_to;
root->add_child(p_to); root->add_child(p_to);
root->update_mouse_cursor_shape(); // Update display for cursor instantly.
root->update_mouse_cursor_state();
} }
} }

View File

@ -2294,7 +2294,8 @@ void Viewport::_perform_drop(Control *p_control, Point2 p_pos) {
gui.dragging = false; gui.dragging = false;
gui.drag_mouse_over = nullptr; gui.drag_mouse_over = nullptr;
_propagate_viewport_notification(this, NOTIFICATION_DRAG_END); _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) { void Viewport::_gui_cleanup_internal_state(Ref<InputEvent> p_event) {
@ -3535,6 +3536,14 @@ Transform2D Viewport::get_screen_transform_internal(bool p_absolute_position) co
return get_final_transform(); 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) { void Viewport::set_canvas_cull_mask(uint32_t p_canvas_cull_mask) {
ERR_MAIN_THREAD_GUARD; ERR_MAIN_THREAD_GUARD;
canvas_cull_mask = p_canvas_cull_mask; canvas_cull_mask = p_canvas_cull_mask;
@ -4137,6 +4146,7 @@ void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_mouse_position"), &Viewport::get_mouse_position); 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("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_get_drag_data"), &Viewport::gui_get_drag_data);
ClassDB::bind_method(D_METHOD("gui_is_dragging"), &Viewport::gui_is_dragging); ClassDB::bind_method(D_METHOD("gui_is_dragging"), &Viewport::gui_is_dragging);

View File

@ -582,6 +582,7 @@ public:
Vector2 get_mouse_position() const; Vector2 get_mouse_position() const;
void warp_mouse(const Vector2 &p_position); void warp_mouse(const Vector2 &p_position);
virtual void update_mouse_cursor_state();
void set_physics_object_picking(bool p_enable); void set_physics_object_picking(bool p_enable);
bool get_physics_object_picking(); bool get_physics_object_picking();

View File

@ -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; ERR_MAIN_THREAD_GUARD;
// The default shape is set in Viewport::_gui_input_event. To instantly // Update states based on mouse cursor position.
// see the shape in the viewport we need to trigger a mouse motion event. // 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; Ref<InputEventMouseMotion> mm;
Vector2 pos = get_mouse_position(); Vector2 pos = get_mouse_position();
Transform2D xform = get_global_canvas_transform().affine_inverse(); Transform2D xform = get_global_canvas_transform().affine_inverse();

View File

@ -261,7 +261,7 @@ public:
void set_visible(bool p_visible); void set_visible(bool p_visible);
bool is_visible() const; bool is_visible() const;
void update_mouse_cursor_shape(); void update_mouse_cursor_state() override;
void show(); void show();
void hide(); void hide();