forked from marierm/mmExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpongeButtons.sc
More file actions
297 lines (253 loc) · 6.92 KB
/
SpongeButtons.sc
File metadata and controls
297 lines (253 loc) · 6.92 KB
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
ButtonBank {
// A ButtonBank manages a bank of buttons. The input has to be a
// ButtonInput. This class makes it easier to assign different behaviors
// to a set of buttons.
var <input, <modes, <currentMode, function;
// input is a ButtonInput.
// modes is an Array of ButtonMode.
*new { |input, modes|
^super.newCopyArgs(input, modes).init();
}
init {
modes = modes ? [ ButtonMode(this) ];
this.mode_(0);
}
mode_ { |id|
currentMode = (id).wrap(0, modes.size);
input.action_({ |buttVal, ids|
modes[currentMode].value(buttVal, ids);
});
}
mode {
^modes[currentMode];
}
nextMode {
this.mode_(currentMode + 1);
}
previousMode {
this.mode_(currentMode - 1);
}
addMode { |mode|
modes = modes.add(mode);
}
newMode {
this.addMode( ButtonMode(this) );
}
removeMode { |id|
modes = modes.removeAt(id);
}
size { ^input.size; }
}
ButtonMode {
// a ButtonMode has a ButtonFunction for each button.
var <bank, <>buttonFunctions, <>level, <>combos;
*new { |buttonBank, buttonFuncs|
^super.newCopyArgs(buttonBank, buttonFuncs).init();
}
init {
level = 0;
buttonFunctions = buttonFunctions ? Array.fill(
bank.size, {|i|
ButtonFunction(this, i, nil);
}
);
combos = Dictionary();
}
value { |val, ids|
// ids represents the bits that changed (val bitXor: oldVal). iterate
// overall bits that changed (only one should change, but it is not
// guaranteed).
ids.asBinaryDigits(bank.size).reverse.indicesOfEqual(1).do({ |i|
buttonFunctions[i].value(val.bitTest(i).asInt, i);
combos.keysValuesDo({|combo, function|
// check if value is a combo.
(combo & val == combo).if({
//check if it was already on.
((ids bitXor: val) & combo != combo).if({
function[1].value(val, i);
});
}, {// if not a combo
// check if it was one
((ids bitXor: val) & combo == combo).if({
function[0].value(val, i);
});
});
});
});
}
// No levels for combos (no modifier keys).
addCombo {|comboBits, function, buttState=1|
var array;
array = combos[comboBits] ? [nil,nil];
combos.put(
comboBits,
array.put(buttState, function)
);
}
removeCombo {|comboBits|
combos.removeAt(comboBits);
}
addFunc { |button=0, function, levels = #[0], buttState=1|
// Add a function to a button.
// button is button number;
// function is a function to which value (0
// or 1), id (button number), level (the modifier keys status) and
// clickCount (if nClick is enabled) are passed;
// levels are the modifier keys statuses at which the function will be
// evaluated;
// buttState is 1 when button is pressed and 0 when it is depressed.
// Other numbers represent the click count if enableNclick has been
// called.
var array;
array = buttonFunctions[button].functions[
buttState
].asArray.extend(levels.maxItem + 1);
array.putEach(levels, function);
buttonFunctions[button].functions.put(buttState, array);
}
}
ButtonFunction {
// Holds the functions for a single button. A ButtonFunction has many
// Arrays of functions: one for when the button is turned ON, one for when
// it is turned off, and others for higher clickCount. Other buttons can
// act as modifier keys and change the "level".
var <buttonMode, <id, <>functions, <>level, <>evalFunc, clickCount,
routine, <>clickCountDelay, <>clickCountMinDelay, waitMore=false;
// functions is an IdentityDictionary with at least two pairs: 1:
// [{},{}, ...] and 0: [{},{}, ...]
// More pairs can be added for double clicks, triple clicks, etc:
// 2: [{},{}, ..], 3: [{},{}, ..]
*new { |buttonMode, id, functions|
^super.newCopyArgs(buttonMode, id).init(functions);
}
init { |funcs|
level = 0;
functions = funcs ?
Dictionary.newFrom([
0, [],
1, []
]);
clickCount = 0;
clickCountDelay = 0.25;
clickCountMinDelay = 0.05;
// Double clicking is disabled by default.
// This allows fast gestures.
this.disableNclick;
}
value {|val, id|
evalFunc.value(val, id);
}
makeModifier { |bit|
functions[1] = [
{buttonMode.level_(buttonMode.level.setBit(bit,true) )}
];
functions[0] = [
{buttonMode.level_(buttonMode.level.setBit(bit,false) )}
];
}
enableNclick {
evalFunc = { |val, id|
val.asBoolean.if({
waitMore.not.if({
routine.stop;
clickCount = clickCount + 1;
level = buttonMode.level;
routine = {
waitMore = true;
clickCountMinDelay.wait;
waitMore = false;
clickCountDelay.wait;
clickCount = 0;
}.fork;
// clickCount.postln;
functions.at(clickCount).notNil.if({
functions.at(clickCount).wrapAt(level).value(
val, level, id, clickCount
);
});
}, {
"Double click not registered: it was too quick and was
considered a rebound.".warn;
});
}, {
// To test: Currently, we have to wait clickCountDelay seconds
// with the button pressed if we want the "off" function to be
// evaluated. The other option would be not to wait and
// evaluate the "off" function every time, including when
// there potentially is a double-click coming.
//
// In that last case, the disable and enableNclick functions
// are mostly useless and a static evalFunc could be used.
// Uncomment the if to wait, comment to not wait.
// (clickCount == 0).if({
// \off.postln;
functions.at(val).notNil.if({
functions.at(0).wrapAt(level).value(
val, level, id, clickCount
);
});
});
};
}
disableNclick {
evalFunc = { |val, id|
val.asBoolean.if{ level = buttonMode.level; };
functions.at(val).notNil.if({
functions.at(val).wrapAt(level).value(val, level, id);
});
};
}
}
ButtonInput {
// This set of classes was designed to work with the sponge, but it is
// possible to use any kind of button input. ButtonInput is a bridge to
// hardware buttons.
// The .sponge method initializes a Button input so that it gets its
// values from an instance of Sponge. The generic *new method is not
// really usable at this point.
var <interface, <size, <>action;
*new { |interface, size|
^super.newCopyArgs(interface, size).init();
}
*sponge { |sponge|
^SpongeButtonInput.new(sponge);
}
init {
var oldVal = 0;
// size = numButtons;
interface.action_(
interface.action.addFunc({|buttsVal|
// buttsVal should be an integer.
var ids;
// Get the bits that changed.
ids = ( buttsVal bitXor: oldVal);
// If at least one bit changed:
ids.asBoolean.if({
action.value(buttsVal, ids);
});
oldVal = buttsVal;
});
);
}
}
SpongeButtonInput : ButtonInput{
// *new { |sponge|
// ^super.new().init(sponge);
// }
init {
var oldVal = 0;
size = 10;
interface.action_(
interface.action.addFunc({|values|
var ids;
// Get the bits that changed.
ids = ( values[8] bitXor: oldVal);
// If at least one bit changed:
ids.asBoolean.if({
action.value(values[8], ids);
});
oldVal = values[8];
});
);
}
}