// version 1.0 class guiElement { String name = ""; float width, height, x0, y0; boolean update() {return false;} boolean offerMousePress() {return false;} float[] position() { float[] r = {x0, y0, width, height}; return r; } } class button extends guiElement { color neutralColor, activeColor, highlightColor; color backgroundColor = color(255); // these make a color palette, that can be used in different ways. // use foreColor() and bkgndColor() for drawing. boolean active = false; boolean pressed = false; boolean hidden = false; int thetextsize = 9; float leading = 0.8; void construct(String name0, float[] pos, color neutral, color actv) { neutralColor = neutral; activeColor = actv; highlightColor = darken(activeColor); x0 = pos[0]; y0 = pos[1]; width = pos[2]; height = pos[3]; name = name0; } void drawFace() { noStroke(); fill(foreColor()); rectMode(CORNER); rect(x0,y0,width,height); } void drawName() { fill(bkgndColor()); textAlign(CENTER); textSize(thetextsize); textLeading(leading*thetextsize); text(name,x0+width/2,y0+height/2+thetextsize/2); } color foreColor() { if (pressed) { return(highlightColor); } else if (over()) { return(activeColor); } else { return(neutralColor); } } color bkgndColor() { return backgroundColor; } void draw() { if (!hidden) { drawFace(); drawName(); } } boolean over() { return (((mouseX>=x0) && (mouseX<=x0+width)) && ((mouseY>=y0) && (mouseY<=y0+height))); } boolean offerMousePress() { pressed = over() && (!hidden); return pressed; } boolean update() { // returns true when the button is released. boolean result = false; if (pressed) { active = over(); pressed = mousePressed; } if (active && (!mousePressed)) { pressed = false; active = false; result = true; } draw(); return result; } } class textButton extends button { textButton(String name0, float[] pos, color neutral, color actv) { construct(name0, pos, neutral, actv); thetextsize = round(height/3); } } class polyButton extends button { float[] px,py; // coords of the polygon boolean drawSecondPoly = false; float[] px2, py2; // optional second polygon, for more complex shapes boolean drawCutoutPoly = false; float[] cx,cy; // option extra polygon in bkgndColor() instead of foreColor() boolean showName = true; polyButton(String name0, float[] pos, color neutral, color actv, float[] xx, float[] yy) { construct(name0, pos, neutral, actv); thetextsize = round(height/3); definePoly(xx,yy); } void definePoly(float[] xx, float[] yy) { px = xx; py = yy; } void defineSecondPoly(float[] xx, float[] yy) { drawSecondPoly = true; px2 = xx; py2 = yy; } void defineCutoutPoly(float[] xx, float[] yy, color col) { drawCutoutPoly = true; cx = xx; cy = yy; } void drawFace() { fill(foreColor()); noStroke(); beginShape(POLYGON); for (int i=0; i