import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 

public class luxusMO14 extends BApplet {
void setup() {
  size(406, 490);
  background(0);
  hint(SMOOTH_IMAGES);
  butfont = loadFont("Univers47.vlw.gz");
  popfont = loadFont("Univers57.vlw.gz");
  colorMode(HSB);
  m = loadImage("Mouse.gif");
  }
  
  
int s = 25; // Schriftgroesse 
int p = 12; // Popupschriftgroesse 
float xangle = 0.0f;
float yangle = 0.0f;
float initxangle, inityangle;
float initx, inity;
boolean autorot= false;
//boolean continuerot= true;
boolean fourd= false;
boolean fourdrot= false;
boolean threedrot= false;
boolean mousefourd = false;
boolean checkPopup = false;
boolean continuerot;
boolean colour1= false;
boolean colour2 = false;
boolean colour3 = false;
boolean colour4 = false;
boolean buttonreact = false;
boolean dragged = false;
boolean colorsel = false;
boolean phase1 = true;
int i;
int def = 255; 
int counter = 0;
int init = 2;
int countdown = init;
int colorkey;
BFont butfont;
BFont popfont;
float c1 = 0;
float c2 = 40;
float c3 = 80;
float c4 = 120;
float bckg = 160;
BImage m; 
float tradius = (150); // radius of trackball
float txpos = (170);  // xpos of trackball
float typos = (245);  // ypos of trackball
String popcolour1[] = {"mark 1st", "dimension"};
String popcolour2[] = {"mark 2nd", "dimension"};
String popcolour3[] = {"mark 3rd", "dimension"};
String popcolour4[] = {"mark 4th", "dimension"};
String popfourdrot[] = {"selects 4D rotation", "(only when central", "is selected)"};
String popthreedrot[] = {"selects 3D rotation", "(only when central", "is selected)"};
String popfourd[] = {"switches between", "parallel and central", "perspective plus", "resets all values"};
String popmousefourd[] = {"mouse rotation", "mode 3D/4D", "(only when central", "is selected)"};
String popautorot[] = {"enables rotation"};




void loop() {

noLights();
noStroke();




//top
fill(bckg, 5, 170, 255);
beginShape(POLYGON); 
  vertex(0, 0);
  vertex(width, 0);
  vertex(width-4, 5);
  vertex(5, 5);
endShape();

// left
fill(bckg, 5, 170, 240);
beginShape(POLYGON); 
  vertex(0, 0);
  vertex(0, height);
  vertex(5, height-4);
  vertex(5, 5);
endShape();

//bottom
fill(bckg, 5, 170, 170);
beginShape(POLYGON); 
  vertex(0, height);
  vertex(width, height);
  vertex(width-4, height-4);
  vertex(4, height-4);
endShape();
 
//right 
fill(bckg, 5, 170, 170);
beginShape(POLYGON); 
  vertex(width, 0);
  vertex(width, height);
  vertex(width-4, height-4);
  vertex(width-4, 4);
endShape();


// background


fill(bckg, 5, 200);
//top
rect(5, 5, 397, 90);
//bottom
rect(5, 394, 397, 92);
//left
rect(5, 94, 12, 302);
//right
rect(317, 94, 85, 302);

noFill();
noStroke();
rect(17, 84, 30, 30);
//stroke(20);

//top
fill(bckg, 5, 170, 170);
beginShape(POLYGON); 
  vertex(17, 94);
  vertex(317, 94);
  vertex(313, 98, -3);
  vertex(21, 98, -3);
endShape();

// left
fill(bckg, 5, 170, 190);
beginShape(POLYGON); 
  vertex(17, 94);
  vertex(21, 98, -3);
  vertex(21, 390, -3);
  vertex(17, 394);
endShape();

//bottom
fill(bckg, 5, 170, 230);
beginShape(POLYGON); 
  vertex(17, 394);
  vertex(317, 394);
  vertex(313, 390, -3);
  vertex(20, 390, -3);
endShape();
 
//right 
fill(bckg, 5, 170);
beginShape(POLYGON); 
  vertex(317, 94);
  vertex(313, 98, -3);
  vertex(313, 390, -3);
  vertex(317, 394);
endShape();


colour1 = drawButton(colour1, 27, 26, 23, 76, 73, "1", "1", s, popcolour1, 80, 50, p, init);
colour2 = drawButton(colour2, 61, 103, 23, 153, 73, "2", "2", s, popcolour2, 80, 50, p, init);
colour3 = drawButton(colour3, 47, 180, 23, 230, 73, "3", "3", s, popcolour3, 80, 50, p, init);
colour4 = drawButton(colour4, 42, 257, 23, 307, 73, "5", "5", s, popcolour4, 80, 50, p, init);

fourdrot = drawButton(fourdrot, 45, 335, 103, 385, 153, "4D", "4D", s, popfourdrot, 80, 43, p, init);
threedrot = drawButton(threedrot, 43, 335, 177, 385, 227, "3D", "3D", s, popthreedrot, 80, 43, p, init);
autorot = drawButton(autorot, 3, 335, 252, 385, 387, "N", "Y", s, popautorot, 71, 19, p, countdown);

fourd = drawButton(fourd, 48, 25, 414, 176, 466, "parallel", "central", s, popfourd, 82, 55, p, init);
mousefourd = drawButton(mousefourd, 44, 255.5f, 414, 306.5f, 466, "3D", "4D", s, popmousefourd, 79, 55, p, init);




if(fourd == false) {
// Parallelprojektion

  
  lights();
  smooth();
  noStroke();
  
  translate (170, 238);
  rotateX(yangle);
  rotateY(xangle); 


if (colour1 == true) {
  fill(c1, 200, 200);
  }
else {
  fill(def);
  }    

  //horizontal  
  fakeLine3D(-75, -25, 75, 25, -25, 75, 2.8f);
  fakeLine3D(-75, 75, 75, 25, 75, 75, 2.8f);
  fakeLine3D(-75, -25, -25, 25, -25, -25, 2.8f);
  fakeLine3D(-75, 75, -25, 25, 75, -25, 2.8f);
  fakeLine3D(-25, 25, 25, 75, 25, 25, 2.8f);
  fakeLine3D(-25, -75, 25, 75, -75, 25, 2.8f);
  fakeLine3D(-25, 25, -75, 75, 25, -75, 2.8f);
  fakeLine3D(-25, -75, -75, 75, -75, -75, 2.8f);
  
  
if (colour2 == true) {
  fill(c2, 200, 200);
  }
else {
  fill(def);
  }   
   
  //vertical  
  fakeLine3D(-75, 75, 75, -75, -25, 75, 2.8f);
  fakeLine3D(25, 75, 75, 25, -25, 75, 2.8f);
  fakeLine3D(-75, 75, -25, -75, -25, -25, 2.8f);
  fakeLine3D(25, 75, -25, 25, -25, -25, 2.8f);
  fakeLine3D(-25, 25, 25, -25, -75, 25, 2.8f);
  fakeLine3D(75, 25, 25, 75, -75, 25, 2.8f);
  fakeLine3D(-25, 25, -75, -25, -75, -75, 2.8f);
  fakeLine3D(75, 25, -75, 75, -75, -75, 2.8f);
  
      
if (colour3 == true) {
  fill(c3, 200, 200);
  }
else {
  fill(def);
  }    
  
  //depth  
  fakeLine3D(-75, -25, 75, -75, -25, -25, 2.8f);
  fakeLine3D(-75, 75, 75, -75, 75, -25, 2.8f);
  fakeLine3D(25, -25, 75, 25, -25, -25, 2.8f);
  fakeLine3D(25, 75, 75, 25, 75, -25, 2.8f);
  fakeLine3D(-25, 25, 25, -25, 25, -75, 2.8f);
  fakeLine3D(-25, -75, 25, -25, -75, -75, 2.8f);  
  fakeLine3D(75, 25, 25, 75, 25, -75, 2.8f);
  fakeLine3D(75, -75, 25, 75, -75, -75, 2.8f);
  
  
if (colour4 == true) {
  fill(c4, 200, 200);
  }
else {
  fill(def);
  }    
    
  //diagonal
  fakeLine3D(-75, -25, 75, -25, -75, 25, 2.8f);
  fakeLine3D(-75, 75, 75, -25, 25, 25, 2.8f);
  fakeLine3D(25, -25, 75, 75, -75, 25, 2.8f);
  fakeLine3D(25, 75, 75, 75, 25, 25, 2.8f);
  fakeLine3D(-75, -25, -25, -25, -75, -75, 2.8f);
  fakeLine3D(-75, 75, -25, -25, 25, -75, 2.8f);
  fakeLine3D(25, -25, -25, 75, -75, -75, 2.8f);
  fakeLine3D(25, 75, -25, 75, 25, -75, 2.8f);
        
                      
  if(autorot == true) {
     xangle += 0.01f;
     }
  }
  
else { 
//zentralprojektion

  
  lights();
  smooth();

  translate (170, 245);

  
         
    rotateX(yangle);
    rotateY(xangle); 
             
  
    fill(255);

  if(i<=75) {
  
       // x-Achse
      if (phase1 == true) {
        if(colour1 == true) {
          fill(c1, 200, 200);
          }
        else {
           fill(def);
           }
        }     
      else {
        if(colour4 == true) {
          fill(c4, 200, 200);
          }
        else {
          fill(def);
          } 
        }            

      fakeLine3D(75, 75-i/4, -75+i/4, -75+i, 75, -75, 3);
      fakeLine3D(37.5f-i/2, 37.5f, 37.5f, -37.5f-i/2, 37.5f+i/4, 37.5f+i/4, 3);
      fakeLine3D(-37.5f-i/2, -37.5f-i/4, 37.5f+i/4, 37.5f-i/2, -37.5f, 37.5f, 3);
      fakeLine3D(75, 75-i/4, 75-i/4, -75+i, 75, 75, 3);
      fakeLine3D(-37.5f-i/2, 37.5f+i/4, -37.5f-i/4, 37.5f-i/2, 37.5f, -37.5f, 3);
      fakeLine3D(75, -75+i/4, -75+i/4, -75+i, -75, -75, 3);
      fakeLine3D(-37.5f-i/2, -37.5f-i/4, -37.5f-i/4, 37.5f-i/2, -37.5f, -37.5f, 3);
      fakeLine3D(-75+i, -75, 75, 75, -75+i/4, 75-i/4, 3); 
       
      // y-Achse
      if (colour2 == true) {
        fill(c2, 200, 200);
        }
      else {
        fill(def);
        }  
      fakeLine3D(37.5f-i/2, -37.5f, 37.5f, 37.5f-i/2, 37.5f, 37.5f, 3);    
      fakeLine3D(-75+i, 75, 75, -75+i, -75, 75, 3);      
      fakeLine3D(75, -75+i/4, 75-i/4, 75, 75-i/4, 75-i/4, 3);
      fakeLine3D(-75+i, 75, -75, -75+i, -75, -75, 3); 
      fakeLine3D(-37.5f-i/2, 37.5f+i/4, 37.5f+i/4, -37.5f-i/2, -37.5f-i/4, 37.5f+i/4, 3);
      fakeLine3D(-37.5f-i/2, -37.5f-i/4, -37.5f-i/4, -37.5f-i/2, 37.5f+i/4, -37.5f-i/4, 3);  
      fakeLine3D(37.5f-i/2, 37.5f, -37.5f, 37.5f-i/2, -37.5f, -37.5f, 3); 
      fakeLine3D(75, -75+i/4, -75+i/4, 75, 75-i/4, -75+i/4, 3);
      
      // z-Achse
      if (colour3 == true) {
        fill(c3, 200, 200);
        }
      else {
        fill(def);
        }    
      fakeLine3D(-75+i, -75, -75, -75+i, -75, 75, 3);
      fakeLine3D(37.5f-i/2, 37.5f, 37.5f, 37.5f-i/2, 37.5f, -37.5f, 3);
      fakeLine3D(-37.5f-i/2, -37.5f-i/4, 37.5f+i/4, -37.5f-i/2, -37.5f-i/4, -37.5f-i/4, 3);
      fakeLine3D(-75+i, 75, -75, -75+i, 75, 75, 3);
      fakeLine3D(-37.5f-i/2, 37.5f+i/4, 37.5f+i/4, -37.5f-i/2, 37.5f+i/4, -37.5f-i/4, 3);
      fakeLine3D(37.5f-i/2, -37.5f, -37.5f, 37.5f-i/2, -37.5f, 37.5f, 3);
      fakeLine3D(75, -75+i/4, 75-i/4, 75, -75+i/4, -75+i/4, 3);
      fakeLine3D(75, 75-i/4, -75+i/4, 75, 75-i/4, 75-i/4, 3);
            
      
      // w-achse 
      if (phase1 == true) {
        if(colour4 == true) {
          fill(c4, 200, 200);
          }
        else {
           fill(255);
           }
        }     
      else {
        if(colour1 == true) {
          fill(c1, 200, 200);
          }
        else {
          fill(def);
          } 
        }            
      fakeLine3D(75, 75-i/4, 75-i/4, 37.5f-i/2, 37.5f, 37.5f, 3);
      fakeLine3D(37.5f-i/2, 37.5f, -37.5f, 75, 75-i/4, -75+i/4, 3);
      fakeLine3D(-75+i, -75, 75, -37.5f-i/2, -37.5f-i/4, 37.5f+i/4, 3);
      fakeLine3D(-37.5f-i/2, 37.5f+i/4, -37.5f-i/4, -75+i, 75, -75, 3);
      fakeLine3D(-75+i, 75, 75, -37.5f-i/2, 37.5f+i/4, 37.5f+i/4, 3);
      fakeLine3D(37.5f-i/2, -37.5f, 37.5f, 75, -75+i/4, 75-i/4, 3);
      fakeLine3D(-75+i, -75, -75, -37.5f-i/2, -37.5f-i/4, -37.5f-i/4, 3);
      fakeLine3D(37.5f-i/2, -37.5f, -37.5f, 75, -75+i/4, -75+i/4, 3);
     }
 
  
  
    else {
     //if(75< i && i<=150) { 
      //x-achse
      
      if (phase1 == true) {
        if(colour1 == true) {
          fill(c1, 200, 200);
          }
        else {
           fill(def);
           }
        }     
      else {
        if(colour4 == true) {
          fill(c4, 200, 200);
          }
        else {
          fill(def);
          } 
        }            
        
      fakeLine3D(-75+i, -75, 75, 75-(i-75)/2, -75+i/4, 75-i/4, 3); 
      fakeLine3D(75-(i-75)/2, 75-i/4, -75+i/4, -75+i, 75, -75, 3);
      fakeLine3D(37.5f-i/2, 37.5f, 37.5f, -75, 56.25f+(i-75)/4, 56.25f+(i-75)/4, 3);
      fakeLine3D(-75, -56.25f-(i-75)/4, 56.25f+(i-75)/4, 37.5f-i/2, -37.5f, 37.5f, 3);   
      fakeLine3D(75-(i-75)/2, 75-i/4, 75-i/4, -75+i, 75, 75, 3);
      fakeLine3D(-75, 56.25f+(i-75)/4, -56.25f-(i-75)/4, 37.5f-i/2, 37.5f, -37.5f, 3);
      fakeLine3D(75-(i-75)/2, -75+i/4, -75+i/4, -75+i, -75, -75, 3);      
      fakeLine3D(-75, -56.25f-(i-75)/4, -56.25f-(i-75)/4, 37.5f-i/2, -37.5f, -37.5f, 3);       
                  
      //y-achse
      
      if (colour2 == true) {
        fill(c2, 200, 200);
        }
      else {
        fill(def);
        }    
        
      fakeLine3D(-75+i, 75, 75, -75+i, -75, 75, 3);
      fakeLine3D(75-(i-75)/2, -75+i/4, 75-i/4, 75-(i-75)/2, 75-i/4, 75-i/4, 3); 
      fakeLine3D(-75, 56.25f+(i-75)/4, 56.25f+(i-75)/4, -75, -56.25f-(i-75)/4, 56.25f+(i-75)/4, 3);
      fakeLine3D(37.5f-i/2, -37.5f, 37.5f, 37.5f-i/2, 37.5f, 37.5f, 3);
      fakeLine3D(-75+i, 75, -75, -75+i, -75, -75, 3);
      fakeLine3D(-75, -56.25f-(i-75)/4, -56.25f-(i-75)/4, -75, 56.25f+(i-75)/4, -56.25f-(i-75)/4, 3);
      fakeLine3D(37.5f-i/2, 37.5f, -37.5f, 37.5f-i/2, -37.5f, -37.5f, 3);
      fakeLine3D(75-(i-75)/2, -75+i/4, -75+i/4,75-(i-75)/2, 75-i/4, -75+i/4, 3);
       
                                                                                             
      //z-achse
      
      if (colour3 == true) {
        fill(c3, 200, 200);
        }
      else {
        fill(def);
        }    
        
      fakeLine3D(37.5f-i/2, 37.5f, 37.5f, 37.5f-i/2, 37.5f, -37.5f, 3);
      fakeLine3D(-75+i, -75, -75, -75+i, -75, 75, 3);
      fakeLine3D(-75, -56.25f-(i-75)/4, 56.25f+(i-75)/4, -75, -56.25f-(i-75)/4, -56.25f-(i-75)/4, 3);
      fakeLine3D(-75+i, 75, -75, -75+i, 75, 75, 3);      
      fakeLine3D(-75, 56.25f+(i-75)/4, 56.25f+(i-75)/4, -75, 56.25f+(i-75)/4, -56.25f-(i-75)/4, 3); 
      fakeLine3D(75-(i-75)/2, 75-i/4, -75+i/4, 75-(i-75)/2, 75-i/4, 75-i/4, 3); 
      fakeLine3D(37.5f-i/2, -37.5f, -37.5f, 37.5f-i/2, -37.5f, 37.5f, 3); 
      fakeLine3D(75-(i-75)/2, -75+i/4, 75-i/4, 75-(i-75)/2, -75+i/4, -75+i/4, 3);     
           
                
      //w-achse
      
      if (phase1 == true) {
        if(colour4 == true) {
          fill(c4, 200, 200);
          }
        else {
           fill(def);
           }
        }     
      else {
        if(colour1 == true) {
          fill(c1, 200, 200);
          }
        else {
          fill(def);
          } 
        }            
        

 
      fakeLine3D(-75+i, 75, 75, -75, 56.25f+(i-75)/4, 56.25f+(i-75)/4, 3);
      fakeLine3D(-75+i, -75, 75, -75, -56.25f-(i-75)/4, 56.25f+(i-75)/4, 3);
      fakeLine3D(75-(i-75)/2, 75-i/4, 75-i/4, 37.5f-i/2, 37.5f, 37.5f, 3);                              
      fakeLine3D(-75, 56.25f+(i-75)/4, -56.25f-(i-75)/4, -75+i, 75, -75, 3);       
      fakeLine3D(37.5f-i/2, 37.5f, -37.5f, 75-(i-75)/2, 75-i/4, -75+i/4, 3);                      
      fakeLine3D(37.5f-i/2, -37.5f, 37.5f, 75-(i-75)/2, -75+i/4, 75-i/4, 3);
      fakeLine3D(-75+i, -75, -75, -75, -56.25f-(i-75)/4, -56.25f-(i-75)/4, 3);
      fakeLine3D(37.5f-i/2, -37.5f, -37.5f, 75-(i-75)/2, -75+i/4, -75+i/4, 3);
      
      }
      

 
    if(autorot == true) {
      if(fourdrot == true) {
        i++;
 
        if(i==150) {
          i=0;
          if(phase1 == true) {
            phase1 = false;
            } 
          else {
            phase1 = true;
            }  
          }
        }   
      
      if(threedrot == true) {
        xangle += 0.01f; 
        } 
     }
  println("i:" +i);
}
}

void fakeLine3D (float x1, float y1, float z1, float x2, float y2, float z2, float ls) {
  //points defining a 3d vector
  float px= x2-x1;
  float py= y2-y1;
  float pz= z2-z1;
  //angles for rotation 
  float xy, xz;
  //length of line (needed only for linestrength)
  float length= sqrt(sq(px) + sq(py) + sq(pz));
  
  if(px > 0) {
    xz= atan2(pz, -px);
    xy= -atan2(py, sqrt(sq(px) + sq(pz)));
    }
  else{
   xz= atan2(-pz, px);
   xy= atan2(-py, -sqrt(sq(px) + sq(pz))) ;
   };
  
  
  push();
  translate(x1 + px/2, y1 + py/2, z1 + pz/2);
  rotateY(xz);
  rotateZ(xy);
  scale(length/ls, 1, 1);
  box(ls);
  pop();
  }
    


  
void keyPressed() { 
   checkPopup = false;
   println(key);
   if (key == 52) {
     if(mousefourd == true) {
     if(i==150) {
         i=0;
         }
       i ++;
       }      
     else {
       xangle -= 0.01f;
       }     
     }
   if (key == 54) {
     if(mousefourd == true) {
        if(i==0) {
         i=150;
         }
       i --;
       }        
     else {
       xangle += 0.01f;
       }     
     }
   if (key == 50) {
       yangle -= 0.01f;
       }
   if (key == 55) {
      xangle -= 0.01f;
      yangle += 0.01f;
      }
   if (key == 57) {
      xangle += 0.01f;  
      yangle += 0.01f;
      }
   if (key == 49) {
      xangle -= 0.01f;  
      yangle -= 0.01f;
      } 
    if (key == 51) {
      xangle += 0.01f;  
      yangle -= 0.01f;
      }        
   }        
    
void mouseMoved() { 
   checkPopup = true;
   }

boolean trackball;
    
void mousePressed() {

   if(colorsel == true) {
      if (colorkey == 27) {
        c1 = hue(getPixel(mouseX, mouseY));
      }
      else { 
        if(colorkey == 61) { 
          c2 = hue(getPixel(mouseX, mouseY));
          }
        else {
          if (colorkey == 47) {  
            c3 = hue(getPixel(mouseX, mouseY));
          }  
          else{
            c4 = hue(getPixel(mouseX, mouseY));
          }  
        } 
      }
    }  

    buttonreact = true; 
    checkPopup = false;
    initx= mouseX-txpos;
    inity= mouseY-typos;
    if(-tradius <= initx && initx<= tradius && -tradius <= inity && inity <= tradius) {
      initxangle = xangle;
      inityangle = yangle;
      trackball = true;
      continuerot = autorot;
      }
    }     

//virtual trackball                                                                                                                                                            
void mouseDragged() {

  dragged = true;
  buttonreact = false;

  
if(trackball == true){      
  if (mousefourd == true && fourd == true) {
    int mpos = (mouseX/2);
    if(mpos<0) {
      mpos+=300;
      }
    i= (mpos)%150;  
    int phase = (((mpos)-i)/150)%2;


    if (phase == 1) {
      phase1 = false;
    }
    else {          
      phase1 = true;
    }
    xangle= 0;
  }
 
   
  else {
    

    float xpos = mouseX-txpos;
    float ypos = mouseY-typos;
    
    float x = xpos%tradius;
    float y = ypos%tradius;
    float xsgn = (xpos - x)/tradius;
    float ysgn = (ypos - y)/tradius;   


    float z = (sqrt(22500-sq(x)));
    xangle = xsgn*PI/2 + (atan2(x-initx, z))+initxangle; 
    //println ("xangle: " + degrees(xangle));
 
    float yz= (sqrt(22500-sq(y))); 
    yangle = -(ysgn*PI/2)-(atan2(y-inity, yz))+inityangle;
    //println ("yangle: " + degrees(yangle));
    autorot = false;
    }
    checkPopup = false;
  } 
}
  
