Gui Classes

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Gui Classes

Sco66
Hi people,

I am trying to create some classes for my GUI's - the general idea is captured in the code below (lifted from various help-files etc.) and modified a bit.  This (rudimentary) class compiles and does a simple job.  It may require some re-writing in the interests of best-practice etc.  Particular question:  how do I put the synthdef name in a variable that I can set when I write in the arguments on instantiation?  I imagine this will be connected to "getter / setter" elements.  The "getter" parts of this class work.  The writing in of arguments does not however.....Any information on the particular question or other observations in relation to best practice in this case would be much appreciated.

SCSA_Demoz2 : Window {

classvar
<width = 300,
<height = 200,
buttonWidth = 100,
buttonHeight = 30;

var button, synth,w;

*new {
|name, bounds,w|
^super.new(name, bounds).init(w);
}

init {|wArg|
w=wArg;
w = Window.new("The Four Noble Truths");
button = Button(w, Rect(
(w.bounds.width - buttonWidth) * 0.5,
(w.bounds.height - buttonHeight) * 0.5,
buttonWidth,
buttonHeight
));
button.states = [
["Hit me!", Color.black, Color.gray],
["Make it stop!", Color.black, Color.red]
];
button.action = {
|theButton|
theButton.value.switch(
1, { synth = Synth.new(\demoSynth) },
0, { synth.set(\gate, 0.0) }
)
};

w.front;


}

}

Many Thanks,

Scott S.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Gui Classes

Sco66
Ok...I think this is probably better,

it is functioning but there doesn't seem to be much information on making these GUI classes - (they are really just an encapsulation).



TestGui2 {
        classvar <>name="zz7",button,w,synth;

        *new {arg place=100,synthType=\demoSynth;

        w = Window.new("The Four Noble Truths");
                button = Button(w, Rect(
                        place,
                        100,
                        50,
                        50
                ));
                button.states = [
                        [name, Color.black, Color.gray],
                        ["Make it stop!", Color.black, Color.red]
                ];
                button.action = {
                        |theButton|
                        theButton.value.switch(
                                1, { synth = Synth.new(synthType) },
                                0, { synth.set(\gate, 0.0) }
                        )
                };

                w.front;

}}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Gui Classes

madamdata
In reply to this post by Sco66
I'm not sure how other people do this but here's how I do it:

YourGuiClass {
    var <>synthDefName, window, button;

*new {
arg synthDefName;
  ^super.newCopyArgs(synthDefName).initYourGuiClass;
}

initYourGuiClass {
   window = Window.new(Rect(100,100,100,100));
   window.front;
   button = Button(w, Rect(
  (w.bounds.width - buttonWidth) * 0.5,
                        (w.bounds.height - buttonHeight) * 0.5,
                        buttonWidth,
                        buttonHeight
                ));
                button.states = [
                        ["Hit me!", Color.black, Color.gray],
                        ["Make it stop!", Color.black, Color.red]
                ];
                button.action = {
                        |theButton|
                        theButton.value.switch(
                                1, { synth = Synth.new(synthDefName) },
                                0, { synth.set(\gate, 0.0) }
                        )
                };

     }
}


this way you can have multiple instances of YourGuiClass. You used classvar in your code - if you made synthDefName a classvar you could only have one type of synthdef. This way everytime you call YourGuiClass.new(\someSynthDefName) it will store its own synthdef name in its instance variable synthDefName, which you can access like so:
~gui = YourGuiClass.new(\bleepSynthDef);
~gui.synthDefName //prints the name of the synthdef this object controls
~gui.synthDefName = \blorpSynthDef //sets it to play a different synth

The code I posted above was just off the top of my head, it might need a bit of tweaking to actually work.
Loading...