diff --git a/Editor/Scripts/Window/PlayableGraphMonitorWindow_Toolbar.cs b/Editor/Scripts/Window/PlayableGraphMonitorWindow_Toolbar.cs index 580e776..52e2a44 100644 --- a/Editor/Scripts/Window/PlayableGraphMonitorWindow_Toolbar.cs +++ b/Editor/Scripts/Window/PlayableGraphMonitorWindow_Toolbar.cs @@ -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; @@ -156,11 +157,7 @@ private void CreateToolbar() { text = "Select Output Node" }; -#if UNITY_2020_1_OR_NEWER - _selectOutputNodeMenu.RegisterCallback(OnClickSelectOutputNodeMenu); -#else - _selectOutputNodeMenu.RegisterCallback(OnClickSelectOutputNodeMenu); -#endif + _selectOutputNodeMenu.RegisterCallback(OnHoverSelectOutputNodeMenu); _toolbar.Add(_selectOutputNodeMenu); // Select root node @@ -168,11 +165,7 @@ private void CreateToolbar() { text = "Select Root Node" }; -#if UNITY_2020_1_OR_NEWER - _selectRootNodeMenu.RegisterCallback(OnClickSelectRootNodeMenu); -#else - _selectRootNodeMenu.RegisterCallback(OnClickSelectRootNodeMenu); -#endif + _selectRootNodeMenu.RegisterCallback(OnHoverSelectRootNodeMenu); _toolbar.Add(_selectRootNodeMenu); } @@ -250,48 +243,53 @@ 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(); _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()) @@ -299,35 +297,41 @@ private void OnClickSelectRootNodeMenu(PointerDownEvent evt) return; } - var rootPlayableCount = playableGraph.GetRootPlayableCount(); - var rootPlayableHandles = new HashSet(); - for (int i = 0; i < rootPlayableCount; i++) - { - var rootPlayableHandle = playableGraph.GetRootPlayable(i).GetHandle(); - rootPlayableHandles.Add(rootPlayableHandle); - } - var nodeList = new List(); _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(); + }); } } }