Object based buttons.
in
Programming Questions
•
1 year ago
I know this might be something simple but its driving me nuts. How do I get each button to do something different? Thanks.
- ImageButtons1 button1;
- ImageButtons1 button2;
- ImageButtons1 button3;
- PImage bg;
- void setup()
- {
- size(800,600);
- bg = loadImage("lcarsbg.jpg");
- PImage b = loadImage("button_orange.gif");
- PImage r = loadImage("button_pale_red.gif");
- PImage d = loadImage("button_orange.gif");
- int x = 225;
- int y = 250;
- int w = b.width;
- int h = b.height;
- button1 = new ImageButtons1(x, y, w, h, b, r, d);
- PImage bf = loadImage("button_orange.gif");
- PImage rf = loadImage("button_pale_red.gif");
- PImage df = loadImage("button_orange.gif");
- int xf = 225;
- int yf = 300;
- int wf = bf.width;
- int hf = bf.height;
- button2 = new ImageButtons1(xf, yf, wf, hf, bf, rf, df);
- PImage bh = loadImage("button_orange.gif");
- PImage rh = loadImage("button_pale_red.gif");
- PImage dh = loadImage("button_orange.gif");
- int xh = 225;
- int yh = 350;
- int wh = bh.width;
- int hh = bh.height;
- button3 = new ImageButtons1(xh, yh, wh, hh, bh, rh, dh);
- }
- void draw()
- {
- background(bg);
- button1.update();
- button1.display();
- button2.update();
- button2.display();
- button3.update();
- button3.display();
- }
- class Button1
- {
- int x, y;
- int w, h;
- color basecolor, highlightcolor;
- color currentcolor;
- boolean over = false;
- boolean pressed = false;
- void pressed() {
- if(over && mousePressed) {
- pressed = true;
- } else {
- pressed = false;
- }
- }
- 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;
- }
- }
- }
- class ImageButtons1 extends Button1
- {
- PImage base;
- PImage roll;
- PImage down;
- PImage currentimage;
- ImageButtons1(int ix, int iy, int iw, int ih, PImage ibase, PImage iroll, PImage idown)
- {
- x = ix;
- y = iy;
- w = iw;
- h = ih;
- base = ibase;
- roll = iroll;
- down = idown;
- currentimage = base;
- }
- void update()
- {
- over();
- pressed();
- if(pressed) {
- currentimage = down;
- } else if (over){
- currentimage = roll;
- } else {
- currentimage = base;
- }
- }
- void over()
- {
- if( overRect(x, y, w, h) ) {
- over = true;
- } else {
- over = false;
- }
- }
- void display()
- {
- image(currentimage, x, y);
- }
- }
1