Thanks for the responses. I have been working on this recently and have run into a problem I can't work out a solution to. Ultimately, each word can me moused over and swapped out, so I want the code to allow for the dynamic repositioning of text.
However, I am struggling to get the text to position itself on the correct row. I thought I could get each word to look at the previous words end point, and then calculate whether the current word needed to be placed on a new line. doesn't seem to work like that though. any suggestions?
I have set the frame rate to 1 to help troubleshooting.
Code:PFont font;
int displayedWords = 32;
int nextWord = displayedWords + 1;
int lineHeight = 30;
int space = 10;
int margin = space * 4;
String s = "You've asked me what the lobster is weaving there with his golden feet? I reply, the ocean knows this. You say, what is the ascidia waiting for in its transparent bell? What is it waiting for?";
String[] vernacular = split(s, ' ');
Word[] words = new Word[displayedWords];
void setup(){
size(600,400);
frameRate(1);
font = loadFont("Serif-24.vlw");
textFont(font);
float xPos = margin;
int row = 1;
for(int i=0;i<words.length;i++){
float wide = textWidth(vernacular[i]);
words[i] = new Word(vernacular[i],xPos,row,wide);
xPos = words[i].end;
}
}
void draw(){
background(0);
for(int i=0;i<words.length;i++){
if(i != 0){
words[i].display();
words[i].update();
//line not working for vertical positioning
//words[i].xPos = words[i-1].end;
if(words[i].end > width-margin){
words[i].row++;
words[i].xPos = margin;
}
}
}
}
class Word{
String word;
float xPos;
float yPos;
float wide;
float end;
float top;
int row;
//Constructor
Word(String word, float xPos, int row, float wide){
this.word = word;
this.xPos = xPos;
this.row = row;
this.yPos = row * lineHeight;
this.wide = wide;
this.end = xPos + wide + space;
this.top = yPos - lineHeight;
}
void display(){
text(word, xPos, yPos);
}
void update(){
end = xPos + wide + space;
yPos = row * lineHeight;
}
}