From b0316531c5952de39e28f10c778b6df129eeaffe Mon Sep 17 00:00:00 2001 From: Ash Blue Date: Fri, 8 Nov 2024 19:32:56 -0800 Subject: [PATCH] feat(visualizer): pausing the game now keeps active node highlighting The game used to clear all activate highlighting on pause. Making it hard to figure out what the BT was up to before pausing. fix #94 --- .../Editor/BehaviorTree/BehaviorTreeWindow.cs | 16 +++++++++++++- .../Printer/BehaviorTreePrinter.cs | 20 ++++++++++------- .../Printer/NodePrintController.cs | 5 ++++- .../Editor/BehaviorTree/Printer/VisualTask.cs | 22 +++++++++++++------ 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/BehaviorTreeWindow.cs b/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/BehaviorTreeWindow.cs index 08139c43..6a03775e 100644 --- a/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/BehaviorTreeWindow.cs +++ b/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/BehaviorTreeWindow.cs @@ -22,7 +22,7 @@ private void OnGUI () { if (!Application.isPlaying) { ClearView(); } - + GUILayout.Label($"Behavior Tree: {_name}", EditorStyles.boldLabel); _printer?.Print(position.size); } @@ -37,5 +37,19 @@ private void Update () { Repaint(); } } + + void OnEnable() { + EditorApplication.update += OnEditorUpdate; + } + + void OnDisable() { + EditorApplication.update -= OnEditorUpdate; + } + + private void OnEditorUpdate() { + // Update faders separately so the current state is maintained when the game is paused + if (!EditorApplication.isPaused) + _printer?.UpdateFaders(); + } } } diff --git a/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/BehaviorTreePrinter.cs b/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/BehaviorTreePrinter.cs index 39e528d7..ef1fb4f9 100644 --- a/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/BehaviorTreePrinter.cs +++ b/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/BehaviorTreePrinter.cs @@ -8,7 +8,7 @@ public class BehaviorTreePrinter { private readonly Rect _containerSize; private Vector2 _scrollPosition; - + public static StatusIcons StatusIcons { get; private set; } public static GuiStyleCollection SharedStyles { get; private set; } @@ -16,14 +16,14 @@ public class BehaviorTreePrinter { public BehaviorTreePrinter (IBehaviorTree tree, Vector2 windowSize) { StatusIcons = new StatusIcons(); SharedStyles = new GuiStyleCollection(); - + var container = new GraphContainerVertical(); container.SetGlobalPosition(SCROLL_PADDING, SCROLL_PADDING); _root = new VisualTask(tree.Root, container); container.CenterAlignChildren(); - - _containerSize = new Rect(0, 0, - container.Width + SCROLL_PADDING * 2, + + _containerSize = new Rect(0, 0, + container.Width + SCROLL_PADDING * 2, container.Height + SCROLL_PADDING * 2); CenterScrollView(windowSize, container); @@ -37,8 +37,8 @@ private void CenterScrollView (Vector2 windowSize, GraphContainerVertical contai public void Print (Vector2 windowSize) { _scrollPosition = GUI.BeginScrollView( - new Rect(0, 0, windowSize.x, windowSize.y), - _scrollPosition, + new Rect(0, 0, windowSize.x, windowSize.y), + _scrollPosition, _containerSize); _root.Print(); GUI.EndScrollView(); @@ -47,5 +47,9 @@ public void Print (Vector2 windowSize) { public void Unbind () { _root.RecursiveTaskUnbind(); } + + public void UpdateFaders () { + _root.UpdateFaders(); + } } -} \ No newline at end of file +} diff --git a/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/NodePrintController.cs b/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/NodePrintController.cs index 143a3782..cf3c5a44 100644 --- a/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/NodePrintController.cs +++ b/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/NodePrintController.cs @@ -28,7 +28,6 @@ public NodePrintController (VisualTask node) { public void Print (bool taskIsActive) { if (!(_node.Task is TaskRoot)) PaintVerticalTop(); - _faders.Update(taskIsActive); PaintBody(); @@ -142,5 +141,9 @@ private static Texture2D CreateTexture (int width, int height, Color color) { return texture; } + + public void SyncFade (bool taskIsActive) { + _faders.Update(taskIsActive); + } } } diff --git a/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/VisualTask.cs b/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/VisualTask.cs index f0f46e26..d839410d 100644 --- a/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/VisualTask.cs +++ b/Assets/com.fluid.behavior-tree/Editor/BehaviorTree/Printer/VisualTask.cs @@ -9,10 +9,10 @@ public class VisualTask { public ITask Task { get; } public IReadOnlyList Children => _children; - + public float Width { get; } = 70; public float Height { get; } = 50; - + public IGraphBox Box { get; private set; } public IGraphBox Divider { get; private set; } public float DividerLeftOffset { get; private set; } @@ -20,7 +20,7 @@ public class VisualTask { public VisualTask (ITask task, IGraphContainer parentContainer) { Task = task; BindTask(); - + var container = new GraphContainerVertical(); AddBox(container); @@ -30,13 +30,13 @@ public VisualTask (ITask task, IGraphContainer parentContainer) { foreach (var child in task.Children) { _children.Add(new VisualTask(child, childContainer)); } - + AddDivider(container, childContainer); container.AddBox(childContainer); } parentContainer.AddBox(container); - + _printer = new NodePrintController(this); } @@ -46,7 +46,7 @@ private void BindTask () { public void RecursiveTaskUnbind () { Task.EditorUtils.EventActive.RemoveListener(UpdateTaskActiveStatus); - + foreach (var child in _children) { child.RecursiveTaskUnbind(); } @@ -79,11 +79,19 @@ private void AddBox (IGraphContainer parent) { public void Print () { _printer.Print(_taskActive); - _taskActive = false; foreach (var child in _children) { child.Print(); } } + + public void UpdateFaders () { + _printer.SyncFade(_taskActive); + _taskActive = false; + + foreach (var child in _children) { + child.UpdateFaders(); + } + } } }