Skip to content

Commit

Permalink
feat: #12 Add a drop-down menu to the toolbar for quick access to out…
Browse files Browse the repository at this point in the history
…put nodes and root nodes
  • Loading branch information
SolarianZ committed Apr 4, 2023
1 parent 4f00c13 commit 463c78e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Editor/Scripts/Node/PlayableNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PlayableNode : GraphViewNode

public PlayableNode ParentNode { get; set; }

private string _extraLabel;
public string ExtraLabel { get; private set; }


public void Update(PlayableGraphViewUpdateContext updateContext, Playable playable)
Expand All @@ -34,14 +34,14 @@ public void Update(PlayableGraphViewUpdateContext updateContext, Playable playab
}

var extraLabel = GetExtraNodeLabel(updateContext.NodeExtraLabelTable);
if (playableChanged || _extraLabel != extraLabel)
if (playableChanged || ExtraLabel != extraLabel)
{
_extraLabel = extraLabel;
ExtraLabel = extraLabel;

var playableTypeName = Playable.GetPlayableType().Name;
var nodeTitle = string.IsNullOrEmpty(_extraLabel)
var nodeTitle = string.IsNullOrEmpty(ExtraLabel)
? playableTypeName
: $"[{_extraLabel}]\n{playableTypeName}";
: $"[{ExtraLabel}]\n{playableTypeName}";

// Expensive operation
title = nodeTitle;
Expand Down Expand Up @@ -105,12 +105,12 @@ public override string ToString()
{
if (Playable.IsValid())
{
if (string.IsNullOrEmpty(_extraLabel))
if (string.IsNullOrEmpty(ExtraLabel))
{
return Playable.GetPlayableType().Name;
}

return $"[{_extraLabel}] {Playable.GetPlayableType().Name}";
return $"[{ExtraLabel}] {Playable.GetPlayableType().Name}";
}

return GetType().Name;
Expand Down Expand Up @@ -139,9 +139,9 @@ protected override void AppendNodeDescription(StringBuilder descBuilder)
}

// Extra label
if (!string.IsNullOrEmpty(_extraLabel))
if (!string.IsNullOrEmpty(ExtraLabel))
{
descBuilder.Append("ExtraLabel: ").AppendLine(_extraLabel)
descBuilder.Append("ExtraLabel: ").AppendLine(ExtraLabel)
.AppendLine(LINE);
}

Expand Down
90 changes: 90 additions & 0 deletions Editor/Scripts/Window/PlayableGraphMonitorWindow_Toolbar.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using GBG.PlayableGraphMonitor.Editor.Node;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Playables;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.UIElements;
using UNode = UnityEditor.Experimental.GraphView.Node;


namespace GBG.PlayableGraphMonitor.Editor
Expand Down Expand Up @@ -53,6 +57,9 @@ public partial class PlayableGraphMonitorWindow
private TextElement _refreshRateLabel;
private ToolbarButton _manualUpdateViewButton;

// Node selection
private ToolbarMenu _selectOutputNodeMenu;
private ToolbarMenu _selectRootNodeMenu;

// Common data
private static Color NotableTextColor => Color.yellow;
Expand Down Expand Up @@ -142,6 +149,23 @@ private void CreateToolbar()
};
frameAllButton.Q<TextElement>(className: "unity-text-element").style.color = NormalTextColor;
_toolbar.Add(frameAllButton);

// Select output node
_toolbar.Add(new ToolbarSpacer());
_selectOutputNodeMenu = new ToolbarMenu
{
text = "Select Output Node"
};
_selectOutputNodeMenu.RegisterCallback<PointerDownEvent>(OnClickSelectOutputNodeMenu);
_toolbar.Add(_selectOutputNodeMenu);

// Select root node
_selectRootNodeMenu = new ToolbarMenu
{
text = "Select Root Node"
};
_selectRootNodeMenu.RegisterCallback<PointerDownEvent>(OnClickSelectRootNodeMenu);
_toolbar.Add(_selectRootNodeMenu);
}

private string GraphPopupFieldFormatter(PlayableGraph graph)
Expand Down Expand Up @@ -217,5 +241,71 @@ private void OnFrameAllButtonClicked()
{
_graphView.FrameAll();
}

private void OnClickSelectOutputNodeMenu(PointerDownEvent evt)
{
var itemCount = _selectOutputNodeMenu.menu.MenuItems().Count;
for (int i = itemCount - 1; i >= 0; i--)
{
_selectOutputNodeMenu.menu.RemoveItemAt(i);
}

var nodeList = new List<UNode>();
_graphView.nodes.ToList(nodeList);
foreach (var node in nodeList)
{
if (node is PlayableOutputNode outputNode)
{
var nodeName = $"{outputNode.PlayableOutput.GetPlayableOutputType().Name}" +
$" ({outputNode.PlayableOutput.GetEditorName()})";
_selectOutputNodeMenu.menu.AppendAction(nodeName, _ =>
{
_graphView.ClearSelection();
_graphView.AddToSelection(outputNode);
_graphView.FrameSelection();
});
}
}
}

private void OnClickSelectRootNodeMenu(PointerDownEvent evt)
{
var itemCount = _selectRootNodeMenu.menu.MenuItems().Count;
for (int i = itemCount - 1; i >= 0; i--)
{
_selectRootNodeMenu.menu.RemoveItemAt(i);
}

var playableGraph = _graphPopupField.value;
var rootPlayableCount = playableGraph.GetRootPlayableCount();
var rootPlayableHandles = new HashSet<PlayableHandle>();
for (int i = 0; i < rootPlayableCount; i++)
{
var rootPlayableHandle = playableGraph.GetRootPlayable(i).GetHandle();
rootPlayableHandles.Add(rootPlayableHandle);
}

var nodeList = new List<UNode>();
_graphView.nodes.ToList(nodeList);
foreach (var node in nodeList)
{
if (node is PlayableNode playableNode &&
rootPlayableHandles.Contains(playableNode.Playable.GetHandle()))
{
var nodeName = $"{playableNode.Playable.GetPlayableType()?.Name ?? "?"}";
if (!string.IsNullOrEmpty(playableNode.ExtraLabel))
{
nodeName = $"{nodeName} ({playableNode.ExtraLabel})";
}

_selectRootNodeMenu.menu.AppendAction(nodeName, _ =>
{
_graphView.ClearSelection();
_graphView.AddToSelection(playableNode);
_graphView.FrameSelection();
});
}
}
}
}
}

0 comments on commit 463c78e

Please sign in to comment.