File: EZKnob.sc

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (297 lines) | stat: -rw-r--r-- 8,971 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
EZKnob : EZGui {

	classvar <>compactRatio=0.87;

	var <knobView, <numberView, <unitView, <>controlSpec, knobSize, unitWidth;
	var <>round = 0.001;

	*new { arg parent, bounds, label, controlSpec, action, initVal,
			initAction=false, labelWidth=60, knobSize,
			unitWidth=0, labelHeight=20,  layout=\vert, gap, margin;

		^super.new.init(parent, bounds, label, controlSpec, action,
			initVal, initAction, labelWidth, knobSize,
				unitWidth, labelHeight, layout, gap, margin)
	}

	init { arg parentView, bounds, label, argControlSpec, argAction, initVal,
			initAction, labelWidth, argKnobSize,argUnitWidth,
			labelHeight, argLayout, argGap, argMargin;

		var labelBounds, numBounds, unitBounds,knobBounds;
		var numberStep;

		// Set Margin and Gap
		this.prMakeMarginGap(parentView, argMargin, argGap);

		unitWidth = argUnitWidth;
		layout=argLayout;
		bounds.isNil.if{bounds = 50@90};

		knobSize = argKnobSize ;

		// if no parent, then pop up window
		# view,bounds = this.prMakeView( parentView,bounds);


		labelSize=labelWidth@labelHeight;

		// calculate bounds of all subviews
		# labelBounds,numBounds,knobBounds, unitBounds
				= this.prSubViewBounds(innerBounds, label.notNil, unitWidth>0);

		// instert the views
		label.notNil.if{ //only add a label if desired
			labelView = StaticText.new(view, labelBounds);
			labelView.string = label;
		};

		(unitWidth>0).if{ //only add a unitLabel if desired
			unitView = StaticText.new(view, unitBounds);
		};

		knobView = Knob.new(view, knobBounds);
		numberView = NumberBox.new(view, numBounds);

		// set view parameters and actions
		controlSpec = argControlSpec.asSpec;
		(unitWidth>0).if{unitView.string = " "++controlSpec.units.asString};
		initVal = initVal ? controlSpec.default;
		action = argAction;

		numberStep = controlSpec.step;
		if (numberStep == 0) {
			numberStep = controlSpec.guessNumberStep
		}{
			// controlSpec wants a step, so zooming in with alt is disabled.
			numberView.alt_scale = 1.0;
			knobView.alt_scale = 1.0;
		};
		numberView.step = numberStep;
		numberView.scroll_step = numberStep;


		if((controlSpec.minval + controlSpec.maxval)==0){knobView.centered=true};

		knobView.action = {
			this.valueActionIfChanged_(controlSpec.map(knobView.value));
		};

		if (controlSpec.step != 0) {
			knobView.step = (controlSpec.step / (controlSpec.maxval - controlSpec.minval));
		};

		knobView.receiveDragHandler = { arg slider;
			slider.valueAction = controlSpec.unmap(View.currentDrag);
		};

		knobView.beginDragAction = { arg slider;
			controlSpec.map(slider.value)
		};

		numberView.action = { this.valueActionIfChanged_(numberView.value) };

		if (initAction) {
			this.valueAction_(initVal);
		}{
			this.value_(initVal);
		};

		this.prSetViewParams;
	}
		// just set, no action
	value_ { arg val;
		value = controlSpec.constrain(val);
		numberView.value = value.round(round);
		knobView.value = controlSpec.unmap(value);
	}
		// set and do action
	valueAction_ { arg val;
		this.value_(val);
		this.doAction;
	}

	doAction { action.value(this) }

	set { arg label, spec, argAction, initVal, initAction = false;
		labelView.notNil.if { labelView.string = label.asString };
		spec.notNil.if { controlSpec = spec.asSpec };
		argAction.notNil.if { action = argAction };
		initVal = initVal ? value ? controlSpec.default;

		if (initAction) {
			this.valueAction_(initVal);
		}{
			this.value_(initVal);
		};
	}

	centered_ { arg bool; knobView.centered_(bool) }

	centered{ ^knobView.centered }


	setColors{arg stringBackground,stringColor,numBackground,
		numStringColor,numNormalColor,numTypingColor,knobColors,background;

			stringBackground.notNil.if{
				labelView.notNil.if{labelView.background_(stringBackground)};
				unitView.notNil.if{unitView.background_(stringBackground)};};
			stringColor.notNil.if{
				labelView.notNil.if{labelView.stringColor_(stringColor)};
				unitView.notNil.if{unitView.stringColor_(stringColor)};};
			numBackground.notNil.if{
				numberView.background_(numBackground);};
			numNormalColor.notNil.if{
				numberView.normalColor_(numNormalColor);};
			numTypingColor.notNil.if{
				numberView.typingColor_(numTypingColor);};
			numStringColor.notNil.if{
				numberView.stringColor_(numStringColor);};
			knobColors.notNil.if{
				knobView.color_(knobColors);};
			background.notNil.if{
				view.background=background;};
			numberView.refresh;
			knobView.refresh;
	}

	font_{ arg font;

			labelView.notNil.if{labelView.font=font};
			unitView.notNil.if{unitView.font=font};
			numberView.font=font;
	}

	///////Private methods ///////

	prSetViewParams{ // sets resize and alignment for different layouts

		switch (layout,
		\line2, {
			labelView.notNil.if{
				labelView.resize_(5);
				unitView.notNil.if{unitView.resize_(9)};
				numberView.resize_(8);
				}{
				unitView.notNil.if{
					unitView.resize_(6);
					numberView.resize_(5);
					}{
					numberView.resize_(5);
					};
				};
			knobView.resize_(9);
			popUp.if{view.resize_(2)};
		},
		\vert, {
			labelView.notNil.if{labelView.resize_(2)};
			unitView.notNil.if{unitView.resize_(6)};
			numberView.resize_(5);
			popUp.if{view.resize_(2)};
		},
		\vert2, {
			labelView.notNil.if{labelView.resize_(2).align_(\center)};
			unitView.notNil.if{unitView.resize_(6)};
			numberView.resize_(5);
			popUp.if{view.resize_(2)};
		},
		\horz, {
			labelView.notNil.if{labelView.resize_(4).align_(\right)};
			unitView.notNil.if{unitView.resize_(6)};
			numberView.resize_(5);
			knobView.resize_(9);
			popUp.if{view.resize_(2)};
		});

	}

	prSubViewBounds{arg rect, hasLabel, hasUnit;  // calculate subview bounds
		var numBounds,labelBounds,knobBounds, unitBounds,knobHeight, numHeight;
		var gap1, gap2, gap3, tmp, labelH, unitH;
		gap1 = gap.copy;
		gap2 = gap.copy;
		gap3 = gap.copy;
		(rect.height<= (labelSize.y*2)).if{labelSize.y=rect.height/2};

		numHeight=labelSize.y;

		switch (layout,
			\line2, {
				knobSize.isNil.if{knobSize=( ((rect.height/compactRatio)-margin.x)@(rect.height))};
				knobSize=(knobSize.x-margin.x)@(knobSize.y.min(rect.height));
				hasUnit.not.if{ gap2 = 0@0; unitWidth = 0};
				labelBounds = Rect(0,0,rect.width-knobSize.x-gap3.x,labelSize.y); //to left
				hasLabel.if{
					unitBounds = Rect(labelBounds.width-unitWidth,labelSize.y+gap1.y,
						unitWidth, rect.height-labelSize.y-gap1.y);

					numBounds = Rect(0,labelSize.y+gap1.y,
						labelBounds.width-gap2.x-unitBounds.width,rect.height-labelSize.y-gap1.y);

				}{
					unitBounds = Rect(labelBounds.width-unitWidth,0,
							unitWidth, rect.height);

					numBounds = Rect(0,0,
						labelBounds.width-gap2.x-unitBounds.width,rect.height);

				};
				knobBounds=knobSize.asRect.moveTo(rect.width-knobSize.x,0);
			},
			\horz, {
				knobSize.isNil.if{knobSize=( ((rect.height/compactRatio)-margin.x)@(rect.height))};
				knobSize=(knobSize.x-margin.x)@(knobSize.y.min(rect.height));
				knobBounds=knobSize.asRect.moveTo(rect.width-knobSize.x,0);
				hasUnit.not.if{ gap2 = 0@0; unitWidth = 0};
				hasLabel.not.if{ gap1 = 0@0; labelSize.x = 0};
				labelBounds = (labelSize.x@rect.height).asRect;
				unitBounds = (unitWidth@rect.height).asRect
					.moveTo(rect.width-knobBounds.width-gap2.x-unitWidth,0);
				numBounds = Rect.newSides(labelSize.x+gap1.x,0,unitBounds.left-gap2.x,rect.height);
			},
			\vert , {
				hasUnit.not.if{ gap3 = 0@0; unitWidth = 0};
				hasLabel.not.if{ gap1 = 0@0; labelSize.y = 0};

				knobSize.isNil.if{
					knobSize=( ((rect.height-labelSize.y-numHeight-gap1.y-gap2.y)/compactRatio)
						@(rect.height-labelSize.y-numHeight-gap1.y-gap2.y))
				};
				knobSize=((knobSize.x).min(rect.width))
					@(knobSize.y.min(rect.height-labelSize.y-numHeight-gap1.y-gap2.y));

				labelBounds = (rect.width@labelSize.y).asRect;
				knobBounds=knobSize.asRect.moveTo(0,labelSize.y+gap1.y);

				numBounds = Rect(0,rect.height-numHeight,rect.width-unitWidth-gap3.x, numHeight);

				unitBounds = Rect(rect.width-unitWidth,rect.height-numHeight,unitWidth,numHeight);

			},
			\vert2 , {
				hasUnit.not.if{ gap3 = 0@0; unitWidth = 0};
				hasLabel.not.if{ gap1 = 0@0; labelSize.y = 0};

				knobSize.isNil.if{
					knobSize=( ((rect.height-labelSize.y-numHeight-gap1.y-gap2.y)/compactRatio)
						@(rect.height-labelSize.y-numHeight-gap1.y-gap2.y))
				};
				knobSize=((knobSize.x).min(rect.width))
					@(knobSize.y.min(rect.height-labelSize.y-numHeight-gap1.y-gap2.y));


				labelBounds = (rect.width@labelSize.y).asRect;
				knobBounds=knobSize.asRect.moveTo(0,labelSize.y+gap1.y);
				knobBounds=knobBounds.moveBy((rect.width-knobBounds.width)/2,0);
				numBounds = Rect(0,rect.height-numHeight,rect.width-unitWidth-gap3.x, numHeight);

				unitBounds = Rect(rect.width-unitWidth,rect.height-numHeight,unitWidth,numHeight);

			}
		);
		((knobBounds.height<0)||(knobBounds.width<0)).if{knobBounds=knobBounds.height_(0).width_(0)};

		^[labelBounds, numBounds, knobBounds, unitBounds].collect{arg v; v.moveBy(margin.x,margin.y)}
	}
}