Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate previously unused NOTIFICATION_WORLD_2D_CHANGED, make CanvasItem/CollisionObject2D use it #57179

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/classes/CanvasItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,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>
Expand Down
9 changes: 9 additions & 0 deletions scene/2d/collision_object_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions scene/main/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,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();
}
}
}

Expand Down Expand Up @@ -1108,6 +1112,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);
Expand Down
23 changes: 23 additions & 0 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,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));
Expand Down Expand Up @@ -3807,6 +3811,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()) {
Comment on lines +3820 to +3821
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (v) {
if (v->world_2d.is_valid()) {
if (v && v->world_2d.is_valid()) {

Can be simplified.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this was written like this to be consistent with 3D:

Viewport *v = Object::cast_to<Viewport>(p_node);
if (v) {
        if (v->world_3d.is_valid() || v->own_world_3d.is_valid()) {
                return;
        }
}

Which would need to become if (v && (v->world_3d.is_valid() || v->own_world_3d.is_valid())) too.

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);
Expand Down
2 changes: 2 additions & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,8 @@ class Viewport : public Node {
bool is_using_xr();
#endif // _3D_DISABLED

void _propagate_world_2d_changed(Node *p_node);

void _validate_property(PropertyInfo &p_property) const;
Viewport();
~Viewport();
Expand Down