Commit | Line | Data |
---|---|---|
49815cc0 MS |
1 | /** |
2 | * Overlay UI that indicates pattern control, etc. This will be moved | |
3 | * into the Processing library once it is stabilized and need not be | |
4 | * regularly modified. | |
5 | */ | |
6 | class OverlayUI { | |
7 | ||
8 | private final PFont titleFont = createFont("Myriad Pro", 10); | |
3f8be614 | 9 | private final PFont itemFont = createFont("Lucida Grande", 11); |
49815cc0 MS |
10 | private final PFont knobFont = titleFont; |
11 | private final int w = 140; | |
12 | private final int leftPos; | |
13 | private final int leftTextPos; | |
14 | private final int lineHeight = 20; | |
15 | private final int sectionSpacing = 12; | |
3f8be614 | 16 | private final int controlSpacing = 18; |
49815cc0 MS |
17 | private final int tempoHeight = 20; |
18 | private final int knobSize = 28; | |
3f8be614 | 19 | private final float knobIndent = .4; |
49815cc0 MS |
20 | private final int knobSpacing = 6; |
21 | private final int knobLabelHeight = 14; | |
22 | private final color lightBlue = #666699; | |
23 | private final color lightGreen = #669966; | |
24 | ||
25 | private final String[] patternNames; | |
26 | private final String[] transitionNames; | |
27 | private final String[] effectNames; | |
28 | ||
29 | private PImage logo; | |
30 | ||
31 | private int firstPatternY; | |
3f8be614 | 32 | private int firstPatternKnobY; |
49815cc0 | 33 | private int firstTransitionY; |
3f8be614 | 34 | private int firstTransitionKnobY; |
49815cc0 | 35 | private int firstEffectY; |
3f8be614 MS |
36 | private int firstEffectKnobY; |
37 | ||
49815cc0 MS |
38 | private int tempoY; |
39 | ||
40 | private Method patternStateMethod; | |
41 | private Method transitionStateMethod; | |
42 | private Method effectStateMethod; | |
43 | ||
809f3518 | 44 | private final int NUM_PATTERN_KNOBS = 8; |
3f8be614 MS |
45 | private final int NUM_TRANSITION_KNOBS = 4; |
46 | private final int NUM_EFFECT_KNOBS = 4; | |
47 | ||
48 | private int activeTransitionIndex = 0; | |
49 | private int activeEffectIndex = 0; | |
50 | ||
809f3518 MS |
51 | public final VirtualPatternKnob[] patternKnobs; |
52 | public final VirtualTransitionKnob[] transitionKnobs; | |
53 | public final VirtualEffectKnob[] effectKnobs; | |
3f8be614 | 54 | |
49815cc0 MS |
55 | OverlayUI() { |
56 | leftPos = width - w; | |
57 | leftTextPos = leftPos + 4; | |
58 | logo = loadImage("logo-sm.png"); | |
59 | ||
3f8be614 MS |
60 | patternNames = classNameArray(patterns, "Pattern"); |
61 | transitionNames = classNameArray(transitions, "Transition"); | |
62 | effectNames = classNameArray(effects, "Effect"); | |
63 | ||
809f3518 MS |
64 | patternKnobs = new VirtualPatternKnob[NUM_PATTERN_KNOBS]; |
65 | for (int i = 0; i < patternKnobs.length; ++i) { | |
66 | patternKnobs[i] = new VirtualPatternKnob(i); | |
67 | } | |
68 | ||
3f8be614 MS |
69 | transitionKnobs = new VirtualTransitionKnob[NUM_TRANSITION_KNOBS]; |
70 | for (int i = 0; i < transitionKnobs.length; ++i) { | |
71 | transitionKnobs[i] = new VirtualTransitionKnob(i); | |
72 | } | |
73 | ||
74 | effectKnobs = new VirtualEffectKnob[NUM_EFFECT_KNOBS]; | |
75 | for (int i = 0; i < effectKnobs.length; ++i) { | |
76 | effectKnobs[i] = new VirtualEffectKnob(i); | |
77 | } | |
49815cc0 MS |
78 | |
79 | try { | |
80 | patternStateMethod = getClass().getMethod("getState", LXPattern.class); | |
81 | effectStateMethod = getClass().getMethod("getState", LXEffect.class); | |
82 | transitionStateMethod = getClass().getMethod("getState", LXTransition.class); | |
83 | } catch (Exception x) { | |
84 | throw new RuntimeException(x); | |
85 | } | |
86 | } | |
87 | ||
88 | void drawHelpTip() { | |
89 | textFont(itemFont); | |
90 | textAlign(RIGHT); | |
91 | text("Tap 'u' to restore UI", width-4, height-6); | |
92 | } | |
93 | ||
94 | void draw() { | |
95 | image(logo, 4, 4); | |
96 | ||
97 | stroke(color(0, 0, 100)); | |
98 | // fill(color(0, 0, 50, 50)); // alpha is bad for perf | |
99 | fill(color(0, 0, 30)); | |
100 | rect(leftPos-1, -1, w+2, height+2); | |
101 | ||
102 | int yPos = 0; | |
103 | firstPatternY = yPos + lineHeight + 6; | |
104 | yPos = drawObjectList(yPos, "PATTERN", patterns, patternNames, patternStateMethod); | |
3f8be614 MS |
105 | yPos += controlSpacing; |
106 | firstPatternKnobY = yPos; | |
49815cc0 | 107 | int xPos = leftTextPos; |
809f3518 MS |
108 | for (int i = 0; i < NUM_PATTERN_KNOBS/2; ++i) { |
109 | drawKnob(xPos, yPos, knobSize, patternKnobs[i]); | |
110 | drawKnob(xPos, yPos + knobSize + knobSpacing + knobLabelHeight, knobSize, patternKnobs[NUM_PATTERN_KNOBS/2 + i]); | |
49815cc0 MS |
111 | xPos += knobSize + knobSpacing; |
112 | } | |
113 | yPos += 2*(knobSize + knobLabelHeight) + knobSpacing; | |
114 | ||
115 | yPos += sectionSpacing; | |
116 | firstTransitionY = yPos + lineHeight + 6; | |
117 | yPos = drawObjectList(yPos, "TRANSITION", transitions, transitionNames, transitionStateMethod); | |
3f8be614 MS |
118 | yPos += controlSpacing; |
119 | firstTransitionKnobY = yPos; | |
120 | xPos = leftTextPos; | |
121 | for (int i = 0; i < transitionKnobs.length; ++i) { | |
122 | drawKnob(xPos, yPos, knobSize, transitionKnobs[i]); | |
123 | xPos += knobSize + knobSpacing; | |
124 | } | |
125 | yPos += knobSize + knobLabelHeight; | |
49815cc0 MS |
126 | |
127 | yPos += sectionSpacing; | |
128 | firstEffectY = yPos + lineHeight + 6; | |
129 | yPos = drawObjectList(yPos, "FX", effects, effectNames, effectStateMethod); | |
3f8be614 MS |
130 | yPos += controlSpacing; |
131 | firstEffectKnobY = yPos; | |
132 | xPos = leftTextPos; | |
133 | for (int i = 0; i < effectKnobs.length; ++i) { | |
134 | drawKnob(xPos, yPos, knobSize, effectKnobs[i]); | |
135 | xPos += knobSize + knobSpacing; | |
136 | } | |
137 | yPos += knobSize + knobLabelHeight; | |
49815cc0 MS |
138 | |
139 | yPos += sectionSpacing; | |
140 | yPos = drawObjectList(yPos, "TEMPO", null, null, null); | |
141 | yPos += 6; | |
142 | tempoY = yPos; | |
143 | stroke(#111111); | |
144 | fill(tempoDown ? lightGreen : color(0, 0, 35 - 8*lx.tempo.rampf())); | |
145 | rect(leftPos + 4, yPos, w - 8, tempoHeight); | |
146 | fill(0); | |
147 | textAlign(CENTER); | |
148 | text("" + ((int)(lx.tempo.bpmf() * 100) / 100.), leftPos + w/2., yPos + tempoHeight - 6); | |
149 | yPos += tempoHeight; | |
150 | ||
151 | fill(#999999); | |
152 | textFont(itemFont); | |
153 | textAlign(LEFT); | |
e8f5282d | 154 | text("Tap 'u' to hide UI", leftTextPos, height-6); |
49815cc0 MS |
155 | } |
156 | ||
3f8be614 | 157 | public LXParameter getOrNull(List<LXParameter> items, int index) { |
49815cc0 MS |
158 | if (index < items.size()) { |
159 | return items.get(index); | |
160 | } | |
161 | return null; | |
162 | } | |
163 | ||
164 | public void drawFPS() { | |
165 | textFont(titleFont); | |
166 | textAlign(LEFT); | |
167 | fill(#666666); | |
168 | text("FPS: " + (((int)(frameRate * 10)) / 10.), 4, height-6); | |
169 | } | |
170 | ||
171 | private final int STATE_DEFAULT = 0; | |
172 | private final int STATE_ACTIVE = 1; | |
173 | private final int STATE_PENDING = 2; | |
174 | ||
175 | public int getState(LXPattern p) { | |
176 | if (p == lx.getPattern()) { | |
177 | return STATE_ACTIVE; | |
178 | } else if (p == lx.getNextPattern()) { | |
179 | return STATE_PENDING; | |
180 | } | |
181 | return STATE_DEFAULT; | |
182 | } | |
183 | ||
184 | public int getState(LXEffect e) { | |
3f8be614 MS |
185 | if (e.isEnabled()) { |
186 | return STATE_PENDING; | |
187 | } else if (effects[activeEffectIndex] == e) { | |
188 | return STATE_ACTIVE; | |
189 | } | |
190 | return STATE_DEFAULT; | |
49815cc0 MS |
191 | } |
192 | ||
193 | public int getState(LXTransition t) { | |
194 | if (t == lx.getTransition()) { | |
195 | return STATE_PENDING; | |
196 | } else if (t == transitions[activeTransitionIndex]) { | |
197 | return STATE_ACTIVE; | |
198 | } | |
199 | return STATE_DEFAULT; | |
200 | } | |
201 | ||
202 | protected int drawObjectList(int yPos, String title, Object[] items, Method stateMethod) { | |
3f8be614 | 203 | return drawObjectList(yPos, title, items, classNameArray(items, null), stateMethod); |
49815cc0 MS |
204 | } |
205 | ||
206 | private int drawObjectList(int yPos, String title, Object[] items, String[] names, Method stateMethod) { | |
207 | noStroke(); | |
208 | fill(#aaaaaa); | |
209 | textFont(titleFont); | |
210 | textAlign(LEFT); | |
211 | text(title, leftTextPos, yPos += lineHeight); | |
212 | if (items != null) { | |
213 | textFont(itemFont); | |
214 | color textColor; | |
215 | boolean even = true; | |
216 | for (int i = 0; i < items.length; ++i) { | |
217 | Object o = items[i]; | |
218 | int state = STATE_DEFAULT; | |
219 | try { | |
220 | state = ((Integer) stateMethod.invoke(this, o)).intValue(); | |
221 | } catch (Exception x) { | |
222 | throw new RuntimeException(x); | |
223 | } | |
224 | switch (state) { | |
225 | case STATE_ACTIVE: | |
226 | fill(lightGreen); | |
227 | textColor = #eeeeee; | |
228 | break; | |
229 | case STATE_PENDING: | |
230 | fill(lightBlue); | |
231 | textColor = color(0, 0, 75 + 15*sin(millis()/200.));; | |
232 | break; | |
233 | default: | |
234 | textColor = 0; | |
235 | fill(even ? #666666 : #777777); | |
236 | break; | |
237 | } | |
238 | rect(leftPos, yPos+6, width, lineHeight); | |
239 | fill(textColor); | |
240 | text(names[i], leftTextPos, yPos += lineHeight); | |
241 | even = !even; | |
242 | } | |
243 | } | |
244 | return yPos; | |
245 | } | |
246 | ||
3f8be614 MS |
247 | private void drawKnob(int xPos, int yPos, int knobSize, LXParameter knob) { |
248 | if (!knobsOn) { | |
249 | return; | |
250 | } | |
49815cc0 MS |
251 | final float knobValue = knob.getValuef(); |
252 | String knobLabel = knob.getLabel(); | |
3f8be614 | 253 | if (knobLabel == null) { |
49815cc0 | 254 | knobLabel = "-"; |
3f8be614 MS |
255 | } else if (knobLabel.length() > 4) { |
256 | knobLabel = knobLabel.substring(0, 4); | |
49815cc0 MS |
257 | } |
258 | ||
259 | ellipseMode(CENTER); | |
260 | fill(#222222); | |
3f8be614 MS |
261 | // For some reason this arc call really crushes drawing performance. Presumably |
262 | // because openGL is drawing it and when we overlap the second set of arcs it | |
263 | // does a bunch of depth buffer intersection tests? Ellipse with a trapezoid cut out is faster | |
264 | // arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + (TWO_PI-2*knobIndent)); | |
265 | ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize); | |
266 | ||
267 | float endArc = HALF_PI + knobIndent + (TWO_PI-2*knobIndent)*knobValue; | |
49815cc0 | 268 | fill(lightGreen); |
3f8be614 MS |
269 | arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, endArc); |
270 | ||
271 | // Center circle of knob | |
49815cc0 MS |
272 | fill(#333333); |
273 | ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize/2, knobSize/2); | |
3f8be614 MS |
274 | |
275 | // Mask notch out of knob | |
276 | fill(color(0, 0, 30)); | |
277 | beginShape(); | |
278 | vertex(xPos + knobSize/2 - 3, yPos + knobSize - 8); | |
279 | vertex(xPos + knobSize/2 - 5, yPos + knobSize); | |
280 | vertex(xPos + knobSize/2 + 5, yPos + knobSize); | |
281 | vertex(xPos + knobSize/2 + 3, yPos + knobSize - 8); | |
282 | endShape(); | |
49815cc0 MS |
283 | |
284 | fill(0); | |
285 | rect(xPos, yPos + knobSize + 2, knobSize, knobLabelHeight - 2); | |
286 | fill(#999999); | |
287 | textAlign(CENTER); | |
288 | textFont(knobFont); | |
289 | text(knobLabel, xPos + knobSize/2, yPos + knobSize + knobLabelHeight - 2); | |
290 | ||
291 | } | |
292 | ||
3f8be614 | 293 | private String[] classNameArray(Object[] objects, String suffix) { |
49815cc0 MS |
294 | if (objects == null) { |
295 | return null; | |
296 | } | |
297 | String[] names = new String[objects.length]; | |
298 | for (int i = 0; i < objects.length; ++i) { | |
3f8be614 | 299 | names[i] = className(objects[i], suffix); |
49815cc0 MS |
300 | } |
301 | return names; | |
302 | } | |
303 | ||
3f8be614 | 304 | private String className(Object p, String suffix) { |
49815cc0 MS |
305 | String s = p.getClass().getName(); |
306 | int li; | |
307 | if ((li = s.lastIndexOf(".")) > 0) { | |
308 | s = s.substring(li + 1); | |
309 | } | |
310 | if (s.indexOf("SugarCubes$") == 0) { | |
3f8be614 MS |
311 | s = s.substring("SugarCubes$".length()); |
312 | } | |
313 | if ((suffix != null) && ((li = s.indexOf(suffix)) != -1)) { | |
314 | s = s.substring(0, li); | |
49815cc0 MS |
315 | } |
316 | return s; | |
317 | } | |
3f8be614 | 318 | |
809f3518 MS |
319 | class VirtualPatternKnob extends LXVirtualParameter { |
320 | private final int index; | |
321 | ||
322 | VirtualPatternKnob(int index) { | |
323 | this.index = index; | |
324 | } | |
325 | ||
326 | public LXParameter getRealParameter() { | |
327 | List<LXParameter> parameters = glucose.getPattern().getParameters(); | |
328 | if (index < parameters.size()) { | |
329 | return parameters.get(index); | |
330 | } | |
331 | return null; | |
332 | } | |
333 | } | |
334 | ||
3f8be614 MS |
335 | class VirtualTransitionKnob extends LXVirtualParameter { |
336 | private final int index; | |
337 | ||
338 | VirtualTransitionKnob(int index) { | |
339 | this.index = index; | |
340 | } | |
341 | ||
342 | public LXParameter getRealParameter() { | |
343 | List<LXParameter> parameters = transitions[activeTransitionIndex].getParameters(); | |
344 | if (index < parameters.size()) { | |
345 | return parameters.get(index); | |
346 | } | |
347 | return null; | |
348 | } | |
349 | } | |
350 | ||
351 | class VirtualEffectKnob extends LXVirtualParameter { | |
352 | private final int index; | |
353 | ||
354 | VirtualEffectKnob(int index) { | |
355 | this.index = index; | |
356 | } | |
357 | ||
358 | public LXParameter getRealParameter() { | |
359 | List<LXParameter> parameters = effects[activeEffectIndex].getParameters(); | |
360 | if (index < parameters.size()) { | |
361 | return parameters.get(index); | |
362 | } | |
363 | return null; | |
364 | } | |
365 | } | |
366 | ||
367 | private int patternKnobIndex = -1; | |
368 | private int transitionKnobIndex = -1; | |
369 | private int effectKnobIndex = -1; | |
49815cc0 | 370 | |
49815cc0 MS |
371 | private int lastY; |
372 | private int releaseEffect = -1; | |
373 | private boolean tempoDown = false; | |
374 | ||
375 | public void mousePressed() { | |
376 | lastY = mouseY; | |
3f8be614 | 377 | patternKnobIndex = transitionKnobIndex = effectKnobIndex = -1; |
49815cc0 MS |
378 | releaseEffect = -1; |
379 | if (mouseY > tempoY) { | |
380 | if (mouseY - tempoY < tempoHeight) { | |
381 | lx.tempo.tap(); | |
382 | tempoDown = true; | |
383 | } | |
3f8be614 MS |
384 | } else if ((mouseY >= firstEffectKnobY) && (mouseY < firstEffectKnobY + knobSize + knobLabelHeight)) { |
385 | effectKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing); | |
49815cc0 MS |
386 | } else if (mouseY > firstEffectY) { |
387 | int effectIndex = (mouseY - firstEffectY) / lineHeight; | |
388 | if (effectIndex < effects.length) { | |
3f8be614 | 389 | if (activeEffectIndex == effectIndex) { |
49815cc0 MS |
390 | effects[effectIndex].enable(); |
391 | releaseEffect = effectIndex; | |
49815cc0 | 392 | } |
3f8be614 | 393 | activeEffectIndex = effectIndex; |
49815cc0 | 394 | } |
3f8be614 MS |
395 | } else if ((mouseY >= firstTransitionKnobY) && (mouseY < firstTransitionKnobY + knobSize + knobLabelHeight)) { |
396 | transitionKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing); | |
49815cc0 MS |
397 | } else if (mouseY > firstTransitionY) { |
398 | int transitionIndex = (mouseY - firstTransitionY) / lineHeight; | |
399 | if (transitionIndex < transitions.length) { | |
400 | activeTransitionIndex = transitionIndex; | |
401 | } | |
3f8be614 MS |
402 | } else if ((mouseY >= firstPatternKnobY) && (mouseY < firstPatternKnobY + 2*(knobSize+knobLabelHeight) + knobSpacing)) { |
403 | patternKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing); | |
404 | if (mouseY >= firstPatternKnobY + knobSize + knobLabelHeight + knobSpacing) { | |
809f3518 | 405 | patternKnobIndex += NUM_PATTERN_KNOBS / 2; |
49815cc0 MS |
406 | } |
407 | } else if (mouseY > firstPatternY) { | |
408 | int patternIndex = (mouseY - firstPatternY) / lineHeight; | |
409 | if (patternIndex < patterns.length) { | |
410 | patterns[patternIndex].setTransition(transitions[activeTransitionIndex]); | |
411 | lx.goIndex(patternIndex); | |
412 | } | |
413 | } | |
414 | } | |
415 | ||
416 | public void mouseDragged() { | |
417 | int dy = lastY - mouseY; | |
418 | lastY = mouseY; | |
809f3518 MS |
419 | if (patternKnobIndex >= 0 && patternKnobIndex < NUM_PATTERN_KNOBS) { |
420 | LXParameter p = patternKnobs[patternKnobIndex]; | |
3f8be614 MS |
421 | p.setValue(constrain(p.getValuef() + dy*.01, 0, 1)); |
422 | } else if (effectKnobIndex >= 0 && effectKnobIndex < NUM_EFFECT_KNOBS) { | |
423 | LXParameter p = effectKnobs[effectKnobIndex]; | |
424 | p.setValue(constrain(p.getValuef() + dy*.01, 0, 1)); | |
425 | } else if (transitionKnobIndex >= 0 && transitionKnobIndex < NUM_TRANSITION_KNOBS) { | |
426 | LXParameter p = transitionKnobs[transitionKnobIndex]; | |
427 | p.setValue(constrain(p.getValuef() + dy*.01, 0, 1)); | |
49815cc0 MS |
428 | } |
429 | } | |
430 | ||
431 | public void mouseReleased() { | |
432 | tempoDown = false; | |
433 | if (releaseEffect >= 0) { | |
434 | effects[releaseEffect].trigger(); | |
435 | releaseEffect = -1; | |
436 | } | |
437 | } | |
3f8be614 | 438 | |
49815cc0 MS |
439 | } |
440 | ||
441 | void mousePressed() { | |
442 | if (mouseX > ui.leftPos) { | |
443 | ui.mousePressed(); | |
444 | } | |
445 | } | |
446 | ||
447 | void mouseReleased() { | |
448 | if (mouseX > ui.leftPos) { | |
449 | ui.mouseReleased(); | |
450 | } | |
451 | } | |
452 | ||
453 | void mouseDragged() { | |
454 | if (mouseX > ui.leftPos) { | |
455 | ui.mouseDragged(); | |
456 | } | |
457 | } | |
458 |