Knob a gui control with round, horizontal or vertical action
Knob.new(parent, bounds)
value - set or get the control's value.
valueAction(newValue) - sets the knob's value and performs the action function.
action(view, x, y, modifiers) - user defined action hook function.
mouseOverAction(view, x, y) - user defined action hook for mouse over event. acceptsMouseOver must be set to true in the parent window for the mouseOverAction hook to to work.
color - array of colors: [center Color, value Color, range Color, dial Color].
canFocus_(bool) - enables/disables focus gain for the control
mode - knob's tracking mode. can be \round, \horiz or \vert. defaults to \round
centered - bool. the knob's center scale, 0.5 is zero in the value scale. eg. as in a pan control. defaults to false.
step - step in which the value is incremented/decremented while draging in \horiz and \vert modes. defaults to 0.01.
keystep - step in which the value is incremented/decremented with the keyboard. defaults to 0.01.
visible - boolean; set knob's visibility true/false.
enabled - boolean; enable/disable the knob.
refresh - redraws the knob.
canFocus - enable/disable the knob's capability to gain focus.
canReceiveDragHandler - customize drag methods see example.
receiveDragHandler - customize drag methods see example.
beginDragAction - customize drag methods see example.
Keys work like on SCSlider when the Knob gains focus:
arrow up, arrow right or ] - increment value by keystep
arrow dn, arrow left or [ decrement value by keystep
x - set value to 1.0
c - set value to 0.5
n - set value to 0.0
r - random value from 0.0 to 1.0
Behavior:
Knob's default tracking mode is \round.
Pressing the Control key and drag places the knob temporarily into \vert tracking mode.
Pressing the Option key and drag places the knob temporarily into \horiz tracking mode.
Pressing the Command or Apple key drags out the knob's value. like for other views.
In \round mode, draging farther from the knob's center increases the control's precision.
*defaultMode - Class variable that states the default tracking mode for new knobs.
Knob.defaultMode;
Knob.defaultMode='vert';
Knob.defaultMode='round';
see also: [EZKnob], [KnobEditor]
// examples
(
var window, size = 32; // try different sizes - from 15 to 200 or more!
window = SCWindow.new("Knob", Rect(640,630,270,70)).front;
k = Knob.new(window, Rect(20, 10, size, size));
k.action_({|v,x,y,m| postf("action func called: %\n", v.value); });
//k.color[1] = Color.gray(alpha:0);
)
k.value
k.value = 0.25
k.valueAction = 0.125
// modes
k.mode = \vert;
k.mode = \horiz;
k.mode = \round; // default
k.visible
k.visible = false
k.visible = true
k.enabled_(false)
k.enabled_(true)
k.canFocus = false
k.canFocus = true
// centered mode - like in a pan or eq gain control etc.
(
var window;
window = SCWindow.new("Pan Knob", Rect(640,630,270,70)).front;
k = Knob.new(window, Rect(20,10,36,36));
k.action_({|v,x,y,m| \pan.asSpec.map(v.value).postln; })
// .mode_(\horiz)
.centered_(false)
.value_(\pan.asSpec.unmap(0)); // 0.5
//k.color[1] = Color.gray(alpha:0);
)
k.centered
k.centered = true
k.centered = false
// step
(
var window, midispec;
midispec = [0,127,'linear',1].asSpec;
window = SCWindow.new("step knob", Rect(640,630,270,70)).front;
k = Knob.new(window, Rect(20,10,32,32));
k.action_({|v,x,y,m| midispec.map(v.value).postln; })
.value_(midispec.unmap(0));
)
k.step = 10/127 // step by 10
k.mode = \vert;
k.mode = \horiz;
k.mode = \round;
// GUI Kit
(
f = {
var size = 32;
w = GUI.window.new("GUI.knob Test", Rect(580,590,300,80));
k = GUI.knob.new(w, Rect(20, 10, size, size));
k.action_({|v| v.value.postln; });
w.front;
};
)
GUI.cocoa; // select cocoa gui
f.value;
GUI.swing; // select swing gui
f.value;
// GUI skins
// default skin
// GUI.skins.knob.default = (
// scale: Color.black.alpha_(0.3),
// dial: Color.black.alpha_(0.7),
// center: Color.blue(0.7, 0.5),
// level: Color.green(0.8, 0.8)
// );
GUI.skins.knob.default.level
GUI.skins.knob.default.center
(
GUI.skins.knob.mySkin = ( );
Sheet({ arg l;
a = Array.fill( 8, {
GUI.skins.knob.mySkin.center_(Color.rand);
Knob(l, 32@32).skin_( GUI.skins.knob.mySkin );
})
}, "Knobs");
)
a.collect(_.value);
// mouseOverAction
(
var size = 28;
GUI.skins.knob.mySkin = ( );
w = SCWindow.new("Knobs", Rect(384,630,320,80));
w.acceptsMouseOver=true; // true in parent window!
w.view.decorator = FlowLayout(w.view.bounds, 10@4, 10@4);
h = SCStaticText(w, 300 @ 16).background_(Color.blue(0.2,0.1));
w.view.decorator.nextLine;
k = Array.fill(8, {|item, i|
var knob;
knob = Knob.new(w, size @ size)
// .canFocus_(false)
.action_({|v,x,y,m| h.string = "value: " ++ v.value.asString; })
.mouseOverAction_({|v,x,y| h.string = "value: " ++ v.value.asString; });
GUI.skins.knob.mySkin.center_(Color.rand);
knob.skin_( GUI.skins.knob.mySkin );
knob;
});
w.front
)
k.collect(_.value);
// drag and drop
(
var w, txt, size = 36;
w = SCWindow.new("knobs", Rect(400,400,250,100)).front;
w.acceptsMouseOver=true;
w.view.decorator = FlowLayout(w.view.bounds).gap_(10 @ 10).margin_(10 @10);
txt = SCStaticText(w, 200 @ 14);
w.view.decorator.nextLine;
k = Knob(w, size @ size);
k.action = {arg v,x,y; v.value.postln; txt.string_("value: " ++ v.value); };
k.mouseOverAction = {|v| txt.string_("value: " ++ v.value); };
j = Knob(w, size @ size);
j.action = {arg v,x,y; j.value.postln; txt.string_("value: " ++ v.value); };
j.mouseOverAction = { txt.string_("value: " ++ j.value); };
n = SCNumberBox(w, 100 @ 20);
//n.setProperty(\boxColor,Color.grey(alpha:0.0));
n.value = 0.0;
)
// customize drag and drop methods
k.canReceiveDragHandler
k.canReceiveDragHandler = false; // don't accept drops
k.canReceiveDragHandler = { SCView.currentDrag.isFloat }; // accept only if drag is float
k.receiveDragHandler = { ("value droped in: " ++ SCView.currentDrag).postln }
k.receiveDragHandler = { k.valueAction = SCView.currentDrag.clip(0.0, 1.0); }
k.beginDragAction = { ("drag out -> " ++ k.value).postln; }
k.beginDragAction = { k.value.asFloat; }
// on Sheet
(
Sheet({ arg l;
l.view.decorator.gap=4@4;
l.view.decorator.margin=4@4;
c = Array.fill(8, { Knob.new(l, 28@28) });
}, "Knobs")
)
c
c.collect(_.value);