-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextNode.cs
283 lines (240 loc) · 9.59 KB
/
ContextNode.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
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using md.stdl.Boolean;
using md.stdl.Interaction;
using Notui;
using md.stdl.Mathematics;
using mp.pddn;
using VVVV.DX11.Nodes.Renderers.Graphics.Touch;
using VVVV.PluginInterfaces.V1;
using VVVV.PluginInterfaces.V2;
using VVVV.Utils.IO;
using VVVV.Utils.VMath;
using VMatrix = VVVV.Utils.VMath.Matrix4x4;
using SMatrix = System.Numerics.Matrix4x4;
namespace Notuiv
{
[PluginInfo(
Name = "Context",
Category = "Notui",
Author = "microdee",
AutoEvaluate = true
)]
public class ContextNode : IPluginEvaluate, IPartImportsSatisfiedNotification, IPluginFeedbackLoop
{
[Import] public IPluginHost2 PluginHost;
[Import] public IHDEHost Host;
[Config("Thread Count", DefaultValue = 4, IsSingle = true)]
public IDiffSpread<int> ThreadsIn;
[Input("Element Prototypes")]
public Pin<ElementPrototype> ElementsIn;
[Input("Force Update Elements", IsSingle = true, IsBang = true)]
public ISpread<bool> ForceUpdateIn;
private Spread<ElementPrototype> _prevElements = new Spread<ElementPrototype>();
[Input("Mouse", IsSingle = true)]
public Pin<Mouse> MouseIn;
[Input("Mouse Touch ID", DefaultValue = -1, IsSingle = true)]
public ISpread<int> MouseIdIn;
[Input("Mouse Always Present", IsSingle = true)]
public ISpread<bool> AlwaysPresentIn;
[Input("Mouse Pressed Force", DefaultValue = 1.0, IsSingle = true)]
public ISpread<float> MouseForceIn;
[Input("DX11 Touches")]
public Pin<TouchData> Dx11TouchesIn;
[Input("DX11 Touch Force", DefaultValue = 1.0, IsSingle = true)]
public ISpread<float> Dx11TouchForceIn;
[Input("Aux Touches ", DimensionNames = new []{"X", "Y", "F", "Id"})]
public IDiffSpread<Vector4D> AuxTouchesIn;
[Input("Minimum Force for Interaction", DefaultValue = 0.5, IsSingle = true)]
public IDiffSpread<float> MinForceIn;
[Input("Consider Touch New Before", DefaultValue = 1, IsSingle = true)]
public ISpread<int> ConsiderNewIn;
[Input("Consider Touch Released After", DefaultValue = 1, IsSingle = true)]
public ISpread<int> ConsiderReleasedIn;
[Input("View", IsSingle = true)]
public Pin<VMatrix> ViewTrIn;
[Input("Projection", IsSingle = true)]
public Pin<VMatrix> ProjTrIn;
[Input("Aspect Ratio", IsSingle = true)]
public Pin<VMatrix> AspTrIn;
[Output("Context")]
public ISpread<NotuiContext> ContextOut;
[Output("Hierarchical Elements")]
public ISpread<NotuiElement> ElementsOut;
[Output("Flat Elements")]
public ISpread<NotuiElement> FlatElementsOut;
[Output("Touches")]
public ISpread<Touch> TouchesOut;
public NotuiContext Context = new NotuiContext
{
UpdateOnlyChangeFlagged = true
};
private double _prevFrameTime = 0;
private bool _initMo = true;
private int _areElementsChanged = 0;
public bool IsTouchDefault()
{
bool def = AuxTouchesIn.SliceCount == 1;
def = def && AuxTouchesIn[0].xy.Length < 0.00001;
def = def && Math.Abs(AuxTouchesIn[0].w) < 0.00001;
def = def && Math.Abs(AuxTouchesIn[0].z) < 0.00001;
return def;
}
public void OnImportsSatisfied()
{
//FElements.Disconnected += (sender, args) =>
//{
// Context.AddOrUpdateElements(true); // this is basically asking all elements to request their deletion
//};
}
/* Button order in rawinput apparently:
* Right, Left, X1, X2, Middle
* 2, 0, 3, 4, 1
* 1, 4, 0, 2, 3
*/
public void Evaluate(int SpreadMax)
{
Context.ConsiderNewBefore = ConsiderNewIn.TryGetSlice(0);
Context.ConsiderReleasedAfter = ConsiderReleasedIn.TryGetSlice(0);
Context.MinimumForce = MinForceIn.TryGetSlice(0);
if (ThreadsIn.IsChanged) Context.ParallelThreads = ThreadsIn.TryGetSlice(0);
var touchcount = AuxTouchesIn.SliceCount;
var touches = Enumerable.Empty<(Vector2, int, float)>();
touches = touches.Concat(Enumerable.Range(0, Dx11TouchesIn.SliceCount).Where(i => Dx11TouchesIn[i] != null).Select(i =>
{
var touch = Dx11TouchesIn[i];
var pos = new Vector2(touch.Pos.X, touch.Pos.Y);
return (pos, touch.Id, Dx11TouchForceIn[i]);
}));
if (!IsTouchDefault())
{
touches = touches.Concat(Enumerable.Range(0, touchcount).Select(i =>
(AuxTouchesIn[i].xy.AsSystemVector(), (int)AuxTouchesIn[i].w, (float)AuxTouchesIn[i].z)));
}
Context.MouseAlwaysPresent = AlwaysPresentIn.TryGetSlice(0);
Context.MouseTouchForce = MouseForceIn.TryGetSlice(0);
Context.MouseTouchId = MouseIdIn.TryGetSlice(0);
if (MouseIn.IsConnected && MouseIn.SliceCount > 0)
{
if (MouseIn[0] != null)
{
if (_initMo)
{
Context.SubmitMouse(MouseIn[0]);
_initMo = false;
}
}
else
{
if (!_initMo)
{
Context.DetachMouse();
_initMo = true;
}
}
}
else
{
if (!_initMo)
{
Context.DetachMouse();
_initMo = true;
}
}
Context.SubmitTouches(touches);
var dt = Host.FrameTime - _prevFrameTime;
if (_prevFrameTime <= 0.00001) dt = 0;
if(ViewTrIn.IsConnected && ViewTrIn.SliceCount > 0)
Context.View = ViewTrIn[0].AsSystemMatrix4X4();
else Context.View = SMatrix.Identity;
if (ProjTrIn.IsConnected && ProjTrIn.SliceCount > 0)
Context.Projection = ProjTrIn[0].AsSystemMatrix4X4();
else Context.Projection = SMatrix.Identity;
if (AspTrIn.IsConnected && AspTrIn.SliceCount > 0)
Context.AspectRatio = AspTrIn[0].AsSystemMatrix4X4();
else Context.AspectRatio = SMatrix.Identity;
if (ElementsIn.IsChanged)
{
for (int i = 0; i < ElementsIn.SliceCount; i++)
{
if (ElementsIn[i] == null) continue;
if (_prevElements.Contains(ElementsIn[i])) continue;
ElementsIn[i].IsChanged = ElementNodeUtils.ChangedFrames;
}
_prevElements.AssignFrom(ElementsIn);
_areElementsChanged = 2;
}
if (_areElementsChanged > 0 || ForceUpdateIn.TryGetSlice(0))
{
Context.AddOrUpdateElements(true, ElementsIn.Where(el => el != null).ToArray());
_areElementsChanged--;
}
Context.Mainloop((float)dt);
ContextOut[0] = Context;
FlatElementsOut.SliceCount = Context.FlatElements.Count;
for (int i = 0; i < Context.FlatElements.Count; i++)
{
var element = Context.FlatElements[i];
FlatElementsOut[i] = element;
switch (element.EnvironmentObject)
{
case null:
element.EnvironmentObject = new VEnvironmentData(element);
break;
case VEnvironmentData venvdat:
if (venvdat.FlattenedEvents != null) continue;
venvdat.FlattenedEvents = new ElementEventFlattener(Host);
venvdat.FlattenedEvents.Subscribe(element);
break;
}
}
ElementsOut.AssignFrom(Context.RootElements.Values);
TouchesOut.AssignFrom(Context.Touches.Values);
_prevFrameTime = Host.FrameTime;
}
public bool OutputRequiresInputEvaluation(IPluginIO inputPin, IPluginIO outputPin)
{
return false;
}
}
[PluginInfo(
Name = "Context",
Category = "Notui",
Version = "Split",
Author = "microdee"
)]
public class ContextSplitNode : ObjectSplitNode<NotuiContext>
{
public override Type TransformType(Type original, MemberInfo member)
{
return MiscExtensions.MapSystemNumericsTypeToVVVV(original);
}
public override object TransformOutput(object obj, MemberInfo member, int i)
{
return MiscExtensions.MapSystemNumericsValueToVVVV(obj);
}
}
[PluginInfo(
Name = "Touch",
Category = "Notui",
Version = "Split",
Author = "microdee"
)]
public class TouchSplitNode : ObjectSplitNode<Touch>
{
public override Type TransformType(Type original, MemberInfo member)
{
return MiscExtensions.MapSystemNumericsTypeToVVVV(original);
}
public override object TransformOutput(object obj, MemberInfo member, int i)
{
return MiscExtensions.MapSystemNumericsValueToVVVV(obj);
}
}
}