public class ButtonStatus { boolean status; public ButtonStatus(boolean _status) { status = _status; } public ButtonStatus() { this(false); } public boolean isActive() { return status; } public void setActive(boolean _status) { status = _status; } public void flip() { status = !status; } } public class Button { PFont bFont; public ButtonStatus active = new ButtonStatus(true); Action action = null; int fontSize; float bWidth = 10; float bHeight = 10; float border; public float totalWidth; public float totalHeight; public float xpos; public float ypos; public int mouseXOff = 0, mouseYOff = 0; String buttonLabel; public Button( PFont _font, int _fontSize) { bFont = _font; fontSize = _fontSize; } public void setup(int newX, int newY, String newLabel, Action newAction, float _border) { xpos = newX; ypos = newY; action = newAction; textFont(bFont, fontSize); totalHeight = bHeight; border = _border; setLabel(newLabel); } public void setLabel(String newLabel) { buttonLabel = newLabel; totalWidth = bWidth + 4 + round(textWidth(buttonLabel)); } public boolean inButtonBound(float x, float y) { if ((x >= xpos - border) && (y >= ypos - border) && (x <= xpos + totalWidth + border) && (y <= ypos + totalHeight + border)) return true; else return false; } public void pressed() { active.flip(); if (active.isActive() && (action != null)) action.doActiveAction(); if (!active.isActive() && (action != null)) action.doInactiveAction(); } public void display() { boolean within = inButtonBound(mouseX + mouseXOff, mouseY + mouseYOff); float fWidth = totalWidth+border*2; float fHeight = totalHeight+border*2; drawButtonField(xpos-border, ypos-border, fWidth, fHeight,within); drawLabel(buttonLabel, xpos + bWidth + 4, ypos + 10, within); if (active.isActive() ) fill(255); else fill(0); float nxpos = xpos; if (buttonLabel == "") nxpos += border/2; rect(nxpos, ypos, bWidth, bHeight); } public void drawButtonField(float x,float y,float width,float height,boolean within) { if (within) { stroke(255); fill(160); } else { stroke(150); fill(80); } rect(x, y, width, height); } public void drawLabel(String label, float x, float y, boolean within) { textFont(bFont, fontSize); if (within) { fill(0); } else{ fill(200); } text(label, x, y); } public void setMouseOff(int xoff, int yoff) { mouseXOff = xoff; mouseYOff = yoff; } } public class OnceButton extends Button { public OnceButton( PFont _font, int _fontSize) { super( _font, _fontSize); } public void pressed() { action.doActiveAction(); } public void display() { boolean within = inButtonBound(mouseX+mouseXOff, mouseY+mouseYOff); rect(xpos-3, ypos-3, totalWidth+6, totalHeight+6); drawButtonField(xpos-3, ypos-border, totalWidth+border*2, totalHeight+border*2,within); drawLabel(buttonLabel, xpos + 4, ypos + 10, within); } } public class Action { public void doActiveAction() { //println("Do action..."); } public void doInactiveAction() { //println("Don't do action..."); } }