dancer-godot/addons/BehaviorDebug/BehaviorDebugControl.cs

139 lines
3.5 KiB
C#

#if TOOLS
using System.Diagnostics;
using Godot.Collections;
[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)
{
(string name, ulong instanceId, Dictionary tree) data = (raw[0].AsString(), raw[1].AsUInt64(),
raw[2].AsGodotDictionary());
var idx = _trees.FindIndex(tuple => tuple.instanceId == data.instanceId);
if (idx == -1)
{
_trees.Add((data.instanceId, data.tree));
_itemList.AddItem($"{data.name} ({data.instanceId})");
return;
}
_trees[idx] = (data.instanceId, data.tree);
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,
};
var state = node["state"].As<string>();
var isStale = node["is_stale"].AsBool();
var theme = EditorInterface.Singleton.GetEditorTheme();
var textColor = theme.GetColor("font_color", "Tree");
var textLightness = textColor.OkHslL;
var bgLightness = textLightness > .5f ? .3f : .8f;
var bgAlpha = isStale ? .06f : .4f;
var bgColor = state switch
{
"Idle" => Colors.Transparent,
"Running" => Color.FromOkHsl(.653f, .7f, bgLightness) with { A = bgAlpha + .1f },
"Success" => Color.FromOkHsl(.375f, .7f, bgLightness) with { A = bgAlpha },
"Failure" => Color.FromOkHsl(.055f, .7f, bgLightness) with { A = bgAlpha },
_ => throw new UnreachableException(),
};
var color = textColor with { A = isStale ? .35f : 1f };
item.SetText(0, displayName);
item.SetText(1, isStale ? $"Stale ({state})" : state);
for (var col = 0; col < 2; col++)
{
item.SetCustomColor(col, color);
item.SetCustomBgColor(col, bgColor);
}
}
public void Clear()
{
_itemList.Clear();
_trees.Clear();
_tree.GetRoot()?.Free();
}
}
#endif