New Grizzly code using LXOutput framework
[SugarCubes.git] / _UIImplementation.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
11 * Custom UI components using the framework.
12 */
13
14 class UIBlendMode extends UIWindow {
15 public UIBlendMode(float x, float y, float w, float h) {
16 super(lx.ui, "BLEND MODE", x, y, w, h);
17 List<UIScrollList.Item> items = new ArrayList<UIScrollList.Item>();
18 for (LXTransition t : glucose.getTransitions()) {
19 items.add(new TransitionScrollItem(t));
20 }
21 final UIScrollList tList;
22 (tList = new UIScrollList(1, UIWindow.TITLE_LABEL_HEIGHT, w-2, 60)).setItems(items).addToContainer(this);
23
24 lx.engine.getDeck(GLucose.RIGHT_DECK).addListener(new LXDeck.AbstractListener() {
25 public void faderTransitionDidChange(LXDeck deck, LXTransition transition) {
26 tList.redraw();
27 }
28 });
29 }
30
31 class TransitionScrollItem extends UIScrollList.AbstractItem {
32 private final LXTransition transition;
33 private String label;
34
35 TransitionScrollItem(LXTransition transition) {
36 this.transition = transition;
37 label = className(transition, "Transition");
38 }
39
40 public String getLabel() {
41 return label;
42 }
43
44 public boolean isSelected() {
45 return transition == glucose.getSelectedTransition();
46 }
47
48 public boolean isPending() {
49 return false;
50 }
51
52 public void onMousePressed() {
53 glucose.setSelectedTransition(transition);
54 }
55 }
56
57 }
58
59 class UICrossfader extends UIWindow {
60
61 private final UIToggleSet displayMode;
62
63 public UICrossfader(float x, float y, float w, float h) {
64 super(lx.ui, "CROSSFADER", x, y, w, h);
65
66 new UISlider(4, UIWindow.TITLE_LABEL_HEIGHT, w-9, 32).setParameter(lx.engine.getDeck(GLucose.RIGHT_DECK).getFader()).addToContainer(this);
67 (displayMode = new UIToggleSet(4, UIWindow.TITLE_LABEL_HEIGHT + 36, w-9, 20)).setOptions(new String[] { "A", "COMP", "B" }).setValue("COMP").addToContainer(this);
68 }
69
70 public UICrossfader setDisplayMode(String value) {
71 displayMode.setValue(value);
72 return this;
73 }
74
75 public String getDisplayMode() {
76 return displayMode.getValue();
77 }
78 }
79
80 class UIEffects extends UIWindow {
81 UIEffects(float x, float y, float w, float h) {
82 super(lx.ui, "FX", x, y, w, h);
83
84 int yp = UIWindow.TITLE_LABEL_HEIGHT;
85 List<UIScrollList.Item> items = new ArrayList<UIScrollList.Item>();
86 for (LXEffect fx : glucose.lx.getEffects()) {
87 items.add(new FXScrollItem(fx));
88 }
89 final UIScrollList effectsList = new UIScrollList(1, yp, w-2, 60).setItems(items);
90 effectsList.addToContainer(this);
91 yp += effectsList.getHeight() + 10;
92
93 final UIKnob[] parameterKnobs = new UIKnob[4];
94 for (int ki = 0; ki < parameterKnobs.length; ++ki) {
95 parameterKnobs[ki] = new UIKnob(5 + 34*(ki % 4), yp + (ki/4) * 48);
96 parameterKnobs[ki].addToContainer(this);
97 }
98
99 GLucose.EffectListener fxListener = new GLucose.EffectListener() {
100 public void effectSelected(LXEffect effect) {
101 int i = 0;
102 for (LXParameter p : effect.getParameters()) {
103 if (i >= parameterKnobs.length) {
104 break;
105 }
106 if (p instanceof BasicParameter) {
107 parameterKnobs[i++].setParameter((BasicParameter) p);
108 }
109 }
110 while (i < parameterKnobs.length) {
111 parameterKnobs[i++].setParameter(null);
112 }
113 }
114 };
115
116 glucose.addEffectListener(fxListener);
117 fxListener.effectSelected(glucose.getSelectedEffect());
118
119 }
120
121 class FXScrollItem extends UIScrollList.AbstractItem {
122
123 private LXEffect effect;
124 private String label;
125
126 FXScrollItem(LXEffect effect) {
127 this.effect = effect;
128 label = className(effect, "Effect");
129 }
130
131 public String getLabel() {
132 return label;
133 }
134
135 public boolean isSelected() {
136 return !effect.isEnabled() && (glucose.getSelectedEffect() == effect);
137 }
138
139 public boolean isPending() {
140 return effect.isEnabled();
141 }
142
143 public void onMousePressed() {
144 if (glucose.getSelectedEffect() == effect) {
145 if (effect.isMomentary()) {
146 effect.enable();
147 } else {
148 effect.toggle();
149 }
150 } else {
151 glucose.setSelectedEffect(effect);
152 }
153 }
154
155 public void onMouseReleased() {
156 if (effect.isMomentary()) {
157 effect.disable();
158 }
159 }
160
161 }
162
163 }
164
165 class UIOutput extends UIWindow {
166 public UIOutput(float x, float y, float w, float h) {
167 super(lx.ui, "OUTPUT", x, y, w, h);
168 float yp = UIWindow.TITLE_LABEL_HEIGHT;
169
170 final UIScrollList outputs = new UIScrollList(1, UIWindow.TITLE_LABEL_HEIGHT, w-2, 80);
171
172 List<UIScrollList.Item> items = new ArrayList<UIScrollList.Item>();
173 for (final PandaDriver panda : pandaBoards) {
174 items.add(new PandaScrollItem(panda));
175 panda.setListener(new PandaDriver.Listener() {
176 public void onToggle(boolean active) {
177 outputs.redraw();
178 }
179 });
180 }
181 outputs.setItems(items).addToContainer(this);
182 }
183
184 class PandaScrollItem extends UIScrollList.AbstractItem {
185 final PandaDriver panda;
186 PandaScrollItem(PandaDriver panda) {
187 this.panda = panda;
188 }
189
190 public String getLabel() {
191 return panda.ip;
192 }
193
194 public boolean isSelected() {
195 return panda.isEnabled();
196 }
197
198 public void onMousePressed() {
199 panda.toggle();
200 }
201 }
202 }
203
204 class UITempo extends UIWindow {
205
206 private final UIButton tempoButton;
207
208 UITempo(float x, float y, float w, float h) {
209 super(lx.ui, "TEMPO", x, y, w, h);
210 tempoButton = new UIButton(4, UIWindow.TITLE_LABEL_HEIGHT, w-10, 20) {
211 protected void onToggle(boolean active) {
212 if (active) {
213 lx.tempo.tap();
214 }
215 }
216 }.setMomentary(true);
217 tempoButton.addToContainer(this);
218 new UITempoBlipper(8, UIWindow.TITLE_LABEL_HEIGHT + 5, 12, 12).addToContainer(this);
219 }
220
221 class UITempoBlipper extends UIObject {
222 UITempoBlipper(float x, float y, float w, float h) {
223 super(x, y, w, h);
224 }
225
226 void onDraw(UI ui, PGraphics pg) {
227 tempoButton.setLabel("" + ((int)(lx.tempo.bpm() * 10)) / 10.);
228
229 // Overlay tempo thing with openGL, redraw faster than button UI
230 pg.fill(color(0, 0, 24 - 8*lx.tempo.rampf()));
231 pg.noStroke();
232 pg.rect(0, 0, width, height);
233
234 redraw();
235 }
236 }
237
238 }
239
240 class UIMapping extends UIWindow {
241
242 private static final String MAP_MODE_ALL = "ALL";
243 private static final String MAP_MODE_CHANNEL = "CHNL";
244 private static final String MAP_MODE_CUBE = "CUBE";
245
246 private static final String CUBE_MODE_ALL = "ALL";
247 private static final String CUBE_MODE_STRIP = "SNGL";
248 private static final String CUBE_MODE_PATTERN = "PTRN";
249
250 private final MappingTool mappingTool;
251
252 private final UIIntegerBox channelBox;
253 private final UIIntegerBox cubeBox;
254 private final UIIntegerBox stripBox;
255
256 UIMapping(MappingTool tool, float x, float y, float w, float h) {
257 super(lx.ui, "MAPPING", x, y, w, h);
258 mappingTool = tool;
259
260 int yp = UIWindow.TITLE_LABEL_HEIGHT;
261 new UIToggleSet(4, yp, w-10, 20) {
262 protected void onToggle(String value) {
263 if (value == MAP_MODE_ALL) mappingTool.mappingMode = mappingTool.MAPPING_MODE_ALL;
264 else if (value == MAP_MODE_CHANNEL) mappingTool.mappingMode = mappingTool.MAPPING_MODE_CHANNEL;
265 else if (value == MAP_MODE_CUBE) mappingTool.mappingMode = mappingTool.MAPPING_MODE_SINGLE_CUBE;
266 }
267 }.setOptions(new String[] { MAP_MODE_ALL, MAP_MODE_CHANNEL, MAP_MODE_CUBE }).addToContainer(this);
268 yp += 24;
269 new UILabel(4, yp+8, w-10, 20).setLabel("CHANNEL ID").addToContainer(this);
270 yp += 24;
271 (channelBox = new UIIntegerBox(4, yp, w-10, 20) {
272 protected void onValueChange(int value) {
273 mappingTool.setChannel(value-1);
274 }
275 }).setRange(1, mappingTool.numChannels()).addToContainer(this);
276 yp += 24;
277
278 new UILabel(4, yp+8, w-10, 20).setLabel("CUBE ID").addToContainer(this);
279 yp += 24;
280 (cubeBox = new UIIntegerBox(4, yp, w-10, 20) {
281 protected void onValueChange(int value) {
282 mappingTool.setCube(value-1);
283 }
284 }).setRange(1, glucose.model.cubes.size()).addToContainer(this);
285 yp += 24;
286
287 yp += 10;
288
289 new UIScrollList(1, yp, w-2, 60).setItems(Arrays.asList(new UIScrollList.Item[] {
290 new ColorScrollItem(ColorScrollItem.COLOR_RED),
291 new ColorScrollItem(ColorScrollItem.COLOR_GREEN),
292 new ColorScrollItem(ColorScrollItem.COLOR_BLUE),
293 })).addToContainer(this);
294 yp += 64;
295
296 new UILabel(4, yp+8, w-10, 20).setLabel("STRIP MODE").addToContainer(this);
297 yp += 24;
298
299 new UIToggleSet(4, yp, w-10, 20) {
300 protected void onToggle(String value) {
301 if (value == CUBE_MODE_ALL) mappingTool.cubeMode = mappingTool.CUBE_MODE_ALL;
302 else if (value == CUBE_MODE_STRIP) mappingTool.cubeMode = mappingTool.CUBE_MODE_SINGLE_STRIP;
303 else if (value == CUBE_MODE_PATTERN) mappingTool.cubeMode = mappingTool.CUBE_MODE_STRIP_PATTERN;
304 }
305 }.setOptions(new String[] { CUBE_MODE_ALL, CUBE_MODE_STRIP, CUBE_MODE_PATTERN }).addToContainer(this);
306
307 yp += 24;
308 new UILabel(4, yp+8, w-10, 20).setLabel("STRIP ID").addToContainer(this);
309
310 yp += 24;
311 (stripBox = new UIIntegerBox(4, yp, w-10, 20) {
312 protected void onValueChange(int value) {
313 mappingTool.setStrip(value-1);
314 }
315 }).setRange(1, Cube.STRIPS_PER_CUBE).addToContainer(this);
316
317 }
318
319 public void setChannelID(int value) {
320 channelBox.setValue(value);
321 }
322
323 public void setCubeID(int value) {
324 cubeBox.setValue(value);
325 }
326
327 public void setStripID(int value) {
328 stripBox.setValue(value);
329 }
330
331 class ColorScrollItem extends UIScrollList.AbstractItem {
332
333 public static final int COLOR_RED = 1;
334 public static final int COLOR_GREEN = 2;
335 public static final int COLOR_BLUE = 3;
336
337 private final int colorChannel;
338
339 ColorScrollItem(int colorChannel) {
340 this.colorChannel = colorChannel;
341 }
342
343 public String getLabel() {
344 switch (colorChannel) {
345 case COLOR_RED: return "Red";
346 case COLOR_GREEN: return "Green";
347 case COLOR_BLUE: return "Blue";
348 }
349 return "";
350 }
351
352 public boolean isSelected() {
353 switch (colorChannel) {
354 case COLOR_RED: return mappingTool.channelModeRed;
355 case COLOR_GREEN: return mappingTool.channelModeGreen;
356 case COLOR_BLUE: return mappingTool.channelModeBlue;
357 }
358 return false;
359 }
360
361 public void onMousePressed() {
362 switch (colorChannel) {
363 case COLOR_RED: mappingTool.channelModeRed = !mappingTool.channelModeRed; break;
364 case COLOR_GREEN: mappingTool.channelModeGreen = !mappingTool.channelModeGreen; break;
365 case COLOR_BLUE: mappingTool.channelModeBlue = !mappingTool.channelModeBlue; break;
366 }
367 }
368 }
369 }
370
371 class UIDebugText extends UIContext {
372
373 private String line1 = "";
374 private String line2 = "";
375
376 UIDebugText(float x, float y, float w, float h) {
377 super(lx.ui, x, y, w, h);
378 }
379
380 public UIDebugText setText(String line1) {
381 return setText(line1, "");
382 }
383
384 public UIDebugText setText(String line1, String line2) {
385 if (!line1.equals(this.line1) || !line2.equals(this.line2)) {
386 this.line1 = line1;
387 this.line2 = line2;
388 setVisible(line1.length() + line2.length() > 0);
389 redraw();
390 }
391 return this;
392 }
393
394 protected void onDraw(UI ui, PGraphics pg) {
395 super.onDraw(ui, pg);
396 if (line1.length() + line2.length() > 0) {
397 pg.noStroke();
398 pg.fill(#444444);
399 pg.rect(0, 0, width, height);
400 pg.textFont(ui.getItemFont());
401 pg.textSize(10);
402 pg.textAlign(LEFT, TOP);
403 pg.fill(#cccccc);
404 pg.text(line1, 4, 4);
405 pg.text(line2, 4, 24);
406 }
407 }
408 }
409
410 class UISpeed extends UIWindow {
411
412 final BasicParameter speed;
413
414 UISpeed(float x, float y, float w, float h) {
415 super(lx.ui, "SPEED", x, y, w, h);
416 speed = new BasicParameter("SPEED", 0.5);
417 speed.addListener(new LXParameterListener() {
418 public void onParameterChanged(LXParameter parameter) {
419 lx.setSpeed(parameter.getValuef() * 2);
420 }
421 });
422 new UISlider(4, UIWindow.TITLE_LABEL_HEIGHT, w-10, 20).setParameter(speed).addToContainer(this);
423 }
424 }
425
426 class UIMidi extends UIWindow {
427
428 private final UIToggleSet deckMode;
429 private final UIButton logMode;
430
431 UIMidi(final MidiEngine midiEngine, float x, float y, float w, float h) {
432 super(lx.ui, "MIDI", x, y, w, h);
433
434 // Processing compiler doesn't seem to get that list of class objects also conform to interface
435 List<UIScrollList.Item> scrollItems = new ArrayList<UIScrollList.Item>();
436 for (SCMidiInput mc : midiEngine.getControllers()) {
437 scrollItems.add(mc);
438 }
439 final UIScrollList scrollList;
440 (scrollList = new UIScrollList(1, UIWindow.TITLE_LABEL_HEIGHT, w-2, 100)).setItems(scrollItems).addToContainer(this);
441 (deckMode = new UIToggleSet(4, 130, 90, 20) {
442 protected void onToggle(String value) {
443 midiEngine.setFocusedDeck(value == "A" ? 0 : 1);
444 }
445 }).setOptions(new String[] { "A", "B" }).addToContainer(this);
446 (logMode = new UIButton(98, 130, w-103, 20)).setLabel("LOG").addToContainer(this);
447
448 SCMidiInputListener listener = new SCMidiInputListener() {
449 public void onEnabled(SCMidiInput controller, boolean enabled) {
450 scrollList.redraw();
451 }
452 };
453 for (SCMidiInput mc : midiEngine.getControllers()) {
454 mc.addListener(listener);
455 }
456
457 midiEngine.addListener(new MidiEngineListener() {
458 public void onFocusedDeck(int deckIndex) {
459 deckMode.setValue(deckIndex == 0 ? "A" : "B");
460 }
461 });
462
463 }
464
465 public boolean logMidi() {
466 return logMode.isActive();
467 }
468
469 public LXDeck getFocusedDeck() {
470 return lx.engine.getDeck(deckMode.getValue() == "A" ? GLucose.LEFT_DECK : GLucose.RIGHT_DECK);
471 }
472 }
473
474 String className(Object p, String suffix) {
475 String s = p.getClass().getName();
476 int li;
477 if ((li = s.lastIndexOf(".")) > 0) {
478 s = s.substring(li + 1);
479 }
480 if (s.indexOf("SugarCubes$") == 0) {
481 s = s.substring("SugarCubes$".length());
482 }
483 if ((suffix != null) && ((li = s.indexOf(suffix)) != -1)) {
484 s = s.substring(0, li);
485 }
486 return s;
487 }