using Animancer.Editor; using System; using System.Collections; using System.Collections.Generic; using Unity.Plastic.Antlr3.Runtime.Tree; using Unity.VisualScripting.Antlr3.Runtime.Tree; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEngine; using UnityEngine.UIElements; namespace Ether { public class EtherNodeView : Node { public EtherNode node; public Port input; public Port output; public Action OnNodeSelected; InspectorViewer inspectorViewer; public EtherNodeView(EtherNode node) : base("Assets/Ether/Package/EtherNode/Editor/UI/NodeView.uxml") { this.node = node; this.title = node.nodeName; this.viewDataKey = node.guid; style.left = node.position.x; style.top = node.position.y; CreateInputPorts(); CreateOutputPorts(); inspectorViewer = this.Q(); inspectorViewer.UpdateSelection(this); Button refreshBtn = new Button(); refreshBtn.name = "RefreshBtn"; refreshBtn.text = "刷新节点"; refreshBtn.clicked += () => { this.title = node.nodeName; }; inspectorViewer.Add(refreshBtn); } /// /// 右键点击菜单 /// public override void BuildContextualMenu(ContextualMenuPopulateEvent evt) { evt.menu.ClearItems(); EtherNodeMenuNameAttribute menuNameAttr = node.GetType().GetAttribute(); if (menuNameAttr != null) { evt.menu.AppendAction($"删除{menuNameAttr.menuName}", (a) => { DeleteNode(); }); } // 阻止事件冒泡 evt.StopPropagation(); } private void DeleteNode() { EtherNodeGraphViewer graph = GetFirstAncestorOfType(); if (graph != null) { graph.RemoveElement(this); graph.Graph.DeleteNode(node); } } private void CreateInputPorts() { Type nodeType = node.GetType(); if (nodeType.IsSubclassOf(typeof(EtherMultiNode))) { input = InstantiatePort(Orientation.Vertical, Direction.Input, Port.Capacity.Multi, typeof(bool)); } else if (nodeType.IsSubclassOf(typeof(EtherSingleNode))) { input = InstantiatePort(Orientation.Vertical, Direction.Input, Port.Capacity.Single, typeof(bool)); } else { input = InstantiatePort(Orientation.Vertical, Direction.Input, Port.Capacity.Multi, typeof(bool)); } if (input != null) { input.portName = ""; input.style.flexDirection = FlexDirection.Column; inputContainer.Add(input); } } private void CreateOutputPorts() { Type nodeType = node.GetType(); if (nodeType.IsSubclassOf(typeof(EtherMultiNode))) { output = InstantiatePort(Orientation.Vertical, Direction.Output, Port.Capacity.Multi, typeof(bool)); } else if (nodeType.IsSubclassOf(typeof(EtherSingleNode))) { output = InstantiatePort(Orientation.Vertical, Direction.Output, Port.Capacity.Single, typeof(bool)); } else { output = InstantiatePort(Orientation.Vertical, Direction.Output, Port.Capacity.Multi, typeof(bool)); } if (output != null) { output.portName = ""; output.style.flexDirection = FlexDirection.ColumnReverse; outputContainer.Add(output); } } public override void OnSelected() { base.OnSelected(); OnNodeSelected?.Invoke(this); Selection.activeObject = node; } public override void SetPosition(Rect newPos) { Undo.RecordObject(node, "Node Tree (SetPosition)"); base.SetPosition(newPos); node.position.x = newPos.x; node.position.y = newPos.y; EditorUtility.SetDirty(node); } //public void SetNodeState() //{ // RemoveFromClassList("running"); // if (Application.isPlaying) // { // switch (node.state) // { // case EtherNode.State.Running: // if (node.started) // { // AddToClassList("running"); // } // break; // case EtherNode.State.Waiting: // break; // default: // break; // } // } //} } }