Compare commits
1 commit
main
...
feat/graph
| Author | SHA1 | Date | |
|---|---|---|---|
| 738e33342e |
6 changed files with 143 additions and 46 deletions
|
|
@ -6,21 +6,21 @@ using Godot.Collections;
|
||||||
[Tool]
|
[Tool]
|
||||||
public partial class BehaviorDebug : EditorPlugin
|
public partial class BehaviorDebug : EditorPlugin
|
||||||
{
|
{
|
||||||
private BtEditorDebugger _btEditorDebugger = new BtEditorDebugger();
|
private BtEditorDebugger _btEditorDebugger = new();
|
||||||
|
|
||||||
public override void _EnterTree() => AddDebuggerPlugin(_btEditorDebugger);
|
public override void _EnterTree() => AddDebuggerPlugin(_btEditorDebugger);
|
||||||
public override void _ExitTree() => RemoveDebuggerPlugin(_btEditorDebugger);
|
public override void _ExitTree() => RemoveDebuggerPlugin(_btEditorDebugger);
|
||||||
|
|
||||||
private partial class BtEditorDebugger : EditorDebuggerPlugin
|
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");
|
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 _HasCapture(string capture) => capture == "behavior_debug";
|
||||||
public override bool _Capture(string message, Array data, int sessionId)
|
public override bool _Capture(string message, Array data, int sessionId)
|
||||||
{
|
{
|
||||||
if (message == "behavior_debug:add_tree")
|
if (message == "behavior_debug:add_tree")
|
||||||
{
|
{
|
||||||
_behaviorTreeView1?.AddTreeData(data);
|
_behaviorDebugControl?.AddTreeData(data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -28,10 +28,10 @@ public partial class BehaviorDebug : EditorPlugin
|
||||||
|
|
||||||
public override void _SetupSession(int sessionId)
|
public override void _SetupSession(int sessionId)
|
||||||
{
|
{
|
||||||
_behaviorTreeView1 = DebuggerScene.Instantiate<BehaviorDebugControl>();
|
_behaviorDebugControl = DebuggerScene.Instantiate<BehaviorDebugControl>();
|
||||||
_behaviorTreeView1.Name = "Behavior Trees";
|
_behaviorDebugControl.Name = "Behavior Trees";
|
||||||
var session = GetSession(sessionId);
|
var session = GetSession(sessionId);
|
||||||
session.AddSessionTab(_behaviorTreeView1);
|
session.AddSessionTab(_behaviorDebugControl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,23 @@
|
||||||
#if TOOLS
|
#if TOOLS
|
||||||
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
|
||||||
using Godot.Collections;
|
using Godot.Collections;
|
||||||
|
|
||||||
namespace Dancer;
|
|
||||||
|
|
||||||
[Tool]
|
[Tool]
|
||||||
public sealed partial class BehaviorDebugControl : Control
|
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 ItemList _itemList = null!;
|
||||||
private int _selectedIndex;
|
private int _selectedIndex;
|
||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_tree = GetNode<Tree>("%Tree");
|
_graph = GetNode<GraphEdit>("%Graph");
|
||||||
_itemList = GetNode<ItemList>("%List");
|
_itemList = GetNode<ItemList>("%List");
|
||||||
|
|
||||||
_tree.Columns = 2;
|
|
||||||
_tree.SetColumnTitle(0, "Node");
|
|
||||||
_tree.SetColumnTitle(1, "State");
|
|
||||||
|
|
||||||
_itemList.ItemSelected += ItemListOnItemSelected;
|
_itemList.ItemSelected += ItemListOnItemSelected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,44 +49,69 @@ public sealed partial class BehaviorDebugControl : Control
|
||||||
|
|
||||||
private void SetTree(Dictionary data)
|
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);
|
Visit(data);
|
||||||
|
_graph.ArrangeNodes();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
void Visit(Dictionary node, TreeItem? parent = null)
|
BehaviorDebugGraphNode Visit(Dictionary btNode, BehaviorDebugGraphNode? parentGraphNode = null)
|
||||||
{
|
{
|
||||||
var item = _tree.CreateItem(parent);
|
var graphNode = BehaviorDebugGraphNodeScene.Instantiate<BehaviorDebugGraphNode>();
|
||||||
UpdateTreeItem(node, item);
|
var btNodeChildren = btNode["children"].As<Array<Dictionary>>();
|
||||||
foreach (var nodeChild in node["children"].As<Array<Dictionary>>()) Visit(nodeChild, item);
|
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)
|
private void UpdateTree(Dictionary data)
|
||||||
{
|
{
|
||||||
if (_tree.GetRoot() is not { } root)
|
if (_graph.GetChildCount() == 1)
|
||||||
{
|
{
|
||||||
SetTree(data);
|
SetTree(data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Visit(data, root);
|
var graphEditIndex = 0;
|
||||||
|
Visit(data);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
void Visit(Dictionary node, TreeItem item)
|
void Visit(Dictionary btNode)
|
||||||
{
|
{
|
||||||
UpdateTreeItem(node, item);
|
BehaviorDebugGraphNode graphNode;
|
||||||
var dataChildren = node["children"].AsGodotArray<Dictionary>();
|
while (true)
|
||||||
for (var i = 0; i < dataChildren.Count; i++)
|
|
||||||
{
|
{
|
||||||
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 rawName = btNode["name"].As<string>();
|
||||||
var rawType = node["type"].As<string>();
|
var rawType = btNode["type"].As<string>();
|
||||||
var displayName = (
|
var displayName = (
|
||||||
!string.IsNullOrWhiteSpace(rawName)
|
!string.IsNullOrWhiteSpace(rawName)
|
||||||
? rawName
|
? rawName
|
||||||
|
|
@ -103,19 +124,8 @@ public sealed partial class BehaviorDebugControl : Control
|
||||||
"Selector" => "?",
|
"Selector" => "?",
|
||||||
var other => other,
|
var other => other,
|
||||||
};
|
};
|
||||||
item.SetText(0, displayName);
|
graphNode.Title = displayName;
|
||||||
var state = node["state"].As<string>();
|
graphNode.State = btNode["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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
45
addons/BehaviorDebug/BehaviorDebugGraphNode.cs
Normal file
45
addons/BehaviorDebug/BehaviorDebugGraphNode.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1
addons/BehaviorDebug/BehaviorDebugGraphNode.cs.uid
Normal file
1
addons/BehaviorDebug/BehaviorDebugGraphNode.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
uid://dktd68jxq4awc
|
||||||
|
|
@ -16,8 +16,11 @@ script = ExtResource("1_6f7wn")
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
layout_mode = 2
|
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
|
unique_name_in_owner = true
|
||||||
layout_mode = 2
|
layout_mode = 2
|
||||||
columns = 2
|
grid_pattern = 1
|
||||||
column_titles_visible = true
|
snapping_enabled = false
|
||||||
|
type_names = {
|
||||||
|
0: ""
|
||||||
|
}
|
||||||
|
|
|
||||||
38
addons/BehaviorDebug/behavior_debug_graph_node.tscn
Normal file
38
addons/BehaviorDebug/behavior_debug_graph_node.tscn
Normal 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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue