Files
FreeJ/scripts/processing/topics/buttons.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

212 lines
3.7 KiB
Plaintext

color currentcolor;
CircleButton circle1, circle2, circle3;
RectButton rect1, rect2;
boolean locked = false;
void setup()
{
size(200, 200);
smooth();
color baseColor = color(102);
currentcolor = baseColor;
// Define and create circle button
color buttoncolor = color(204);
color highlight = color(153);
ellipseMode(CENTER);
circle1 = new CircleButton(30, 100, 100, buttoncolor, highlight);
// Define and create rectangle button
buttoncolor = color(204);
highlight = color(153);
circle2 = new CircleButton(130, 110, 24, buttoncolor, highlight);
// Define and create rectangle button
buttoncolor = color(153);
highlight = color(102);
circle3 = new CircleButton(130, 140, 24, buttoncolor, highlight);
// Define and create rectangle button
buttoncolor = color(102);
highlight = color(51);
rect1 = new RectButton(150, 20, 100, buttoncolor, highlight);
// Define and create rectangle button
buttoncolor = color(51);
highlight = color(0);
rect2 = new RectButton(90, 20, 50, buttoncolor, highlight);
}
void draw()
{
background(currentcolor);
stroke(255);
update(mouseX, mouseY);
circle1.display();
circle2.display();
circle3.display();
rect1.display();
rect2.display();
}
void update(int x, int y)
{
if(locked == false) {
circle1.update();
circle2.update();
circle3.update();
rect1.update();
rect2.update();
}
else {
locked = false;
}
if(mousePressed) {
if(circle1.pressed()) {
currentcolor = circle1.basecolor;
}
else if(circle2.pressed()) {
currentcolor = circle2.basecolor;
}
else if(circle3.pressed()) {
currentcolor = circle3.basecolor;
}
else if(rect1.pressed()) {
currentcolor = rect1.basecolor;
}
else if(rect2.pressed()) {
currentcolor = rect2.basecolor;
}
}
}
class Button
{
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean isOver = false;
boolean isPressed = false;
void update()
{
if(over()) {
currentcolor = highlightcolor;
}
else {
currentcolor = basecolor;
}
}
boolean pressed()
{
if(isOver) {
locked = true;
return true;
}
else {
locked = false;
return false;
}
}
boolean over()
{
return true;
}
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;
}
}
boolean overCircle(int x, int y, int diameter)
{
float disX = x - mouseX;
float disY = y - mouseY;
if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
}
else {
return false;
}
}
}
class CircleButton extends Button
{
CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overCircle(x, y, size) ) {
isOver = true;
return true;
}
else {
isOver = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
ellipse(x, y, size, size);
}
}
class RectButton extends Button
{
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overRect(x, y, size, size) ) {
isOver = true;
return true;
}
else {
isOver = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}