1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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);
}
}
}
|