com.alicizax.unity.animatio.../Editor/Graph/NodeView.cs
陈思海 11e5744b46 init
2025-02-07 16:05:13 +08:00

124 lines
4.7 KiB
C#

using System;
using System.Linq;
using AlicizaX.AnimationFlow.Runtime;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
using static UnityEditor.Experimental.GraphView.Port;
namespace AlicizaX.AnimationFlow.Editor {
public class NodeView : Node {
public readonly ActionNode actionNode;
public Port portIn;
public Port portOut;
private readonly GraphWindow graphWindow;
private ProgressBar progressPlaying;
private Image imageComplete;
private Image imageWarning;
private Label sub_title;
public NodeView(ActionNode node, GraphWindow graphWindow) {
this.actionNode = node;
this.graphWindow = graphWindow;
Draw();
}
private void Draw() {
Type type = actionNode.GetType();
NameAttribute nameAttribute = type.GetCustomAttributes(typeof(NameAttribute), true).FirstOrDefault() as NameAttribute;
title = nameAttribute != null && !string.IsNullOrEmpty(nameAttribute.name) ? nameAttribute.name : type.Name;
if (actionNode.HasInputPort()) {
portIn = InstantiatePort(Orientation.Horizontal, Direction.Input, actionNode.MultiInputPort() ? Capacity.Multi : Capacity.Single, typeof(bool));
inputContainer.Add(portIn);
portIn.portName = "in";
}
if (actionNode.HasOutPort()) {
portOut = InstantiatePort(Orientation.Horizontal, Direction.Output, actionNode.MultiOutPort() ? Capacity.Multi : Capacity.Single, typeof(bool));
outputContainer.Add(portOut);
portOut.portName = "out";
}
titleContainer.Remove(titleButtonContainer);
imageComplete = new() {
image = AssetDatabase.LoadAssetAtPath<Texture2D>(EditorResourceTool.editorAssets + "/Complete.png"),
tintColor = new Color(0.4f, 0.7f, 0.2f),
style = { marginLeft = 3, marginRight = 10, visibility = Visibility.Hidden }
};
titleContainer.Add(imageComplete);
imageWarning = new() {
image = AssetDatabase.LoadAssetAtPath<Texture2D>(EditorResourceTool.editorAssets + "/Warning.png"),
tintColor = Color.red,
style = { position = Position.Absolute, right = 0, top = 5 }
};
titleContainer.Add(imageWarning);
RefreshState();
if (actionNode.HasSubTitle()) {
sub_title = new(actionNode.SubTitle()) {
style = { fontSize = 12, paddingLeft = 8, paddingBottom = 5, backgroundColor = new Color(0.23f, 0.23f, 0.23f, 1f) }
};
this.Q<VisualElement>("node-border").Insert(1, sub_title);
}
this.Q<VisualElement>("title").style.height = 25;
}
public void RefreshState() {
imageWarning.style.visibility = actionNode.Valid() ? Visibility.Hidden : Visibility.Visible;
if (sub_title != null) {
if (string.IsNullOrEmpty(actionNode.SubTitle())) {
sub_title.text = "<Empty>";
sub_title.style.color = Color.red;
} else {
sub_title.text = actionNode.SubTitle();
sub_title.style.color = new Color(0.82f, 0.82f, 0.82f, 1f);
}
}
}
public void AddComplete() {
imageComplete.style.visibility = Visibility.Visible;
}
public void RemoveComplete() {
imageComplete.style.visibility = Visibility.Hidden;
}
public void AddProgress() {
if (actionNode.Duration() == 0) {
return;
}
progressPlaying = new();
progressPlaying.style.height = 10;
contentContainer.Add(progressPlaying);
progressPlaying.highValue = actionNode.Duration();
progressPlaying.value = 0;
RefreshExpandedState();
}
public void UpdateProgress() {
if (actionNode.Duration() == 0) {
return;
}
if (progressPlaying == null) {
AddProgress();
}
progressPlaying.value = actionNode.elapsedTime;
}
public void RemoveProgress() {
if (progressPlaying == null) {
return;
}
contentContainer.Remove(progressPlaying);
progressPlaying = null;
RefreshExpandedState();
}
public override void OnSelected() {
base.OnSelected();
graphWindow.OnSelect(this);
}
}
}