summaryrefslogtreecommitdiff
path: root/Runtime/Graphs/UnityEngine.Graphs/LogicNodeTestLibrary.cs
blob: b92555afa6795321a5dd4710bc946ba71d03d894 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

#if NOT
// Nodes for trying-out purposes. Complete mess.

using System.Collections;
using UnityEngine;
using EmptyDelegate = UnityEngine.Graphs.LogicGraph.LogicNodeUtility.EmptyDelegate;
using ColliderDelegate = UnityEngine.Graphs.LogicGraph.NodeLibrary.ColliderDelegate;

namespace UnityEngine.Graphs.LogicGraph
{
	public class LogicNodeTestLibrary
	{
		[Logic]
		public static IEnumerator DoSomethingWithAnimationCurve(AnimationCurve curve)
		{
			var go = GameObject.Find("Cube");
		
			for (float f = 0; f < 2f; f+=0.01f)
			{
				go.transform.Rotate(new Vector3(curve.Evaluate(f) * 10f, 0,0));
				yield return new WaitForFixedUpdate();
			}
		}

		[Logic]
		public class LimitedInvoker
		{
			public int invokeTimes = 500;
			int currentInvokeIndex = 0;

			public delegate void VoidDelegate();

			public VoidDelegate myOut;

			public void In()
			{
				if (++currentInvokeIndex > invokeTimes)
					return;

				if (currentInvokeIndex % 500 == 0)
					Debug.Log(currentInvokeIndex);

				if (myOut != null)
					myOut();
			}
		}

		[Logic(null, null, typeof(int))]
		public static void Counter(ref int state, int UpTo, EmptyDelegate done)
		{
			state++;
			if (state >= UpTo)
				if (done != null)
					done();
		}

		[Logic]
		public static void HasTag(GameObject go, string tag, EmptyDelegate True, EmptyDelegate False)
		{
			if (go.tag == tag)
			{
				if (True != null)
					True();
			}
			else
			{
				if (False != null)
					False();
			}
		}

		[Logic]
		public static event EmptyDelegate StaticEvent;

		public static void CauseStaticEvent()
		{
			if (StaticEvent != null)
				StaticEvent();
		}

		// Enums for in slots
		public enum StartStopEvents { Start, Stop }
		public enum StartPauseStopEvents { Start, Pause, Stop }

		[LogicEval(typeof(Transform))]
		public static float GetPositionY(Transform self)
		{
			return self.position.y;
		}

		[Logic]
		public static string PassString()
		{
			return "Some String";
		}

		[Logic]
		public static string AddStrings(string a, string b)
		{
			return a + " + " + b;
		}

		[Logic]
		public static void LogCollidersGO(Collider collider)
		{
			Debug.Log("Ouch!.. " + collider.gameObject.name);
		}

		[Logic(typeof(Collider))]
		public static void ColliderFunction(Collider self)
		{
			Debug.Log("ColliderFunction " + self.gameObject.name);
		}

		[Logic]
		public static IEnumerator YieldedFunction(string strParam)
		{
			yield return new WaitForSeconds(3);
			Debug.Log(strParam);
		}

		public enum MyActions { Start, Stop, Pause }

		[Logic(typeof(AudioSource), typeof(MyActions))]
		public static void PlayAudio(AudioSource self, MyActions action)
		{
			switch (action)
			{
				case MyActions.Start:
					self.Play();
					break;
				case MyActions.Stop:
					self.Stop();
					break;
				case MyActions.Pause:
					self.Pause();
					break;
			}
		}

		public class Expressions
		{
			[LogicExpression]
			public static int CustomFn(int f)
			{
				return f*f;
			}
	
			[LogicExpression]
			public static float Prop
			{
				get
				{
					return 3f;
				}
			}
		}

		[Logic]
		public static int intVar;

		[Logic]
		public static string strProperty { get { return "0"; } set { Debug.Log("setting to: " + value); } }

		[Logic]
		public static int SetInt (int a)
		{
			return a;
		}

		[Logic]
		public static void EvaluateBool (bool b, EmptyDelegate True, EmptyDelegate False)
		{
			if (b)
				True();
			else
				False();
		}

		[Logic]
		public static void Destroy (GameObject self)
		{
			Object.Destroy(self);
		}

		[Logic]
		public static GameObject Instantiate (GameObject obj, Vector3 position, Quaternion rotation)
		{
			return (GameObject)GameObject.Instantiate(obj, position, rotation);
		}

		[Logic]
		public static void Do() { }

