Skip to content

Commit

Permalink
fix: #12 Fix the issue of the refresh delay in the select node menu& …
Browse files Browse the repository at this point in the history
…Fix the issue of the select node menu items displaying in a random order
  • Loading branch information
SolarianZ committed Apr 6, 2023
1 parent 850002b commit 774e048
Showing 1 changed file with 63 additions and 59 deletions.
122 changes: 63 additions & 59 deletions Editor/Scripts/Window/PlayableGraphMonitorWindow_Toolbar.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GBG.PlayableGraphMonitor.Editor.Node;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Playables;
using UnityEditor.UIElements;
Expand Down Expand Up @@ -156,23 +157,15 @@ private void CreateToolbar()
{
text = "Select Output Node"
};
#if UNITY_2020_1_OR_NEWER
_selectOutputNodeMenu.RegisterCallback<ClickEvent>(OnClickSelectOutputNodeMenu);
#else
_selectOutputNodeMenu.RegisterCallback<PointerDownEvent>(OnClickSelectOutputNodeMenu);
#endif
_selectOutputNodeMenu.RegisterCallback<PointerEnterEvent>(OnHoverSelectOutputNodeMenu);
_toolbar.Add(_selectOutputNodeMenu);

// Select root node
_selectRootNodeMenu = new ToolbarMenu
{
text = "Select Root Node"
};
#if UNITY_2020_1_OR_NEWER
_selectRootNodeMenu.RegisterCallback<ClickEvent>(OnClickSelectRootNodeMenu);
#else
_selectRootNodeMenu.RegisterCallback<PointerDownEvent>(OnClickSelectRootNodeMenu);
#endif
_selectRootNodeMenu.RegisterCallback<PointerEnterEvent>(OnHoverSelectRootNodeMenu);
_toolbar.Add(_selectRootNodeMenu);
}

Expand Down Expand Up @@ -250,84 +243,95 @@ private void OnFrameAllButtonClicked()
_graphView.FrameAll();
}

#if UNITY_2020_1_OR_NEWER
private void OnClickSelectOutputNodeMenu(ClickEvent evt)
#else
private void OnClickSelectOutputNodeMenu(PointerDownEvent evt)
#endif
private void OnHoverSelectOutputNodeMenu(PointerEnterEvent evt)
{
var itemCount = _selectOutputNodeMenu.menu.MenuItems().Count;
for (int i = itemCount - 1; i >= 0; i--)
_selectOutputNodeMenu.menu.MenuItems().Clear();

var playableGraph = _graphPopupField.value;
if (!playableGraph.IsValid())
{
_selectOutputNodeMenu.menu.RemoveItemAt(i);
return;
}

var nodeList = new List<UNode>();
_graphView.nodes.ToList(nodeList);
foreach (var node in nodeList)
// _graphView.nodes.ToList() method returns nodes in random order,
// ensure that the menu items are ordered
var outputCount = playableGraph.GetOutputCount();
for (int i = 0; i < outputCount; i++)
{
if (node is PlayableOutputNode outputNode &&
outputNode.PlayableOutput.IsOutputValid())
var playableOutput = playableGraph.GetOutput(i);
var outputNode = nodeList.First(node =>
{
var nodeName = $"{outputNode.PlayableOutput.GetPlayableOutputType().Name}" +
$" ({outputNode.PlayableOutput.GetEditorName()})";
_selectOutputNodeMenu.menu.AppendAction(nodeName, _ =>
if (node is PlayableOutputNode oNode)
{
_graphView.ClearSelection();
_graphView.AddToSelection(outputNode);
_graphView.FrameSelection();
});
return oNode.PlayableOutput.GetHandle() == playableOutput.GetHandle();
}
return false;
}) as PlayableOutputNode;

if (!outputNode.PlayableOutput.IsOutputValid())
{
continue;
}

var nodeName = $"{outputNode.PlayableOutput.GetPlayableOutputType().Name}" +
$" ({outputNode.PlayableOutput.GetEditorName()})";
_selectOutputNodeMenu.menu.AppendAction(nodeName, _ =>
{
_graphView.ClearSelection();
_graphView.AddToSelection(outputNode);
_graphView.FrameSelection();
});
}
}

#if UNITY_2020_1_OR_NEWER
private void OnClickSelectRootNodeMenu(ClickEvent evt)
#else
private void OnClickSelectRootNodeMenu(PointerDownEvent evt)
#endif
private void OnHoverSelectRootNodeMenu(PointerEnterEvent evt)
{
var itemCount = _selectRootNodeMenu.menu.MenuItems().Count;
for (int i = itemCount - 1; i >= 0; i--)
{
_selectRootNodeMenu.menu.RemoveItemAt(i);
}
_selectRootNodeMenu.menu.MenuItems().Clear();

var playableGraph = _graphPopupField.value;
if (!playableGraph.IsValid())
{
return;
}

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)
// _graphView.nodes.ToList() method returns nodes in random order,
// ensure that the menu items are ordered
var rootPlayableCount = playableGraph.GetRootPlayableCount();
for (int i = 0; i < rootPlayableCount; i++)
{
if (node is PlayableNode playableNode &&
playableNode.Playable.IsValid() &&
rootPlayableHandles.Contains(playableNode.Playable.GetHandle()))
var playable = playableGraph.GetRootPlayable(i);
var playableNode = nodeList.First(node =>
{
var nodeName = $"{playableNode.Playable.GetPlayableType()?.Name ?? "?"}";
if (!string.IsNullOrEmpty(playableNode.ExtraLabel))
if (node is PlayableNode pNode)
{
nodeName = $"{nodeName} ({playableNode.ExtraLabel})";
return pNode.Playable.GetHandle() == playable.GetHandle();
}
_selectRootNodeMenu.menu.AppendAction(nodeName, _ =>
{
_graphView.ClearSelection();
_graphView.AddToSelection(playableNode);
_graphView.FrameSelection();
});
return false;
}) as PlayableNode;

if (!playableNode.Playable.IsValid())
{
continue;
}

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();
});
}
}
}
Expand Down

0 comments on commit 774e048

Please sign in to comment.