diff options
author | chai <chaifix@163.com> | 2022-06-28 09:40:37 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2022-06-28 09:40:37 +0800 |
commit | 49b25e755b70ec412feaaf0b898d6f7e09d2bea6 (patch) | |
tree | 3c5f4260f30d1c2d7196db93153700d7ddec3157 /Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor | |
parent | c92269331692feca2c276649f6c4ee8911f1f859 (diff) |
+node example
Diffstat (limited to 'Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor')
4 files changed, 178 insertions, 0 deletions
diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicGraphEditor.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicGraphEditor.cs new file mode 100644 index 00000000..1ba88780 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicGraphEditor.cs @@ -0,0 +1,111 @@ +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 { + readonly Color boolColor = new Color(0.1f, 0.6f, 0.6f); + private List<ObjectLastOnTimer> lastOnTimers = new List<ObjectLastOnTimer>(); + private double lastFrame; + + /// <summary> Used for tracking when an arbitrary object was last 'on' for fading effects </summary> + private class ObjectLastOnTimer { + public object obj; + public double lastOnTime; + + public ObjectLastOnTimer(object obj, bool on) { + this.obj = obj; + } + } + + /// <summary> + /// 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. + /// </summary> + 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(); + + // Timer + if (Event.current.type == EventType.Repaint) { + for (int i = 0; i < target.nodes.Count; i++) { + ITimerTick timerTick = target.nodes[i] as ITimerTick; + if (timerTick != null) { + float deltaTime = (float) (EditorApplication.timeSinceStartup - lastFrame); + timerTick.Tick(deltaTime); + } + } + } + lastFrame = EditorApplication.timeSinceStartup; + } + + /// <summary> Controls graph noodle colors </summary> + public override Gradient GetNoodleGradient(NodePort output, NodePort input) { + LogicNode node = output.node as LogicNode; + Gradient baseGradient = base.GetNoodleGradient(output, input); + HighlightGradient(baseGradient, Color.yellow, output, (bool) node.GetValue(output)); + return baseGradient; + } + + /// <summary> Controls graph type colors </summary> + public override Color GetTypeColor(System.Type type) { + if (type == typeof(bool)) return boolColor; + else return base.GetTypeColor(type); + } + + /// <summary> Returns the time at which an arbitrary object was last 'on' </summary> + public double GetLastOnTime(object obj, bool high) { + ObjectLastOnTimer timer = lastOnTimers.FirstOrDefault(x => x.obj == obj); + if (timer == null) { + timer = new ObjectLastOnTimer(obj, high); + lastOnTimers.Add(timer); + } + if (high) timer.lastOnTime = EditorApplication.timeSinceStartup; + return timer.lastOnTime; + } + + /// <summary> Returns a color based on if or when an arbitrary object was last 'on' </summary> + public Color GetLerpColor(Color off, Color on, object obj, bool high) { + double lastOnTime = GetLastOnTime(obj, high); + + if (high) return on; + else { + float t = (float) (lastOnTime - EditorApplication.timeSinceStartup); + t *= 8f; + if (t > 0) return Color.Lerp(off, on, t); + else return off; + } + } + + /// <summary> Returns a color based on if or when an arbitrary object was last 'on' </summary> + public void HighlightGradient(Gradient gradient, Color highlightColor, object obj, bool high) { + double lastOnTime = GetLastOnTime(obj, high); + float t; + + if (high) t = 1f; + else { + t = (float) (lastOnTime - EditorApplication.timeSinceStartup); + t *= 8f; + t += 1; + } + t = Mathf.Clamp01(t); + GradientColorKey[] colorKeys = gradient.colorKeys; + for (int i = 0; i < colorKeys.Length; i++) { + GradientColorKey colorKey = colorKeys[i]; + colorKey.color = Color.Lerp(colorKeys[i].color, highlightColor, t); + colorKeys[i] = colorKey; + } + gradient.SetKeys(colorKeys, gradient.alphaKeys); + } + } +} diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicGraphEditor.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicGraphEditor.cs.meta new file mode 100644 index 00000000..662c8e67 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/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/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicNodeEditor.cs b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicNodeEditor.cs new file mode 100644 index 00000000..24a8d603 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicNodeEditor.cs @@ -0,0 +1,45 @@ +using UnityEditor; +using UnityEngine; +using XNode; +using XNode.Examples.LogicToy; + +namespace XNodeEditor.Examples.LogicToy { + [CustomNodeEditor(typeof(LogicNode))] + public class LogicNodeEditor : NodeEditor { + private LogicNode node; + private LogicGraphEditor graphEditor; + + public override void OnHeaderGUI() { + // Initialization + if (node == null) { + node = target as LogicNode; + graphEditor = NodeGraphEditor.GetEditor(target.graph, window) as LogicGraphEditor; + } + + base.OnHeaderGUI(); + Rect dotRect = GUILayoutUtility.GetLastRect(); + dotRect.size = new Vector2(16, 16); + dotRect.y += 6; + + GUI.color = graphEditor.GetLerpColor(Color.red, Color.green, node, node.led); + GUI.DrawTexture(dotRect, NodeEditorResources.dot); + GUI.color = Color.white; + } + + public override void OnBodyGUI() { + if (target == null) { + Debug.LogWarning("Null target node for node editor!"); + return; + } + NodePort input = target.GetPort("input"); + NodePort output = target.GetPort("output"); + + GUILayout.BeginHorizontal(); + if (input != null) NodeEditorGUILayout.PortField(GUIContent.none, input, GUILayout.MinWidth(0)); + if (output != null) NodeEditorGUILayout.PortField(GUIContent.none, output, GUILayout.MinWidth(0)); + GUILayout.EndHorizontal(); + EditorGUIUtility.labelWidth = 60; + base.OnBodyGUI(); + } + } +}
\ No newline at end of file diff --git a/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicNodeEditor.cs.meta b/Other/NodeEditorExamples/Assets/xNode-examples/Examples/LogicToy/Editor/LogicNodeEditor.cs.meta new file mode 100644 index 00000000..24a3edfa --- /dev/null +++ b/Other/NodeEditorExamples/Assets/xNode-examples/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: |