1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

Improved broken connection cleanup. Example updates

This commit is contained in:
Thor Brigsted 2017-10-13 20:28:40 +02:00
parent 899e5ae979
commit bc5c060dee
12 changed files with 79 additions and 40 deletions

Binary file not shown.

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: e8c47bc953732464a9bf3a76273d99ef
timeCreated: 1507916591
licenseType: Free
NativeFormatImporter:
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,15 +0,0 @@
using UnityEngine;
[System.Serializable]
public class BaseNode : Node {
[Input] public string input;
[Output] public string output;
public bool concat;
[TextArea]
public string SomeString;
[Header("New stuff")]
public Color col;
public AnimationCurve anim;
public Vector3 vec;
}

View File

@ -0,0 +1,13 @@
[System.Serializable]
public class ConstantValue : ExampleNodeBase {
public float a;
[Output] public float value;
protected override void Init() {
name = "Constant Value";
}
public override object GetValue(NodePort port) {
return a;
}
}

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 23665941e8cd89a48b1272ac5fd6510c guid: 707240ce8955a0240a7c0c4177d83bf5
timeCreated: 1505462705 timeCreated: 1505937586
licenseType: Free licenseType: Free
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2

View File

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

View File

@ -12,20 +12,7 @@ public class DisplayValueEditor : NodeEditor {
} }
public float GetResult() { public float GetResult() {
float result = 0f; ExampleNodeBase t = target as ExampleNodeBase;
NodePort port = target.GetInputByFieldName("value"); return t.GetInputFloat("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,22 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExampleNodeBase : Node {
public float GetInputFloat(string fieldName) {
float result = 0f;
NodePort port = GetInputByFieldName(fieldName);
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: 923bada49e668fd4a98b04fcb49999d7
timeCreated: 1507917099
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,9 +1,9 @@
using UnityEngine; using UnityEngine;
[System.Serializable] [System.Serializable]
public class MathNode : Node { public class MathNode : ExampleNodeBase {
public float a; [Input] public float c;
public float b; [Input] public float b;
[Output] public float result; [Output] public float result;
public enum MathType { Add, Subtract, Multiply, Divide} public enum MathType { Add, Subtract, Multiply, Divide}
public MathType mathType = MathType.Add; public MathType mathType = MathType.Add;
@ -13,6 +13,9 @@ public class MathNode : Node {
} }
public override object GetValue(NodePort port) { public override object GetValue(NodePort port) {
float a = GetInputFloat("c");
float b = GetInputFloat("b");
switch(port.fieldName) { switch(port.fieldName) {
case "result": case "result":
switch(mathType) { switch(mathType) {
@ -25,8 +28,4 @@ public class MathNode : Node {
} }
return 0f; return 0f;
} }
public override void OnCreateConnection(NodePort from, NodePort to) {
}
} }

View File

@ -128,7 +128,9 @@ public partial class NodeEditorWindow {
if (!portConnectionPoints.ContainsKey(output)) continue; if (!portConnectionPoints.ContainsKey(output)) continue;
Vector2 from = _portConnectionPoints[output].center; Vector2 from = _portConnectionPoints[output].center;
for (int k = 0; k < output.ConnectionCount; k++) { for (int k = 0; k < output.ConnectionCount; k++) {
NodePort input = output.GetConnection(k); NodePort input = output.GetConnection(k);
if (input == null) return; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return.
Vector2 to = _portConnectionPoints[input].center; Vector2 to = _portConnectionPoints[input].center;
DrawConnection(from, to, NodeEditorUtilities.GetTypeColor(output.type)); DrawConnection(from, to, NodeEditorUtilities.GetTypeColor(output.type));
} }

View File

@ -77,7 +77,16 @@ public class NodePort {
} }
public NodePort GetConnection(int i) { public NodePort GetConnection(int i) {
//If the connection is broken for some reason, remove it.
if (connections[i].node == null || string.IsNullOrEmpty(connections[i].fieldName)) {
connections.RemoveAt(i);
return null;
}
NodePort port = connections[i].node.GetPortByFieldName(connections[i].fieldName); NodePort port = connections[i].node.GetPortByFieldName(connections[i].fieldName);
if (port == null) {
connections.RemoveAt(i);
return null;
}
return port; return port;
} }