Merge pull request #57179 from spacechase0/notify-world2d-changed-master

Propagate previously unused NOTIFICATION_WORLD_2D_CHANGED, make CanvasItem/CollisionObject2D use it
This commit is contained in:
Rémi Verschelde 2023-05-08 16:40:00 +02:00
commit bbe05b60c8
No known key found for this signature in database
GPG Key ID: C3336907360768E1
5 changed files with 42 additions and 0 deletions

View File

@ -655,6 +655,9 @@
<constant name="NOTIFICATION_EXIT_CANVAS" value="33">
The [CanvasItem] has exited the canvas.
</constant>
<constant name="NOTIFICATION_WORLD_2D_CHANGED" value="36">
The [CanvasItem]'s active [World2D] changed.
</constant>
<constant name="TEXTURE_FILTER_PARENT_NODE" value="0" enum="TextureFilter">
The [CanvasItem] will inherit the filter from its parent.
</constant>

View File

@ -118,6 +118,15 @@ void CollisionObject2D::_notification(int p_what) {
}
} break;
case NOTIFICATION_WORLD_2D_CHANGED: {
RID space = get_world_2d()->get_space();
if (area) {
PhysicsServer2D::get_singleton()->area_set_space(rid, space);
} else {
PhysicsServer2D::get_singleton()->body_set_space(rid, space);
}
} break;
case NOTIFICATION_DISABLED: {
_apply_disabled();
} break;

View File

@ -338,6 +338,10 @@ void CanvasItem::_notification(int p_what) {
case NOTIFICATION_VISIBILITY_CHANGED: {
emit_signal(SceneStringNames::get_singleton()->visibility_changed);
} break;
case NOTIFICATION_WORLD_2D_CHANGED: {
_exit_canvas();
_enter_canvas();
}
}
}
@ -1120,6 +1124,7 @@ void CanvasItem::_bind_methods() {
BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
BIND_CONSTANT(NOTIFICATION_ENTER_CANVAS);
BIND_CONSTANT(NOTIFICATION_EXIT_CANVAS);
BIND_CONSTANT(NOTIFICATION_WORLD_2D_CHANGED);
BIND_ENUM_CONSTANT(TEXTURE_FILTER_PARENT_NODE);
BIND_ENUM_CONSTANT(TEXTURE_FILTER_NEAREST);

View File

@ -1091,7 +1091,11 @@ void Viewport::set_world_2d(const Ref<World2D> &p_world_2d) {
}
if (p_world_2d.is_valid()) {
bool do_propagate = world_2d.is_valid() && is_inside_tree();
world_2d = p_world_2d;
if (do_propagate) {
_propagate_world_2d_changed(this);
}
} else {
WARN_PRINT("Invalid world_2d");
world_2d = Ref<World2D>(memnew(World2D));
@ -3839,6 +3843,25 @@ float Viewport::get_texture_mipmap_bias() const {
#endif // _3D_DISABLED
void Viewport::_propagate_world_2d_changed(Node *p_node) {
if (p_node != this) {
if (Object::cast_to<CanvasItem>(p_node)) {
p_node->notification(CanvasItem::NOTIFICATION_WORLD_2D_CHANGED);
} else {
Viewport *v = Object::cast_to<Viewport>(p_node);
if (v) {
if (v->world_2d.is_valid()) {
return;
}
}
}
}
for (int i = 0; i < p_node->get_child_count(); ++i) {
_propagate_world_2d_changed(p_node->get_child(i));
}
}
void Viewport::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_world_2d", "world_2d"), &Viewport::set_world_2d);
ClassDB::bind_method(D_METHOD("get_world_2d"), &Viewport::get_world_2d);

View File

@ -734,6 +734,8 @@ public:
bool is_using_xr();
#endif // _3D_DISABLED
void _propagate_world_2d_changed(Node *p_node);
void _validate_property(PropertyInfo &p_property) const;
Viewport();
~Viewport();