diff options
| author | yum <yum.food.vr@gmail.com> | 2026-01-02 03:21:42 -0800 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2026-01-02 03:21:42 -0800 |
| commit | babfcc18ceb3a30eecdf4d00a4f42ff03ca10b5b (patch) | |
| tree | a1ae5451f4e1cc99bc51f06af970d90b2c00f37b | |
| parent | 22273213986052c9464a852c7240b106bc22bebb (diff) | |
Fold: simplify code
| -rw-r--r-- | Scripts/Fold/Editor/FoldGraphView.cs | 9 | ||||
| -rw-r--r-- | Scripts/Fold/Editor/FoldWindow.cs | 32 | ||||
| -rw-r--r-- | Scripts/Fold/Editor/KeyframeNodeView.cs | 110 |
3 files changed, 52 insertions, 99 deletions
diff --git a/Scripts/Fold/Editor/FoldGraphView.cs b/Scripts/Fold/Editor/FoldGraphView.cs index 2a94b9f..f77e97b 100644 --- a/Scripts/Fold/Editor/FoldGraphView.cs +++ b/Scripts/Fold/Editor/FoldGraphView.cs @@ -3,6 +3,7 @@ using UnityEditor; using GraphProcessor; using System; using System.Collections.Generic; +using System.Linq; public class FoldGraphView : BaseGraphView { @@ -10,12 +11,8 @@ public class FoldGraphView : BaseGraphView public override IEnumerable<(string path, Type type)> FilterCreateNodeMenuEntries() { - // Only return our subset of nodes - foreach (var nodeMenuItem in NodeProvider.GetNodeMenuEntries(graph)) - { - if (typeof(BaseFoldNode).IsAssignableFrom(nodeMenuItem.type)) - yield return nodeMenuItem; - } + return NodeProvider.GetNodeMenuEntries(graph) + .Where(entry => typeof(BaseFoldNode).IsAssignableFrom(entry.type)); } } diff --git a/Scripts/Fold/Editor/FoldWindow.cs b/Scripts/Fold/Editor/FoldWindow.cs index 69d7066..270bc2a 100644 --- a/Scripts/Fold/Editor/FoldWindow.cs +++ b/Scripts/Fold/Editor/FoldWindow.cs @@ -4,41 +4,29 @@ using GraphProcessor; public class FoldWindow : BaseGraphWindow { - const string DefaultGraphPath = "Assets/FoldGraph.asset"; + const string GraphPath = "Assets/FoldGraph.asset"; [MenuItem("Tools/yum_food/Fold")] - public static BaseGraphWindow Open() + public static void Open() { - var graphWindow = GetWindow<FoldWindow>(); - - var graph = AssetDatabase.LoadAssetAtPath<FoldGraph>(DefaultGraphPath); + var graph = AssetDatabase.LoadAssetAtPath<FoldGraph>(GraphPath); if (graph == null) { graph = ScriptableObject.CreateInstance<FoldGraph>(); - AssetDatabase.CreateAsset(graph, DefaultGraphPath); + AssetDatabase.CreateAsset(graph, GraphPath); AssetDatabase.SaveAssets(); } - graphWindow.InitializeGraph(graph); - - graphWindow.Show(); - return graphWindow; + var w = GetWindow<FoldWindow>(); + w.InitializeGraph(graph); + w.Show(); } - protected override void OnDestroy() - { - graphView?.Dispose(); - } + protected override void OnDestroy() => graphView?.Dispose(); protected override void InitializeWindow(BaseGraph graph) { titleContent = new GUIContent("Fold"); - - // Create the graph view with our custom view - if (graphView == null) - graphView = new FoldGraphView(this); - - rootView.Add(graphView); + if (graphView == null) rootView.Add(graphView = new FoldGraphView(this)); } -} - +}
\ No newline at end of file diff --git a/Scripts/Fold/Editor/KeyframeNodeView.cs b/Scripts/Fold/Editor/KeyframeNodeView.cs index c35b215..f2530ec 100644 --- a/Scripts/Fold/Editor/KeyframeNodeView.cs +++ b/Scripts/Fold/Editor/KeyframeNodeView.cs @@ -6,88 +6,56 @@ using System.Linq; [NodeCustomEditor(typeof(KeyframeNode))] public class KeyframeNodeView : BaseNodeView { - Button generateButton; - const string ERROR_MSG_NO_GAMEOBJECT = "GameObject input has wrong type"; - const string ERROR_MSG_NO_RENDERER = "GameObject must have a MeshRenderer component"; + Button generateButton; + const string ERROR_TYPE = "GameObject input has wrong type"; + const string ERROR_REND = "GameObject must have a MeshRenderer component"; - public override void Enable() - { - var node = nodeTarget as KeyframeNode; - - generateButton = new Button(OnGenerateClick) + public override void Enable() { - text = "Generate Keyframe" - }; - controlsContainer.Add(generateButton); - - // Update validation on connectivity changes. - onPortConnected += _ => Validate(); - onPortDisconnected += _ => Validate(); - - // Check periodically in case the value in the GameObjectNode changes. - // Best effort. - schedule.Execute(Validate).Every(200); + generateButton = new Button(OnGenerateClick) { text = "Generate Keyframe" }; + controlsContainer.Add(generateButton); - Validate(); - } + onPortConnected += _ => Validate(); + onPortDisconnected += _ => Validate(); + schedule.Execute(Validate).Every(200); - void OnGenerateClick() - { - var go = GetConnectedGameObject(); - // The button should be disabled if invalid, but safety check: - if (go == null || go.GetComponent<MeshRenderer>() == null) return; - - var foldNode = GetConnectedFoldNode(); - Debug.Log($"Generating Keyframe for '{go.name}' using Fold Data: {(foldNode != null ? foldNode.name : "None")}"); - // TODO - } - - GameObject GetConnectedGameObject() - { - var portView = GetFirstPortViewFromFieldName(nameof(KeyframeNode.targetObject)); - if (portView == null || portView.GetEdges().Count == 0) return null; + Validate(); + } - var edge = portView.GetEdges().First(); - var outputNode = (edge.output.node as BaseNodeView)?.nodeTarget; + void OnGenerateClick() + { + var go = GetConnectedGameObject(); + if (go?.GetComponent<MeshRenderer>() == null) return; - if (outputNode is GameObjectNode goNode) { - return goNode.output; + var foldNode = GetConnectedFoldNode(); + Debug.Log($"Generating Keyframe for '{go.name}' using Fold Data: {(foldNode?.name ?? "None")}"); } - return null; - } - BaseFoldNode GetConnectedFoldNode() - { - var portView = GetFirstPortViewFromFieldName(nameof(KeyframeNode.foldData)); - if (portView == null || portView.GetEdges().Count == 0) return null; + T GetConnectedNode<T>(string fieldName) where T : class + { + var edge = GetFirstPortViewFromFieldName(fieldName)?.GetEdges().FirstOrDefault(); + return (edge?.output.node as BaseNodeView)?.nodeTarget as T; + } - var edge = portView.GetEdges().First(); - return (edge.output.node as BaseNodeView)?.nodeTarget as BaseFoldNode; - } + GameObject GetConnectedGameObject() => GetConnectedNode<GameObjectNode>(nameof(KeyframeNode.targetObject))?.output; - void Validate() - { - var go = GetConnectedGameObject(); - bool hasTargetConnection = GetFirstPortViewFromFieldName(nameof(KeyframeNode.targetObject))?.GetEdges().Count > 0; - bool hasFoldConnection = GetFirstPortViewFromFieldName(nameof(KeyframeNode.foldData))?.GetEdges().Count > 0; + BaseFoldNode GetConnectedFoldNode() => GetConnectedNode<BaseFoldNode>(nameof(KeyframeNode.foldData)); - // Clear previous error. - RemoveMessageView(ERROR_MSG_NO_RENDERER); - RemoveMessageView(ERROR_MSG_NO_GAMEOBJECT); + void Validate() + { + var go = GetConnectedGameObject(); + bool hasGoConn = GetFirstPortViewFromFieldName(nameof(KeyframeNode.targetObject))?.GetEdges().Any() ?? false; + bool hasFoldConn = GetFirstPortViewFromFieldName(nameof(KeyframeNode.foldData))?.GetEdges().Any() ?? false; - bool isValid = false; - if (hasTargetConnection) { - if (go == null) { - // Has connection but it's not a GameObject. - AddMessageView(ERROR_MSG_NO_GAMEOBJECT, NodeMessageType.Error); - } else if (go.GetComponent<MeshRenderer>() == null) { - AddMessageView(ERROR_MSG_NO_RENDERER, NodeMessageType.Error); - } else { - isValid = true; - } - } + RemoveMessageView(ERROR_TYPE); + RemoveMessageView(ERROR_REND); - generateButton.SetEnabled(isValid && hasFoldConnection); - } -} + if (hasGoConn) + { + if (go == null) AddMessageView(ERROR_TYPE, NodeMessageType.Error); + else if (go.GetComponent<MeshRenderer>() == null) AddMessageView(ERROR_REND, NodeMessageType.Error); + } + generateButton.SetEnabled(hasGoConn && hasFoldConn && go?.GetComponent<MeshRenderer>() != null); + } +}
\ No newline at end of file |
