add debugger

This commit is contained in:
2025-07-18 20:58:34 +10:00
parent 32eb6a8953
commit 343543bd02
5 changed files with 292 additions and 0 deletions

View File

@@ -18,6 +18,10 @@ boot_splash/bg_color=Color(0, 0, 0, 1)
boot_splash/show_image=false
config/icon="res://icon.svg"
[autoload]
Debugger="*res://scenes/debugger.tscn"
[debug]
gdscript/warnings/untyped_declaration=2
@@ -34,6 +38,14 @@ window/size/window_height_override=720
window/stretch/mode="canvas_items"
window/stretch/aspect="expand"
[input]
toggle_debug={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":96,"key_label":0,"unicode":96,"location":0,"echo":false,"script":null)
]
}
[physics]
common/physics_jitter_fix=0.0

108
scenes/debugger.tscn Normal file
View File

@@ -0,0 +1,108 @@
[gd_scene load_steps=3 format=3 uid="uid://i4fvg0xxoi80"]
[ext_resource type="Script" uid="uid://dtfteh6q2cdc6" path="res://scripts/debug/debugger.gd" id="1_xl6mi"]
[sub_resource type="SystemFont" id="SystemFont_184hu"]
font_names = PackedStringArray("Monospace")
font_weight = 700
[node name="Debugger" type="CanvasLayer"]
process_mode = 3
process_priority = 1000
process_physics_priority = 1000
layer = 100
script = ExtResource("1_xl6mi")
[node name="Control" type="Control" parent="."]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
[node name="LabelContainer1" type="VBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 9
anchor_bottom = 1.0
offset_right = 1300.0
grow_vertical = 2
mouse_filter = 2
[node name="Label1" type="RichTextLabel" parent="Control/LabelContainer1"]
clip_contents = false
layout_mode = 2
mouse_filter = 2
theme_override_constants/outline_size = 10
theme_override_fonts/normal_font = SubResource("SystemFont_184hu")
theme_override_font_sizes/normal_font_size = 24
bbcode_enabled = true
text = "Lorem Ipsum"
fit_content = true
[node name="LabelContainer2" type="VBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 9
anchor_bottom = 1.0
offset_right = 1300.0
grow_vertical = 2
mouse_filter = 2
alignment = 2
[node name="Label2" type="RichTextLabel" parent="Control/LabelContainer2"]
clip_contents = false
layout_mode = 2
mouse_filter = 2
theme_override_constants/outline_size = 10
theme_override_fonts/normal_font = SubResource("SystemFont_184hu")
theme_override_font_sizes/normal_font_size = 24
bbcode_enabled = true
text = "Lorem Ipsum"
fit_content = true
[node name="LabelContainer3" type="VBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 11
anchor_left = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = -600.0
grow_horizontal = 0
grow_vertical = 2
mouse_filter = 2
[node name="Label3" type="RichTextLabel" parent="Control/LabelContainer3"]
clip_contents = false
layout_mode = 2
mouse_filter = 2
theme_override_constants/outline_size = 10
theme_override_fonts/normal_font = SubResource("SystemFont_184hu")
theme_override_font_sizes/normal_font_size = 24
bbcode_enabled = true
text = "Lorem Ipsum"
fit_content = true
[node name="LabelContainer4" type="VBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 11
anchor_left = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = -600.0
grow_horizontal = 0
grow_vertical = 2
mouse_filter = 2
alignment = 2
[node name="Label4" type="RichTextLabel" parent="Control/LabelContainer4"]
clip_contents = false
layout_mode = 2
mouse_filter = 2
theme_override_constants/outline_size = 10
theme_override_fonts/normal_font = SubResource("SystemFont_184hu")
theme_override_font_sizes/normal_font_size = 24
bbcode_enabled = true
text = "DEBUG CONTROLS
toggle debug: [ ` ]"
fit_content = true

169
scripts/debug/debugger.gd Normal file
View File

@@ -0,0 +1,169 @@
extends CanvasLayer
## Handles displaying debug info.
signal mode_changed(mode: Mode)
enum Mode { DISABLED, PERFORMANCE, FULL }
var mode: Mode = Mode.PERFORMANCE
var _text_to_draw: Dictionary = {}
var _events_to_draw: Dictionary = {}
var _label1_text: String = ""
var _label2_text: String = ""
var _label3_text: String = ""
@onready var _label1: RichTextLabel = $Control/LabelContainer1/Label1
@onready var _label2: RichTextLabel = $Control/LabelContainer2/Label2
@onready var _label3: RichTextLabel = $Control/LabelContainer3/Label3
@onready var _label4: RichTextLabel = $Control/LabelContainer4/Label4
func _ready() -> void:
assert(_label1, str(self) + ": _label1 missing!")
assert(_label2, str(self) + ": _label2 missing!")
assert(_label3, str(self) + ": _label2 missing!")
assert(_label4, str(self) + ": _label2 missing!")
# enabled = OS.has_feature("editor")
_update_visibility()
mode_changed.emit(mode)
func _process(_delta: float) -> void:
call_deferred(&"_process_text_labels")
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("toggle_debug"):
mode = wrapi(mode + 1, 0, Mode.size()) as Mode
_update_visibility()
mode_changed.emit(mode)
func show_debug() -> bool:
return mode == Mode.FULL
func text(key: String, value: Variant, label_index: int = 1) -> void:
if not show_debug():
return
_text_to_draw[key] = {"value": value, "label_index": label_index}
func add_event(key: String) -> void:
_events_to_draw[key] = {"frame": -9999, "args": []}
func event_emitted(key: String, args: Array[Variant] = []) -> void:
if not show_debug():
return
_events_to_draw[key] = {"frame": Engine.get_physics_frames(), "args": args}
func _update_visibility() -> void:
visible = mode != Mode.DISABLED
func _append_text(key: String, value: Variant, label_index: int) -> void:
var line_text := str(value)
if value is int:
line_text = (" " if value >= 0 else "") + line_text
if (value as int) > 0:
line_text = "[color=sky_blue]%s[/color]" % line_text
elif (value as int) < 0:
line_text = "[color=salmon]%s[/color]" % line_text
elif value is float:
line_text = (" " if value >= 0 else "") + "%.6f" % value
if value > 0:
line_text = "[color=sky_blue]%s[/color]" % line_text
elif value < 0:
line_text = "[color=salmon]%s[/color]" % line_text
elif value is bool:
if value:
line_text = "[color=sky_blue]%s[/color]" % line_text
else:
line_text = "[color=salmon]%s[/color]" % line_text
elif value is Vector3:
line_text = (
"(%s, %s, %s)"
% [
(" " if value.x >= 0 else "") + ("%.6f" % value.x),
(" " if value.y >= 0 else "") + ("%.6f" % value.y),
(" " if value.z >= 0 else "") + ("%.6f" % value.z),
]
)
elif value is Vector2:
line_text = (
"(%s, %s)"
% [
(" " if value.x >= 0 else "") + ("%.6f" % value.x),
(" " if value.y >= 0 else "") + ("%.6f" % value.y),
]
)
line_text = "%s: %s\n" % [key, line_text]
if label_index == 2:
_label2_text += line_text
else:
_label1_text += line_text
func _append_event(key: String, frame: int, args: Array[Variant]) -> void:
var line_text := key
if args.size() > 0:
line_text += "(%s)" % ", ".join(args.map(str))
var physics_frame := Engine.get_physics_frames()
var color := Color.SALMON.lerp(
Color.WHITE, clampf(float(physics_frame - frame) / 30.0, 0, 1)
)
if physics_frame - frame < 5:
color = Color.SKY_BLUE
line_text = "[color=#%s]%s[/color]\n" % [color.to_html(), line_text]
_label3_text += line_text
func _set_label_texts() -> void:
_label1.text = _label1_text
_label2.text = _label2_text
_label3.text = _label3_text
func _process_text_labels() -> void:
if mode != Mode.DISABLED:
_label1_text = ""
_label2_text = ""
_label3_text = ""
_append_text("fps", Engine.get_frames_per_second() as int, 0)
_append_text("gpu", RenderingServer.get_video_adapter_name(), 0)
_append_text(
"resolution",
Vector2i(get_viewport().size * get_viewport().scaling_3d_scale as Vector2),
0
)
if mode == Mode.PERFORMANCE:
_set_label_texts()
return
if mode == Mode.FULL:
for k: String in _text_to_draw.keys():
var v: Dictionary = _text_to_draw[k]
_append_text(k, v["value"] as Variant, v["label_index"] as int)
for k: String in _events_to_draw.keys():
var v: Dictionary = _events_to_draw[k]
_append_event(k, v["frame"] as int, v["args"] as Array[Variant])
_set_label_texts()

View File

@@ -0,0 +1 @@
uid://dtfteh6q2cdc6

View File

@@ -40,6 +40,8 @@ func _process(_delta: float) -> void:
_process_leg_ik(_leg_ik_l, _leg_ik_magnet_l)
_process_leg_ik(_leg_ik_r, _leg_ik_magnet_r)
_process_position()
Debugger.text("_is_moving_leg_l", _is_moving_leg_l, 2)
Debugger.text("_is_moving_leg_r", _is_moving_leg_r, 2)
func _physics_process(_delta: float) -> void: