-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotuiUtils.cs
239 lines (215 loc) · 10.4 KB
/
NotuiUtils.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
using System;
using System.Collections.Generic;
using System.Linq;
using md.stdl.Interaction;
using md.stdl.Interfaces;
using Notui;
using md.stdl.Mathematics;
using VVVV.PluginInterfaces.V2;
using VVVV.Utils.IO;
using VVVV.Utils.Reflection;
using VVVV.Utils.VMath;
namespace Notuiv
{
public class EventBangs : IUpdateable<EventBangs>
{
public bool OnInteractionBegin { get; set; }
public bool OnInteractionEnd { get; set; }
public bool OnTouchBegin { get; set; }
public bool OnTouchEnd { get; set; }
public bool OnHitBegin { get; set; }
public bool OnHitEnd { get; set; }
public bool OnInteracting { get; set; }
public bool OnChildrenUpdated { get; set; }
public bool OnDeletionStarted { get; set; }
public bool OnDeleting { get; set; }
public bool OnFadedIn { get; set; }
public uint MouseButtonsPressed { get; set; }
public int HorizontalWheel { get; set; }
public int VerticalWheel { get; set; }
public void UpdateFrom(EventBangs other)
{
OnInteractionBegin = other.OnInteractionBegin;
OnInteractionEnd = other.OnInteractionEnd;
OnTouchBegin = other.OnTouchBegin;
OnTouchEnd = other.OnTouchEnd;
OnHitBegin = other.OnHitBegin;
OnHitEnd = other.OnHitEnd;
OnInteracting = other.OnInteracting;
OnChildrenUpdated = other.OnChildrenUpdated;
OnDeletionStarted = other.OnDeletionStarted;
OnDeleting = other.OnDeleting;
OnFadedIn = other.OnFadedIn;
MouseButtonsPressed = other.MouseButtonsPressed;
HorizontalWheel = other.HorizontalWheel;
VerticalWheel = other.VerticalWheel;
}
}
public class ElementEventFlattener
{
public EventBangs CurrentFrame { get; } = new EventBangs();
public EventBangs PreviousFrame { get; } = new EventBangs();
private void _OnInteractionBeginHandler(object sender, TouchInteractionEventArgs e) => CurrentFrame.OnInteractionBegin = true;
private void _OnInteractionEndHandler(object sender, TouchInteractionEventArgs e) => CurrentFrame.OnInteractionEnd = true;
private void _OnTouchBeginHandler(object sender, TouchInteractionEventArgs e) => CurrentFrame.OnTouchBegin = true;
private void _OnTouchEndHandler(object sender, TouchInteractionEventArgs e) => CurrentFrame.OnTouchEnd = true;
private void _OnHitBeginHandler(object sender, TouchInteractionEventArgs e) => CurrentFrame.OnHitBegin = true;
private void _OnHitEndHandler(object sender, TouchInteractionEventArgs e) => CurrentFrame.OnHitEnd = true;
private void _OnInteractingHandler(object sender, EventArgs e) => CurrentFrame.OnInteracting = true;
private void _OnChildrenUpdatedHandler(object sender, ChildrenUpdatedEventArgs e) => CurrentFrame.OnChildrenUpdated = true;
private void _OnDeletionStartedHandler(object sender, EventArgs e) => CurrentFrame.OnDeletionStarted = true;
private void _OnDeletingHandler(object sender, EventArgs e) => CurrentFrame.OnDeleting = true;
private void _OnFadedInHandler(object sender, EventArgs e) => CurrentFrame.OnFadedIn = true;
private void _OnMouseButtonPressed(object sender, MouseInteractionEventArgs e)
{
CurrentFrame.MouseButtonsPressed = CurrentFrame.MouseButtonsPressed | (uint)e.Buttons;
}
private void _OnMouseButtonReleased(object sender, MouseInteractionEventArgs e)
{
CurrentFrame.MouseButtonsPressed = CurrentFrame.MouseButtonsPressed & ~(uint)e.Buttons;
}
private void _OnHorizontalMouseWheelChange(object sender, MouseInteractionEventArgs e)
{
CurrentFrame.HorizontalWheel = e.Touch.MouseDelta.AccumulatedHorizontalWheelDelta;
}
private void _OnVerticalMouseWheelChange(object sender, MouseInteractionEventArgs e)
{
CurrentFrame.VerticalWheel = e.Touch.MouseDelta.AccumulatedWheelDelta;
}
public void Subscribe(NotuiElement element)
{
try
{
element.OnInteractionBegin -= _OnInteractionBeginHandler;
element.OnInteractionEnd -= _OnInteractionEndHandler;
element.OnTouchBegin -= _OnTouchBeginHandler;
element.OnTouchEnd -= _OnTouchEndHandler;
element.OnHitBegin -= _OnHitBeginHandler;
element.OnHitEnd -= _OnHitEndHandler;
element.OnInteracting -= _OnInteractingHandler;
element.OnChildrenUpdated -= _OnChildrenUpdatedHandler;
element.OnDeletionStarted -= _OnDeletionStartedHandler;
element.OnDeleting -= _OnDeletingHandler;
element.OnFadedIn -= _OnFadedInHandler;
element.OnMouseButtonPressed -= _OnMouseButtonPressed;
element.OnMouseButtonReleased -= _OnMouseButtonReleased;
element.OnHorizontalMouseWheelChange -= _OnHorizontalMouseWheelChange;
element.OnVerticalMouseWheelChange -= _OnVerticalMouseWheelChange;
}
catch { }
element.OnInteractionBegin += _OnInteractionBeginHandler;
element.OnInteractionEnd += _OnInteractionEndHandler;
element.OnTouchBegin += _OnTouchBeginHandler;
element.OnTouchEnd += _OnTouchEndHandler;
element.OnHitBegin += _OnHitBeginHandler;
element.OnHitEnd += _OnHitEndHandler;
element.OnInteracting += _OnInteractingHandler;
element.OnChildrenUpdated += _OnChildrenUpdatedHandler;
element.OnDeletionStarted += _OnDeletionStartedHandler;
element.OnDeleting += _OnDeletingHandler;
element.OnFadedIn += _OnFadedInHandler;
element.OnMouseButtonPressed += _OnMouseButtonPressed;
element.OnMouseButtonReleased += _OnMouseButtonReleased;
element.OnHorizontalMouseWheelChange += _OnHorizontalMouseWheelChange;
element.OnVerticalMouseWheelChange += _OnVerticalMouseWheelChange;
}
public void Reset()
{
PreviousFrame.UpdateFrom(CurrentFrame);
CurrentFrame.OnInteractionBegin = false;
CurrentFrame.OnInteractionEnd = false;
CurrentFrame.OnTouchBegin = false;
CurrentFrame.OnTouchEnd = false;
CurrentFrame.OnHitBegin = false;
CurrentFrame.OnHitEnd = false;
CurrentFrame.OnInteracting = false;
CurrentFrame.OnChildrenUpdated = false;
CurrentFrame.OnDeletionStarted = false;
CurrentFrame.OnDeleting = false;
CurrentFrame.OnFadedIn = false;
CurrentFrame.HorizontalWheel = 0;
CurrentFrame.VerticalWheel = 0;
}
private IHDEHost _hdeHost;
public ElementEventFlattener(IHDEHost host)
{
_hdeHost = host;
_hdeHost.MainLoop.OnPrepareGraph += (sender, args) => Reset();
}
}
public class VEnvironmentData : AuxiliaryObject
{
public Dictionary<string, object> NodeSpecific { get; set; } = new Dictionary<string, object>();
public ElementEventFlattener FlattenedEvents;
public string TypeCSharpName { get; }
public Spread<Touch> Touches { get; } = new Spread<Touch>();
public Spread<bool> TouchesHitting { get; } = new Spread<bool>();
public Spread<IntersectionPoint> TouchingIntersections { get; } = new Spread<IntersectionPoint>();
public Spread<Touch> HittingTouches { get; } = new Spread<Touch>();
public Spread<IntersectionPoint> HittingIntersections { get; } = new Spread<IntersectionPoint>();
public Spread<Mouse> Mice { get; } = new Spread<Mouse>();
public Spread<NotuiElement> Children { get; } = new Spread<NotuiElement>();
public Spread<InteractionBehavior> Behaviors { get; } = new Spread<InteractionBehavior>();
public Spread<NotuiElement> Parent { get; } = new Spread<NotuiElement>();
public Matrix4x4 VDispTr { get; private set; }
private readonly NotuiElement _element;
public VEnvironmentData(NotuiElement element)
{
_element = element;
TypeCSharpName = element.GetType().GetCSharpName();
element.OnMainLoopEnd += (sender, args) =>
{
Touches.AssignFrom(_element.Touching.Keys);
TouchesHitting.AssignFrom(_element.Touching.Values.Select(t => t != null));
TouchingIntersections.AssignFrom(_element.Touching.Values.Where(t => t != null));
HittingTouches.AssignFrom(_element.Hitting.Keys);
HittingIntersections.AssignFrom(_element.Hitting.Values);
Mice.AssignFrom(_element.Mice.Select(t => t.AttachadMouse));
Children.AssignFrom(_element.Children.Values);
Behaviors.AssignFrom(_element.Behaviors);
if (_element.Parent == null)
Parent.SliceCount = 0;
else
{
Parent.SliceCount = 1;
Parent[0] = element.Parent;
}
VDispTr = _element.DisplayMatrix.AsVMatrix4X4();
};
}
public override IAuxiliaryObject Copy()
{
return new VEnvironmentData(_element);
}
public override void UpdateFrom(IAuxiliaryObject other)
{
if (other is VEnvironmentData venvdat)
{
NodeSpecific = venvdat.NodeSpecific;
}
}
}
public static class NotuiUtils
{
public static void AttachManagementObject(this NotuiElement element, string nodepath, object obj)
{
if (element.EnvironmentObject == null)
element.EnvironmentObject = new VEnvironmentData(element);
if (element.EnvironmentObject is VEnvironmentData venvdat)
{
if (venvdat.NodeSpecific.ContainsKey(nodepath))
{
venvdat.NodeSpecific[nodepath] = obj;
}
else
{
venvdat.NodeSpecific.Add(nodepath, obj);
}
}
}
public static void AttachSliceId(this NotuiElement element, string nodepath, int id)
{
element.AttachManagementObject(nodepath, id);
}
}
}