here's some stuff GUI code i've written for my current project
Makes a panel to hold other GUI elements, panels can be laid inside each other using the second constructor to specify the panel's parent.
Quote:class Panel{
int x,y,w,h;
int sX,sY;
Panel parent;
Panel(int ix, int iy, int iw, int ih){
x=ix;y=iy;w=iw;h=ih;
sX=ix;sY=iy;
}
Panel(Panel p,int ix, int iy, int iw, int ih){
parent=p;
x=ix;y=iy;w=iw;h=ih;
sX=parent.sX+x;sY=parent.sY+y;
}
void update(){
stroke(panelEdge);
fill(panelBG);
try{
rect(x+parent.x,y+parent.y,w,h);
} catch (Exception e){
rect(x,y,w,h);
}
}
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}
}
Base Button class
Quote:class Button {
int x,y,w,h;
boolean over, pressed, locked = false;
color backC = bFill;
color edgeC = panelEdge;
color overC = bOver;
int wait;
Panel parent;
Button(int ix, int iy, int ih, int iw, Panel p){
x=ix; y=iy; h=ih; w=iw;
parent = p;
}
void update(){
}
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= parent.sX+x && mouseX <= parent.sX+x+width &&
mouseY >= parent.sY+y && mouseY <= parent.sY+y+height) {
return true;
}
else {
return false;
}
}
}
A Text button extension
Quote:class TextButton extends Button{
String label;
TextButton(Panel p, String txt, int ix, int iy, int iw, int ih){
super(ix, iy, 14+ih, iw, p);
label=txt;
}
TextButton copy(){
return this;
}
void update(){
fill(backC);
edgeC=panelEdge;
if (overRect(x,y,w,h)){
edgeC=bOver;
if(mLeft){
pressed=true;
edgeC=select;
}
}
if(pressed==true){
edgeC=select;
}
stroke(edgeC);
rect(parent.sX+x,parent.sY+y,w,h);
textAlign(CENTER);
fill(edgeC);
//text(label, parent.sX+x+(w*0.5),parent.sY+y+(h*0.5));
text(label, parent.sX+x+(w*0.5),parent.sY+y+(h*0.75)-((h-14)*0.25));
}
}
I would then build GUI elements by creating a class, and adding all the bits i need and then updating them every call.
I should however point out that this is WRONG.
If i were to rewrite this again, i'd do it differently, i'd have everything extend a formElement class that would contain loads of common variables and functions.
I hope that helps a bit.
Martin