float screenwidth = 400;
float screenheight = 600;
float fontsize = 12;

boolean spinning;
int whatSpin;


String listnames[] = {"characters","items","descriptors","places","events","jobs"};

int boxcount = listnames.length;

spinbox boxes[] = new spinbox[boxcount];
PImage pix[] = new PImage[boxcount];

void setup(){
  //  framerate(10);
  size(200,600);
  background(128); 
  for(int i = 0 ;i< boxcount;i++){
    boxes[i] = new spinbox(screenwidth * 3 /4, 80 +( i * 80), loadStrings(listnames[i]+".txt"), -30*(float)Math.random());
    pix[i] = loadImage(listnames[i]+".gif");
  	


    }
  textFont(loadFont("ArialNarrow-48.vlw")); 
  textSize(fontsize);
  stroke(0); 
  textAlign(CENTER);


}

void draw(){

  background(128);

textSize(fontsize);

  for(int i = 0 ;i< boxcount;i++){
    if(spinning && (whatSpin == i || whatSpin == -1) ){
      boxes[i].bumpspeed((float)Math.random());
    }

    boxes[i].movebox();
    boxes[i].drawbox();
  }  
  
stroke(128);
fill(128);

rect(0,0,93,600);
rect(195,0,10,600);


fill(255);

  for(int i = 0 ;i< boxcount;i++){
image(pix[i], 0,50 +( i * 80));
        text(listnames[i],93,45 +( i * 80));

  }


textSize(25);
text("Sci Fi Idea Machine", 100, 25);

stroke(255);

triangle(60,535,60,585,100,560);
triangle(100,535,100,585,140,560);




}

void mousePressed() 
{ 
  float m = mouseY;
  if(m >= 40 && m <= 40+(boxcount*80)){
    
    whatSpin = Math.round((m-80) / 80);
  spinning = true;

        } else {
     if(m >= 535) {
        spinning = true;
        whatSpin = -1;
     } 
    }
} 

void mouseReleased(){
   spinning = false; 
}

class spinbox {
  float centerX, centerY;
  float boxwidth=100;
  float boxheight=60;
  float currentX;
  float fullwidth = screenwidth;
  float speed = 0;
  int boxcount;
  boolean finalmove;
  String txt[];

  void bumpspeed(float s){
    speed -=  s;
    finalmove = false;
    if(speed < -30) { speed = -30;}
  }

  spinbox(float centerx, float centery, String []txts, float startspeed){
        this.centerX = centerx;
    this.centerY = centery;
    currentX = centerX;
    this.boxcount = txts.length;
    finalmove = false;
speed = startspeed;
        txt = txts;
        Collections.shuffle(Arrays.asList(txt));
//println(centerY);
  } 

  void movebox(){
    currentX+=speed;
    currentX = currentX % (boxcount * fullwidth);

    if(!finalmove){
      speed *= .98;
      if(abs(speed) < .2){
        //speed = 0;
        finalmove = true;
      }

    }
    else{
      float calc = (currentX + (boxwidth * boxcount*10)+(boxwidth/2)) % boxwidth ;

      if(calc >= (boxwidth / 2)){
        speed += .1; 
      } 
      else {
        speed -= .1 ;
      }
      speed *= .95;
      if(abs(speed) < .2 && (calc < 1 || calc > (boxwidth-1))){
        speed = 0; 
        //        println("stop at ");
      }
      // println("speed is "+abs(speed)+" annd calc is "+calc);
    }
  }

  void drawbox(){
    for(int i = -boxcount*10; i < boxcount*10; i++){

      int val = (i + (boxcount*2000))% boxcount;

      float realXpos = currentX +(boxwidth*i);

      drawTextBox(realXpos, centerY - (boxheight/2), 
      boxwidth, boxheight,txt[val]);



    }
  }
}



void drawTextBox(float x, float y, float width, float height, String txt){
x += 45;
    if(x >= -width && x <= screenwidth){
    fill(255);

    rect(x,y,width,height);
    fill(0);
    Object lines[] = breakTxt(txt,width);
    int numlines = lines.length;
    for(int i = 0; i < numlines; i++){
        text((String)lines[i],x+(width/2),y + (i+1)*fontsize + ((height  - (numlines * fontsize)) / 2 ) );//(height/2) + (i * fontsize ) - ((fontsize/2) * numlines) );
  }
        

  }

}


Object[] breakTxt(String txt, float width){
//txt="little fuzzy animal (with big sharp teeth or with more brains than humans expect)";
//txt="Astronomical event (meteor shower, nova, supernova, big bang etc.)";
  ArrayList res = new ArrayList();
  StringTokenizer tok = new StringTokenizer(txt); 

  StringBuffer sb = new StringBuffer();
  if(tok.hasMoreTokens()){
    sb.append(tok.nextToken());
  }

        while(tok.hasMoreTokens()){
        String s = tok.nextToken();
        if(textWidth(sb.toString()+" "+s) < width){
           sb.append(" "+s);
        }else {
           res.add(sb.toString());
          sb = new StringBuffer(s); 
        }

  }

           res.add(sb.toString());
  return res.toArray();
}