-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHadronConManager.sc
233 lines (191 loc) · 5.64 KB
/
HadronConManager.sc
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
HadronConManager
{
var areArgsGood;
*new
{|argSourcePlug, argTargetPlug|
^super.new.init(argSourcePlug, argTargetPlug);
}
init
{|argSourcePlug, argTargetPlug|
var win, field, preBakedStr = "", tempInIndexes = List.new, tempOutIndexes = List.new,
isConnectedResponse, isSourceMono, isTargetMono, sourceFreeOut, targetFreeIn;
isConnectedResponse = this.isConnectedBetween(argSourcePlug, argTargetPlug);
isSourceMono = if(argSourcePlug.outConnections.size < 2, { true; }, { false; });
isTargetMono = if(argTargetPlug.inConnections.size < 2, { true; }, { false; });
sourceFreeOut = this.findStereoFreeSlot(argSourcePlug, \out);
targetFreeIn = this.findStereoFreeSlot(argTargetPlug, \in);
//connection prediction
if(argSourcePlug === argTargetPlug, //if there is one source
{
if(isConnectedResponse[0] == true, //is the plugs outs are connected to its own inputs, show their disconnection string
{
isConnectedResponse[1].do
({|item|
preBakedStr = preBakedStr ++ (item + 1).asString ++ ",0,";
});
preBakedStr = preBakedStr[0..(preBakedStr.size - 2)];
},
{ //if connected to stg else, propose disconnectiong all
argSourcePlug.outConnections.do
({|item, cnt|
if(item != [nil, nil],
{
tempOutIndexes.add(cnt+1);
});
});
if(tempOutIndexes.size > 0,
{
tempOutIndexes.do
({|item|
preBakedStr = preBakedStr ++ item.asString ++ ",0,";
});
});
preBakedStr = preBakedStr[0..(preBakedStr.size - 2)];
});
},
{ //is source and target is different
if(isConnectedResponse[0] == true, //if there are connections between source and target, propose disconnecting them
{
isConnectedResponse[1].do
({|item|
preBakedStr = preBakedStr ++ (item + 1).asString ++ ",0,";
});
preBakedStr = preBakedStr[0..(preBakedStr.size - 2)];
},
{
//if there are no connections between source and target (then we are in this func),
//propose to connect first consecutively free two outlets
//to first consecutively free two outlets, if there is only
//one outlet of source, propose to connect it to the first odd
//inlet of the target (also if the next one is free in the target)
if((sourceFreeOut == [nil, nil]) or: { targetFreeIn == [nil, nil] },
{
preBakedStr = "";
},
{
if(sourceFreeOut[1] == nil,
{
preBakedStr = (sourceFreeOut[0] + 1).asString ++ "," ++ (targetFreeIn[0] + 1).asString;
},
{
if(targetFreeIn[1] == nil,
{
preBakedStr = (sourceFreeOut[0] + 1).asString ++ "," ++ (targetFreeIn[0] + 1).asString;
},
{
preBakedStr =
(sourceFreeOut[0] + 1).asString ++ "," ++ (targetFreeIn[0] + 1).asString ++ "," ++
(sourceFreeOut[1] + 1).asString ++ "," ++ (targetFreeIn[1] + 1).asString;
});
});
});
});
});
//create window
win = Window("Comma separated \"in,out\" pairs:", Rect(400, 400, 300, 50));
field = TextField(win, Rect(10, 20, 280, 20)).string_(preBakedStr)
.action_
({|fld|
var pairs;
if(fld.value.size != 0,
{
pairs = fld.value.replace(" ", "").split($,).collect(_.asInteger).clump(2);
//check for bounds
pairs.do
({|item|
//no need to subtract values for .size, they are already +1
if(item[1] > argTargetPlug.inBusses.size or: { item[1] < 0 },
{
areArgsGood = false;
});
if(item[0] > argSourcePlug.outBusses.size or: { item[0] < 1 },
{
areArgsGood = false;
});
});
if(areArgsGood,
{
pairs.do
({|item|
if(item[1] == 0,
{
argSourcePlug.setOutBus(nil, nil, item[0] - 1); //setOutBus knows how to handle this.
},
{
argSourcePlug.setOutBus(argTargetPlug, item[1] - 1, item[0] - 1);
});
});
win.close;
argSourcePlug.parentApp.alivePlugs.do({|plug| if(plug.conWindow.isClosed.not, { plug.refreshConWindow; }) });
argSourcePlug.parentApp.displayStatus("Right click on canvas to add plugins. Shift+click on a plugin to make connections", 0);
},
{
argSourcePlug.parentApp.displayStatus("One of the indexes are out of bounds...", -1);
areArgsGood = true;
});
});
})
.focus(true);
areArgsGood = true; //ahh... good intentions...
argSourcePlug.parentApp.displayStatus("Enter input output pairs, comma separated. Ex: 1,1,2,2", 0);
win.front;
}
isConnectedBetween
{|argSource, argTarget|
var connected = false;
var conPoints = List.new;
argSource.outConnections.do
({|item, cnt|
if(item[0] === argTarget,
{
connected = true;
conPoints.add(cnt);
});
});
^[connected, conPoints];
}
findStereoFreeSlot
{|argPlug, argDirection| //direction is \in or \out
var isLastFree = false;
if(argDirection == \out,
{
if(argPlug.outConnections.size == 1,
{
if(argPlug.outConnections[0] == [nil, nil], { ^[0, nil]; }, { ^[nil, nil] });
},
{
argPlug.outConnections.do
({|item, cnt|
if(item == [nil, nil],
{
if(isLastFree, { if(cnt.odd, { ^[cnt - 1, cnt] }); });
isLastFree = true;
},
{
isLastFree = false;
});
});
});
},
{ //if direction is \in
if(argPlug.inConnections.size == 1,
{
if(argPlug.inConnections[0] == [nil, nil], { ^[0, nil]; }, { ^[nil, nil] });
},
{
argPlug.inConnections.do
({|item, cnt|
if(item == [nil, nil],
{
if(isLastFree, { if(cnt.odd, { ^[cnt - 1, cnt] }); });
isLastFree = true;
},
{
isLastFree = false;
});
});
});
});
^[nil, nil];
}
}