Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Allow toggling SceneTree::debug_collisions_hint at runtime

Thomas ten Catec83dfbfe7660
diff --git a/doc/classes/SceneTree.xml b/doc/classes/SceneTree.xml
index 4ef591aec77..f4b0d786ba8 100644
--- a/doc/classes/SceneTree.xml
+++ b/doc/classes/SceneTree.xml
@@ -265,7 +265,6 @@
</member>
<member name="debug_collisions_hint" type="bool" setter="set_debug_collisions_hint" getter="is_debugging_collisions_hint" default="false">
If [code]true[/code], collision shapes will be visible when running the game from the editor for debugging purposes.
- [b]Note:[/b] This property is not designed to be changed at run-time. Changing the value of [member debug_collisions_hint] while the project is running will not have the desired effect.
</member>
<member name="debug_navigation_hint" type="bool" setter="set_debug_navigation_hint" getter="is_debugging_navigation_hint" default="false">
If [code]true[/code], navigation polygons will be visible when running the game from the editor for debugging purposes.
diff --git a/editor/debugger/debugger_editor_plugin.cpp b/editor/debugger/debugger_editor_plugin.cpp
index 75f4b9b19ac..84a34448641 100644
--- a/editor/debugger/debugger_editor_plugin.cpp
+++ b/editor/debugger/debugger_editor_plugin.cpp
@@ -35,6 +35,7 @@
#include "editor/debugger/editor_debugger_node.h"
#include "editor/debugger/editor_debugger_server.h"
#include "editor/debugger/editor_file_server.h"
+#include "editor/debugger/script_editor_debugger.h"
#include "editor/docks/editor_dock_manager.h"
#include "editor/editor_node.h"
#include "editor/run/run_instances_dialog.h"
@@ -147,10 +148,11 @@ void DebuggerEditorPlugin::_menu_option(int p_option) {
} break;
case RUN_DEBUG_COLLISIONS: {
- bool ischecked = debug_menu->is_item_checked(debug_menu->get_item_index(RUN_DEBUG_COLLISIONS));
- debug_menu->set_item_checked(debug_menu->get_item_index(RUN_DEBUG_COLLISIONS), !ischecked);
+ bool enable = !debug_menu->is_item_checked(debug_menu->get_item_index(RUN_DEBUG_COLLISIONS));
+ debug_menu->set_item_checked(debug_menu->get_item_index(RUN_DEBUG_COLLISIONS), enable);
if (!initializing) {
- EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_debug_collisions", !ischecked);
+ EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_debug_collisions", enable);
+ EditorDebuggerNode::get_singleton()->set_debug_collisions(enable);
}
} break;
diff --git a/editor/debugger/editor_debugger_node.cpp b/editor/debugger/editor_debugger_node.cpp
index 9605a8e8f27..a7accbe1be5 100644
--- a/editor/debugger/editor_debugger_node.cpp
+++ b/editor/debugger/editor_debugger_node.cpp
@@ -920,6 +920,12 @@ bool EditorDebuggerNode::get_debug_mute_audio() const {
return debug_mute_audio;
}
+void EditorDebuggerNode::set_debug_collisions(bool p_enabled) {
+ _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
+ dbg->set_debug_collisions(p_enabled);
+ });
+}
+
void EditorDebuggerNode::set_camera_override(CameraOverride p_override) {
_for_all(tabs, [&](ScriptEditorDebugger *dbg) {
dbg->set_camera_override(p_override);
diff --git a/editor/debugger/editor_debugger_node.h b/editor/debugger/editor_debugger_node.h
index 0d410e1d623..25328d2200f 100644
--- a/editor/debugger/editor_debugger_node.h
+++ b/editor/debugger/editor_debugger_node.h
@@ -217,6 +217,8 @@ public:
void set_debug_mute_audio(bool p_mute);
bool get_debug_mute_audio() const;
+ void set_debug_collisions(bool p_enabled);
+
void set_camera_override(CameraOverride p_override);
CameraOverride get_camera_override();
diff --git a/editor/debugger/script_editor_debugger.cpp b/editor/debugger/script_editor_debugger.cpp
index 58cb3e81e7b..f7a9844a9d3 100644
--- a/editor/debugger/script_editor_debugger.cpp
+++ b/editor/debugger/script_editor_debugger.cpp
@@ -1714,6 +1714,11 @@ void ScriptEditorDebugger::_mute_audio_on_break(bool p_mute) {
audio_muted_on_break = p_mute;
}
+void ScriptEditorDebugger::set_debug_collisions(bool p_enable) {
+ Array msg = { p_enable };
+ _put_msg("scene:set_debug_collisions", msg);
+}
+
CameraOverride ScriptEditorDebugger::get_camera_override() const {
return camera_override;
}
diff --git a/editor/debugger/script_editor_debugger.h b/editor/debugger/script_editor_debugger.h
index 35eea208476..41a3f693747 100644
--- a/editor/debugger/script_editor_debugger.h
+++ b/editor/debugger/script_editor_debugger.h
@@ -372,6 +372,8 @@ public:
bool get_debug_mute_audio() const;
void set_debug_mute_audio(bool p_mute);
+ void set_debug_collisions(bool p_enable);
+
EditorDebuggerNode::CameraOverride get_camera_override() const;
void set_camera_override(EditorDebuggerNode::CameraOverride p_override);
diff --git a/modules/csg/csg_shape.cpp b/modules/csg/csg_shape.cpp
index ff5c47cb169..d36bc9253bc 100644
--- a/modules/csg/csg_shape.cpp
+++ b/modules/csg/csg_shape.cpp
@@ -1062,6 +1062,14 @@ void CSGShape3D::_notification(int p_what) {
}
_on_transform_changed();
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ if (_is_debug_collision_shape_visible()) {
+ _update_debug_collision_shape();
+ } else {
+ _clear_debug_collision_shape();
+ }
+ } break;
#endif // PHYSICS_3D_DISABLED
}
}
diff --git a/modules/gridmap/grid_map.cpp b/modules/gridmap/grid_map.cpp
index 81c44bcca2c..c51e2a3994b 100644
--- a/modules/gridmap/grid_map.cpp
+++ b/modules/gridmap/grid_map.cpp
@@ -1280,6 +1280,10 @@ void GridMap::_notification(int p_what) {
case NOTIFICATION_VISIBILITY_CHANGED: {
_update_visibility();
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ _recreate_octant_data();
+ } break;
}
}
diff --git a/modules/tilemap/tile_map_layer.cpp b/modules/tilemap/tile_map_layer.cpp
index 9ab005480da..aa526f3bcb0 100644
--- a/modules/tilemap/tile_map_layer.cpp
+++ b/modules/tilemap/tile_map_layer.cpp
@@ -2171,6 +2171,11 @@ void TileMapLayer::_notification(int p_what) {
dirty.flags[DIRTY_FLAGS_LAYER_VISIBILITY] = true;
_queue_internal_update();
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ dirty.flags[DIRTY_FLAGS_LAYER_COLLISION_VISIBILITY_MODE] = true;
+ _queue_internal_update();
+ } break;
}
_rendering_notification(p_what);
diff --git a/scene/2d/physics/collision_polygon_2d.cpp b/scene/2d/physics/collision_polygon_2d.cpp
index 44462be7d3c..5b22777e14a 100644
--- a/scene/2d/physics/collision_polygon_2d.cpp
+++ b/scene/2d/physics/collision_polygon_2d.cpp
@@ -170,6 +170,10 @@ void CollisionPolygon2D::_notification(int p_what) {
draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
}
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ queue_redraw();
+ } break;
}
}
diff --git a/scene/2d/physics/collision_shape_2d.cpp b/scene/2d/physics/collision_shape_2d.cpp
index 5ba9dd6d968..bde83bbb210 100644
--- a/scene/2d/physics/collision_shape_2d.cpp
+++ b/scene/2d/physics/collision_shape_2d.cpp
@@ -135,6 +135,10 @@ void CollisionShape2D::_notification(int p_what) {
draw_primitive(pts, cols, Vector<Vector2>());
}
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ queue_redraw();
+ } break;
}
}
diff --git a/scene/2d/physics/joints/joint_2d.cpp b/scene/2d/physics/joints/joint_2d.cpp
index 2769d8dc53b..e4d1d8938fe 100644
--- a/scene/2d/physics/joints/joint_2d.cpp
+++ b/scene/2d/physics/joints/joint_2d.cpp
@@ -189,6 +189,10 @@ void Joint2D::_notification(int p_what) {
}
_update_joint(true);
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ queue_redraw();
+ } break;
}
}
diff --git a/scene/2d/physics/ray_cast_2d.cpp b/scene/2d/physics/ray_cast_2d.cpp
index 9406a02ea65..b7f61a3458d 100644
--- a/scene/2d/physics/ray_cast_2d.cpp
+++ b/scene/2d/physics/ray_cast_2d.cpp
@@ -180,6 +180,10 @@ void RayCast2D::_notification(int p_what) {
}
_update_raycast_state();
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ queue_redraw();
+ } break;
}
}
diff --git a/scene/2d/physics/shape_cast_2d.cpp b/scene/2d/physics/shape_cast_2d.cpp
index 04bfc80c28b..b9284e47e1f 100644
--- a/scene/2d/physics/shape_cast_2d.cpp
+++ b/scene/2d/physics/shape_cast_2d.cpp
@@ -283,6 +283,10 @@ void ShapeCast2D::_notification(int p_what) {
}
_update_shapecast_state();
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ queue_redraw();
+ } break;
}
}
diff --git a/scene/2d/physics/touch_screen_button.cpp b/scene/2d/physics/touch_screen_button.cpp
index 3693910638e..292cee42878 100644
--- a/scene/2d/physics/touch_screen_button.cpp
+++ b/scene/2d/physics/touch_screen_button.cpp
@@ -217,6 +217,10 @@ void TouchScreenButton::_notification(int p_what) {
_release();
}
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ queue_redraw();
+ } break;
}
}
diff --git a/scene/3d/physics/collision_object_3d.cpp b/scene/3d/physics/collision_object_3d.cpp
index ae7b2be354f..fa9a1a6413d 100644
--- a/scene/3d/physics/collision_object_3d.cpp
+++ b/scene/3d/physics/collision_object_3d.cpp
@@ -140,6 +140,17 @@ void CollisionObject3D::_notification(int p_what) {
case NOTIFICATION_ENABLED: {
_apply_enabled();
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ if (_are_collision_shapes_visible()) {
+ for (const KeyValue<uint32_t, ShapeData> &E : shapes) {
+ debug_shapes_to_update.insert(E.key);
+ }
+ _update_debug_shapes();
+ } else {
+ _clear_debug_shapes();
+ }
+ } break;
}
}
diff --git a/scene/3d/physics/ray_cast_3d.cpp b/scene/3d/physics/ray_cast_3d.cpp
index f3cb0f96521..de4a8d134b3 100644
--- a/scene/3d/physics/ray_cast_3d.cpp
+++ b/scene/3d/physics/ray_cast_3d.cpp
@@ -218,6 +218,14 @@ void RayCast3D::_notification(int p_what) {
}
}
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ if (get_tree()->is_debugging_collisions_hint()) {
+ _update_debug_shape();
+ } else {
+ _clear_debug_shape();
+ }
+ } break;
}
}
diff --git a/scene/3d/physics/shape_cast_3d.cpp b/scene/3d/physics/shape_cast_3d.cpp
index 495f4ef8a9f..1637e104057 100644
--- a/scene/3d/physics/shape_cast_3d.cpp
+++ b/scene/3d/physics/shape_cast_3d.cpp
@@ -103,6 +103,14 @@ void ShapeCast3D::_notification(int p_what) {
}
}
} break;
+
+ case NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED: {
+ if (get_tree()->is_debugging_collisions_hint()) {
+ _update_debug_shape();
+ } else {
+ _clear_debug_shape();
+ }
+ } break;
}
}
diff --git a/scene/debugger/scene_debugger.cpp b/scene/debugger/scene_debugger.cpp
index dbae7a40da5..d060deb9949 100644
--- a/scene/debugger/scene_debugger.cpp
+++ b/scene/debugger/scene_debugger.cpp
@@ -270,6 +270,13 @@ Error SceneDebugger::_msg_hdr_output_toggle_requested(const Array &p_args) {
return OK;
}
+Error SceneDebugger::_msg_set_debug_collisions(const Array &p_args) {
+ ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
+ bool enabled = p_args[0];
+ SceneTree::get_singleton()->set_debug_collisions_hint(enabled);
+ return OK;
+}
+
Error SceneDebugger::_msg_override_cameras(const Array &p_args) {
ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
bool enable = p_args[0];
@@ -621,6 +628,7 @@ void SceneDebugger::_init_message_handlers() {
message_handlers["window_request_size"] = _msg_window_request_size;
message_handlers["hdr_output_request_state"] = _msg_hdr_output_request_state;
message_handlers["hdr_output_toggle_requested"] = _msg_hdr_output_toggle_requested;
+ message_handlers["set_debug_collisions"] = _msg_set_debug_collisions;
message_handlers["override_cameras"] = _msg_override_cameras;
message_handlers["transform_camera_2d"] = _msg_transform_camera_2d;
#ifndef _3D_DISABLED
diff --git a/scene/debugger/scene_debugger.h b/scene/debugger/scene_debugger.h
index 56a1c602d44..b0912fa70e7 100644
--- a/scene/debugger/scene_debugger.h
+++ b/scene/debugger/scene_debugger.h
@@ -85,6 +85,7 @@ private:
static Error _msg_window_request_size(const Array &p_args);
static Error _msg_hdr_output_request_state(const Array &p_args);
static Error _msg_hdr_output_toggle_requested(const Array &p_args);
+ static Error _msg_set_debug_collisions(const Array &p_args);
static Error _msg_override_cameras(const Array &p_args);
static Error _msg_set_object_property(const Array &p_args);
static Error _msg_set_object_property_field(const Array &p_args);
diff --git a/scene/main/node.h b/scene/main/node.h
index e9fc807b213..b558dbc7b4b 100644
--- a/scene/main/node.h
+++ b/scene/main/node.h
@@ -507,7 +507,10 @@ public:
NOTIFICATION_APPLICATION_PIP_MODE_ENTERED = 2019,
NOTIFICATION_APPLICATION_PIP_MODE_EXITED = 2020,
- // Editor specific node notifications
+ // Debug-related notifications.
+ NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED = 4000,
+
+ // Editor specific node notifications.
NOTIFICATION_EDITOR_PRE_SAVE = 9001,
NOTIFICATION_EDITOR_POST_SAVE = 9002,
NOTIFICATION_SUSPENDED = 9003,
diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp
index b3760bcdffa..16caef4e7f0 100644
--- a/scene/main/scene_tree.cpp
+++ b/scene/main/scene_tree.cpp
@@ -966,7 +966,13 @@ void SceneTree::set_quit_on_go_back(bool p_enable) {
#ifdef DEBUG_ENABLED
void SceneTree::set_debug_collisions_hint(bool p_enabled) {
+ if (debug_collisions_hint == p_enabled) {
+ return;
+ }
debug_collisions_hint = p_enabled;
+ if (root) {
+ root->propagate_notification(Node::NOTIFICATION_DEBUG_COLLISIONS_HINT_CHANGED);
+ }
}
bool SceneTree::is_debugging_collisions_hint() const {