Skip to content

Instantly share code, notes, and snippets.

@pzp1997
Created November 19, 2014 21:04
Show Gist options
  • Save pzp1997/5dba3dfb62af98480f36 to your computer and use it in GitHub Desktop.
Save pzp1997/5dba3dfb62af98480f36 to your computer and use it in GitHub Desktop.
Button button;
int count;
void setup() {
size(300, 300);
rectMode(CENTER);
textSize(48);
textAlign(CENTER);
button = new Button(width/2, height/2 - 30, 100);
}
void draw() {
background(0);
button.update();
fill(255);
text(count, width/2, height/2 + 70);
}
void mousePressed() {
if (button.overButton()) {
count++;
}
}
class Button {
float x;
float y;
int sz;
boolean hover;
Button(float x_, float y_, int sz_) {
x = x_;
y = y_;
sz = sz_;
}
boolean overButton() {
return mouseX >= x-sz/2 && mouseX <= x+sz/2 &&
mouseY >= y-sz/2 && mouseY <= y+sz/2;
}
void update() {
stroke(255);
strokeWeight(2);
if (overButton()) {
fill(100);
} else {
noFill();
}
rect(x, y, sz, sz);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment