Skip to content

Commit 8bf2afd

Browse files
committed
Fix CollisionShape{2D,3D}.debug_color inconsistencies
1 parent eb51030 commit 8bf2afd

8 files changed

+92
-44
lines changed

doc/classes/CollisionShape2D.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
<link title="2D Kinematic Character Demo">https://godotengine.org/asset-library/asset/2719</link>
1414
</tutorials>
1515
<members>
16-
<member name="debug_color" type="Color" setter="set_debug_color" getter="get_debug_color" default="Color(0, 0, 0, 1)">
17-
The collision shape debug color.
18-
[b]Note:[/b] The default value is [member ProjectSettings.debug/shapes/collision/shape_color]. The [code]Color(0, 0, 0, 1)[/code] value documented here is a placeholder, and not the actual default debug color.
16+
<member name="debug_color" type="Color" setter="set_debug_color" getter="get_debug_color" default="Color(0, 0, 0, 0)">
17+
The collision shape color that is displayed in the editor, or in the running project if [b]Debug &gt; Visible Collision Shapes[/b] is checked at the top of the editor.
18+
[b]Note:[/b] The default value is [member ProjectSettings.debug/shapes/collision/shape_color]. The [code]Color(0, 0, 0, 0)[/code] value documented here is a placeholder, and not the actual default debug color.
1919
</member>
2020
<member name="disabled" type="bool" setter="set_disabled" getter="is_disabled" default="false" keywords="enabled">
2121
A disabled collision shape has no effect in the world. This property should be changed with [method Object.set_deferred].

doc/classes/CollisionShape3D.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
</methods>
3131
<members>
3232
<member name="debug_color" type="Color" setter="set_debug_color" getter="get_debug_color" default="Color(0, 0, 0, 0)">
33-
The collision shape color that is displayed in the editor, or in the running project if [b]Debug &gt; Visible Collision Shapes[/b] is checked at the top of the editor. If this is reset to its default value of [code]Color(0, 0, 0, 0)[/code], the value of [member ProjectSettings.debug/shapes/collision/shape_color] will be used instead.
33+
The collision shape color that is displayed in the editor, or in the running project if [b]Debug &gt; Visible Collision Shapes[/b] is checked at the top of the editor.
34+
[b]Note:[/b] The default value is [member ProjectSettings.debug/shapes/collision/shape_color]. The [code]Color(0, 0, 0, 0)[/code] value documented here is a placeholder, and not the actual default debug color.
3435
</member>
3536
<member name="debug_fill" type="bool" setter="set_enable_debug_fill" getter="get_enable_debug_fill" default="true">
3637
If [code]true[/code], when the shape is displayed, it will show a solid fill color in addition to its wireframe.

scene/2d/physics/collision_shape_2d.cpp

+22-8
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ void CollisionShape2D::_update_in_shape_owner(bool p_xform_only) {
4949
collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
5050
}
5151

52-
Color CollisionShape2D::_get_default_debug_color() const {
53-
SceneTree *st = SceneTree::get_singleton();
54-
return st ? st->get_debug_collisions_color() : Color();
55-
}
56-
5752
void CollisionShape2D::_notification(int p_what) {
5853
switch (p_what) {
5954
case NOTIFICATION_PARENTED: {
@@ -232,7 +227,18 @@ real_t CollisionShape2D::get_one_way_collision_margin() const {
232227
return one_way_collision_margin;
233228
}
234229

230+
#ifdef DEBUG_ENABLED
231+
232+
Color CollisionShape2D::_get_default_debug_color() const {
233+
const SceneTree *st = SceneTree::get_singleton();
234+
return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);
235+
}
236+
235237
void CollisionShape2D::set_debug_color(const Color &p_color) {
238+
if (debug_color == p_color) {
239+
return;
240+
}
241+
236242
debug_color = p_color;
237243
queue_redraw();
238244
}
@@ -266,6 +272,8 @@ void CollisionShape2D::_validate_property(PropertyInfo &p_property) const {
266272
}
267273
}
268274

275+
#endif // DEBUG_ENABLED
276+
269277
void CollisionShape2D::_bind_methods() {
270278
ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);
271279
ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);
@@ -275,20 +283,26 @@ void CollisionShape2D::_bind_methods() {
275283
ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);
276284
ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionShape2D::set_one_way_collision_margin);
277285
ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionShape2D::get_one_way_collision_margin);
278-
ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape2D::set_debug_color);
279-
ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape2D::get_debug_color);
280286

