Skip to content

feat(visualizer): pausing the game now keeps active node highlighting #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}
}
Loading