-
Notifications
You must be signed in to change notification settings - Fork 2
/
AutomationGui.sc
387 lines (328 loc) · 12 KB
/
AutomationGui.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
Automation is a SuperCollider Quark.
(c) 2009 Neels J. Hofmeyr <neels@hofmeyr.de>, GNU GPL v3 or later.
Automation allows to record and playback live changes made to
other GUI elements in particular, or anything else you wish to
setup.
This is AutomationGui, the class of the Automation Quark that provides
the transport control GUI to interface with the Automation class.
This can be setup automatically by calling Automation.gui.
See: Automation
*/
AutomationGui {
var <>automation,
<win, bounds,
playBtn, rewindBtn, recordBtn, snapshotBtn, saveBtn, loadBtn,
timeSlider, timeNumberBox,
doUpdateTimeSlider = true,
doUpdateTimeNumber = true;
/*
Description of Parameters
* automation
The Automation instance this is the GUI of.
* win
The window that the transport control GUI was put in.
This is either passed into or automatically created
by the constructor.
*/
/* automation: The Automation instance this is the GUI of.
* win: If nil, opens a new window. Otherwise the
* given GUI.window is used to show the transport GUI.
* bounds: A Rect(x,y,w,h) for size and position of the transport
* GUI.
* If bounds==nil, it will take up the whole window.
* If win==nil, these bounds give the size of the new window.
* If both win and bounds are nil, defaults are used.
*/
*new { |automation, win=nil, bounds=nil|
^super.new.constructor(automation, win, bounds);
}
// administers saving of all clients, first opening a directory
// dialog to choose a location.
saveDialog {|preset=nil|
if (preset == nil){
preset = automation.presetDir;
};
automation.defer{
this.chooseDirectoryDialog(
title:"SAVE: select a directory name",
preset:preset,
onSuccess:{|dir|
automation.save(dir);
});
};
}
// administers loading of all clients, first opening a directory
// dialog to choose a location.
loadDialog {|preset=nil|
if (preset == nil){
preset = automation.presetDir;
};
automation.defer{
this.chooseDirectoryDialog(
title:"LOAD: enter a directory name",
preset:preset,
onSuccess:{|dir|
automation.load(dir);
});
};
}
setPlaying {|val=true|
if (val){
playBtn.value = 1;
}{
playBtn.value = 0;
};
}
/* val == 0 : recording is off (grey)
* val == 1 : recording is ready (orange)
* val == 2 : recording is really happening (red)
*/
setRecording {|val=1|
if ((val == 1) || (val == 2)){
recordBtn.value = val;
}{
recordBtn.value = 0;
};
}
// internal constructor function
constructor { |iautomation, iwin, ibounds|
// `bfoo' means "bounds for the GUI element foo":
var bplay, brew, brec, bsnap, bsave, bload, bslider, bnr;
// evaluate input args
automation = iautomation;
automation.gui = this;
win = iwin;
bounds = ibounds;
// setting up the GUI elements of the transport control.
// did the caller supply a window?
if (win == nil){
// no. make one that suits me.
// if no bounds supplied, invent some:
if (bounds == nil){
bounds = Rect(0, 0, 450, 25);
};
win = GUI.window.new("Automation Control", bounds);
win.front;
// if the window is created by me, take all space
// in the window.
bounds = nil;
};
// simple hack. If the window class' name starts with a
// J as in JSCWindow, we don't need to defer GUI signals.
// see automation.defer().
if (("" ++ win.class)[0] == $J){
automation.doDefer = false;
}{
automation.doDefer = true;
};
// did the caller supply a bounding rectangle?
if (bounds == nil){
// no. take up the whole window.
bounds = Rect(0, 0, win.bounds.width, win.bounds.height);
};
// now the buttons and sliders...
// NOTE: If you add new control elements, be sure to also
// add them in the dock() function that checks for
// own GUI elements.
bplay = bounds.copy;
if (bounds.width > bounds.height) {
bplay.width = min(bplay.height, 0.1 * bounds.width);
}{
bplay.height = min(bplay.width, 0.1 * bounds.height);
};
playBtn = GUI.button.new( win, bplay );
playBtn.states = [[ ">", Color.blue, Color.grey ],
[ "||", Color.white, Color.blue ]];
playBtn.action = {
if (playBtn.value == 1) {
automation.play;
}{
automation.stop;
};
};
brew = bounds.copy;
if (bounds.width > bounds.height) {
brew.left = bplay.left + bplay.width;
brew.width = min(brew.height, 0.1 * bounds.width);
}{
brew.top = bplay.top + bplay.height;
brew.height = min(brew.width, 0.1 * bounds.height);
};
rewindBtn = GUI.button.new( win, brew );
rewindBtn.states = [[ "<<", Color.blue, Color.grey ]];
rewindBtn.action = {
automation.seek;
};
brec = bounds.copy;
if (bounds.width > bounds.height) {
brec.left = brew.left + brew.width;
brec.width = min(brec.height, 0.1 * bounds.width);
}{
brec.top = brew.top + brew.height;
brec.height = min(brec.width, 0.1 * bounds.height);
};
recordBtn = GUI.button.new( win, brec );
recordBtn.states = [["O", Color.red, Color.grey]
,["O", Color.grey, Color.new255(255, 165, 0)]
,["O", Color.white, Color.red]];
recordBtn.action = {
if (recordBtn.value == 1) {
automation.enableRecording;
}{
automation.stopRecording;
};
};
bsnap = bounds.copy;
if (bounds.width > bounds.height) {
bsnap.left = brec.left + brec.width;
bsnap.width = min(bsnap.height, 0.1 * bounds.width);
}{
bsnap.top = brec.top + brec.height;
bsnap.height = min(bsnap.width, 0.1 * bounds.height);
};
if (automation.showSnapshot){
snapshotBtn = GUI.button.new( win, bsnap );
snapshotBtn.states = [["[ô]", Color.black, Color.grey],
["[ô]", Color.white, Color.red]];
snapshotBtn.action = {
automation.snapshot;
{
snapshotBtn.value = 0;
}.defer(0.2);
};
};
bsave = bounds.copy;
if (bounds.width > bounds.height) {
if(automation.showSnapshot){
bsave.left = bsnap.left + bsnap.width;
} {
bsave.left = brec.left + brec.width;
};
bsave.width = min(bsave.height, 0.1 * bounds.width);
}{
bsave.top = bsnap.top + bsnap.height;
bsave.height = min(bsave.width, 0.1 * bounds.height);
};
if(automation.showLoadSave){
saveBtn = GUI.button.new( win, bsave );
saveBtn.states = [[ "^", Color.white, Color.grey ]];
saveBtn.action = {
this.saveDialog;
};
};
bload = bounds.copy;
if (bounds.width > bounds.height) {
bload.left = bsave.left + bsave.width;
bload.width = min(bload.height, 0.1 * bounds.width);
}{
bload.top = bsave.top + bsave.height;
bload.height = min(bload.width, 0.1 * bounds.height);
};
if(automation.showLoadSave){
loadBtn = GUI.button.new( win, bload );
loadBtn.states = [[ "...", Color.white, Color.grey ]];
loadBtn.action = {
this.loadDialog;
};
};
// number field goes to the end
bnr = bounds.copy;
if (bounds.width > bounds.height) {
bnr.width = min(2 * bnr.height, 0.2 * bounds.width);
bnr.left = bounds.left + bounds.width - bnr.width;
}{
bnr.height = min(bnr.width, 0.2 * bounds.height);
bnr.top = bounds.top + bounds.height - bnr.height;
};
timeNumberBox = GUI.numberBox.new(win, bnr);
timeNumberBox.value = 0.0;
timeNumberBox.action = {
automation.seek(max(0, timeNumberBox.value));
};
// slider goes in-between
bslider = bounds.copy;
if (bounds.width > bounds.height) {
if(automation.showLoadSave){
bslider.left = bload.left + bload.width;
} {
if(automation.showSnapshot){
bslider.left = bsnap.left + bsnap.width;
} {
bslider.left = brec.left + brec.width;
};
};
bslider.width = bnr.left - bslider.left;
}{
bslider.top = bload.top + bload.height;
bslider.height = bnr.top - bslider.top;
};
timeSlider = GUI.slider.new(win, bslider);
timeSlider.action = {
automation.seek(timeSlider.value * automation.length);
};
// Some signals that prevent automatic updating of
// controls while the user is busy modifying them.
timeSlider.addAction({
doUpdateTimeSlider = false;
}, \mouseDownAction);
timeSlider.addAction({
doUpdateTimeSlider = true;
automation.defer{this.updateTimeGUI(automation.now)};
}, \mouseUpAction);
timeNumberBox.addAction({
doUpdateTimeNumber = doUpdateTimeNumber.not;
}, \mouseDownAction);
} // constructor()
// private function that ensures waking up from blocking
// changes on the time slider or time numberbox.
unblock {|number=true, slider=false|
if (number) { doUpdateTimeNumber = true; };
if (slider) { doUpdateTimeSlider = true; };
}
// update both time slider and the time number box.
updateTimeGUI{|time|
if (doUpdateTimeSlider){
timeSlider.value = (time / automation.length);
};
if (doUpdateTimeNumber){
timeNumberBox.value = floor(time);
};
}
// opens a really simple dialog window to enter a string.
chooseDirectoryDialog {|title="Select Directory",
onSuccess, onFailure=nil,
preset=nil, bounds=nil|
automation.defer{
var dwin, textField, success=false;
if (bounds == nil) { bounds = Rect(100,300,300,30); };
if (preset == nil) { preset = "HOME".getenv ++ "/automation/"; };
dwin = GUI.window.new(title, bounds);
dwin.onClose = {
if (success.not){
onFailure.value(textField.value);
};
};
textField = GUI.textField.new(dwin, Rect(0,0,bounds.width,bounds.height));
textField.value = preset;
textField.action = {
success = true;
onSuccess.value(textField.value);
dwin.close;
};
dwin.front;
};
}
isMyGuiElement {|guiElement|
^ (
(guiElement == playBtn) ||
(guiElement == rewindBtn) ||
(guiElement == recordBtn) ||
(guiElement == snapshotBtn) ||
(guiElement == saveBtn) ||
(guiElement == loadBtn) ||
(guiElement == timeSlider) ||
(guiElement == timeNumberBox)
);
}
}