-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEZSliderRounding.sc
executable file
·248 lines (178 loc) · 5.14 KB
/
EZSliderRounding.sc
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
// http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/EZSlider-issues-td7581060.html
// http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/EZSlider-value-display-resolution-td7594744.html#a7594845
// Methods for analyzing step precision and adapting EZSliders
+Float {
decimalStrings { |precision = 8|
var precString = this.asStringPrec(precision), prePointString,
postPointString,
signString, manString, expString, posOfPoint, shift;
#manString, expString = precString.split($e);
(manString[0] == $-).if {
manString = manString.drop(1);
signString = "-";
};
expString.isNil.if {
// no exponent
posOfPoint = precString.find(".");
posOfPoint.isNil.if {
prePointString = precString;
}{
prePointString = precString.copyFromStart(posOfPoint-1);
postPointString = precString.copyToEnd(posOfPoint+1);
}
}{
// with exponent
shift = expString.drop(1).asInteger;
(expString[0] == $+).if {
(manString.size == 1).if {
// no comma
prePointString = manString ++ ($0!shift).join;
}{
(shift < (manString.size-2)).if {
prePointString = manString[0].asString ++ manString.copyRange(2,
shift+1);
postPointString = manString.copyToEnd(shift+1);
}{
prePointString = manString[0].asString ++ manString.copyToEnd(2)
++
($0 ! (shift - (manString.size-2))).join; }
}
}{
prePointString = "0";
(manString.size == 1).if {
// no comma
postPointString = ($0!(shift-1)).join ++ manString;
}{
postPointString = ($0!(shift-1)).join ++
manString[0].asString ++ manString.copyToEnd(2);
}
}
};
^[signString, prePointString, postPointString]
}
}
+SimpleNumber {
decimalStrings { ^this.asFloat.decimalStrings }
}
+EZSlider {
adaptToControlStep { |precision = 8|
var stepString, range;
stepString = controlSpec.step.decimalStrings(precision);
this.round = 10 ** stepString.last.size.neg;
(GUI.id == \qt).if {
numberView.maxDecimals = stepString.last.size;
controlSpec.warp.isKindOf(LinearWarp).if {
range = controlSpec.constrain(controlSpec.clipHi) -
controlSpec.constrain(controlSpec.clipLo);
sliderView.step = 1 / (range / controlSpec.step).round;
}
}
^this
}
}
/*
Examples:
GUI.qt;
// jitter with Qt in this example disappears with
// uncommenting method for adaption to step size
(
a = [0, 10, \lin, 2, 0];
w = Window.new.front;
e = EZSlider(w, controlSpec: a.asSpec)
// .adaptToControlStep;
)
// jitter with Qt and no numbers in neither kit
// you would have to set step (round, maxDecimals)
// with .adaptToControlStep no jitter in Qt and
// numbers displayed correctly in both GUIs
(
a = [0, 0.0001, \lin, 0.00001, 0];
w = Window.new.front();
e = EZSlider(w, controlSpec: a.asSpec, numberWidth: 90)
// .adaptToControlStep;
)
// probably it will / should remain users responsibility to set
// appropriate numberWidth, as there is also Font ...
// bounds set correctly but jitter in Qt:
(
a = [10, 40, \exp, 5, 10];
w = Window.new.front;
e = EZSlider(w, controlSpec: a.asSpec)
)
// wrongs bounds
(
a = [10, 40, \exp, 6, 10];
w = Window.new.front;
e = EZSlider(w, controlSpec: a.asSpec)
)
An alternative and easy workaround for step precision adaption
is, of course, scaling of slider values afterwards ...
Greetings
Daniel
*/
// This still doesn't handle step = 0 as expected, it could be like this:
+EZSlider {
*new2 { arg parent, bounds, label, controlSpec, action, initVal,
initAction=false, labelWidth=60,
numberWidth=45,
unitWidth=0, labelHeight=20, layout=\horz,
gap, margin,
precision = 8, stepEqualZeroDiv = 100;
^this.new(parent, bounds, label, controlSpec, action,
initVal, initAction, labelWidth, numberWidth,
unitWidth, labelHeight, layout, gap, margin
).adaptToControlStep(precision, stepEqualZeroDiv);
}
adaptToControlStep { |precisionSpan = 8, stepEqualZeroDiv =
100|
var stepString, range, estimatedControlStep;
estimatedControlStep = (controlSpec.step.abs < (10
** precisionSpan.neg)).if {
(controlSpec.clipHi - controlSpec.clipLo) /
stepEqualZeroDiv
}{
controlSpec.step
};
stepString = estimatedControlStep.decimalStrings(precisionSpan);
this.round = 10 ** stepString.last.size.neg;
range = controlSpec.constrain(controlSpec.clipHi) -
controlSpec.constrain(controlSpec.clipLo);
sliderView.step = 1 / (range /
estimatedControlStep).round;
(GUI.id == \qt).if { numberView.maxDecimals =
stepString.last.size };
^this
}
}
/*
// division of 100 (stepEqualZeroDiv) for step = 0 and
// else by precision implicitely defined by step -
// the precision arg of adaptToControlStep indicates
// the span of decimals to be regarded for step
(
a = [0, 1, \lin, 0, 0];
e = EZSlider.new2(controlSpec: a.asSpec)
)
(
a = [0, 10, \lin, 0, 0];
e = EZSlider.new2(controlSpec: a.asSpec)
)
(
a = [0, 100, \lin, 0, 0];
e = EZSlider.new2(controlSpec: a.asSpec)
)
(
a = [0, 1000, \lin, 0, 0];
e = EZSlider.new2(controlSpec: a.asSpec)
)
(
a = [0, 1000, \lin, 0.1, 0];
e = EZSlider.new2(controlSpec: a.asSpec)
)
(
a = [0, 1, \lin, 0.1, 0];
e = EZSlider.new2(controlSpec: a.asSpec)
)
Greetings
Daniel
*/