Ok so I'm back at work now and things just get more an more busy. I had a spare moment to see if I can figure out how to get an event driven button class to be a composite object of a layout class.
I have pretty much just guessed how it might work due to my limited time. Could anyone tell me what rules I am breaking here and what the work arounds might be?
I understand if that's asking too much. I'd be happy event if someone could just point me in the right direction!
Thanks.
Quote:Layout lo;// Declare the object
void setup() {
size(100, 100);
smooth();
noStroke();
lo = new Layout();
}
void draw() {
background(0);
lo.display();
}
class Layout {
PImage BaseImage_01 = loadImage("keyboard_01_off.jpg"); //load "01_off"
PImage PressImage_01 = loadImage("keyboard_01_hit.jpg"); //load "01_off"
Spot sp1 = new Spot(20,70,30);
Spot sp2 = new Spot(50,25,30);
Button overButton = null;
Button clickedButton = null;
Button releasedButton = null;
Button b = new Button(200, 200, 52, color(204), color(255), color(102),BaseImage_01,PressImage_01, 2);
registerDraw(b);
registerMouseEvent(b);
void display() {
sp1.display();
sp2.display();
if(overButton != null)
println("Mouse is over " + overButton.id);
if(clickedButton != null)
println("Mouse has clicked " + clickedButton.id);
if(releasedButton != null)
println("Mouse has released " + releasedButton.id);
// These must be last 2 lines in draw method
overButton = null;
clickedButton = null;
releasedButton = null;
}
}
class Spot {
float x, y, diameter;
Spot(float xpos, float ypos, float dia) {
x = xpos; // Assign 33 to x
y = ypos; // Assign 50 to y
diameter = dia; // Assign 30 to diameter
}
void display() {
ellipse(x, y, diameter, diameter);
}
}
public class Button {
int x, y;
int size;
color baseGray;
color overGray;
color pressGray;
boolean over = false;
boolean pressed = false;
boolean released = false;
PImage baseImage;
PImage pressImage;
int id;
Button(int xp, int yp, int s, color b, color o, color p,PImage bi, PImage pi,int ident) {
x = xp;
y = yp;
size = s;
id = ident;
baseGray = b;
overGray = o;
pressGray = p;
baseImage = bi;
pressImage = pi;
}
void mouseEvent(MouseEvent event){
if ((mouseX >= x) && (mouseX <= x+size) && (mouseY >= y) && (mouseY <= y+size)) {
over = true;
overButton = this;
switch(event.getID()){
case MouseEvent.MOUSE_PRESSED:
pressed = true;
clickedButton = this;
break;
case MouseEvent.MOUSE_RELEASED:
released = true;
pressed = false;
releasedButton = this;
break;
}
}
else {
over = false;
pressed = false;
released = false;
}
}
void draw() {
fill(baseGray);
stroke(255);
if (pressed != true) {
rect(x, y, size, size);
image(baseImage,x,y);
}
else {
rect(x, y, size, size);
image(pressImage,x,y);
}
}
}