class Menu { int x, y; PFont font; java.util.List togglebuttons; java.util.List radiobuttons; boolean SHOW_SAMPLES = false; Menu(int x, int y) { this.x = x; this.y = y; font = loadFont("BitstreamVeraSans-Roman-10.vlw"); textFont(font, 10); togglebuttons = new java.util.ArrayList(); togglebuttons.add(new ToggleButton(this, 0, true, "display links", 10, 10, 10, 10)); togglebuttons.add(new ToggleButton(this, 1, false, "display signals", 10, 30, 10, 10)); togglebuttons.add(new ToggleButton(this, 2, false, "display sensors", 10, 50, 10, 10)); radiobuttons = new java.util.ArrayList(); for (int i = 0; i < samples.length; i++) radiobuttons.add(new RadioButton(this, i, samples[i].file, 10, 110 + i*14, 8, 8)); } void handleMouseMoved() { for (java.util.ListIterator it = togglebuttons.listIterator(); it.hasNext(); ) { ToggleButton b = (ToggleButton)it.next(); b.testIfMouseOver(); } if (SHOW_SAMPLES) { for (java.util.ListIterator it = radiobuttons.listIterator(); it.hasNext(); ) { RadioButton b = (RadioButton)it.next(); b.testIfMouseOver(); } } } void handleMousePressed() { for (java.util.ListIterator it = togglebuttons.listIterator(); it.hasNext(); ) { ToggleButton b = (ToggleButton)it.next(); b.testIfPressed(); } if (SHOW_SAMPLES) { for (java.util.ListIterator it = radiobuttons.listIterator(); it.hasNext(); ) { RadioButton b = (RadioButton)it.next(); b.testIfPressed(); } } } void display() { fill(200); rect(x, y, width - x, height - y); fill(100); text("left button: click & drag to move the shapes.\nA-Z: rotate. | right button: sample preview.\ndouble-click: start playing. have fun! ;-)", 10, 10, width, height); text("BPM : " + bpm, x + 10, 80); for (java.util.ListIterator it = togglebuttons.listIterator(); it.hasNext(); ) { ToggleButton b = (ToggleButton)it.next(); b.display(); } // menu spécifique au sample sélectionné if (selShape != null) { text("change sample :", x + 10, y + 100); SHOW_SAMPLES = true; for (java.util.ListIterator it = radiobuttons.listIterator(); it.hasNext(); ) { RadioButton b = (RadioButton)it.next(); if (b.id == selShape.sampleNum) b.pushed = true; else b.pushed = false; b.display(); } } else { SHOW_SAMPLES = false; } } } class ToggleButton { Menu root; int id; boolean pushed = false; boolean hover = false; String label; int x, y, w, h; ToggleButton(Menu root, int id, boolean pushedAtStart, String label, int x, int y, int w, int h) { this.root = root; this.id = id; if (pushedAtStart) push(); this.label = label; this.x = root.x + x; this.y = root.y + y; this.w = w; this.h = h; } void display() { stroke(0); strokeWeight(1); fill(100); if (hover) fill(255); if (pushed) fill(200, 100, 100); rect(x, y, w, h); fill(0); text(label, x + w + 5, y + h - 1); } void testIfPressed() { if (isUnderMouse()) push(); } void push() { pushed = !pushed; switch (id) { //display links case 0: VIEW_LINKS = !VIEW_LINKS; break; //display signals case 1: VIEW_SIGNALS = !VIEW_SIGNALS; break; //display sensors case 2: VIEW_SENSORS = !VIEW_SENSORS; break; } } void testIfMouseOver() { hover = isUnderMouse(); } boolean isUnderMouse() { if (mouseX >= x && mouseX <= x + w && mouseY >= y && mouseY <= y + h) return true; return false; } } class RadioButton { Menu root; int id; boolean pushed = false; boolean hover = false; String label; int x, y, w, h; RadioButton(Menu root, int id, String label, int x, int y, int w, int h) { this.root = root; this.id = id; this.label = label; this.x = root.x + x; this.y = root.y + y; this.w = w; this.h = h; } void display() { stroke(0); strokeWeight(1); fill(100); if (hover) fill(255); if (pushed) fill(200, 100, 100); rect(x, y, w, h); fill(0); text(label, x + w + 5, y + h); } void testIfPressed() { if (!isUnderMouse()) return; pushed = true; selShape.setSample(id); for (java.util.ListIterator it = root.radiobuttons.listIterator(); it.hasNext(); ) { RadioButton b = (RadioButton)it.next(); if (b != this) b.pushed = false; } } void testIfMouseOver() { hover = isUnderMouse(); } boolean isUnderMouse() { if (mouseY < y || mouseY > y + h) return false; if (mouseX < x || mouseX > x + 5 + w + textWidth(label)) return false; return true; } }