/** * EDARE 2009 * get text input and do stuff with it * also an example of 'modes' - as if switching between screens */ int mode; String letters = ""; //text input starts off empty String inname,inname2, type, ss1; PFont fontA; void setup() { size(352, 288); mode = 0; //set mode to beginning fontA = loadFont("ArialMT-20.vlw"); } void draw() { background(255); textFont(fontA, 20); if(mode == 0) { fill(0); text("Welcome to the simple text parser \nwhat is your name?", 15, 30, 350, 100); fill(163); rect(18, 88, 200, 20); //gray rectangle to help users visually when they type in it fill(0); text(letters, 20, 104); //blank text to start, we fill in through interface //and pass to iname and iname2 variables: inname = letters; inname2 = letters; text("press enter >>", 225, 270); //do anything else you want, display an image, call a method, draw a shape , play a sound etc } else if (mode ==1){ fill(18); text("what is your type happy,\nsad, or miserable " + inname + " ? ", 15, 40); fill(0); fill(143); rect(18, 88, 200, 20); //text area for users fill(0); text(letters, 20, 104); //text users will write themselves type = letters; //type = happy, sad or miserable //do anything else you want, display an image, call a method, draw a shape , play a sound etc text("continue >>", 230, 270); } else if (mode ==2){ if(type.equals("happy")){ //basic String match // find out if they wrote 'happy', if so: fill(255, 0, 0); text("Hooray you are happy! Go to the dancehall\nYou will twirl " + inname2, 20, 30, 200, 200); //do anything else you want, display an image, call a method, draw a shape , play a sound etc } else{ //if they didnt write happy do this: text("You are not Happy! Oh dear, cheer up " + inname2, 20, 30, 200, 200); //do anything else you want, display an image, call a method, draw a shape , play a sound etc } text("continue >>", 230, 270); } else if (mode ==3){ //do more interesting things! fill(20, 245, 123); text(type + ", you are so interesting! " + inname2, 20, 20); //do anything else you want, display an image, call a method, draw a shape , play a sound etc text("continue >>", 230, 270); } else if (mode ==4){ //do more interesting things! fill(255, 0,0); text(inname2 + ", one more mode to go ", 20, 20); text("continue >>", 230, 270); } else if (mode ==5){ //do more interesting things! fill(255, 0, 0); rect(0, 0, width, height); fill(255); text(inname2 + " you can go round again!!", 20, 20); text("continue >>", 230, 270); } else if (mode ==6){ //last mode fill(25, 250, 0); rect(0, 0, width, height); fill(255); text(mode, 50, 50); mode = 0; //go back to begininng } } /////check for enter etc void keyPressed() { if (key == ENTER || key == RETURN) { letters = letters.toLowerCase(); if (mode == 1) { inname = letters; //pass first lot of text to 'iname' variable //i.e the user's name. print (letters); } letters = ""; // Clear the variable mode++; } else if ((key > 31) && (key != CODED)) { //get any irregular letters letters = letters + key; } else if (key == BACKSPACE) { //allow user to backspace if (letters.length()>0){ letters=letters.substring(0, letters.length()-1); } } }//end