scide: GenericCodeEditor - accept drag/drop of text/plain
[supercollider.git] / SCClassLibrary / JITLib / GUI / ProxyMonitorGui.sc
blobc59b6cf8854defe084962f0af73e7bf09bdf572e
1 ProxyMonitorGui {
2         classvar <>lastOutBus = 99;
4         var <proxy, <usesPlayN = false, <usesName, <usesPausSend;
5         var <win, <zone, <flow;
6         var <ampSl, <playBut, <nameView, <setOutBox, <playNDialogBut, <pauseBut, <sendBut;
7         var <skipjack, <oldState = #[];
9         *new { |proxy, w, bounds, showLevel=false, showPlayN=true, showName=true, showPauseSend = true,
10                 makeWatcher=true, skin|
12                 ^super.new.init(w, bounds, showLevel, showPlayN, showName, showPauseSend, makeWatcher, skin)
13                         .proxy_(proxy);
14         }
15         proxy_ { |inproxy|
16                 if (proxy.isNil or: proxy.isKindOf(NodeProxy)) {
17                         proxy = inproxy;
18                         this.updateAll;
19                 } {
20                         warn("ProxyMonitorGui: % is not a nodeproxy.".format(inproxy))
21                 };
22         }
24         init { |w, bounds, showLevel, showPlayN, showName, showPauseSend, makeWatcher, skin|
25                 var font;
26                 var nameWid = 60;
27                 var playWid = 34;
28                 var outWid = 20;
29                 var playNWid = 20;
30                 var pausSendWid = playWid * 2;
31                 var defaultBounds = 400@20;
32                 var height;
33                 var widthSum, sliderWidth, winBounds, viewBounds;
35                 skin = skin ? GUI.skins[\jitSmall];
36                 font = GUI.font.new(*skin.fontSpecs);
38                 usesPlayN = showPlayN;
39                 usesName = showName;
40                 usesPausSend = showPauseSend;
41                 bounds = bounds ? defaultBounds;
43                 if (w.notNil) {
44                         win = w;
45                         viewBounds = bounds ? defaultBounds;
46                 } {
47                         viewBounds = bounds;
48                         if (bounds.isKindOf(Point)) {
49                                 winBounds = Rect(80,400,0,0).setExtent(bounds.x, bounds.y);
50                         } {
51                                 winBounds = bounds;
52                         };
53                 //      [\winBounds, winBounds, \viewBounds,    viewBounds].postln;
54                         win = Window(this.class.name.asString, winBounds, false).front;
55                 };
58                 zone = GUI.compositeView.new(win, viewBounds);
59                 zone.background_(skin.foreground);
60                 flow = FlowLayout(zone.bounds, 0@0, 0@0);
61                 zone.decorator = flow;
62                 zone.resize_(2);
64                 if (viewBounds.isKindOf(Rect)) { viewBounds = viewBounds.extent };
66         //      [\bounds, bounds, \winBounds, winBounds, \viewBounds, viewBounds].postln;
67                 widthSum =
68                         (showName.binaryValue * nameWid)
69                         + playWid + outWid
70                         + (showPlayN.binaryValue * playNWid)
71                         + (showPauseSend.binaryValue * pausSendWid);
72                 sliderWidth = viewBounds.x - widthSum;
74                 height = viewBounds.y;
76                 ampSl = EZSlider(zone, (sliderWidth @ height), \vol, \db,
77                         { arg slid;
78                                 if(proxy.notNil) {
79                                         proxy.vol_(slid.value.dbamp);
80                                 }
81                         }, 0, false,
82                         labelWidth: showLevel.binaryValue * 20,
83                         numberWidth: showLevel.binaryValue * 40);
84                 ampSl.labelView.font_(font).align_(0);
85                 ampSl.view.resize_(2);
88                         // should have four states: ...
90                 playBut = Button(zone, Rect(0,0,playWid, height))
91                         .font_(font).resize_(3)
92                         .states_([
93                                 [ if (usesPlayN, \playN, \play), skin.fontColor, skin.offColor],
94                                 [ \stop, skin.fontColor, skin.onColor ]
95                         ]);
97                 playBut.action_({ arg btn, modif;
98                         if(proxy.notNil) {
99                                 [       {       if (modif.isAlt) { proxy.end } { proxy.stop }; },
100                                         {       if (modif.isAlt) { proxy.vol_(0) };
101                                                 if (usesPlayN) { proxy.playN } { proxy.play }
102                                         }
103                                 ].at(btn.value).value
104                         }
105                 });
107                 setOutBox = EZNumber(zone, outWid@height, nil, [0, lastOutBus, \lin, 1],
108                         { |box, mod|
109                                 if (proxy.notNil) {
110                                         if (proxy.monitor.isNil) {
111                                                 "ProxyMonitorGui - cant set outs yet.".postln
112                                         } {
113                                                 proxy.monitor.out_(box.value.asInteger);
114                                         };
115                                 };
116                         }, 0);
118                 setOutBox.view.resize_(3);
119                 setOutBox.numberView.font_(font).align_(0);
121                 if (usesPlayN) {
122                         playNDialogBut = GUI.button.new(zone, Rect(0,0, playNWid, height))
123                                 .font_(font).resize_(3)
124                                 .states_([
125                                         ["-=", skin.fontColor, skin.offColor],
126                                         ["-<", skin.fontColor, skin.onColor]
127                                 ])
128                                 .action_({ |box, mod|
129                                         if (proxy.notNil) { proxy.playNDialog };
130                                         box.value_(1 - box.value);
131                                 });
132                 };
134                 if (usesName) {
135                         nameView = DragBoth(zone, Rect(0,0, nameWid, height));
136                         nameView.font_(font).align_(0).resize_(3)
137                                 .setBoth_(false)
138                                 .receiveDragHandler = { this.proxy_(View.implClass.currentDrag) };
139                 };
141                 if (usesPausSend) {
142                         pauseBut = GUI.button.new(zone, Rect(0,0,34,height))
143                                 .font_(font).resize_(3)
144                                 .states_([
145                                         ["paus", skin.fontColor, skin.onColor],
146                                         ["rsum", skin.fontColor, skin.offColor]
147                                 ])
148                                 .action_({ arg btn;
149                                 if(proxy.notNil, {
150                                                 [       { proxy.resume; }, { proxy.pause; }  ].at(btn.value).value;
151                                         })
152                                 });
154                         sendBut = Button(zone, Rect(0,0,34,height))
155                                 .font_(font).resize_(3)
156                                 .states_([
157                                         ["send", skin.fontColor, skin.offColor],
158                                         ["send", skin.fontColor, skin.onColor]
159                                 ])
160                                 .action_({ arg btn, mod;
161                                         if(proxy.notNil and: (btn.value == 0)) {
162                                                 if (mod.isAlt) { proxy.rebuild } { proxy.send }
163                                         };
164                                         btn.value_(1 - btn.value)
165                                 })
166                 };
168                 if (makeWatcher) { this.makeWatcher };
169         }
171         makeWatcher {
172                 skipjack.stop;
173                 skipjack = SkipJack({ this.updateAll },
174                         0.5, \r                  { win.isClosed },\r                      ("ProxyMon" + try { proxy.key }).asSymbol\r              );\r             skipjack.start;\r        }\r      \r       updateAll {\r            var monitor, outs, amps, newHasSeriesOut;
176                 var currState;
177                 var currVol=0, pxname='<no proxy>', isAudio=false, plays=0, playsSpread=false, pauses=0, canSend=0;
178                 var type = "-";
180                 if (win.isClosed) {skipjack.stop; ^this };
182                 if (proxy.notNil) {
184                         pxname = proxy.key ? 'anon';
185                         type = proxy.typeStr;
186                         canSend = proxy.objects.notEmpty.binaryValue;
187                         pauses = proxy.paused.binaryValue;
189                         isAudio = proxy.rate == \audio;
190                         monitor = proxy.monitor;
191                         plays = monitor.isPlaying.binaryValue;
193                         if (monitor.notNil, {
194                                 currVol = proxy.monitor.vol;
195                                 playsSpread = proxy.monitor.hasSeriesOuts.not;
196                                 outs = monitor.outs;
197                         });
198                 };
200                 currState = [currVol, pxname, isAudio, plays, outs, playsSpread, pauses, canSend];
202                 if (currState != oldState) {
203                 //      "ProxyMonitorGui - updating.".postln;
204                         if (currVol.notNil) { ampSl.value_(currVol.ampdb) };
205                         if (usesName) { nameView.object_(proxy).string_(pxname) };
207                         playBut.value_(plays);
208                         if (usesPausSend) {
209                                 pauseBut.value_(pauses);
210                                 sendBut.value_(canSend);
211                         };
213                         if (isAudio != oldState[2]) {
214                                 [ampSl, playBut, setOutBox, playNDialogBut].reject(_.isNil).do(_.enabled_(isAudio));
215                         };
216                                 // dont update if typing into numberbox - should be tested with SwingOSC!
217                         if (setOutBox.numberView.hasFocus.not) {
218                                 setOutBox.value_(try { outs[0] } ? 0);
219                                 if (usesPlayN) {
220                                         playNDialogBut.value_(playsSpread.binaryValue)
221                                 };
222                         }
223                 };
224                 oldState = currState;
225         }
226         clear { proxy = nil }