From 9130237e3acef4893b2e85bf3248992c9b2198b6 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Tue, 24 Sep 2019 00:49:00 +0200 Subject: [PATCH] Added LogicToy example --- Examples/LogicToy/Editor.meta | 8 + Examples/LogicToy/Editor/LogicGraphEditor.cs | 41 ++++ .../LogicToy/Editor/LogicGraphEditor.cs.meta | 11 + Examples/LogicToy/Editor/LogicNodeEditor.cs | 36 +++ .../LogicToy/Editor/LogicNodeEditor.cs.meta | 11 + Examples/LogicToy/Editor/PulseNodeEditor.cs | 27 +++ .../LogicToy/Editor/PulseNodeEditor.cs.meta | 11 + Examples/LogicToy/Example.asset | 223 ++++++++++++++++++ Examples/LogicToy/Example.asset.meta | 8 + Examples/LogicToy/LogicGraph.cs | 6 + Examples/LogicToy/LogicGraph.cs.meta | 11 + Examples/LogicToy/Nodes.meta | 8 + Examples/LogicToy/Nodes/LogicNode.cs | 22 ++ Examples/LogicToy/Nodes/LogicNode.cs.meta | 11 + Examples/LogicToy/Nodes/PulseNode.cs | 16 ++ Examples/LogicToy/Nodes/PulseNode.cs.meta | 11 + Examples/LogicToy/Nodes/ToggleNode.cs | 17 ++ Examples/LogicToy/Nodes/ToggleNode.cs.meta | 11 + 18 files changed, 489 insertions(+) create mode 100644 Examples/LogicToy/Editor.meta create mode 100644 Examples/LogicToy/Editor/LogicGraphEditor.cs create mode 100644 Examples/LogicToy/Editor/LogicGraphEditor.cs.meta create mode 100644 Examples/LogicToy/Editor/LogicNodeEditor.cs create mode 100644 Examples/LogicToy/Editor/LogicNodeEditor.cs.meta create mode 100644 Examples/LogicToy/Editor/PulseNodeEditor.cs create mode 100644 Examples/LogicToy/Editor/PulseNodeEditor.cs.meta create mode 100644 Examples/LogicToy/Example.asset create mode 100644 Examples/LogicToy/Example.asset.meta create mode 100644 Examples/LogicToy/LogicGraph.cs create mode 100644 Examples/LogicToy/LogicGraph.cs.meta create mode 100644 Examples/LogicToy/Nodes.meta create mode 100644 Examples/LogicToy/Nodes/LogicNode.cs create mode 100644 Examples/LogicToy/Nodes/LogicNode.cs.meta create mode 100644 Examples/LogicToy/Nodes/PulseNode.cs create mode 100644 Examples/LogicToy/Nodes/PulseNode.cs.meta create mode 100644 Examples/LogicToy/Nodes/ToggleNode.cs create mode 100644 Examples/LogicToy/Nodes/ToggleNode.cs.meta diff --git a/Examples/LogicToy/Editor.meta b/Examples/LogicToy/Editor.meta new file mode 100644 index 0000000..d198e91 --- /dev/null +++ b/Examples/LogicToy/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e5de235328e16fc4dbc4dcdd3794863e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/LogicToy/Editor/LogicGraphEditor.cs b/Examples/LogicToy/Editor/LogicGraphEditor.cs new file mode 100644 index 0000000..a363b6c --- /dev/null +++ b/Examples/LogicToy/Editor/LogicGraphEditor.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEngine; +using XNode; +using XNode.Examples.LogicToy; + +namespace XNodeEditor.Examples.LogicToy { + [CustomNodeGraphEditor(typeof(LogicGraph))] + public class LogicGraphEditor : NodeGraphEditor { + private class NoodleTimer { + public NodePort output, input; + public double lastOnTime; + } + + /// + /// Overriding GetNodeMenuName lets you control if and how nodes are categorized. + /// In this example we are sorting out all node types that are not in the XNode.Examples namespace. + /// + public override string GetNodeMenuName(System.Type type) { + if (type.Namespace == "XNode.Examples.LogicToy") { + return base.GetNodeMenuName(type).Replace("X Node/Examples/Logic Toy/", ""); + } else return null; + } + + public override void OnGUI() { + // Repaint each frame + window.Repaint(); + } + + public override Color GetNoodleColor(NodePort output, NodePort input) { + LogicNode node = output.node as LogicNode; + LogicNodeEditor nodeEditor = NodeEditor.GetEditor(node, window) as LogicNodeEditor; + Color baseColor = base.GetNoodleColor(output, input); + + float t = (float) (EditorApplication.timeSinceStartup - nodeEditor.lastOnTime); + t *= 2f; + return Color.Lerp(Color.yellow, baseColor, t); + } + } +} \ No newline at end of file diff --git a/Examples/LogicToy/Editor/LogicGraphEditor.cs.meta b/Examples/LogicToy/Editor/LogicGraphEditor.cs.meta new file mode 100644 index 0000000..662c8e6 --- /dev/null +++ b/Examples/LogicToy/Editor/LogicGraphEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6c9bb5ae9dee9af43a89fa0db3ed8de0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/LogicToy/Editor/LogicNodeEditor.cs b/Examples/LogicToy/Editor/LogicNodeEditor.cs new file mode 100644 index 0000000..265cef2 --- /dev/null +++ b/Examples/LogicToy/Editor/LogicNodeEditor.cs @@ -0,0 +1,36 @@ +using UnityEditor; +using UnityEngine; +using XNode.Examples.LogicToy; + +namespace XNodeEditor.Examples.LogicToy { + [CustomNodeEditor(typeof(LogicNode))] + public class LogicNodeEditor : NodeEditor { + private LogicNode node; + public double lastOnTime; + + public override void OnHeaderGUI() { + // Initialization + if (node == null) { + node = target as LogicNode; + } + + base.OnHeaderGUI(); + Rect dotRect = GUILayoutUtility.GetLastRect(); + dotRect.size = new Vector2(16, 16); + dotRect.y += 6; + + if (node.on) { + GUI.color = Color.green; + lastOnTime = EditorApplication.timeSinceStartup; + } else { + float t = (float) (EditorApplication.timeSinceStartup - lastOnTime); + t *= 2f; + if (t < 1) { + GUI.color = Color.Lerp(Color.green, Color.red, t); + } else GUI.color = Color.red; + } + GUI.DrawTexture(dotRect, NodeEditorResources.dot); + GUI.color = Color.white; + } + } +} \ No newline at end of file diff --git a/Examples/LogicToy/Editor/LogicNodeEditor.cs.meta b/Examples/LogicToy/Editor/LogicNodeEditor.cs.meta new file mode 100644 index 0000000..24a3edf --- /dev/null +++ b/Examples/LogicToy/Editor/LogicNodeEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f1280a9d3431638429b64ea41000b794 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/LogicToy/Editor/PulseNodeEditor.cs b/Examples/LogicToy/Editor/PulseNodeEditor.cs new file mode 100644 index 0000000..ccd6008 --- /dev/null +++ b/Examples/LogicToy/Editor/PulseNodeEditor.cs @@ -0,0 +1,27 @@ +using UnityEditor; +using UnityEngine; +using XNode.Examples.LogicToy; + +namespace XNodeEditor.Examples.LogicToy { + [CustomNodeEditor(typeof(PulseNode))] + public class PulseNodeEditor : LogicNodeEditor { + private PulseNode node; + + public override void OnBodyGUI() { + // Initialization + if (node == null) { + node = target as PulseNode; + lastOnTime = EditorApplication.timeSinceStartup; + } + + // Timer + if (EditorApplication.timeSinceStartup - lastOnTime > node.interval) { + lastOnTime = EditorApplication.timeSinceStartup; + node.FirePulse(); + } + + // Basic GUI + base.OnBodyGUI(); + } + } +} \ No newline at end of file diff --git a/Examples/LogicToy/Editor/PulseNodeEditor.cs.meta b/Examples/LogicToy/Editor/PulseNodeEditor.cs.meta new file mode 100644 index 0000000..c12e5dd --- /dev/null +++ b/Examples/LogicToy/Editor/PulseNodeEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 16a713ef662e1bd4086b83032f1d97c9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/LogicToy/Example.asset b/Examples/LogicToy/Example.asset new file mode 100644 index 0000000..215d301 --- /dev/null +++ b/Examples/LogicToy/Example.asset @@ -0,0 +1,223 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1577ad53614c9e0498c962eaa9ba304f, type: 3} + m_Name: Example + m_EditorClassIdentifier: + nodes: + - {fileID: 114046633862954194} + - {fileID: 114880916489599218} + - {fileID: 114572916788169214} + - {fileID: 114104949491275850} + - {fileID: 114524369431968588} +--- !u!114 &114046633862954194 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 54822dcd4ea0525409ad71a5fa416755, type: 3} + m_Name: Pulse + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -248, y: -88} + ports: + keys: + - output + values: + - _fieldName: output + _node: {fileID: 114046633862954194} + _typeQualifiedName: XNode.Examples.LogicToy.LogicNode, Assembly-CSharp, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + connections: + - fieldName: input + node: {fileID: 114880916489599218} + reroutePoints: [] + - fieldName: input + node: {fileID: 114524369431968588} + reroutePoints: [] + _direction: 1 + _connectionType: 0 + _typeConstraint: 0 + _dynamic: 0 + interval: 1 + output: {fileID: 0} +--- !u!114 &114104949491275850 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c7d4066122e5859498bbf1f1db2593cf, type: 3} + m_Name: Toggle + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: 264, y: -40} + ports: + keys: + - input + - output + values: + - _fieldName: input + _node: {fileID: 114104949491275850} + _typeQualifiedName: XNode.Examples.LogicToy.LogicNode, Assembly-CSharp, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + connections: + - fieldName: output + node: {fileID: 114572916788169214} + reroutePoints: [] + _direction: 0 + _connectionType: 0 + _typeConstraint: 0 + _dynamic: 0 + - _fieldName: output + _node: {fileID: 114104949491275850} + _typeQualifiedName: XNode.Examples.LogicToy.LogicNode, Assembly-CSharp, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + connections: [] + _direction: 1 + _connectionType: 0 + _typeConstraint: 0 + _dynamic: 0 + input: {fileID: 0} + output: {fileID: 0} +--- !u!114 &114524369431968588 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c7d4066122e5859498bbf1f1db2593cf, type: 3} + m_Name: Toggle + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -56, y: -8} + ports: + keys: + - input + - output + values: + - _fieldName: input + _node: {fileID: 114524369431968588} + _typeQualifiedName: XNode.Examples.LogicToy.LogicNode, Assembly-CSharp, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + connections: + - fieldName: output + node: {fileID: 114046633862954194} + reroutePoints: [] + _direction: 0 + _connectionType: 0 + _typeConstraint: 0 + _dynamic: 0 + - _fieldName: output + _node: {fileID: 114524369431968588} + _typeQualifiedName: XNode.Examples.LogicToy.LogicNode, Assembly-CSharp, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + connections: [] + _direction: 1 + _connectionType: 0 + _typeConstraint: 0 + _dynamic: 0 + input: {fileID: 0} + output: {fileID: 0} +--- !u!114 &114572916788169214 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c7d4066122e5859498bbf1f1db2593cf, type: 3} + m_Name: Toggle + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: 88, y: -168} + ports: + keys: + - input + - output + values: + - _fieldName: input + _node: {fileID: 114572916788169214} + _typeQualifiedName: XNode.Examples.LogicToy.LogicNode, Assembly-CSharp, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + connections: + - fieldName: output + node: {fileID: 114880916489599218} + reroutePoints: [] + _direction: 0 + _connectionType: 0 + _typeConstraint: 0 + _dynamic: 0 + - _fieldName: output + _node: {fileID: 114572916788169214} + _typeQualifiedName: XNode.Examples.LogicToy.LogicNode, Assembly-CSharp, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + connections: + - fieldName: input + node: {fileID: 114104949491275850} + reroutePoints: [] + _direction: 1 + _connectionType: 0 + _typeConstraint: 0 + _dynamic: 0 + input: {fileID: 0} + output: {fileID: 0} +--- !u!114 &114880916489599218 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c7d4066122e5859498bbf1f1db2593cf, type: 3} + m_Name: Toggle + m_EditorClassIdentifier: + graph: {fileID: 11400000} + position: {x: -72, y: -168} + ports: + keys: + - input + - output + values: + - _fieldName: input + _node: {fileID: 114880916489599218} + _typeQualifiedName: XNode.Examples.LogicToy.LogicNode, Assembly-CSharp, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + connections: + - fieldName: output + node: {fileID: 114046633862954194} + reroutePoints: [] + _direction: 0 + _connectionType: 0 + _typeConstraint: 0 + _dynamic: 0 + - _fieldName: output + _node: {fileID: 114880916489599218} + _typeQualifiedName: XNode.Examples.LogicToy.LogicNode, Assembly-CSharp, Version=0.0.0.0, + Culture=neutral, PublicKeyToken=null + connections: + - fieldName: input + node: {fileID: 114572916788169214} + reroutePoints: [] + _direction: 1 + _connectionType: 0 + _typeConstraint: 0 + _dynamic: 0 + input: {fileID: 0} + output: {fileID: 0} diff --git a/Examples/LogicToy/Example.asset.meta b/Examples/LogicToy/Example.asset.meta new file mode 100644 index 0000000..188868d --- /dev/null +++ b/Examples/LogicToy/Example.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: df29bbd59126610439c3391f59eac02a +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/LogicToy/LogicGraph.cs b/Examples/LogicToy/LogicGraph.cs new file mode 100644 index 0000000..873d277 --- /dev/null +++ b/Examples/LogicToy/LogicGraph.cs @@ -0,0 +1,6 @@ +using UnityEngine; + +namespace XNode.Examples.LogicToy { + [CreateAssetMenu(fileName = "New LogicToy Graph", menuName = "xNode Examples/LogicToy Graph")] + public class LogicGraph : NodeGraph { } +} \ No newline at end of file diff --git a/Examples/LogicToy/LogicGraph.cs.meta b/Examples/LogicToy/LogicGraph.cs.meta new file mode 100644 index 0000000..c0f0d7f --- /dev/null +++ b/Examples/LogicToy/LogicGraph.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1577ad53614c9e0498c962eaa9ba304f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/LogicToy/Nodes.meta b/Examples/LogicToy/Nodes.meta new file mode 100644 index 0000000..3cc6de9 --- /dev/null +++ b/Examples/LogicToy/Nodes.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8852475771ad63a45a0854e9419d4e19 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/LogicToy/Nodes/LogicNode.cs b/Examples/LogicToy/Nodes/LogicNode.cs new file mode 100644 index 0000000..e8530ad --- /dev/null +++ b/Examples/LogicToy/Nodes/LogicNode.cs @@ -0,0 +1,22 @@ +namespace XNode.Examples.LogicToy { + /// Base node for the LogicToy system + public abstract class LogicNode : Node { + public abstract bool on { get; } + + protected abstract void OnTrigger(); + + public void SendPulse(NodePort port) { + // Loop through port connections + int connectionCount = port.ConnectionCount; + for (int i = 0; i < connectionCount; i++) { + NodePort connectedPort = port.GetConnection(i); + + // Get connected ports logic node + LogicNode logicNode = connectedPort.node as LogicNode; + + // Trigger it + if (logicNode != null) logicNode.OnTrigger(); + } + } + } +} \ No newline at end of file diff --git a/Examples/LogicToy/Nodes/LogicNode.cs.meta b/Examples/LogicToy/Nodes/LogicNode.cs.meta new file mode 100644 index 0000000..435ffe0 --- /dev/null +++ b/Examples/LogicToy/Nodes/LogicNode.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c1731c3983afdaa4e824178fcd76d66e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/LogicToy/Nodes/PulseNode.cs b/Examples/LogicToy/Nodes/PulseNode.cs new file mode 100644 index 0000000..c74347c --- /dev/null +++ b/Examples/LogicToy/Nodes/PulseNode.cs @@ -0,0 +1,16 @@ +namespace XNode.Examples.LogicToy { + [NodeWidth(140)] + public class PulseNode : LogicNode { + public float interval = 1f; + [Output] public LogicNode output; + public override bool on { get { return false; } } + + /// Called from editor + public void FirePulse() { + SendPulse(GetPort("output")); + } + + /// This node has no inputs, so this does nothing + protected override void OnTrigger() { } + } +} \ No newline at end of file diff --git a/Examples/LogicToy/Nodes/PulseNode.cs.meta b/Examples/LogicToy/Nodes/PulseNode.cs.meta new file mode 100644 index 0000000..268ec7d --- /dev/null +++ b/Examples/LogicToy/Nodes/PulseNode.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 54822dcd4ea0525409ad71a5fa416755 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Examples/LogicToy/Nodes/ToggleNode.cs b/Examples/LogicToy/Nodes/ToggleNode.cs new file mode 100644 index 0000000..9f80d05 --- /dev/null +++ b/Examples/LogicToy/Nodes/ToggleNode.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +namespace XNode.Examples.LogicToy { + [NodeWidth(140)] + public class ToggleNode : LogicNode { + private bool _on; + [Input] public LogicNode input; + [Output] public LogicNode output; + public override bool on { get { return _on; } } + + /// This node has no inputs, so this does nothing + protected override void OnTrigger() { + _on = !_on; + if (on) SendPulse(GetPort("output")); + } + } +} \ No newline at end of file diff --git a/Examples/LogicToy/Nodes/ToggleNode.cs.meta b/Examples/LogicToy/Nodes/ToggleNode.cs.meta new file mode 100644 index 0000000..4b19b67 --- /dev/null +++ b/Examples/LogicToy/Nodes/ToggleNode.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c7d4066122e5859498bbf1f1db2593cf +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: