1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 01:06:01 +08:00

Added DisplayValue and GetValue

This commit is contained in:
Thor Brigsted 2017-10-09 00:29:40 +02:00
parent cd1864fce3
commit 147424ca73
10 changed files with 117 additions and 9 deletions

Binary file not shown.

View File

@ -0,0 +1,5 @@
using UnityEngine;
public class DisplayValue : Node {
[Input] public float value;
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 98f6f901f0da53142b79277ea3f42518
timeCreated: 1507499149
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,32 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomNodeEditor(typeof(DisplayValue), "Display Value")]
public class DisplayValueEditor : NodeEditor {
public override void OnNodeGUI(out Dictionary<NodePort, Vector2> portPositions) {
base.OnNodeGUI(out portPositions);
EditorGUILayout.LabelField("Value: " + GetResult());
}
public float GetResult() {
float result = 0f;
NodePort port = target.GetInputByFieldName("value");
if (port == null) return result;
int connectionCount = port.ConnectionCount;
for (int i = 0; i < connectionCount; i++) {
NodePort connection = port.GetConnection(i);
if (connection == null) continue;
object obj = connection.GetValue();
if (obj == null) continue;
if (connection.type == typeof(int)) result += (int)obj;
else if (connection.type == typeof(float)) result += (float)obj;
}
return result;
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 429e0671aa9024e449642837aeadc9c2
timeCreated: 1507499229
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,8 +2,8 @@
[System.Serializable]
public class MathNode : Node {
[Input] public float a;
[Input] public float b;
public float a;
public float b;
[Output] public float result;
public enum MathType { Add, Subtract, Multiply, Divide}
public MathType mathType = MathType.Add;
@ -12,6 +12,20 @@ public class MathNode : Node {
name = "Math";
}
public override object GetValue(NodePort port) {
switch(port.fieldName) {
case "result":
switch(mathType) {
case MathType.Add: return a + b;
case MathType.Subtract: return a - b;
case MathType.Multiply: return a * b;
case MathType.Divide: return a / b;
}
break;
}
return 0f;
}
public override void OnCreateConnection(NodePort from, NodePort to) {
}

View File

@ -58,7 +58,6 @@ public class NodeEditor {
if (NodeEditorUtilities.HasAttrib<Node.InputAttribute>(fieldAttribs) || NodeEditorUtilities.HasAttrib<Node.OutputAttribute>(fieldAttribs)) continue;
DrawFieldInfoDrawerGUI(fields[i]);
}
EditorGUILayout.Space();
}
/// <summary> Draw node port GUI using automatic layouting. Returns port handle position. </summary>

View File

@ -167,6 +167,8 @@ public partial class NodeEditorWindow {
//Draw node contents
Dictionary<NodePort, Vector2> portHandlePoints;
nodeEditor.OnNodeGUI(out portHandlePoints);
EditorGUILayout.Space();
if (e.type == EventType.Repaint) {
foreach (var kvp in portHandlePoints) {
Vector2 portHandlePos = kvp.Value;

View File

@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;
/// <summary> Base class for all nodes </summary>
@ -13,16 +14,18 @@ public abstract class Node : ScriptableObject {
/// <summary> Input <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
[SerializeField] public List<NodePort> inputs = new List<NodePort>();
/// <summary> Output <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
[SerializeField] public NodePort[] outputs = new NodePort[0];
[SerializeField] public List<NodePort> outputs = new List<NodePort>();
public int InputCount { get { return inputs.Count; } }
public int OutputCount { get { return outputs.Length; } }
public int OutputCount { get { return outputs.Count; } }
protected Node() {
CachePorts(); //Cache the ports at creation time so we don't have to use reflection at runtime
}
protected void OnEnable() {
VerifyConnections();
CachePorts();
Init();
}
@ -49,6 +52,7 @@ public abstract class Node : ScriptableObject {
for (int i = 0; i < OutputCount; i++) {
if (outputs[i].fieldName == fieldName) return outputs[i];
}
Debug.LogWarning("No outputs with fieldName '" + fieldName+"'");
return null;
}
@ -57,6 +61,12 @@ public abstract class Node : ScriptableObject {
for (int i = 0; i < InputCount; i++) {
if (inputs[i].fieldName == fieldName) return inputs[i];
}
Debug.LogWarning("No inputs with fieldName '" + fieldName+"'");
return null;
}
public virtual object GetValue(NodePort port) {
Debug.LogWarning("No GetValue(NodePort port) override defined for " + GetType());
return null;
}
@ -75,7 +85,7 @@ public abstract class Node : ScriptableObject {
return -1;
}
public int GetOutputId(NodePort output) {
for (int i = 0; i < outputs.Length; i++) {
for (int i = 0; i < outputs.Count; i++) {
if (output == outputs[i]) return i;
}
@ -86,7 +96,7 @@ public abstract class Node : ScriptableObject {
for (int i = 0; i < inputs.Count; i++) {
inputs[i].ClearConnections();
}
for (int i = 0; i < outputs.Length; i++) {
for (int i = 0; i < outputs.Count; i++) {
outputs[i].ClearConnections();
}
}
@ -114,6 +124,7 @@ public abstract class Node : ScriptableObject {
System.Reflection.FieldInfo[] fieldInfo = GetType().GetFields();
for (int i = 0; i < fieldInfo.Length; i++) {
//Get InputAttribute and OutputAttribute
object[] attribs = fieldInfo[i].GetCustomAttributes(false);
InputAttribute inputAttrib = null;
@ -122,12 +133,29 @@ public abstract class Node : ScriptableObject {
if (attribs[k] is InputAttribute) inputAttrib = attribs[k] as InputAttribute;
else if (attribs[k] is OutputAttribute) outputAttrib = attribs[k] as OutputAttribute;
}
if (inputAttrib != null && outputAttrib != null) Debug.LogError("Field " + fieldInfo + " cannot be both input and output.");
else if (inputAttrib != null) inputPorts.Add(new NodePort(fieldInfo[i], this));
else if (outputAttrib != null) outputPorts.Add(new NodePort(fieldInfo[i], this));
}
inputs = inputPorts;
outputs = outputPorts.ToArray();
//Remove
for (int i = inputs.Count-1; i >= 0; i--) {
//If input nodeport does not exist, remove it
if (!inputPorts.Any(x => inputs[i].fieldName == x.fieldName)) inputs.RemoveAt(i);
}
for (int i = outputs.Count - 1; i >= 0; i--) {
//If output nodeport does not exist, remove it
if (!outputPorts.Any(x => outputs[i].fieldName == x.fieldName)) outputs.RemoveAt(i);
}
//Add
for (int i = 0; i < inputPorts.Count; i++) {
//If inputports contains a new port, add it
if (!inputs.Any(x => x.fieldName == inputPorts[i].fieldName)) inputs.Add(inputPorts[i]);
}
for (int i = 0; i < outputPorts.Count; i++) {
//If inputports contains a new port, add it
if (!outputs.Any(x => x.fieldName == outputPorts[i].fieldName)) outputs.Add(outputPorts[i]);
}
}
}

View File

@ -55,6 +55,10 @@ public class NodePort {
}
}
public object GetValue() {
return node.GetValue(this);
}
/// <summary> Connect this <see cref="NodePort"/> to another </summary>
/// <param name="port">The <see cref="NodePort"/> to connect to</param>
public void Connect(NodePort port) {