/* built with Studio Sketchpad: * https://sketchpad.cc * * observe the evolution of this sketch: * https://studio.sketchpad.cc/sp/pad/view/ro.$7$tT1-oKam/rev.34 * * authors: * GoToLoop * license (unless otherwise specified): * creative commons attribution-share alike 3.0 license. * https://creativecommons.org/licenses/by-sa/3.0/ */ /** * Buttons on Android (v2.23) * by jimthree (2012/Oct/31) * tweaked by GoToLoop * * http://forum.processing.org/topic/improving-responsiveness-of-laggy-buttons * http://studio.processingtogether.com/sp/pad/export/ro.9vAkI4JDuLs8s/latest * */ final static color light = #00688D; final static color dark = #002157; final static color txt = 255; final static color bg = 0; final static byte txtSize = 14; final static byte maxLines = 16; final static byte thickness = 2; final static byte activeDelay = 2; final static byte fps = 15; //final static List<CharSequence> debugText = new ArrayList(maxLines); final static List<CharSequence> debugText = new ArrayList(); final static byte uiElemNum = 35; final static uiElem[] uiElems = new uiElem[uiElemNum]; final class uiElem { int x, y, w, h; int xw, yh, lx, ly; byte active = 1; String label; uiElem(int xx, int yy, int ww, int hh, int lxx, int lyy, String caption) { x = xx; y = yy; w = ww; h = hh; xw = xx+ww; yh = yy+hh; lx = xx + lxx; ly = yy + lyy; label = caption; } final void drawElem() { rect(x, y, w, h); fill(txt); text(label, lx, ly); } final void drawActiveElem() { fill(light); drawElem(); active = activeDelay; } final void drawInactiveElem() { fill(dark); drawElem(); } final void updateTextArea() { if (debugText.size() == maxLines) debugText.remove(0); debugText.add(label + " pressed"); drawTextArea(40, 500, 1050, 250, 10, 10); } final void drawActivePlusUpdateText() { drawActiveElem(); updateTextArea(); } final boolean checkButtonPress() { return mouseX>x && mouseX<xw && mouseY>y && mouseY<yh; } } final void setup() { //orientation(LANDSCAPE); size(1280, 800, P2D); frameRate(fps); background(bg); strokeWeight(thickness); stroke(light); textSize(txtSize - 2); textAlign(LEFT, TOP); addUiElems(); drawTextArea(40, 500, 1050, 250, 10, 10); } final void draw() { drawUiElems(); } final void mousePressed() { final int mx = mouseX, my = mouseY; byte minRange = 24, maxRange = uiElemNum; if ( mx<640 && my<450 && mx>40 && my>50 ) { maxRange = minRange; minRange = 0; } else if ( mx<1141 && my>449 || mx<41 || my<51 || mx>1239 || my>749 ) maxRange = minRange = 0; for (byte ui=maxRange; ui!=minRange;) if ( uiElems[--ui].checkButtonPress() ) { uiElems[ui].drawActivePlusUpdateText(); loop(); break; } } final void drawUiElems() { byte nullCounter = uiElemNum; for (uiElem ui: uiElems) { if (ui.active == 1) ui.drawInactiveElem(); if (ui.active > 0) --ui.active; else --nullCounter; } if (nullCounter == 0) noLoop(); } final void drawTextArea (int x, int y, int w, int h, final int px, final int py) { fill(dark); rect(x, y, w, h); fill(txt); x += px; y += py; w -= px; h -= py; for ( int i = debugText.size(); i != 0; ) text( debugText.get(--i).toString(), x, txtSize*i + y, w, h ); } final void addUiElems() { final String led = "LED "; for (byte row=0, pos=0; row!=4; ++row) for (byte col=0; col!=6; ++col) uiElems[pos++] = new uiElem(40 + col*100, 50 + row*100, 100, 100, 5, 5, led+pos); uiElems[24] = new uiElem(690, 50, 100, 100, 5, 5, "F1"); uiElems[25] = new uiElem(840, 50, 100, 100, 5, 5, "F2"); uiElems[26] = new uiElem(990, 50, 100, 100, 5, 5, "F3"); uiElems[27] = new uiElem(1140, 50, 100, 100, 5, 5, "F4"); uiElems[28] = new uiElem(690, 200, 100, 250, 5, 5, "Slide1"); uiElems[29] = new uiElem(840, 200, 100, 250, 5, 5, "Slide2"); uiElems[30] = new uiElem(990, 200, 100, 250, 5, 5, "Slide3"); uiElems[31] = new uiElem(1140, 200, 100, 250, 5, 5, "Slide4"); uiElems[32] = new uiElem(1140, 500, 100, 50, 5, 5, "App1"); uiElems[33] = new uiElem(1140, 600, 100, 50, 5, 5, "App2"); uiElems[34] = new uiElem(1140, 700, 100, 50, 5, 5, "App3"); }