Skip to content

Commit

Permalink
Merge pull request #57179 from spacechase0/notify-world2d-changed-master
Browse files Browse the repository at this point in the history
Propagate previously unused NOTIFICATION_WORLD_2D_CHANGED, make CanvasItem/CollisionObject2D use it
  • Loading branch information
akien-mga committed May 8, 2023
2 parents 491a437 + 46e06ee commit bbe05b6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/classes/CanvasItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
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 @@ -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();
}
}
}

Expand Down Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,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

0 comments on commit bbe05b6

Please sign in to comment.