-
Notifications
You must be signed in to change notification settings - Fork 3
/
OscInputNode.cs
129 lines (103 loc) · 3.94 KB
/
OscInputNode.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
using OscCore;
using System;
using Warudo.Core.Attributes;
using Warudo.Core.Data;
using Warudo.Core.Graphs;
[NodeType(Id = "com.barinzaya.oscinput.nodes.oscmessage", Title = "On OSC Message", Category = "External Integration")]
public class OscInputNode : Node {
public new OscInputPlugin Plugin => base.Plugin as OscInputPlugin;
[DataInput] public string Address = "/value";
[Label("Arguments")]
[DataInput] public OscInputType[] ArgumentTypes = new OscInputType[0];
[FlowOutput] public Continuation Exit;
private object[] values;
protected override void OnCreate() {
base.OnCreate();
ConfigureOutputs(null, ArgumentTypes);
Watch<OscInputType[]>(nameof(ArgumentTypes), (oldArgs, newArgs) => ConfigureOutputs(oldArgs, newArgs));
Plugin?.AddHandler(Address, OnReceivedOscMessage);
Watch<string>(nameof(Address), (oldAddress, newAddress) => {
Plugin?.RemoveHandler(oldAddress, OnReceivedOscMessage);
Plugin?.AddHandler(newAddress, OnReceivedOscMessage);
});
}
protected override void OnDestroy() {
Plugin?.RemoveHandler(Address, OnReceivedOscMessage);
base.OnDestroy();
}
protected void OnReceivedOscMessage(OscMessage message) {
var numArgs = Math.Min(message.Count, values.Length);
for (var i = 0; i < numArgs; i++) {
var value = message[i];
values[i] = ArgumentTypes[i] switch {
OscInputType.Boolean => value switch {
bool x => x,
double x => x != 0,
float x => x != 0,
int x => x != 0,
long x => x != 0,
_ => value
},
OscInputType.Float => value switch {
bool x => x ? 1f : 0f,
double x => (float)x,
float x => x,
int x => (float)x,
long x => (float)x,
_ => value
},
OscInputType.Int => value switch {
bool x => x ? 1 : 0,
double x => (int)Math.Round(x),
float x => (int)Math.Round(x),
int x => x,
long x => (int)x,
_ => value
},
OscInputType.String => value switch {
string x => x,
_ => value
},
_ => throw new ArgumentException("Unsupported argument type", $"ArgumentTypes[{i}]")
};
}
Graph?.InvokeFlow(this, "Exit");
}
private void ConfigureOutputs(OscInputType[] oldArgs, OscInputType[] newArgs) {
var oldCount = oldArgs?.Length ?? 0;
var newCount = newArgs?.Length ?? 0;
if (newCount != oldCount) {
Array.Resize(ref values, newCount);
}
for (var i = newCount; i < oldCount; i++) {
DataOutputPortCollection.RemovePort($"Arg{i+1}");
}
for (var i = 0; i < newCount; i++) {
if (i < oldCount && oldArgs[i] == newArgs[i]) {
continue;
}
var name = $"Arg{i+1}";
DataOutputPortCollection.RemovePort(name);
object value = ArgumentTypes[i] switch {
OscInputType.Boolean => false,
OscInputType.Float => 0f,
OscInputType.Int => 0,
OscInputType.String => "",
_ => throw new ArgumentException("Unhandled input type", $"ArgumentTypes[{i}]")
};
values[i] = value;
var j = i;
DataOutputPortCollection.AddPort(new DataOutputPort(name, value.GetType(), () => values[j], new DataOutputProperties {
label = name,
order = i,
}));
}
Broadcast();
}
}
public enum OscInputType {
Boolean,
Float,
Int,
String,
}