Skip to content

Instantly share code, notes, and snippets.

@kritzikratzi
Created November 26, 2010 21:31
Show Gist options
  • Save kritzikratzi/717240 to your computer and use it in GitHub Desktop.
Save kritzikratzi/717240 to your computer and use it in GitHub Desktop.
color currentcolor;
RectButton rect1, rect2, rect3, rect4, rect5, rect6, rect7;
boolean locked = false;
Button focusElement = null;
Button focusOrder[];
void setup()
{
size(1400, 100);
smooth();
color baseColor = color(102);
currentcolor = baseColor;
// Define and create circle button
color buttoncolor = color(204);
color highlight = color(153);
ellipseMode(CENTER);
// Define and create circle button
buttoncolor = color(204);
highlight = color(153);
// Define and create circle button
buttoncolor = color(153);
highlight = color(102);
// Define and create rectangle button
buttoncolor = color(102);
highlight = color(51);
rect1 = new RectButton(1300, 20, 50, buttoncolor, highlight);
//PFont font;
//font = loadFont("Serif-48.vlw");
//text("word", 10, 30);
rect2 = new RectButton(1100, 20, 50, buttoncolor, highlight);
rect3 = new RectButton(900, 20, 50, buttoncolor, highlight);
rect4 = new RectButton(700, 20, 50, buttoncolor, highlight);
rect5 = new RectButton(500, 20, 50, buttoncolor, highlight);
rect6 = new RectButton(300, 20, 50, buttoncolor, highlight);
rect7 = new RectButton(100, 20, 50, buttoncolor, highlight);
// Define and create rectangle button
// buttoncolor = color(51);
// highlight = color(0);
// by default give the focus to the first button,
// also define the focus order to be the elements from left to right (opposed to their names)
focusElement = rect1;
focusOrder = new Button[]{ rect7, rect6, rect5, rect4, rect3, rect2, rect1 };
}
void draw()
{
background(currentcolor);
stroke(255);
update(mouseX, mouseY);
rect1.display();
PFont font;
font = loadFont("Serif-48.vlw");
text("Name", 100, 90);
rect2.display();
font = loadFont("Serif-48.vlw");
text("Price", 300, 90);
rect3.display();
font = loadFont("Serif-48.vlw");
text("Quality", 500, 90);
rect4.display();
font = loadFont("Serif-48.vlw");
text("Calories", 700, 90);
rect5.display();
font = loadFont("Serif-48.vlw");
text("Alcohol", 900, 90);
rect6.display();
font = loadFont("Serif-48.vlw");
text("Bitters", 1100, 90);
rect7.display();
font = loadFont("Serif-48.vlw");
text("Malty", 1300, 90);
}
void update(int x, int y)
{
if(locked == false) {
rect1.update();
rect2.update();
rect3.update();
rect4.update();
rect5.update();
rect6.update();
rect7.update();
}
else {
locked = false;
}
//if(mousePressed) {
// currentcolor = rect1.basecolor;
// }
// else if(rect2.pressed()) {
// currentcolor = rect2.basecolor;
// }
}
void keyPressed(){
if( keyCode == TAB ){
println( "Focusing next..." );
int focusIndex = -1;
for( int i = 0; i < focusOrder.length; i++ ){
if( focusOrder[i] == focusElement ){
// aha... here it is! we can break out of the loop!
focusIndex = i; break;
}
}
// is shift pressed as well? then go back!
if( keyEvent.getModifiers() == KeyEvent.SHIFT_MASK )
focusElement = focusOrder[ (focusIndex-1+focusOrder.length) % focusOrder.length ];
// nope, go forward
else
focusElement = focusOrder[ (focusIndex+1) % focusOrder.length ];
println( "new focus element is: " + focusElement );
}
}
class Button
{
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
void update()
{
if(over()) {
currentcolor = highlightcolor;
}
else {
currentcolor = basecolor;
}
}
boolean pressed()
{
if(over) {
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) ) {
over = true;
return true;
}
else {
over = 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) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(255);
//PFont font;
//font = loadFont("Serif-48.vlw");
//text("word", 100, 90);
//fill(currentcolor);
rect(x, y, size, size);
// do your own thing here!
if( focusElement == this ){
// me? focus? hurray!
stroke( 255, 0, 0 );
rect( x-1, y-1, size+2, size+2 );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment