last mod: 31-jul-07 sciss
Note: please use the abstraction layer GUI.scrollBar if possible! (see GUI). See also JSCView.
A kind of range slider that is commonly used to specify the viewport into a virtual scrollable canvas. See also JSCScrollPane for a combination of scroll bars with a container viewport.
( w = JSCWindow( "ScrollBars", Rect( 360, 240, 400, 400 )); v = JSCScrollBar( w, Rect( 380, 4, 16, 372 )) .resize_( 6 ) .extent_( 0.1 ) // 1/10th of the virtual viewport extent .action_({ w.refresh }); h = JSCScrollBar( w, Rect( 4, 380, 372, 16 )) .resize_( 8 ) .extent_( 0.1 ) // 1/10th of the virtual viewport extent .action_({ w.refresh }); JSCUserView( w, Rect( 4, 4, 372, 372 )) .resize_( 5 ) .background_( Color.black ) .canFocus_( false ) .drawFunc_({ arg view; var b = view.bounds; JPen.addRect( b ); JPen.clip; JPen.translate( b.left, b.top ); JPen.scale( b.width / (10 * h.extent), b.height / (10 * v.extent) ); JPen.translate( h.value * -10, v.value * -10 ); JPen.font = JSCFont( "Monospaced", 2 ); 5.do({ arg y; JPen.fillColor = Color.hsv( y / 5, 1, 1 ); JPen.stringAtPoint( "VIEWPORT", 0.1 @ ((y << 1) - 0.5) ); }); }); w.front; ) // assign a function that is called when button toggles: // value returns float between 0 and 1. isAdjusting // returns true when the slider is currently being dragged, // false if the mouse button is released or the slider // is positioned programmatically using valueAction_ v.action = { arg butt; ("New value = " ++ butt.value ++ "; isAdjusting? " ++ butt.isAdjusting ).postln; w.refresh }; // set value (without invoking action function). // the valid range is 0 ... (1 - extent). values outside // this range will be clipped automatically v.value = 0.2; v.value = 0.6; v.value = 1.0; // clips to 0.9 when extent is 0.1! // set state and invoke action function if it results in a change of value v.valueAction = 0.5; // set the extent, i.e. visible amount of the viewport (default is 1.0) v.extent = 0.2; w.refresh; h.extent = 0.4; v.extent = 0.4; w.refresh; h.extent = 1.0; v.extent = 1.0; w.refresh; // set both value and extent by specifying a span from 'lo' to 'hi' h.setSpan( 0.2, 0.4 ); h.setSpanActive( 0.4, 0.6 ); // invokes action function // the unit increment is a factor of the extent // by which the scrollbar moves when clicking into the arrows. // the default is 0.1 (1/10th of the scrollbar extent) h.unitIncrement = 0.01; // very fine h.unitIncrement = 0.5; // very coarse // the block increment is a factor of the extent // by which the scrollbar moves when clicking next to // the handle into the track. // the default is 1.0 (jump one full scrollbar extent) h.blockIncrement = 0.1; // fine h.blockIncrement = 2.0; // coarse