118 lines
3.8 KiB
C#
118 lines
3.8 KiB
C#
using System;
|
|
using System.Configuration;
|
|
using UnityEditor.Experimental.GraphView;
|
|
using UnityEngine;
|
|
using ImageProcessingGraph.Editor.Nodes.NodeAttributes;
|
|
using System.Collections.Generic;
|
|
using Input = ImageProcessingGraph.Editor.Nodes.NodeAttributes.Input;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace ImageProcessingGraph.Editor
|
|
{
|
|
[System.Serializable]
|
|
public abstract class BaseImageNode
|
|
{
|
|
[SerializeField] private string guid;
|
|
|
|
[SerializeField] private Rect position;
|
|
|
|
[SerializeField] public string typeName;
|
|
|
|
[SerializeField] public (int, int) widthHeight;
|
|
|
|
public string ID => guid;
|
|
public Rect Position => position;
|
|
|
|
public ImageProcessingGraphAsset asset;
|
|
|
|
public BaseImageNode()
|
|
{
|
|
guid = Guid.NewGuid().ToString();
|
|
|
|
/*Type t = this.GetType();
|
|
Debug.Log(t.Name);*/
|
|
}
|
|
|
|
public void SetNodeInputs()
|
|
{
|
|
Type type = this.GetType();
|
|
string t = type.Name;
|
|
FieldInfo[] properties = type.GetFields();
|
|
|
|
var inputProperties = properties
|
|
.Where(p => p.IsDefined(typeof(Input), false)).ToList();
|
|
|
|
foreach (var inputProperty in inputProperties)
|
|
{
|
|
// Find the connection that leads to this input property
|
|
GraphConnection connection = default;
|
|
|
|
foreach (var conn in asset.Connections)
|
|
{
|
|
if (conn.inputPort.nodeID == this.ID)
|
|
{
|
|
if (conn.inputPort.portID == inputProperties.IndexOf(inputProperty))
|
|
{
|
|
connection = conn;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (connection.Equals((default(GraphConnection))))
|
|
return;
|
|
|
|
// Get the output node from the connection
|
|
var outputNode = asset.Nodes.FirstOrDefault(n => n.ID == connection.outputPort.nodeID);
|
|
|
|
if (outputNode != null)
|
|
{
|
|
// Use reflection to get the output data from the output node's corresponding output property
|
|
Type outputType = outputNode.GetType();
|
|
FieldInfo[] fields = outputType.GetFields();
|
|
|
|
var outputFields = fields
|
|
.Where(p => p.IsDefined(typeof(Output), false)).ToList();
|
|
|
|
FieldInfo outputProperty = null;
|
|
|
|
foreach (var da in outputFields)
|
|
{
|
|
if (outputFields.IndexOf(da) == connection.outputPort.portID)
|
|
{
|
|
outputProperty = da;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*var outputProperty = outputNode.GetType().GetProperties()
|
|
.FirstOrDefault(p => p.IsDefined(typeof(Output), false) &&
|
|
outputFields == connection.outputPort.portID);*/
|
|
|
|
if (outputProperty != null)
|
|
{
|
|
// Assign the output data to the input property
|
|
var outputData = outputProperty.GetValue(outputNode);
|
|
inputProperty.SetValue(this, outputData);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void RunNode()
|
|
{
|
|
SetNodeInputs();
|
|
this.Process();
|
|
}
|
|
|
|
public virtual void Process()
|
|
{
|
|
Debug.Log("Uppies");
|
|
}
|
|
|
|
public void SetPosition(Rect position) => this.position = position;
|
|
}
|
|
}
|