Skip to content

Commit

Permalink
feat(visualizer): pausing the game now keeps active node highlighting
Browse files Browse the repository at this point in the history
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
  • Loading branch information
clevercrowgames committed Nov 9, 2024
1 parent a97f7c0 commit b031653
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private void OnGUI () {
if (!Application.isPlaying) {
ClearView();
}

GUILayout.Label($"Behavior Tree: {_name}", EditorStyles.boldLabel);
_printer?.Print(position.size);
}
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ public class BehaviorTreePrinter {
private readonly Rect _containerSize;

private Vector2 _scrollPosition;

public static StatusIcons StatusIcons { get; private set; }
public static GuiStyleCollection SharedStyles { get; private set; }


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);
Expand All @@ -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();
Expand All @@ -47,5 +47,9 @@ public void Print (Vector2 windowSize) {
public void Unbind () {
_root.RecursiveTaskUnbind();
}

public void UpdateFaders () {
_root.UpdateFaders();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public NodePrintController (VisualTask node) {

public void Print (bool taskIsActive) {
if (!(_node.Task is TaskRoot)) PaintVerticalTop();
_faders.Update(taskIsActive);

PaintBody();

Expand Down Expand Up @@ -142,5 +141,9 @@ private static Texture2D CreateTexture (int width, int height, Color color) {

return texture;
}

public void SyncFade (bool taskIsActive) {
_faders.Update(taskIsActive);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ public class VisualTask {

public ITask Task { get; }
public IReadOnlyList<VisualTask> 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; }

public VisualTask (ITask task, IGraphContainer parentContainer) {
Task = task;
BindTask();

var container = new GraphContainerVertical();

AddBox(container);
Expand All @@ -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);
}

Expand All @@ -46,7 +46,7 @@ private void BindTask () {

public void RecursiveTaskUnbind () {
Task.EditorUtils.EventActive.RemoveListener(UpdateTaskActiveStatus);

foreach (var child in _children) {
child.RecursiveTaskUnbind();
}
Expand Down Expand Up @@ -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();
}
}
}
}

0 comments on commit b031653

Please sign in to comment.