Skip to content

Commit

Permalink
Port robust signal (dis)connection to ShapeCast2D
Browse files Browse the repository at this point in the history
Ported from ShapeCast3D.
  • Loading branch information
rburing committed Mar 24, 2023
1 parent b31d00a commit 5bed055
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
13 changes: 10 additions & 3 deletions scene/2d/shape_cast_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,18 @@ bool ShapeCast2D::is_enabled() const {
}

void ShapeCast2D::set_shape(const Ref<Shape2D> &p_shape) {
if (p_shape == shape) {
return;
}
if (shape.is_valid()) {
shape->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &ShapeCast2D::_shape_changed));
}
shape = p_shape;
if (p_shape.is_valid()) {
shape->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &ShapeCast2D::_redraw_shape));
if (shape.is_valid()) {
shape->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &ShapeCast2D::_shape_changed));
shape_rid = shape->get_rid();
}

update_configuration_warnings();
queue_redraw();
}
Expand Down Expand Up @@ -186,7 +193,7 @@ bool ShapeCast2D::get_exclude_parent_body() const {
return exclude_parent_body;
}

void ShapeCast2D::_redraw_shape() {
void ShapeCast2D::_shape_changed() {
queue_redraw();
}

Expand Down
2 changes: 1 addition & 1 deletion scene/2d/shape_cast_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ShapeCast2D : public Node2D {
real_t collision_unsafe_fraction = 1.0;

Array _get_collision_result() const;
void _redraw_shape();
void _shape_changed();

protected:
void _notification(int p_what);
Expand Down
6 changes: 2 additions & 4 deletions scene/3d/shape_cast_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,14 @@ void ShapeCast3D::set_shape(const Ref<Shape3D> &p_shape) {
if (p_shape == shape) {
return;
}
if (!shape.is_null()) {
if (shape.is_valid()) {
shape->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &ShapeCast3D::_shape_changed));
shape->unregister_owner(this);
}
shape = p_shape;
if (!shape.is_null()) {
if (shape.is_valid()) {
shape->register_owner(this);
shape->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &ShapeCast3D::_shape_changed));
}
if (p_shape.is_valid()) {
shape_rid = shape->get_rid();
}

Expand Down

0 comments on commit 5bed055

Please sign in to comment.