From 738e33342e614d8b756c9de56418d2ede75c2308 Mon Sep 17 00:00:00 2001 From: Lilith Schier Date: Mon, 29 Jun 2026 00:23:09 +0200 Subject: [PATCH 01/60] buggy mess of a graph based behavior tree debugger (not good, pls ignore) --- addons/BehaviorDebug/BehaviorDebug.cs | 12 +-- addons/BehaviorDebug/BehaviorDebugControl.cs | 84 +++++++++++-------- .../BehaviorDebug/BehaviorDebugGraphNode.cs | 45 ++++++++++ .../BehaviorDebugGraphNode.cs.uid | 1 + addons/BehaviorDebug/behavior_debug_dock.tscn | 9 +- .../behavior_debug_graph_node.tscn | 38 +++++++++ 6 files changed, 143 insertions(+), 46 deletions(-) create mode 100644 addons/BehaviorDebug/BehaviorDebugGraphNode.cs create mode 100644 addons/BehaviorDebug/BehaviorDebugGraphNode.cs.uid create mode 100644 addons/BehaviorDebug/behavior_debug_graph_node.tscn diff --git a/addons/BehaviorDebug/BehaviorDebug.cs b/addons/BehaviorDebug/BehaviorDebug.cs index a259bec..5a402c0 100644 --- a/addons/BehaviorDebug/BehaviorDebug.cs +++ b/addons/BehaviorDebug/BehaviorDebug.cs @@ -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("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(); - _behaviorTreeView1.Name = "Behavior Trees"; + _behaviorDebugControl = DebuggerScene.Instantiate(); + _behaviorDebugControl.Name = "Behavior Trees"; var session = GetSession(sessionId); - session.AddSessionTab(_behaviorTreeView1); + session.AddSessionTab(_behaviorDebugControl); } } } diff --git a/addons/BehaviorDebug/BehaviorDebugControl.cs b/addons/BehaviorDebug/BehaviorDebugControl.cs index 04389a7..2bb0ace 100644 --- a/addons/BehaviorDebug/BehaviorDebugControl.cs +++ b/addons/BehaviorDebug/BehaviorDebugControl.cs @@ -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("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"); + _graph = GetNode("%Graph"); _itemList = GetNode("%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(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>()) Visit(nodeChild, item); + var graphNode = BehaviorDebugGraphNodeScene.Instantiate(); + var btNodeChildren = btNode["children"].As>(); + 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(); - 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(graphEditIndex); + graphEditIndex += 1; + if (inspectedNode is not null) + { + graphNode = inspectedNode; + break; + } } + + UpdateGraphNode(btNode, graphNode); + var btNodeChildren = btNode["children"].AsGodotArray(); + 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(); - var rawType = node["type"].As(); + var rawName = btNode["name"].As(); + var rawType = btNode["type"].As(); 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(); - 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(); } } diff --git a/addons/BehaviorDebug/BehaviorDebugGraphNode.cs b/addons/BehaviorDebug/BehaviorDebugGraphNode.cs new file mode 100644 index 0000000..306afc5 --- /dev/null +++ b/addons/BehaviorDebug/BehaviorDebugGraphNode.cs @@ -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