IndieGame/client/Assets/Ether/Package/EtherNode/Editor/Scripts/EtherNodeView.cs

160 lines
5.0 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00

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<EtherNodeView> 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>();
inspectorViewer.UpdateSelection(this);
Button refreshBtn = new Button();
refreshBtn.name = "RefreshBtn";
refreshBtn.text = "刷新节点";
refreshBtn.clicked += () =>
{
this.title = node.nodeName;
};
inspectorViewer.Add(refreshBtn);
}
/// <summary>
/// 右键点击菜单
/// </summary>
public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
{
evt.menu.ClearItems();
EtherNodeMenuNameAttribute menuNameAttr = node.GetType().GetAttribute<EtherNodeMenuNameAttribute>();
if (menuNameAttr != null)
{
evt.menu.AppendAction($"删除{menuNameAttr.menuName}", (a) => { DeleteNode(); });
}
// 阻止事件冒泡
evt.StopPropagation();
}
private void DeleteNode()
{
EtherNodeGraphViewer graph = GetFirstAncestorOfType<EtherNodeGraphViewer>();
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;
// }
// }
//}
}
}