buggy mess of a graph based behavior tree debugger (not good, pls ignore)

This commit is contained in:
Lilith Schier 2026-06-29 00:23:09 +02:00
parent b5d5a8f9f3
commit 738e33342e
6 changed files with 143 additions and 46 deletions

View file

@ -1,27 +1,23 @@
#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 static readonly PackedScene BehaviorDebugGraphNodeScene =
GD.Load<PackedScene>("res://addons/BehaviorDebug/behavior_debug_graph_node.tscn");
private GraphEdit _graph = null!;
private ItemList _itemList = null!;
private int _selectedIndex;
public override void _Ready()
{
_tree = GetNode<Tree>("%Tree");
_graph = GetNode<GraphEdit>("%Graph");
_itemList = GetNode<ItemList>("%List");
_tree.Columns = 2;
_tree.SetColumnTitle(0, "Node");
_tree.SetColumnTitle(1, "State");
_itemList.ItemSelected += ItemListOnItemSelected;
}
@ -53,44 +49,69 @@ public sealed partial class BehaviorDebugControl : Control
private void SetTree(Dictionary data)
{
_tree.GetRoot()?.Free();
for (var i = 0; i < _graph.GetChildCount(); i++)
{
if (_graph.GetChildOrNull<BehaviorDebugGraphNode>(i) is { } node) node.QueueFree();
}
Visit(data);
_graph.ArrangeNodes();
return;
void Visit(Dictionary node, TreeItem? parent = null)
BehaviorDebugGraphNode Visit(Dictionary btNode, BehaviorDebugGraphNode? parentGraphNode = null)
{
var item = _tree.CreateItem(parent);
UpdateTreeItem(node, item);
foreach (var nodeChild in node["children"].As<Array<Dictionary>>()) Visit(nodeChild, item);
var graphNode = BehaviorDebugGraphNodeScene.Instantiate<BehaviorDebugGraphNode>();
var btNodeChildren = btNode["children"].As<Array<Dictionary>>();
graphNode.SetDescendantCount(btNodeChildren.Count);
_graph.AddChild(graphNode);
UpdateGraphNode(btNode, graphNode);
for (var index = 0; index < btNodeChildren.Count; index++)
{
var nodeChild = btNodeChildren[index];
var childGraphNode = Visit(nodeChild, graphNode);
_graph.ConnectNode(graphNode.Name, index, childGraphNode.Name, 0);
}
return graphNode;
}
}
private void UpdateTree(Dictionary data)
{
if (_tree.GetRoot() is not { } root)
if (_graph.GetChildCount() == 1)
{
SetTree(data);
return;
}
Visit(data, root);
var graphEditIndex = 0;
Visit(data);
return;
void Visit(Dictionary node, TreeItem item)
void Visit(Dictionary btNode)
{
UpdateTreeItem(node, item);
var dataChildren = node["children"].AsGodotArray<Dictionary>();
for (var i = 0; i < dataChildren.Count; i++)
BehaviorDebugGraphNode graphNode;
while (true)
{
Visit(dataChildren[i], item.GetChild(i));
if (graphEditIndex >= _graph.GetChildCount()) throw new UnreachableException();
var inspectedNode = _graph.GetChildOrNull<BehaviorDebugGraphNode>(graphEditIndex);
graphEditIndex += 1;
if (inspectedNode is not null)
{
graphNode = inspectedNode;
break;
}
}
UpdateGraphNode(btNode, graphNode);
var btNodeChildren = btNode["children"].AsGodotArray<Dictionary>();
foreach (var btNodeChild in btNodeChildren) Visit(btNodeChild);
}
}
private static void UpdateTreeItem(Dictionary node, TreeItem item)
private static void UpdateGraphNode(Dictionary btNode, BehaviorDebugGraphNode graphNode)
{
var rawName = node["name"].As<string>();
var rawType = node["type"].As<string>();
var rawName = btNode["name"].As<string>();
var rawType = btNode["type"].As<string>();
var displayName = (
!string.IsNullOrWhiteSpace(rawName)
? rawName
@ -103,19 +124,8 @@ public sealed partial class BehaviorDebugControl : Control
"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);
graphNode.Title = displayName;
graphNode.State = btNode["state"].As<string>();
}
}