clean up indentation and spacing
[supercollider.git] / SCClassLibrary / Common / GUI / PlusGUI / Control / SynthDescPlusGUI.sc
blob194e573b63a5c7917108a2fb4e80c3f4ea291143
1 +SynthDesc {
2         makeWindow{
3                 if (Platform.makeSynthDescWindowAction.notNil) {
4                         ^Platform.makeSynthDescWindowAction.value(this)
5                 } {
6                         ^this.makeGui;
7                 }
8         }
10         makeGui {
11                 var w, startButton, sliders;
12                 var synth, cmdPeriodFunc;
13                 var usefulControls, numControls;
14                 var getSliderValues, gui;
16                 gui = GUI.current;
18                 usefulControls = controls.select {|controlName, i|
19                         var ctlname;
20                         ctlname = controlName.name.asString;
21                         (ctlname != "?") && (msgFuncKeepGate or: { ctlname != "gate" })
22                 };
24                 numControls = usefulControls.collect(_.defaultValue).flatten.size;
25                 sliders = IdentityDictionary(numControls);
27                 // make the window
28                 w = gui.window.new("another control panel", Rect(20, 400, 440, numControls * 28 + 32));
29                 w.view.decorator = FlowLayout(w.view.bounds);
31                 w.view.background = Color.rand(0.5, 1.0);
33                 // add a button to start and stop the sound.
34                 startButton = gui.button.new(w, 75 @ 24);
35                 startButton.states = [
36                         ["Start", Color.black, Color.green],
37                         ["Stop", Color.white, Color.red]
38                 ];
40                 getSliderValues = {
41                         var envir;
43                         envir = ();
44                         usefulControls.do {|controlName, i|
45                                 var ctlname = controlName.name.asString;
46                                 var sliderEntry;
47                                 if(ctlname[1] == $_ and: { "ti".includes(ctlname[0]) }) {
48                                         ctlname = ctlname[2..];
49                                 };
51                                 if (sliderEntry.isArray) {
52                                         envir.put(controlName.name, sliderEntry.collect(_.value));
53                                 } {
54                                         envir.put(controlName.name, sliderEntry.value);
55                                 }
56                         };
57                         envir.use {
58                                 msgFunc.valueEnvir
59                         };
60                 };
63                 startButton.action = {|view|
64                         if (view.value == 1) {
65                                 Server.default.bind {
66                                         synth = Synth(name, getSliderValues.value.postln).register;
67                                 };
68                         } {
69                                 if (this.hasGate) {
70                                         synth.release;
71                                 } {
72                                         synth.free
73                                 };
74                                 synth = nil;
75                         };
76                 };
78                 // create controls for all parameters
79                 usefulControls.do {|controlName|
80                         var ctlname, ctlname2, capname, spec, controlIndex, slider;
81                         ctlname = controlName.name;
82                         capname = ctlname.asString;
83                         capname[0] = capname[0].toUpper;
84                         w.view.decorator.nextLine;
85                         ctlname = ctlname.asSymbol;
86                         if((spec = metadata.tryPerform(\at, \specs).tryPerform(\at, ctlname)).notNil) {
87                                 spec = spec.asSpec
88                         } {
89                                 spec = ctlname.asSpec;
90                         };
92                         if (spec.notNil) {
93                                 slider = EZSlider(w, 400 @ 24, capname, spec,
94                                         { |ez|
95                                                 if(synth.isPlaying) { synth.set(ctlname, ez.value) }
96                                         }, controlName.defaultValue);
97                         } {
98                                 spec = ControlSpec(-1e10, 1e10);
99                                 if (controlName.defaultValue.isNumber) {
100                                         slider = EZNumber(w, 400 @ 24, capname, spec,
101                                                 { |ez|
102                                                         if(synth.isPlaying) { synth.set(ctlname, ez.value) }
103                                                 }, controlName.defaultValue)
104                                 } {
105                                         slider = Array(controlName.defaultValue.size);
106                                         controlName.defaultValue.do {|value, i|
107                                                 slider.add(EZNumber(w, 400 @ 24, "%[%]".format(capname, i), spec,
108                                                         { |ez|
109                                                                 if(synth.isPlaying) { synth.set(controlName.index + i, ez.value) }
110                                                         }, value))
111                                         }
112                                 }
113                         };
114                         sliders.put(ctlname, slider)
115                 };
117                 // set start button to zero upon a cmd-period
118                 cmdPeriodFunc = { startButton.value = 0; };
119                 CmdPeriod.add(cmdPeriodFunc);
121                 // stop the sound when window closes and remove cmdPeriodFunc.
122                 w.onClose = {
123                         synth.release;
124                         CmdPeriod.remove(cmdPeriodFunc);
125                 };
127                 ^w.front; // make window visible and front window.
128         }