Files
FreeJ/scripts/processing/topics/imagebutton.pde
Jaromil ae7b1ad056 progresses on processing script
processing-js 0.4 has been merged in some relevant parts
basic and topic scripts added for test
color handling fixed, more scripts show up now
2010-02-12 18:36:54 +01:00

98 lines
1.6 KiB
Plaintext

ImageButtons button;
void setup()
{
size(200, 200);
background(102, 102, 102);
// Define and create image button
PImage b = loadImage("data/base.gif");
PImage r = loadImage("data/roll.gif");
PImage d = loadImage("data/down.gif");
int x = width/2 - b.width/2;
int y = height/2 - b.height/2;
int w = b.width;
int h = b.height;
button = new ImageButtons(x, y, w, h, b, r, d);
}
void draw()
{
button.update();
button.display();
}
class Button
{
int x, y;
int w, h;
color basecolor, highlightcolor;
color currentcolor;
boolean isOver = false;
boolean isPressed = false;
void pressed() {
if(isOver && mousePressed) {
isPressed = true;
} else {
isPressed = 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 ImageButtons extends Button
{
PImage base;
PImage roll;
PImage down;
PImage currentimage;
ImageButtons(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(isPressed) {
currentimage = down;
} else if (over){
currentimage = roll;
} else {
currentimage = base;
}
}
void over()
{
if( overRect(x, y, w, h) ) {
isOver = true;
} else {
isOver = false;
}
}
void display()
{
image(currentimage, x, y);
}
}