281287
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
282288
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
283289
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
284290
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "one_way_collision_margin", PROPERTY_HINT_RANGE, "0,128,0.1,suffix:px"), "set_one_way_collision_margin", "get_one_way_collision_margin");
291+
292+
#ifdef DEBUG_ENABLED
293+
ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape2D::set_debug_color);
294+
ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape2D::get_debug_color);
295+
285296
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
286297
// Default value depends on a project setting, override for doc generation purposes.
287-
ADD_PROPERTY_DEFAULT("debug_color", Color());
298+
ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));
299+
#endif // DEBUG_ENABLED
288300
}
289301

290302
CollisionShape2D::CollisionShape2D() {
291303
set_notify_local_transform(true);
292304
set_hide_clip_children(true);
305+
#ifdef DEBUG_ENABLED
293306
debug_color = _get_default_debug_color();
307+
#endif // DEBUG_ENABLED
294308
}

scene/2d/physics/collision_shape_2d.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,26 @@ class CollisionShape2D : public Node2D {
4545
bool disabled = false;
4646
bool one_way_collision = false;
4747
real_t one_way_collision_margin = 1.0;
48-
Color debug_color;
4948

5049
void _shape_changed();
5150
void _update_in_shape_owner(bool p_xform_only = false);
51+
52+
// Not wrapped in `#ifdef DEBUG_ENABLED` as it is used for rendering.
53+
Color debug_color = Color(0.0, 0.0, 0.0, 0.0);
54+
55+
#ifdef DEBUG_ENABLED
5256
Color _get_default_debug_color() const;
57+
#endif // DEBUG_ENABLED
5358

5459
protected:
5560
void _notification(int p_what);
61+
62+
#ifdef DEBUG_ENABLED
5663
bool _property_can_revert(const StringName &p_name) const;
5764
bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
5865
void _validate_property(PropertyInfo &p_property) const;
66+
#endif // DEBUG_ENABLED
67+
5968
static void _bind_methods();
6069

6170
public:
@@ -77,8 +86,10 @@ class CollisionShape2D : public Node2D {
7786
void set_one_way_collision_margin(real_t p_margin);
7887
real_t get_one_way_collision_margin() const;
7988

89+
#ifdef DEBUG_ENABLED
8090
void set_debug_color(const Color &p_color);
8191
Color get_debug_color() const;
92+
#endif // DEBUG_ENABLED
8293

8394
PackedStringArray get_configuration_warnings() const override;
8495

scene/3d/physics/collision_shape_3d.cpp

+39-27
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ void CollisionShape3D::_update_in_shape_owner(bool p_xform_only) {
8181
void CollisionShape3D::_notification(int p_what) {
8282
switch (p_what) {
8383
case NOTIFICATION_PARENTED: {
84-
#ifdef DEBUG_ENABLED
85-
if (debug_color == get_placeholder_default_color()) {
86-
debug_color = SceneTree::get_singleton()->get_debug_collisions_color();
87-
}
88-
#endif // DEBUG_ENABLED
89-
9084
collision_object = Object::cast_to<CollisionObject3D>(get_parent());
9185
if (collision_object) {
9286
owner_id = collision_object->create_shape_owner(this);
@@ -189,7 +183,7 @@ void CollisionShape3D::_bind_methods() {
189183

190184
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
191185
// Default value depends on a project setting, override for doc generation purposes.
192-
ADD_PROPERTY_DEFAULT("debug_color", get_placeholder_default_color());
186+
ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));
193187

194188
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_fill"), "set_enable_debug_fill", "get_enable_debug_fill");
195189
#endif // DEBUG_ENABLED
@@ -200,27 +194,27 @@ void CollisionShape3D::set_shape(const Ref<Shape3D> &p_shape) {
200194
return;
201195
}
202196
if (shape.is_valid()) {
203-
shape->disconnect_changed(callable_mp(this, &CollisionShape3D::shape_changed));
197+
#ifdef DEBUG_ENABLED
198+
shape->disconnect_changed(callable_mp(this, &CollisionShape3D::_shape_changed));
199+
#endif // DEBUG_ENABLED
204200
shape->disconnect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
205201
}
206202
shape = p_shape;
207203
if (shape.is_valid()) {
208204
#ifdef DEBUG_ENABLED
209-
if (shape->get_debug_color() != get_placeholder_default_color()) {
205+
if (shape->are_debug_properties_edited()) {
210206
set_debug_color(shape->get_debug_color());
211207
set_debug_fill_enabled(shape->get_debug_fill());
212-
} else if (get_debug_color() != get_placeholder_default_color()) {
213-
shape->set_debug_color(debug_color);
214-
shape->set_debug_fill(debug_fill);
215208
} else {
216-
set_debug_color(SceneTree::get_singleton()->get_debug_collisions_color());
217-
shape->set_debug_color(SceneTree::get_singleton()->get_debug_collisions_color());
209+
shape->set_debug_color(debug_color);
218210
shape->set_debug_fill(debug_fill);
219211
}
220212
#endif // DEBUG_ENABLED
221213

222214
shape->connect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
223-
shape->connect_changed(callable_mp(this, &CollisionShape3D::shape_changed));
215+
#ifdef DEBUG_ENABLED
216+
shape->connect_changed(callable_mp(this, &CollisionShape3D::_shape_changed));
217+
#endif // DEBUG_ENABLED
224218
}
225219
update_gizmos();
226220
if (collision_object) {
@@ -254,15 +248,21 @@ bool CollisionShape3D::is_disabled() const {
254248
}
255249

256250
#ifdef DEBUG_ENABLED
251+
252+
Color CollisionShape3D::_get_default_debug_color() const {
253+
const SceneTree *st = SceneTree::get_singleton();
254+
return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);
255+
}
256+
257257
void CollisionShape3D::set_debug_color(const Color &p_color) {
258-
if (p_color == get_placeholder_default_color()) {
259-
debug_color = SceneTree::get_singleton()->get_debug_collisions_color();
260-
} else if (debug_color != p_color) {
261-
debug_color = p_color;
258+
if (debug_color == p_color) {
259+
return;
260+
}
262261

263-
if (shape.is_valid()) {
264-
shape->set_debug_color(p_color);
265-
}
262+
debug_color = p_color;
263+
264+
if (shape.is_valid()) {
265+
shape->set_debug_color(p_color);
266266
}
267267
}
268268

@@ -295,27 +295,39 @@ bool CollisionShape3D::_property_can_revert(const StringName &p_name) const {
295295

296296
bool CollisionShape3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
297297
if (p_name == "debug_color") {
298-
r_property = SceneTree::get_singleton()->get_debug_collisions_color();
298+
r_property = _get_default_debug_color();
299299
return true;
300300
}
301301
return false;
302302
}
303-
#endif // DEBUG_ENABLED
304303

305-
void CollisionShape3D::shape_changed() {
306-
#ifdef DEBUG_ENABLED
304+
void CollisionShape3D::_validate_property(PropertyInfo &p_property) const {
305+
if (p_property.name == "debug_color") {
306+
if (debug_color == _get_default_debug_color()) {
307+
p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE;
308+
} else {
309+
p_property.usage = PROPERTY_USAGE_DEFAULT;
310+
}
311+
}
312+
}
313+
314+
void CollisionShape3D::_shape_changed() {
307315
if (shape->get_debug_color() != debug_color) {
308316
set_debug_color(shape->get_debug_color());
309317
}
310318
if (shape->get_debug_fill() != debug_fill) {
311319
set_debug_fill_enabled(shape->get_debug_fill());
312320
}
313-
#endif // DEBUG_ENABLED
314321
}
315322

323+
#endif // DEBUG_ENABLED
324+
316325
CollisionShape3D::CollisionShape3D() {
317326
//indicator = RenderingServer::get_singleton()->mesh_create();
318327
set_notify_local_transform(true);
328+
#ifdef DEBUG_ENABLED
329+
debug_color = _get_default_debug_color();
330+
#endif // DEBUG_ENABLED
319331
}
320332

321333
CollisionShape3D::~CollisionShape3D() {

scene/3d/physics/collision_shape_3d.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ class CollisionShape3D : public Node3D {
4444
CollisionObject3D *collision_object = nullptr;
4545

4646
#ifdef DEBUG_ENABLED
47-
Color debug_color = get_placeholder_default_color();
47+
Color debug_color;
4848
bool debug_fill = true;
4949

50-
static const Color get_placeholder_default_color() { return Color(0.0, 0.0, 0.0, 0.0); }
50+
Color _get_default_debug_color() const;
51+
void _shape_changed();
5152
#endif // DEBUG_ENABLED
5253

5354
#ifndef DISABLE_DEPRECATED
@@ -65,10 +66,9 @@ class CollisionShape3D : public Node3D {
6566
#ifdef DEBUG_ENABLED
6667
bool _property_can_revert(const StringName &p_name) const;
6768
bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
69+
void _validate_property(PropertyInfo &p_property) const;
6870
#endif // DEBUG_ENABLED
6971

70-
void shape_changed();
71-
7272
public:
7373
void make_convex_from_siblings();
7474

scene/resources/3d/shape_3d.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ void Shape3D::set_margin(real_t p_margin) {
6767
}
6868

6969
#ifdef DEBUG_ENABLED
70+
7071
void Shape3D::set_debug_color(const Color &p_color) {
7172
if (p_color == debug_color) {
7273
return;
7374
}
7475

7576
debug_color = p_color;
77+
debug_properties_edited = true;
7678
_update_shape();
7779
}
7880

@@ -86,12 +88,14 @@ void Shape3D::set_debug_fill(bool p_fill) {
8688
}
8789

8890
debug_fill = p_fill;
91+
debug_properties_edited = true;
8992
_update_shape();
9093
}
9194

9295
bool Shape3D::get_debug_fill() const {
9396
return debug_fill;
9497
}
98+
9599
#endif // DEBUG_ENABLED
96100

97101
Ref<ArrayMesh> Shape3D::get_debug_mesh() {

scene/resources/3d/shape_3d.h

+6
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ class Shape3D : public Resource {
4747
Ref<ArrayMesh> debug_mesh_cache;
4848
Ref<Material> collision_material;
4949

50+
// Not wrapped in `#ifdef DEBUG_ENABLED` as it is used for rendering.
5051
Color debug_color = Color(0.0, 0.0, 0.0, 0.0);
5152
bool debug_fill = true;
53+
#ifdef DEBUG_ENABLED
54+
bool debug_properties_edited = false;
55+
#endif // DEBUG_ENABLED
5256

5357
protected:
5458
static void _bind_methods();
@@ -83,6 +87,8 @@ class Shape3D : public Resource {
8387

8488
void set_debug_fill(bool p_fill);
8589
bool get_debug_fill() const;
90+
91+
_FORCE_INLINE_ bool are_debug_properties_edited() const { return debug_properties_edited; }
8692
#endif // DEBUG_ENABLED
8793

8894
Shape3D();

0 commit comments

Comments
 (0)