asCode
SCView extensions
part of wslib
Method for generating GUI style code from SCView objects
note: asCode only works on CocoaGUI objects for now
asCode ( varName, extraMethods )
asCode works as an 'asCompileString' for SCViews.
varName : name of the var used in the code (default: "w" )
extraMethods : every view has specific methods to be added to the code, based on its properties.
These methods can be Symbols, or arrays with one Symbol and a value which is regarded as the
default value for that specific method. If no default value is specified, asCode uses nil as the default
value. A method is only posted if the value it returns is different from the default value. The extraMethods
argument is used internally by asCode, but as a user you can also specify extra methods to be posted.
Every Object in SuperCollider will respond to asCode, but for most objects the reponse will be the same as
asCompileString. For SCViews and SCWindows and some related classes asCode produces full code for
the recreation of that view or window. This includes the decorators for SCWindow and SCContainerView too.
(
w = SCWindow( "test-asCode" ).front;
10.do({ SCSlider( w, Rect.rand( 380, 380, 100, 100 ) ).value_( 1.0.rand ); });
w.view.background = Color.blue(0.8).alpha_(0.25);
)
w.asCode;
/* result:
w = GUI.window.new("test-asCode", Rect(128, 64, 400, 400) ).front;
w.view.background_(Color(0.0, 0.0, 0.8, 0.25));
*/
w[0].asCode; // (note the SC2 compatible .at for SCWindow views)
/* result:
GUI.slider.new( w, Rect(44.0, 200.0, 32.0, 4.0) )
.value_( 0.5038806200027466 );
*/
w.drawHook = { GUI.pen.width_(5).line( 10@10, 390@390 ).stroke }; w.refresh;
w.asCode; // includes drawHook functions. But only if they use GUI.pen
/*
w = GUI.window.new("test-asCode", Rect(128, 64, 400, 400) ).front;
w.view.background_(Color(0.0, 0.0, 0.8, 0.25));
w.drawHook = {
GUI.pen.width_( 5 );
GUI.pen.moveTo( Point(10, 10) );
GUI.pen.lineTo( Point(390, 390) );
GUI.pen.stroke;
};
*/
SCWindow:asFullCode ( varName, extraMethods )
calls asCode on all views in a window and returns the code for the full window, between brackets
w.asFullCode( "~window" ); // if the window from the above examples is still open
/* result:
(
~window = GUI.window.new("test-asCode", Rect(128, 64, 400, 400) ).front;
~window.view.background_(Color(0.0, 0.0, 0.8, 0.25));
~window.drawHook = {
GUI.pen.width_( 5 );
GUI.pen.moveTo( Point(10, 10) );
GUI.pen.lineTo( Point(390, 390) );
GUI.pen.stroke;
};
GUI.slider.new( ~window, Rect(44.0, 200.0, 32.0, 4.0) )
.value_( 0.5038806200027466 );
GUI.slider.new( ~window, Rect(170.0, 169.0, 1.0, 25.0) )
.value_( 0.0577695369720459 );
GUI.slider.new( ~window, Rect(95.0, 32.0, 31.0, 82.0) )
.value_( 0.041653990745544434 );
GUI.slider.new( ~window, Rect(317.0, 332.0, 20.0, 98.0) )
.value_( 0.87993323802948 );
GUI.slider.new( ~window, Rect(128.0, 98.0, 8.0, 73.0) )
.value_( 0.3661167621612549 );
GUI.slider.new( ~window, Rect(366.0, 110.0, 77.0, 19.0) )
.value_( 0.23308014869689941 );
GUI.slider.new( ~window, Rect(191.0, 368.0, 14.0, 29.0) )
.value_( 0.41184818744659424 );
GUI.slider.new( ~window, Rect(306.0, 198.0, 9.0, 31.0) )
.value_( 0.4233088493347168 );
GUI.slider.new( ~window, Rect(211.0, 170.0, 51.0, 23.0) )
.value_( 0.5840545892715454 );
GUI.slider.new( ~window, Rect(248.0, 291.0, 23.0, 80.0) )
.value_( 0.12104594707489014 );
);
*/