add debugger

This commit is contained in:
2025-08-21 04:53:53 +10:00
parent d48d75d384
commit a19ace6afc
7 changed files with 295 additions and 1 deletions

View File

@@ -61,6 +61,11 @@ jump={
, 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":4194320,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
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)
]
}
[rendering]

View File

@@ -0,0 +1,4 @@
[gd_resource type="Theme" format=3 uid="uid://c6pgpbs87i3t0"]
[resource]
RichTextLabel/constants/outline_size = 1

107
scenes/debugger.tscn Normal file
View File

@@ -0,0 +1,107 @@
[gd_scene load_steps=3 format=3 uid="uid://xlkx300774in"]
[ext_resource type="Script" uid="uid://o556lddu81ty" path="res://scripts/debugger.gd" id="1_sw8ts"]
[ext_resource type="Theme" uid="uid://c6pgpbs87i3t0" path="res://resources/debugger_theme.tres" id="2_qadbm"]
[node name="Debugger" type="CanvasLayer"]
process_mode = 3
process_priority = 1000
process_physics_priority = 1000
layer = 100
script = ExtResource("1_sw8ts")
[node name="Control" type="Control" parent="."]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 5.0
offset_top = 5.0
offset_right = -5.0
offset_bottom = -5.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
theme = ExtResource("2_qadbm")
[node name="LabelContainer1" type="VBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
[node name="Label1" type="RichTextLabel" parent="Control/LabelContainer1"]
clip_contents = false
layout_mode = 2
mouse_filter = 2
bbcode_enabled = true
text = "Lorem Ipsum"
fit_content = true
[node name="LabelContainer2" type="VBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
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
bbcode_enabled = true
text = "Lorem Ipsum"
fit_content = true
[node name="LabelContainer3" type="VBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
[node name="Label3" type="RichTextLabel" parent="Control/LabelContainer3"]
clip_contents = false
layout_mode = 2
mouse_filter = 2
bbcode_enabled = true
text = "Lorem Ipsum"
fit_content = true
horizontal_alignment = 2
[node name="LabelContainer4" type="VBoxContainer" parent="Control"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
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
bbcode_enabled = true
text = "DEBUG CONTROLS
toggle debug: [ ` ]"
fit_content = true
horizontal_alignment = 2
[node name="RichTextLabel" type="RichTextLabel" parent="Control"]
layout_mode = 0
offset_left = 40.0
offset_top = 55.0
offset_right = 402.0
offset_bottom = 233.0
theme_override_font_sizes/normal_font_size = 36
text = "фыы 汉语ывывф"

View File

@@ -1,7 +1,8 @@
[gd_scene load_steps=25 format=3 uid="uid://cfvb33kf48sga"]
[gd_scene load_steps=26 format=3 uid="uid://cfvb33kf48sga"]
[ext_resource type="PackedScene" uid="uid://db7d5pwp4gds" path="res://scenes/player.tscn" id="1_bl13t"]
[ext_resource type="Texture2D" uid="uid://j3xt1tbjcu2r" path="res://assets/textures/reference/jumpking_0.png" id="1_iyx0m"]
[ext_resource type="PackedScene" uid="uid://xlkx300774in" path="res://scenes/debugger.tscn" id="1_lpvoh"]
[ext_resource type="Texture2D" uid="uid://01wmnd4hn8t8" path="res://assets/textures/reference/jumpking_1.png" id="2_lpvoh"]
[ext_resource type="Texture2D" uid="uid://dwarw2lae1ag" path="res://assets/textures/reference/jumpking_2.png" id="3_at5dv"]
[ext_resource type="Script" uid="uid://ceyu5der8j8gq" path="res://scripts/trajectory.gd" id="4_74lek"]
@@ -65,6 +66,8 @@ size = Vector2(72, 19)
[node name="Main" type="Node2D"]
[node name="Debugger" parent="." instance=ExtResource("1_lpvoh")]
[node name="Center" type="Node2D" parent="."]
position = Vector2(240, 180)

171
scripts/debugger.gd Normal file
View File

@@ -0,0 +1,171 @@
class_name Debugger
extends CanvasLayer
## Handles displaying debug info.
signal mode_changed(mode: Mode)
enum Mode { DISABLED, PERFORMANCE, FULL }
static var mode: Mode = Mode.FULL
static var _instance: Debugger
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!")
_instance = self
# 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
static func text(key: String, value: Variant, label_index: int = 1) -> void:
if not _instance or not _instance.show_debug():
return
_instance._text_to_draw[key] = {"value": value, "label_index": label_index}
static func add_event(key: String) -> void:
if not _instance:
return
_instance._events_to_draw[key] = {"frame": -9999, "args": []}
static func event_emitted(key: String, args: Array[Variant] = []) -> void:
if not _instance or not _instance.show_debug():
return
_instance._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)
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()

1
scripts/debugger.gd.uid Normal file
View File

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

View File

@@ -62,3 +62,6 @@ func _physics_process(delta: float) -> void:
# bounce off walls
if not is_on_floor() and collided:
velocity.x = -new_velocity.x * _wall_bounce_velocity_loss
Debugger.text("velocity", velocity)
Debugger.text("position", global_position)