  void mouseReleased() {
    dragged = false;
    if(trackball == true) {
      autorot = continuerot;
      }
    trackball = false;

    }
  
  
boolean drawButton (boolean cond, int k, float x1, float y1, float x2, float y2, String prev, String after, int fs, String popup[], float hor, float vert, int ps, int del) { 
int highlight = 0; 
float fil;
boolean inside = ((mouseX >= x1) && (mouseX <= x2) && (mouseY >= y1) && (mouseY <= y2));
boolean mark= (k == 27 || k == 61 || k == 47 || k ==  42);
           

  if((inside == true) && (dragged == false)) {
    highlight = 3;
    }
    
  if(((inside == true) && (mousePressed == true) && (buttonreact == true) && (colorsel == false)) || ((keyPressed == true) && key == k)) {
  //Bedingungen setzen
   
    if(cond == false) {
      if((k == 43 || k == 44 || k == 45) && fourd == false) {
        cond = false;
        highlight = 0;
        } 
      else {  
        cond = true;
        }
      if(k == 48) {
        xangle = 0;
        yangle = 0;
        autorot = false;
        threedrot = true;
        }  
      buttonreact = false;
      }
    else {
      cond = false;
      if(k == 48) {
        i = 0; 
        autorot = false;
        xangle = 0;
        yangle = 0;
        threedrot = false; 
        fourdrot = false;
        mousefourd = false;
        }  
      buttonreact = false;
    }  
  } 

      
  if ((inside == true ) && (checkPopup == true)) {
    counter = 0;  
    if(mark == true) {
      
      colorkey = k;
        
      if(countdown == 0) {
        colorsel = true;
        noLights();
        smooth();
        float fx = x1-20*x1/width;
        float fy = 25;
             
        push();
              
             
        translate(0, 0, 16);
             
        noStroke();
        fill(0, 90);
        rect(fx+1.5f, fy+1.5f, 60, 60);  
             
        stroke(0); 
        fill(0, 200, 200);
        rect(fx, fy, 15, 15);
            
        fill(10, 200, 200);
        rect(fx+15, fy, 15, 15);
            
        fill(20, 200, 200);
        rect(fx+30, fy, 15, 15);
            
        fill(30, 200, 200);
        rect(fx+45, fy, 15, 15);
             
        fill(40, 200, 200);
        rect(fx, fy+15, 15, 15);
            
        fill(50, 200, 200);
        rect(fx+15, fy+15, 15, 15);
             
        fill(60, 200, 200);
        rect(fx+30, fy+15, 15, 15);
              
        fill(70, 200, 200);
        rect(fx+45, fy+15, 15, 15);
              
        fill(80, 200, 200);
        rect(fx, fy+30, 15, 15);
              
        fill(90, 200, 200);
        rect(fx+15, fy+30, 15, 15);
              
        fill(100, 200, 200);
        rect(fx+30, fy+30, 15, 15);
              
        fill(110, 200, 200);
        rect(fx+45, fy+30, 15, 15);
              
        fill(120, 200, 200);
        rect(fx, fy+45, 15, 15);
             
        fill(130, 200, 200);
        rect(fx+15, fy+45, 15, 15);
              
        fill(140, 200, 200);
        rect(fx+30, fy+45, 15, 15);
              
        fill(150, 200, 200);
        rect(fx+45, fy+45, 15, 15);
              
        if(mousePressed) {
          if(k == 27) {
            c1 = hue(getPixel(mouseX, mouseY));
            }
          else {
            if(k == 61) { 
              c2 = hue(getPixel(mouseX, mouseY));
              }
            else {
              if (k == 47) {
                c3 = hue(getPixel(mouseX, mouseY));
                }
              else {
                c4 = hue(getPixel(mouseX, mouseY));
                } 
              } 
            } 
          }                
        pop();     
        }             
      else { // weiter runterzaehlen (fuer colorfield)
        countdown--;
        //println ("countdown: " + countdown);
        }  
      }
       
    else { // normale popups
            
      if(countdown == 0) {
        noLights();
        noSmooth();
        fill(255); 
        float x = mouseX+35-152*mouseX/width;
        float y = mouseY-vert/50*35+70-135*mouseY/height;
            
        push();
        translate(0, 0, 100);
        fill(0, 90);
        noStroke();
        rect(x+1.5f, y+1.5f, hor, vert);
        fill(255);
        stroke(0); 
        rect(x, y, hor, vert); 
        fill(0);
        setFont(popfont, ps);
        hint(SMOOTH_IMAGES);
        for (int p=0; p<popup.length; p++) {
          text(popup[p], x+ps/2, y+ps+p*ps);
          }
        pop();
        }
      else {  // weiter runterzaehlen fuer normale popups
        countdown--;
        //println ("countdown: " + countdown);
        }  //inner
      } // farbfeld     
    }  //popup 
  else { // checkpopup == false || inside == false
    //  counter = 0;  
    } 
    
  
  if (inside == false) {  
    counter ++;  
    if (counter == 10) {
      counter = 1;
      countdown = del;
      colorsel = false;
      }  
    } 
 // println("counter: " + counter); 
 // println("countdown: " + countdown);      

    
          
  if(cond == true) {
    if (mark == true) {       
      noStroke();
      noLights();
      //noSmooth();
       
      fill(0);
       
      push();
      translate(0, 0, highlight);
       
      if(k == 27) {
        fil = c1;
        }
      else { 
        if(k == 61) {
          fil = c2;
          }
        else {
          if(k == 47) {
            fil = c3;
            }
          else {
            fil = c4;
            }
          }     
        }
        
                              
      //base
      fill(20);  
      beginShape(QUADS);
      vertex (x1-1.5f, y1-1.5f, 0);
      vertex (x2+1.5f, y1-1.5f, 0);
      vertex (x2+1.5f, y2+1.5f, 0);
      vertex (x1-1.5f, y2+1.5f, 0);
      endShape();
         
      //cover
      fill(fil, 190, 255);
      beginShape(QUADS);
      vertex (x1+4.5f, y1+3.8f, 2.25f);
      vertex (x2-4.5f, y1+3.8f, 2.25f);
      vertex (x2-4.5f, y2-4.2f, 2.25f);
      vertex (x1+4.5f, y2-4.2f, 2.25f);
      endShape();
         
         
      //top
      fill(fil, 190, 255, 240);                                       
      beginShape(POLYGON);
      vertex (x1, y1, 0);
      vertex (x2, y1, 0);
      vertex (x2-5, y1+5, 3);
      vertex (x1+5, y1+5, 3);
      endShape();
         
      //left
      fill(fil, 190, 255, 220);  
      beginShape(POLYGON);
      vertex (x1, y1, 0);
      vertex (x1, y2, 0);
      vertex (x1+5, y2-5, 3);
      vertex (x1+5, y1+5, 3);
      endShape();
         
      //right
      fill(fil, 190, 255, 200);  
      beginShape(POLYGON);
      vertex (x2, y1, 0);
      vertex (x2, y2, 0);
      vertex (x2-5, y2-5, 3);
      vertex (x2-5, y1+5, 3);
      endShape();
         
      //bottom
      fill(fil, 190, 255, 180);  
      beginShape(POLYGON);
      vertex (x1, y2, 0);
      vertex (x2, y2, 0);
      vertex (x2-5, y2-5, 3);
      vertex (x1+5, y2-5, 3);
      endShape();
        
      push();
      
      translate(0, 0, 3);
      
      setFont(butfont, s);
      fill(180);
      text(after, x1+(x2-x1)/2 - 10*after.length()/2+1, y1+(y2-y1)/2+s/3+1);
      fill(0);
      text(after, x1+(x2-x1)/2 - 10*after.length()/2, y1+(y2-y1)/2+s/3);
        
      pop();      
        
      pop();
        
      }
          
    else {
      noStroke();
      noLights();
      //noSmooth();
       
      fill(0);
       
      push();
      translate(0, 0, highlight);

      //base
      fill(20);  
      beginShape(QUADS);
      vertex (x1-1.5f, y1-1.5f, 0);
      vertex (x2+1.5f, y1-1.5f, 0);
      vertex (x2+1.5f, y2+1.5f, 0);
      vertex (x1-1.5f, y2+1.5f, 0);
      endShape();
         
      //cover
      fill(bckg, 2, 255);
      beginShape(QUADS);
      vertex (x1+4.5f, y1+3.8f, 2.25f);
      vertex (x2-4.5f, y1+3.8f, 2.25f);
      vertex (x2-4.5f, y2-4.2f, 2.25f);
      vertex (x1+4.5f, y2-4.2f, 2.25f);
      endShape();
         
         
      //top
      fill(bckg, 2, 255, 240);                                       
      beginShape(POLYGON);
      vertex (x1, y1, 0);
      vertex (x2, y1, 0);
      vertex (x2-5, y1+5, 3);
      vertex (x1+5, y1+5, 3);
      endShape();
         
      //left
      fill(bckg, 2, 255, 220);  
      beginShape(POLYGON);
      vertex (x1, y1, 0);
      vertex (x1, y2, 0);
      vertex (x1+5, y2-5, 3);
      vertex (x1+5, y1+5, 3);
      endShape();
         
      //right
      fill(bckg, 2, 255, 200);  
      beginShape(POLYGON);
      vertex (x2, y1, 0);
      vertex (x2, y2, 0);
      vertex (x2-5, y2-5, 3);
      vertex (x2-5, y1+5, 3);
      endShape();
         
      //bottom
      fill(bckg, 2, 255, 180);  
      beginShape(POLYGON);
      vertex (x1, y2, 0);
      vertex (x2, y2, 0);
      vertex (x2-5, y2-5, 3);
      vertex (x1+5, y2-5, 3);
      endShape();
        
      push();

      translate(0, 0, 3);
      setFont(butfont, s);
      fill(180);
      text(after, x1+(x2-x1)/2 - 10*after.length()/2+1, y1+(y2-y1)/2+s/3);
      fill(0);
      text(after, x1+(x2-x1)/2 - 10*after.length()/2, y1+(y2-y1)/2+s/3);
        
      pop();      
        
      pop();
      }
    }  
          
  else {  
    
    if (mark == true) {
    
      if(k == 27) {
        fil = c1;
        }
      else { 
        if(k == 61) {
          fil = c2;
          }
        else {
          if(k == 47) {
            fil = c3;
            }
          else {
            fil = c4;
            }
          }     
        }
          
      push();
       
      translate(0, 0, highlight);
             
      noStroke();
      noLights();
         
      //base
      fill(20);   
      beginShape(QUADS);
      vertex (x1-1.5f, y1-1.5f, 0);
      vertex (x2+1.5f, y1-1.5f, 0);
      vertex (x2+1.5f, y2+1.5f, 0);
      vertex (x1-1.5f, y2+1.5f, 0);
      endShape();
       
      //cover
      fill(fil, 180, 170);
      beginShape(QUADS);
      vertex (x1+4.5f, y1+4.5f, 9);
      vertex (x2-4.5f, y1+4.5f, 9);
      vertex (x2-4.5f, y2-4.5f, 9);
      vertex (x1+4.5f, y2-4.5f, 9);
      endShape();
      

      //top
      fill(fil, 180, 170, 210);                                       
      beginShape(POLYGON);
      vertex (x1, y1, 0);
      vertex (x2, y1, 0);
      vertex (x2-5, y1+5, 10);
      vertex (x1+5, y1+5, 10);
      endShape();
         
      //left
      fill(fil, 200, 200, 190);  
      beginShape(POLYGON);
      vertex (x1, y1, 0);
      vertex (x1, y2, 0);
      vertex (x1+5, y2-5, 10);
      vertex (x1+5, y1+5, 10);
      endShape();
         
      //right
      fill(fil, 180, 170, 170);  
      beginShape(POLYGON);
      vertex (x2, y1, 0);
      vertex (x2, y2, 0);
      vertex (x2-5, y2-5, 10);
      vertex (x2-5, y1+5, 10);
      endShape();
        
      //bottom
      fill(fil, 180, 170, 150);  
      beginShape(POLYGON);
      vertex (x1, y2, 0);
      vertex (x2, y2, 0);
      vertex (x2-5, y2-5, 10);
      vertex (x1+5, y2-5, 10);
      endShape();
       
      push();
      translate(0, 0, 10);
      
      

      setFont(butfont, s);
      fill(180);
      text(prev, x1+(x2-x1)/2 - 10*prev.length()/2+1, y1+(y2-y1)/2+s/3);
      fill(fil, 255, 130);
      text(prev, x1+(x2-x1)/2 - 10*prev.length()/2, y1+(y2-y1)/2+s/3);
      pop(); 
       
      pop();          
      }
      
    else {
      push();
       
      translate(0, 0, highlight);
             
      noStroke();
      noLights();
      
      if ((k == 43 || k == 44 || k == 45) && fourd == false) {
        fil = 170;
        }
      else {
        fil = 245;
        }  
         
      //base
      fill(20);   
      beginShape(QUADS);
      vertex (x1-1.5f, y1-1.5f, 0);
      vertex (x2+1.5f, y1-1.5f, 0);
      vertex (x2+1.5f, y2+1.5f, 0);
      vertex (x1-1.5f, y2+1.5f, 0);
      endShape();
       
      //cover
      fill(bckg, 5, fil);
      beginShape(QUADS);
      vertex (x1+4.5f, y1+4.5f, 9);
      vertex (x2-4.5f, y1+4.5f, 9);
      vertex (x2-4.5f, y2-4.5f, 9);
      vertex (x1+4.5f, y2-4.5f, 9);
      endShape();
         
         
      //top
      fill(bckg, 5, fil, 230);                                       
      beginShape(POLYGON);
      vertex (x1, y1, 0);
      vertex (x2, y1, 0);
      vertex (x2-5, y1+5, 10);
      vertex (x1+5, y1+5, 10);
      endShape();
         
      //left
      fill(bckg, 5, fil, 210);  
      beginShape(POLYGON);
      vertex (x1, y1, 0);
      vertex (x1, y2, 0);
      vertex (x1+5, y2-5, 10);
      vertex (x1+5, y1+5, 10);
      endShape();
         
      //right
      fill(bckg, 5, fil, 190);  
      beginShape(POLYGON);
      vertex (x2, y1, 0);
      vertex (x2, y2, 0);
      vertex (x2-5, y2-5, 10);
      vertex (x2-5, y1+5, 10);
      endShape();
        
      //bottom
      fill(bckg, 5, fil, 170);  
      beginShape(POLYGON);
      vertex (x1, y2, 0);
      vertex (x2, y2, 0);
      vertex (x2-5, y2-5, 10);
      vertex (x1+5, y2-5, 10);
      endShape();
       
      //push();
      //translate(0, 0, 10);
      //if(k == 44) {
        //image(m, x1+3, y1+3);
        //} 
      //pop();   
       
       
      push();
      translate(0, 0, 10);
      setFont(butfont, s);
      fill(180);
      text(prev, x1+(x2-x1)/2 - 10*prev.length()/2+1, y1+(y2-y1)/2+s/3);
      fill(90);
      text(prev, x1+(x2-x1)/2 - 10*prev.length()/2, y1+(y2-y1)/2+s/3);
      
      pop(); 
       
      pop();          
   
      }  
    } 
    println ("colorfield: " + colorsel);        
    return cond;  
  }

}
