// Excerpt from Textension
// by Josh Nimoy <http://www.jtnimoy.com>

// simple example of interactive text typing
// instructions: type letters and press backspace.

// note for paste&compilers: "Univers45.vlw.gz"
//
must be copied from "fonts" to the sketch's "data" folder.

// Created 27 January 2003

BFont f;
int leftmargin = 10;
int rightmargin = 20;
String buff = "";
boolean didntTypeYet = true;

void setup(){
    size(200,200);
    background(#FFFFFF);
    fill(#000000);
    hint(SMOOTH_IMAGES);
    f = loadFont("Univers45.vlw.gz");
    setFont(f, 25);
}

void loop(){
    if(millis()%500<250){ //only fill cursor half the time.
        noFill();
    }else{
        fill(#D5B28B);
        stroke(#D5B28B);
    }
    float rPos;
    //store the cursor rectangle's position
    rPos = f.stringWidth(buff)+leftmargin;
    rect(rPos+1,19,10,21);

    fill(0,0,0);
    //some instructions at first
    if(didntTypeYet){
        fill(#D5B28B);
        text("Use the keyboard.",22,40);
    }


    push();
        translate(rPos,10+25);
        char k;
        for(int i=0;i<buff.length();i++){
            k = buff.charAt(i);
            translate(-f.charWidth(k),0);
            rotateY(-f.charWidth(k)/70.0f);
            rotateX(f.charWidth(k)/70.0f);
            scale(1.1);
            text(k,0,0);
        }
    pop();
    stroke(#000000);
    noFill();
    rect(0,0,width-1,height-1);
}

void keyPressed(){
    char k;
    k = (char)key;
    switch(k){
    case 8:
        if(buff.length()>0){
            buff = buff.substring(1);
        }
        break;
    case 13://avoid special keys
    case 10:
    case 65535:
    case 127:
    case 27:
        break;
    default:
        if(f.stringWidth(buff+k)+leftmargin<width-rightmargin){
            didntTypeYet = false;
            buff=k+buff;
        }
        break;
    }
}

//back to Josh's Processing tutorial