Compare commits

...
Sign in to create a new pull request.

1 commit

6 changed files with 143 additions and 46 deletions

View file

@ -6,21 +6,21 @@ using Godot.Collections;
[Tool]
public partial class BehaviorDebug : EditorPlugin
{
private BtEditorDebugger _btEditorDebugger = new BtEditorDebugger();
private BtEditorDebugger _btEditorDebugger = new();
public override void _EnterTree() => AddDebuggerPlugin(_btEditorDebugger);
public override void _ExitTree() => RemoveDebuggerPlugin(_btEditorDebugger);
private partial class BtEditorDebugger : EditorDebuggerPlugin
{
private BehaviorDebugControl? _behaviorTreeView1;
private BehaviorDebugControl? _behaviorDebugControl;
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);
_behaviorDebugControl?.AddTreeData(data);
return true;
}
return false;
@ -28,10 +28,10 @@ public partial class BehaviorDebug : EditorPlugin
public override void _SetupSession(int sessionId)
{
_behaviorTreeView1 = DebuggerScene.Instantiate<BehaviorDebugControl>();
_behaviorTreeView1.Name = "Behavior Trees";
_behaviorDebugControl = DebuggerScene.Instantiate<BehaviorDebugControl>();
_behaviorDebugControl.Name = "Behavior Trees";
var session = GetSession(sessionId);
session.AddSessionTab(_behaviorTreeView1);
session.AddSessionTab(_behaviorDebugControl);
}
}
}

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>();
}
}

View file

@ -0,0 +1,45 @@
using System.Diagnostics;
[Tool]
public sealed partial class BehaviorDebugGraphNode : GraphNode
{
public Label StateLabel { get; set; } = null!;
public override void _Ready()
{
StateLabel = GetNode<Label>("%StateLabel");
}
public string State
{
get;
set
{
field = value;
StateLabel.Text = value;
var themeStylebox = GetThemeStylebox("panel")!.Duplicate() as StyleBoxFlat;
themeStylebox?.BgColor = GetStateColor(value);
AddThemeStyleboxOverride("panel", themeStylebox);
}
} = "Idle";
private Color GetStateColor(string state) =>
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(),
};
public void SetDescendantCount(int count)
{
for (var i = 0; i < count; i++)
{
var label = new Label { Text = (i + 1).ToString() };
AddChild(label);
SetSlot(i + 1, false, 0, Colors.Transparent, true, 0, Colors.FloralWhite);
}
}
}

View file

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

View file

@ -16,8 +16,11 @@ script = ExtResource("1_6f7wn")
unique_name_in_owner = true
layout_mode = 2
[node name="Tree" type="Tree" parent="." unique_id=1995434049]
[node name="Graph" type="GraphEdit" parent="." unique_id=1268385235]
unique_name_in_owner = true
layout_mode = 2
columns = 2
column_titles_visible = true
grid_pattern = 1
snapping_enabled = false
type_names = {
0: ""
}

View file

@ -0,0 +1,38 @@
[gd_scene format=3 uid="uid://dhsml4v08ufm2"]
[ext_resource type="Script" uid="uid://dktd68jxq4awc" path="res://addons/BehaviorDebug/BehaviorDebugGraphNode.cs" id="1_d8tp1"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_d8tp1"]
bg_color = Color(0.12495678, 0.1907599, 0.22987583, 1)
corner_detail = 11
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_2xpl6"]
content_margin_left = 8.0
content_margin_top = 2.0
content_margin_right = 8.0
content_margin_bottom = 2.0
bg_color = Color(0.13319641, 0.13319641, 0.13319638, 1)
[node name="BehaviorDebugGraphNode" type="GraphNode" unique_id=2051222350]
offset_left = -1.0
offset_right = 184.0
offset_bottom = 55.0
theme_override_styles/panel = SubResource("StyleBoxFlat_d8tp1")
theme_override_styles/titlebar = SubResource("StyleBoxFlat_2xpl6")
title = "Node name is missing"
slot/0/left_enabled = true
slot/0/left_type = 0
slot/0/left_color = Color(1, 1, 1, 1)
slot/0/left_icon = null
slot/0/right_enabled = false
slot/0/right_type = 0
slot/0/right_color = Color(1, 1, 1, 1)
slot/0/right_icon = null
slot/0/draw_stylebox = true
script = ExtResource("1_d8tp1")
[node name="StateLabel" type="Label" parent="." unique_id=1027287615]
unique_name_in_owner = true
layout_mode = 2
text = "Idle"
horizontal_alignment = 1