Hi there again,
The code is still quite messy. But as soon as I get some free time I will try to work on it and simplify it.
This version allows to visualize:
- delay per letter -> spacing of letters
- word break -> space + change in color
- line break -> line break
I will soon get it to change the alpha of letters and words, depending on the delay per letter and/or per word.
NOTE 1: this version will only work on version Processing BETA v85. we must get on board!!
NOTE 2: your programming knowledge isn't that bad, you understood pretty well where the changes had to be made (and my code is bad, with no comments at all)!!
Code:
PFont f;
TypeTracker tf;
int leftmargin = 20;
int rightmargin = 20;
int topmargin = 40;
int bottommargin = 20;
float now;
float horizPos=0, vertPos=0;
float coeffAdv = 0.05;
float coeffScl = 0.005;
float coeffCol = 255;
float coeffAlp = 0.002;
boolean flagAdv = false;
boolean flagCol = false;
boolean flagAlp = false;
float fontSize=24;
void setup()
{
size(500, 500);
framerate(30);
// load the font. fonts are located within the
// main Processing directory/folder and they
// must be placed within the data directory
// of your sketch for them to load
f = loadFont("Univers66.vlw.gz");
textFont(f, fontSize);
// Store the cursor rectangle's position
horizPos = leftmargin;
vertPos = topmargin;
rect(horizPos, vertPos, 10, 21);
// create the class TypeTracker
tf = new TypeTracker();
// white type, black background
fill(0);
}
void draw()
{
background(255);
tf.draw();
}
void keyPressed()
{
if((keyCode==CONTROL)){
flagAdv=!flagAdv;
flagCol=!flagCol;
flagAlp=!flagAlp;
}
if ((key >= 0x20 && key <= 0x7e)||(key ==8 )||(key==10))
{
float dt=millis()-now;
now+=dt;
tf.addChar(char(key),dt);
}
}
class TypeTracker{
String texts="";
float delays[];
float delaysWords[];
int index=0;
int indexOfLastSpace=0;
int indexOfSecondLastSpace=0;
color[] colors={color(120,0,0),color(0,120,0)};
TypeTracker(){
delays = new float[1000];
delaysWords = new float[1000];
}
void draw(){
pushMatrix();
translate(horizPos,vertPos); // place the cursor at the beginning of the text display field
//scale(25.0f); // scale the text
char k;
float horizMax=0,horizAdv=0,vertAdv=0,scaling=1.0,adv=0;
int wordbreaks=0,linebreaks=0;
color letterColor=0;
float wordAlpha=0;
for(int i=0;i<texts.length();i++){
k = texts.charAt(i);
float coefficient;
switch(k){
case 10:
adv+=horizMax-horizAdv;
linebreaks++;
letterColor=colors[(wordbreaks+linebreaks)%2];
coefficient=(flagAlp)?coeffAlp:0;
wordAlpha=constrain(delaysWords[i+1]*coefficient,0,255);
wordAlpha=255-wordAlpha;
break;
case 32:
wordbreaks++;
coefficient=(flagAlp)?coeffAlp:0;
wordAlpha=constrain(delaysWords[i+1]*coefficient,0,255);
wordAlpha=255-wordAlpha;
default:
coefficient=(flagAdv)?coeffAdv:0;
adv+=delays[i]*coefficient;
letterColor=colors[(wordbreaks+linebreaks)%2];
break;
}
//letterColor=color(red(letterColor),green(letterColor),blue(letterColor),wordAlpha);
if(!flagCol){
letterColor=0;
}
horizMax=width-leftmargin-rightmargin-20;
horizAdv=adv%horizMax;
vertAdv=fontSize*(floor(adv/horizMax));
pushMatrix();
fill(letterColor);
translate(horizAdv,vertAdv);
text(k,0,0);
popMatrix();
adv+=fontSize*f.width(k);
}
popMatrix();
}
void addChar(char k,float dt){
if(k == char(8)) // special case for Backspace
{
if(texts.length()>0){
texts=texts.substring(0,texts.length() - 1);
index--;
}
}
else
{
delays[index]=dt;
if((k == char(32))||(k == char(10))){ // special case for Space (Calculate the word delay)
float avgDelay=0.0;
int indexOfLastLinebreak=texts.lastIndexOf(10);
int indexOfLastSpace=texts.lastIndexOf(32);
// calculate the average delay per word
int indexOfLastCut=max(indexOfLastSpace,indexOfLastLinebreak);
for(int i=indexOfLastCut+1;i<=index;i++){
avgDelay+=delays[i];
}
avgDelay=avgDelay/(index-indexOfLastCut);
delaysWords[indexOfLastCut+1]=avgDelay;
}
index++;
texts=texts + k;
}
}
}