-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractBehaviorNode.cs
355 lines (320 loc) · 13.1 KB
/
AbstractBehaviorNode.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Numerics;
using System.Reflection;
using Notui;
using mp.pddn;
using VVVV.PluginInterfaces.V2;
using VVVV.PluginInterfaces.V2.NonGeneric;
namespace Notuiv
{
public abstract class BehaviorInstanceInfoNode<TBehav, TState> : IPluginEvaluate
where TBehav : InteractionBehavior where TState : IAuxiliaryObject
{
public const string BehaviorStatePrefix = "Internal.Behavior:";
[Input("Element Instance")]
public ISpread<NotuiElement> FElement;
[Input("Behavior")]
public ISpread<TBehav> FBehavior;
[Output("BehaviorState")]
public ISpread<ISpread<TState>> FState;
public void Evaluate(int SpreadMax)
{
FState.SliceCount = FElement.SliceCount;
for (int i = 0; i < FElement.SliceCount; i++)
{
FState[i].SliceCount = 0;
if (FElement[i].Value == null)
{
FState[i].SliceCount = 0;
continue;
}
if (!FElement[i].Value.Auxiliary.ContainsKey(BehaviorStatePrefix + FBehavior[0].Id))
{
FState[i].SliceCount = 0;
continue;
}
FState[i].SliceCount = 1;
try
{
FState[i][0] = (TState)FElement[i].Value.Auxiliary[BehaviorStatePrefix + FBehavior[0].Id];
}
catch
{
FState[i].SliceCount = 0;
}
}
}
}
public abstract class AbstractBehaviorNode<T> : IPartImportsSatisfiedNotification, IPluginEvaluate
where T: new()
{
[Output("Output")] public Pin<T> FOutput;
[Import] protected IPluginHost2 FPluginHost;
[Import] protected IIOFactory FIOFactory;
protected bool ExposePrivate = false;
protected T Default = new T();
public virtual void OnImportsSatisfiedBegin() { }
public virtual void OnImportsSatisfiedEnd() { }
public virtual void OnEvaluateBegin() { }
public virtual void OnEvaluateEnd() { }
public virtual void OnChangedBegin() { }
public virtual void OnChangedEnd() { }
/// <summary>
/// If an input property is IEnumerable, please specify how to clear it
/// </summary>
/// <param name="enumerable">extracted enumerable</param>
/// <param name="member">Current property info</param>
/// <param name="i">Current Slice</param>
public virtual void ClearEnumerable(IEnumerable enumerable, PropertyInfo member, int i) { }
/// <summary>
/// If an input property is IEnumerable, please specify how to add objects to it
/// </summary>
/// <param name="enumerable">extracted enumerable</param>
/// <param name="member">Current property info</param>
/// <param name="obj">The object to be added to the IEnumerable</param>
/// <param name="i">Current Slice</param>
public virtual void AddToEnumerable(IEnumerable enumerable, object obj, PropertyInfo member, int i) { }
/// <summary>
/// Transform a property to a different value
/// </summary>
/// <param name="obj">Original value of the property</param>
/// <param name="member">Property info</param>
/// <param name="i">Current slice</param>
/// <returns>The resulting transformed object</returns>
public virtual object TransformInput(object obj, PropertyInfo member, int i)
{
return MiscExtensions.MapVVVVValueToSystemNumerics(obj);
}
/// <summary>
/// Transform the default value of the property to a different value
/// </summary>
/// <param name="obj">Original value of the property</param>
/// <param name="member">Property info</param>
/// <param name="i">Current slice</param>
/// <returns>The resulting transformed object</returns>
public virtual object TransformDefaultValue(object obj, PropertyInfo member)
{
return MiscExtensions.MapSystemNumericsValueToVVVV(obj);
}
protected virtual double[] TransformDefaultToValues(object obj)
{
switch (obj)
{
case float v: return new[] { (double)v };
case double v: return new[] { v };
case short v: return new[] { (double)v };
case int v: return new[] { (double)v };
case long v: return new[] { (double)v };
case ushort v: return new[] { (double)v };
case uint v: return new[] { (double)v };
case ulong v: return new[] { (double)v };
case decimal v: return new[] { (double)v };
case Vector2 v:
{
return new double[] {v.X, v.Y};
}
case Vector3 v:
{
return new double[] { v.X, v.Y, v.Z };
}
case Vector4 v:
{
return new double[] { v.X, v.Y, v.Z, v.W };
}
case Quaternion v:
{
return new double[] { v.X, v.Y, v.Z, v.W };
}
default: return new [] { 0.0 };
}
}
/// <summary>
/// Transform the type a property to a different one
/// </summary>
/// <param name="original">Original type of the property</param>
/// <param name="member">Property info</param>
/// <returns>The resulting transformed type</returns>
public virtual Type TransformType(Type original, PropertyInfo member)
{
return MiscExtensions.MapSystemNumericsTypeToVVVV(original);
}
protected Dictionary<PropertyInfo, bool> IsMemberEnumerable = new Dictionary<PropertyInfo, bool>();
protected Dictionary<PropertyInfo, bool> IsMemberDictionary = new Dictionary<PropertyInfo, bool>();
protected Type CType;
protected PinDictionary Pd;
private void SetDefaultValue(PropertyInfo member, object defaultValue)
{
var defVals = TransformDefaultToValues(defaultValue);
var attr = new InputAttribute(member.Name)
{
DefaultValue = defVals[0],
DefaultValues = defVals,
DefaultString = defaultValue?.ToString() ?? "",
DefaultNodeValue = defaultValue
};
if (defaultValue is bool b) attr.DefaultBoolean = b;
Pd.AddInput(TransformType(member.PropertyType, member), attr);
var spread = Pd.InputPins[member.Name].Spread;
spread.SliceCount = 1;
spread[0] = TransformDefaultValue(defaultValue, member);
}
private void AddMemberPin(PropertyInfo member)
{
Type memberType = typeof(object);
if (!member.CanRead) return;
if (member.GetIndexParameters().Length > 0) return;
memberType = member.PropertyType;
var enumerable = false;
var dictionary = false;
var defaultValue = typeof(T).GetProperty(member.Name)?.GetValue(Default);
if (memberType.GetInterface("IDictionary") != null)
{
try
{
var interfaces = memberType.GetInterfaces().ToList();
interfaces.Add(memberType);
var stype = interfaces
.Where(type =>
{
try
{
var res = type.GetGenericTypeDefinition();
if (res == null) return false;
return res == typeof(IDictionary<,>);
}
catch (Exception)
{
return false;
}
})
.First().GenericTypeArguments;
Pd.AddInput(TransformType(stype[0], member), new InputAttribute(member.Name + " Keys"), binSized: true);
Pd.AddInput(TransformType(stype[1], member), new InputAttribute(member.Name + " Values"), binSized: true);
dictionary = true;
}
catch (Exception)
{
Pd.AddInput(TransformType(memberType, member), new InputAttribute(member.Name));
dictionary = false;
}
}
else if (memberType.GetInterface("IEnumerable") != null && memberType != typeof(string))
{
try
{
var interfaces = memberType.GetInterfaces().ToList();
interfaces.Add(memberType);
var stype = interfaces
.Where(type =>
{
try
{
if (!type.IsConstructedGenericType) return false;
var res = type.GetGenericTypeDefinition();
if (res == null) return false;
return res == typeof(IEnumerable<>);
}
catch (Exception)
{
return false;
}
})
.First().GenericTypeArguments[0];
Pd.AddInput(TransformType(stype, member), new InputAttribute(member.Name), binSized: true);
enumerable = true;
}
catch (Exception)
{
Pd.AddInput(TransformType(memberType, member), new InputAttribute(member.Name));
SetDefaultValue(member, defaultValue);
enumerable = false;
}
}
else
{
SetDefaultValue(member, defaultValue);
enumerable = false;
}
IsMemberEnumerable.Add(member, enumerable);
IsMemberDictionary.Add(member, dictionary);
}
private void FillObject(PropertyInfo member, T behav, int i)
{
if (IsMemberDictionary[member])
{
if (!(member.GetValue(behav) is IDictionary dict))
{
return;
}
var keyspread = (ISpread)Pd.InputPins[member.Name + " Keys"].Spread[i];
var valuespread = (ISpread)Pd.InputPins[member.Name + " Values"].Spread[i];
dict.Clear();
for (int j = 0; j < keyspread.SliceCount; j++)
{
dict.Add(keyspread[j], valuespread[j]);
}
}
else if (IsMemberEnumerable[member])
{
if (!(member.GetValue(behav) is IEnumerable enumerable))
{
return;
}
var spread = (ISpread)Pd.InputPins[member.Name].Spread[i];
ClearEnumerable(enumerable, member, i);
for (int j = 0; j < spread.SliceCount; j++)
{
AddToEnumerable(enumerable, spread[j], member, i);
}
}
else
{
member.SetValue(behav, TransformInput(Pd.InputPins[member.Name].Spread[i], member, i));
}
}
public void OnImportsSatisfied()
{
Pd = new PinDictionary(FIOFactory);
CType = typeof(T);
OnImportsSatisfiedBegin();
foreach (var prop in CType.GetProperties().Where(p => p.CanWrite))
AddMemberPin(prop);
OnImportsSatisfiedEnd();
}
public void Evaluate(int SpreadMax)
{
OnEvaluateBegin();
if (Pd.InputSpreadMin == 0)
{
FOutput.SliceCount = 0;
OnEvaluateEnd();
return;
}
if (Pd.InputPins.Values.Select(p => p.Spread).Any(o => o == null))
{
OnEvaluateEnd();
return;
}
FOutput.Stream.IsChanged = false;
if (Pd.InputChanged)
{
OnChangedBegin();
var sprmax = Pd.InputSpreadMax;
FOutput.SliceCount = sprmax;
for (int i = 0; i < sprmax; i++)
{
if (FOutput[i] == null) FOutput[i] = new T();
foreach (var member in IsMemberEnumerable.Keys)
FillObject(member, FOutput[i], i);
}
FOutput.Stream.IsChanged = true;
OnChangedEnd();
}
OnEvaluateEnd();
}
}
}