tree based bt debugger

This commit is contained in:
Lilith Schier 2026-06-28 22:46:37 +02:00
parent c9967f09d9
commit b5d5a8f9f3
12 changed files with 271 additions and 19 deletions

View file

@ -0,0 +1,38 @@
#if TOOLS
using Dancer;
using Godot.Collections;
[Tool]
public partial class BehaviorDebug : EditorPlugin
{
private BtEditorDebugger _btEditorDebugger = new BtEditorDebugger();
public override void _EnterTree() => AddDebuggerPlugin(_btEditorDebugger);
public override void _ExitTree() => RemoveDebuggerPlugin(_btEditorDebugger);
private partial class BtEditorDebugger : EditorDebuggerPlugin
{
private BehaviorDebugControl? _behaviorTreeView1;
private static readonly PackedScene DebuggerScene = GD.Load<PackedScene>("res://addons/BehaviorDebug/behavior_debug_dock.tscn");
public override bool _HasCapture(string capture) => capture == "behavior_debug";
public override bool _Capture(string message, Array data, int sessionId)
{
if (message == "behavior_debug:add_tree")
{
_behaviorTreeView1?.AddTreeData(data);
return true;
}
return false;
}
public override void _SetupSession(int sessionId)
{
_behaviorTreeView1 = DebuggerScene.Instantiate<BehaviorDebugControl>();
_behaviorTreeView1.Name = "Behavior Trees";
var session = GetSession(sessionId);
session.AddSessionTab(_behaviorTreeView1);
}
}
}
#endif

View file

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

View file

@ -0,0 +1,122 @@
#if TOOLS
using System.Diagnostics;
using System.Linq;
using Godot.Collections;
namespace Dancer;
[Tool]
public sealed partial class BehaviorDebugControl : Control
{
private Tree _tree = null!;
private ItemList _itemList = null!;
private int _selectedIndex;
public override void _Ready()
{
_tree = GetNode<Tree>("%Tree");
_itemList = GetNode<ItemList>("%List");
_tree.Columns = 2;
_tree.SetColumnTitle(0, "Node");
_tree.SetColumnTitle(1, "State");
_itemList.ItemSelected += ItemListOnItemSelected;
}
private void ItemListOnItemSelected(long index)
{
_selectedIndex = (int)index;
SetTree(_trees[(int)index].tree);
}
private readonly System.Collections.Generic.List<(ulong instanceId, Dictionary tree)> _trees = [];
public void AddTreeData(Array raw)
{
(ulong instanceId, Dictionary tree) data = (raw[0].AsUInt64(), raw[1].AsGodotDictionary());
var idx = _trees.FindIndex(tuple => tuple.instanceId == data.instanceId);
if (idx == -1)
{
_trees.Add(data);
_itemList.AddItem($"{data.instanceId}");
return;
}
_trees[idx] = data;
if (idx == _selectedIndex)
{
UpdateTree(data.tree);
}
}
private void SetTree(Dictionary data)
{
_tree.GetRoot()?.Free();
Visit(data);
return;
void Visit(Dictionary node, TreeItem? parent = null)
{
var item = _tree.CreateItem(parent);
UpdateTreeItem(node, item);
foreach (var nodeChild in node["children"].As<Array<Dictionary>>()) Visit(nodeChild, item);
}
}
private void UpdateTree(Dictionary data)
{
if (_tree.GetRoot() is not { } root)
{
SetTree(data);
return;
}
Visit(data, root);
return;
void Visit(Dictionary node, TreeItem item)
{
UpdateTreeItem(node, item);
var dataChildren = node["children"].AsGodotArray<Dictionary>();
for (var i = 0; i < dataChildren.Count; i++)
{
Visit(dataChildren[i], item.GetChild(i));
}
}
}
private static void UpdateTreeItem(Dictionary node, TreeItem item)
{
var rawName = node["name"].As<string>();
var rawType = node["type"].As<string>();
var displayName = (
!string.IsNullOrWhiteSpace(rawName)
? rawName
: rawType
)
switch
{
"Sequence" => "→",
"MemoSequence" => "→*",
"Selector" => "?",
var other => other,
};
item.SetText(0, displayName);
var state = node["state"].As<string>();
var bgColor = state switch
{
"Idle" => Color.FromOkHsl(.653f, .3f, .2f),
"Running" => Color.FromOkHsl(.222f, .7f, .3f),
"Success" => Color.FromOkHsl(.375f, .3f, .2f),
"Failure" => Color.FromOkHsl(.055f, .3f, .2f),
_ => throw new UnreachableException(),
};
item.SetCustomBgColor(1, bgColor);
item.SetCustomColor(1, Colors.AntiqueWhite);
item.SetText(1, state);
}
}
#endif

View file

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

View file

@ -0,0 +1,23 @@
[gd_scene format=3 uid="uid://b8j1734h5i2ve"]
[ext_resource type="Script" uid="uid://btd3yxqnv5nhw" path="res://addons/BehaviorDebug/BehaviorDebugControl.cs" id="1_6f7wn"]
[node name="HSplitContainer" type="HSplitContainer" unique_id=381231242]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
split_offsets = PackedInt32Array(200)
split_offset = 200
script = ExtResource("1_6f7wn")
[node name="List" type="ItemList" parent="." unique_id=664607271]
unique_name_in_owner = true
layout_mode = 2
[node name="Tree" type="Tree" parent="." unique_id=1995434049]
unique_name_in_owner = true
layout_mode = 2
columns = 2
column_titles_visible = true

View file

@ -0,0 +1,7 @@
[plugin]
name="BehaviorDebug"
description=""
author="blossomfiend"
version="1.0"
script="BehaviorDebug.cs"