		[Logic]
		public static void FindCollidersInRadius (Vector3 center, float radius, ColliderDelegate affected, ColliderDelegate done)
		{
			Collider[] colliders = Physics.OverlapSphere(center, radius);
			foreach (Collider col in colliders)
			{
				affected(col);
			}
			if (done == null)
				Debug.LogWarning("done delegate is null");
			else
				done(colliders[0]);
		}

		// Eval

		[LogicEval]
		public static Vector3 Vector3FromFloats (float x, float y, float z)
		{
			return new Vector3(x, y, z);
		}

		[LogicEval]
		public static int Add (int a, int b)
		{
			return a + b;
		}

		[LogicEval]
		public static float Random (float min, float max)
		{
			return UnityEngine.Random.Range(min, max);
		}

		[LogicEval]
		public static float InputAxis (string axisName)
		{
			return Input.GetAxis(axisName);
		}

		[LogicEval]
		public static GameObject GameObjectVar (GameObject obj)
		{
			return obj;
		}

		[LogicEval]
		public static Vector3 InverseDistVector (Vector3 from, Vector3 to, float multiplier)
		{
			float dist = Vector3.Distance(from, to);
			if (dist == 0)
				return Vector3.zero;
			else
				return (to - from) / (dist * dist) * multiplier;
		}

	}

	[Logic]
	public class NodeInClass
	{
		public int simpleVariable;
		public string onlyGet
		{
			get { return string.Empty; }
		}

		public string onlySet
		{
			set { }
		}

		public delegate void VoidDelegate();
		public VoidDelegate ExitLink1;
		public VoidDelegate ExitLink2;

		public void Input1() { Debug.Log("input1");}
		public IEnumerator YieldedInput() { return null; }
	}

	[Logic(typeof(Collider))]
	public class ColliderNodeInClass
	{
		public Collider target;
		public void DoSomething() { Debug.Log(target);}
	}

	[Logic]
	public class TitledStuff
	{
		public enum TitledEnum { [Title("Crazy")]Start, [Title("Thing")] Stop, [Title("ToDo")] Pause }

		public delegate void TwoStringTitledDelegate([Title("String 1")]string str1, [Title("String 2")]string str2);
		public delegate void TwoObjectsTitledEvent([Title("GO arg")]Object go, [Title("other arg")]Object other);

		[Logic]
		[Title("-::Custom Named Fn::-")]
		[return: Title("My ÀÛT")]
		public static int FunctionWithCustomTitles([Title("First variable")]string var1, [Title("Second variable")]int var2) { return 0; }

		[Logic]
		[Title("-::Custom Named delegate Fn::-")]
		public static void CustomNameFnWithDelegates([Title("Str input")]string string1, [Title("Output 1")]TwoStringTitledDelegate out1, [Title("Output 2")]TwoStringTitledDelegate out2) { }

		[Logic]
		[Title("@#^@#$")]
		public static TwoObjectsTitledEvent TitledEvent;

		[Logic]
		[Title("Ghy")]
		public static int titledVar;
		[Logic]
		[Title("Ghy@$^")]
		public static int titledProp { get { return 0; } }

		[LogicEval]
		[Title("Eval 123")]
		[return: Title("Eval Ret")]
		public static int TitledEval([Title("eval arg")]string str) { return 0; }

		[Logic(null, typeof(TitledEnum))]
		[Title("it's really titled")]
		public static void TitledMultiInputFunction(TitledEnum actions, [Title("Var In 1")]int prm1, [Title("Var In 2")]string prm2) { }
	}

	[Logic]
	[Title("-::Custom Named Class::-")]
	public class CustomTitleNodeInClass
	{
		[Title("Var Custom")]
		public int simpleVariable;

		[Title("Property Custom")]
		public string onlyGet { get { return string.Empty; } }

		public delegate void VoidDelegate();
		[Title("Exit link custom")]
		public VoidDelegate ExitLink1;

		[Title("Input custom")]
		public void Input1() { }
	}

	[Logic]
	public class ValidatingNodes
	{
		public delegate void	GOEvent(GameObject go);
		[Logic(typeof(GameObject)), Validate("MyValidate")] public static GOEvent ValidatedDelegate;
		[Logic(typeof(GameObject)), Validate("MyValidate")] public static event GOEvent ValidatedEvent;

		[Logic(typeof(GameObject)), Validate("MyValidate")]
		public static void FunctionNodeWithValidate(GameObject target){}

		public static bool MyValidate(GameObject target) { return target.name == "ShowMe"; }
	}

	[Logic(typeof(GameObject)), Validate("ValidatingNodes.MyValidate")]
	public class ClassWithValidate
	{
		public GameObject target;
		public void Input(){}
	}
}
#endif