Samstag, 10. Oktober 2015

Interactive Art Homework - Week 4

A pattern in my daily life (or atleast on weekendss...)



Code uses images. So a download is necessary:
Download me!


































class Button
{
  int     sizeX;
  int     sizeY;
  int     posX;
  int     posY;
  String  text;
  String  toolTip;
  int     colorR;
  int     colorG;
  int     colorB;
  int     colorA;
  boolean border = true;
  
  private Button(int posX, int posY, int sizeX, int sizeY, String text, String toolTip)
  {
    this.posX    = posX;
    this.posY    = posY;
    this.sizeX   = sizeX;
    this.sizeY   = sizeY;
    this.text    = text;
    this.toolTip = toolTip;
    colorR       = 0;
    colorG       = 0; 
    colorB       = 0;      
    colorA       = 255;
  }
  
  // Zeichnet den Button
  
  private void drawButton()
  {
    pushStyle();
    noSmooth();
    fill(colorR, colorG, colorB, colorA);
    if (!border)
    {
      noStroke();
    }
    rect(posX, posY, sizeX, sizeY);
    fill(255,255,255);
    textSize(12);
    textAlign(CENTER);
    text(text, posX + sizeX / 2, posY + sizeY / 1.4);    
    popStyle();
  }
  
  // Prüft ob die Maus über dem Button ist.
  
  private boolean isCollission()
  {
    
    if ((mouseX > posX && mouseX < posX + sizeX) && (mouseY > posY && mouseY < posY + sizeY))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
}

class Ingredient
{
  
  String item;
  color  colour;
  
  public Ingredient(String item)
  {
    this.item = item;
    updateColour();
  }
  
  void updateColour()
  {
    if(item == "empty")colour = color(255,255,255,0); 
    if(item == "Wodka")colour = color(255,255,255,40);        
    if(item == "Bacardi")colour = color(255,255,255,40);      
    if(item == "Whiskey")colour = color(79,57,31,200);
    if(item == "Sprite")colour = color(255,255,255,40);
    if(item == "Coke")colour = color(48,0,5,230);
    if(item == "Blue Curacao")colour = color(0,0,255,90);
    if(item == "Maracuja Juice")colour = color(223,241,76,240);
    if(item == "Energy")colour = color(254,255,210,240);  
  }
}


Button[]     buttons;  
Ingredient[] mixture;
int          ingredientsAdded;
color        currentColor;
PImage       cocktail, straw, swirl;

void setup()
{
  size(800, 600);

  buttons = new Button [8];
  buttons[0] = new Button(20, 30, 100, 20, "Wodka", "Add Wodka");  
  buttons[1] = new Button(20, 60, 100, 20, "Bacardi", "Add Bacardi");  
  buttons[2] = new Button(20, 90, 100, 20, "Whiskey", "Add Whiskey");  
  buttons[3] = new Button(20, 120, 100, 20, "Sprite", "Add Sprite");    
  buttons[4] = new Button(20, 150, 100, 20, "Coke", "Add Coke");  
  buttons[5] = new Button(20, 180, 100, 20, "Blue Curacao", "Add Blue Curacao");  
  buttons[6] = new Button(20, 210, 100, 20, "Maracuja Juice", "Add Maracuja");  
  buttons[7] = new Button(20, 240, 100, 20, "Energy", "Add Energy");  

  mixture = new Ingredient[10];
  for (int i = 0; i < mixture.length; i++)
  {
    mixture[i] = new Ingredient("empty");
  }
  ingredientsAdded = 0;
  currentColor = color(127,127,127,0);
  cocktail = loadImage("cocktail.png");
  straw    = loadImage("straw.png");  
  swirl    = loadImage("straw.png");  
}


void draw()
{
  background(45);
  drawGUI();
}

void drawGUI()
{
  for(int i = 0; i < buttons.length; i++)
  {
    buttons[i].drawButton();
    if(buttons[i].isCollission())
    {
      giveToolTip(i);
    }
  }
  
  for(int i = 0; i < mixture.length; i++)
  {
    if(mixture[i].item != "empty")
    {
      pushStyle();
      noStroke();
      fill(mixture[i].colour);
      rect(350,400 - i * 20,100,20);
      fill(255,255,255,255);      
      text(mixture[i].item, 480,400 - i * 20 + 15);
      popStyle();
    }
  }
  image(cocktail, 320, 189); 
 
  if(ingredientsAdded == 10)
  {
    image(straw, 320, 66);
  }  
}

void mouseReleased()
{
  for(int i = 0; i < buttons.length; i++)
  {
    if(buttons[i].isCollission())
    {
      addIngredient(buttons[i].text);
    }
  }
}

void addIngredient(String ingredient)
{
  if(ingredientsAdded <10)
  {
    mixture[ingredientsAdded].item = ingredient;
    mixture[ingredientsAdded].updateColour();
    //updateAllColours();
    ingredientsAdded++;
  }
}

void updateAllColours()
{
  if(ingredientsAdded == 0)
  {
    currentColor = color(mixture[ingredientsAdded].colour);
  }
  else
  {
    currentColor = lerpColor(currentColor, mixture[ingredientsAdded].colour, 0.3);
  }
  
  
  for(int i = 0; i < mixture.length; i++)
  {
    if(mixture[i].item != "empty")
    {
      mixture[i].colour = currentColor;
    }
  }  
}

private void giveToolTip(int i)
{
    int toolTipPanelSizeX = floor(textWidth(buttons[i].toolTip));  
    int toolTipVerticalDistance = 0;
    
    pushStyle();
    noSmooth();

    if(mouseY < 300)
    {
      toolTipVerticalDistance = 20;
    }
    else
    {
      toolTipVerticalDistance = -20;
    }
    if(buttons[i].posX < 400)
    {
      fill(0,0,0, 150);      
      rect(mouseX , mouseY - 5, + toolTipPanelSizeX + 10, toolTipVerticalDistance);     
      textAlign(LEFT);
      fill(255,255,255);
      textSize(12); 
      text(buttons[i].toolTip, mouseX +5 , mouseY + toolTipVerticalDistance / 2 );      
    }
    if(buttons[i].posX >= 400)
    {
      fill(0,0,0, 150);     
      rect(mouseX , mouseY - 5, - toolTipPanelSizeX - 10, toolTipVerticalDistance);      
      textAlign(RIGHT);
      fill(255,255,255);
      textSize(12);
      text(buttons[i].toolTip, mouseX - 5, mouseY + toolTipVerticalDistance / 2 );      
    }
    popStyle();  
}

















Keine Kommentare:

Kommentar veröffentlichen