We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Scrolling text timed [SOLVED]
Page Index Toggle Pages: 1
Scrolling text timed [SOLVED] (Read 1163 times)
Scrolling text timed [SOLVED]
Dec 11th, 2009, 8:23am
 
So I'm trying to build a class which displays text in dos like way, so when the number of lines exceed the possible number of lines on screen it shifts all the lines up to make room for the next line. The text does not have to be scrollable and text going off the top does no have to be stored. I have made something which does it, but loops (I cannibilized bits of a processing example), and also I does not work with files over double the size of the possible lines on screen. I am stumped at the moment, I tried messing around with some for conditions but draw() does not play well with it. Also I am getting a performace hit (the text slide slows down every so often), so I guess it is not very efficient. Any ideas?

Quote:
TextEngine newEngine;
boolean excessTrue;
int numFrames;
int frame;
String[] loadText;

void setup()
{
  newEngine = new TextEngine();
  size(700,400);
  frameRate(30);
  background(0);
  fill(0,255,0);
  loadText = loadStrings("test.txt");
  newEngine.updateText(loadText);
  excessTrue = newEngine.getExcess();
  frame = 0;
  numFrames = newEngine.getExLines();
  println(numFrames);
}

void draw()
{
  if (excessTrue == true);
  {
    frame = (frame+1) % numFrames;
    newEngine.excessDisplay(loadText, frame);
  }
}

class TextEngine

  boolean excessLines;
  int numLines;
  int maxLines;
  int diffLines;
  int row;
  int numRow;
  int maxRow;
  int fSize;
  int leftmargin;
  String[] lineBuff;
  TextEngine()
  {
    row = 0;
    numRow = 50;
    maxRow = 24;
    fSize = 16;
    leftmargin = 10;
    lineBuff = new String[numRow];
    textFont(loadFont("DOS16.vlw"),fSize);
  }
  void updateText(String lines[])
  {
    numLines = lines.length;
    if(numLines <= maxRow)
    {
      maxLines = numLines;
      excessLines = false;
    } else {
      maxLines = maxRow - 1;
      diffLines = numLines - maxRow;
      excessLines = true;
    }
    displayLines(lines);
  }
  void excessDisplay(String lines[], int scrollNum)
  {
    for (int i = 0; i < scrollNum; i++)
    {
      row = 0;
      int j = i + maxRow;
      String[] copyBuff = new String[numRow];
      arraycopy(lineBuff, 1, copyBuff, 0, maxRow-1);
      copyBuff[maxRow-1] = lines[j];
      lineBuff = copyBuff;
      displayLines(lineBuff);
    }
  } 
  void displayLines(String lines[])
  {
    background(0);
    for(int i = 0; i < maxRow; i++)
    {
      lineBuff[i] = lines[i];
      text(lineBuff[i],leftmargin,(row*fSize)+fSize);
      row = row + 1;
    }
  }
  boolean getExcess()
  {
    return excessLines;
  }
  int getExLines()
  {
    return diffLines;
  }
}

Re: Scrolling text timed
Reply #1 - Dec 11th, 2009, 3:24pm
 
I find your code quite difficult to read and overly complicated.
So I thought I re-write the code so that it's more easy to read and understand.

I hope it's more or less what you intended to do.

Code:

TextEngine textEngine;
String[] loadText;
boolean useScroll = true;
void setup()
{
 size(700,400);
 frameRate(30);

 textEngine = new TextEngine();
 loadText = loadStrings("test.txt");
 textEngine.setText(loadText);
}

void draw()
{
 if(useScroll)
 {
   //Scroll the commands
   textEngine.scrollText();
 }
 else
 {
   //Just draw the commands
   textEngine.draw();
 }
}

class TextEngine
{
 LinkedList lineBuffer = new LinkedList();
 
 final int fontSize = 16;
 final int maxBufferSize = 50;
 final int maxLines = 24;
 final int leftMargin = 10;
 
 int scrollIndex = 0;

 TextEngine()
 {
   textFont(createFont("Arial",fontSize));
 }

 /**
  * Set the text being displayed in the window
  */
 void setText(String[] text)
 {
   lineBuffer.clear();
   addText(text);
 }


 /**
  * Add the strings to the line buffer
  */
 void addText(String[] text)
 {
   for(int i = 0; i < text.length; i++)
   {
     addString(text[i]);
   }
 }

 /**
  * Add a string to the line buffer
  */
 void addString(String text)
 {
   lineBuffer.add(text);
   removeExcess();
 }

 /**
  * Draw the visible lines on the screen
  */
 void draw()
 {
   draw(max(0, lineBuffer.size()-maxLines));
 }

 /**
  * Draw the visible lines on the screen
  * Starting from the given index in the line buffer
  */
 private void draw(int start)
 {
   background(0);
   fill(0,255,0);
   
   int nbLines = min(maxLines, lineBuffer.size());
   for(int i = start; i < start+nbLines ; i++)
   {
     String line = (String)lineBuffer.get(i%lineBuffer.size());
     text(line,leftMargin,((i-start)*fontSize)+fontSize);
   }
 }

 /**
  * Scroll the line buffer across the screen
  */
 void scrollText()
 {
   draw(scrollIndex);
   scrollIndex = (scrollIndex + 1)%lineBuffer.size();
 }

 /**
  * Remove the first line if the buffer is larger than the
  * maximum buffer size
  */
 private void removeExcess()
 {
   if(lineBuffer.size() > maxBufferSize)
   {
     lineBuffer.removeFirst();
   }
 }
}

Re: Scrolling text timed
Reply #2 - Dec 11th, 2009, 3:46pm
 
Yes I am afraid my coding skills are nothing too awesome yet, but I'm learning. Thanks for giving me that nice bit of code, again you smash my original hacked up code with an elegant bit of advance (to me at least) code which will take me a bit of time to digest. I appreciate it : D. I will have a go myself, but since I don't quite grasp the things you have done, I don't know how to adjust this to my original intent. Having text scroll down untill the last line last is printed, and then stop, thus not going back to the first item. I am not trying to build a matrix effect ; ), although that is what my original sketch looked like.
Re: Scrolling text timed
Reply #3 - Dec 11th, 2009, 3:49pm
 
If you don't understand bits of the code, please do ask!
Re: Scrolling text timed
Reply #4 - Dec 11th, 2009, 3:53pm
 
Making the scroll stop when all lines are displayed:
add one line in scrollText : )
Quote:
 void scrollText()
 {
   draw(scrollIndex);
   
   if(lineBuffer.size() - scrollIndex > maxLines)
   {
     scrollIndex = (scrollIndex + 1)%lineBuffer.size();
   }
 }
Re: Scrolling text timed
Reply #5 - Dec 11th, 2009, 5:07pm
 
Thanks again!

Well since you asked for it ; ). Here are some questions about your code.

I noticed the use of private and final, I am guessing you are being more explicit here for easy reading purposes, or is there another reason?

I see you used the a draw function inside a class, I am guessing this is the best way to make a loop inside a class?

Also what is happening in this bit of code, is this recursive, I am really puzzeled. And what is max determining here, why do you want to know what the max value is between 0 and the size of excess lines (?), perhaps trying to prevent a null pointer error?
 void draw()
 {
   draw(max(0, lineBuffer.size()-maxLines));
 }

What is happening here, especially the one with the arrow confuses me. I can only find get in the refrence in conjecture with pVectors, and I don't think we're working with those. I am guessing it gets the difference between the excess and the buffer size one at a time or something? How does the % work in this?
   for(int i = start; i < start+nbLines ; i++)
   {
     String line = (String)lineBuffer.get(i%lineBuffer.size()); <==
     text(line,leftMargin,((i-start)*fontSize)+fontSize);
   }

Again the draw(); function weirds me out. I suppose this is the bit that does the timing, I can't get around how though.

   draw(scrollIndex);
   if(lineBuffer.size() - scrollIndex > maxLines)
   {
    scrollIndex = (scrollIndex + 1)%lineBuffer.size();
   }

Also linkedList() and removeFirst() (just removes the first item of an array maybe?) seem to be things in the processing library you call, but I don't really know what they are, and I can't find them in the reference.
Re: Scrolling text timed
Reply #6 - Dec 12th, 2009, 12:33am
 
Quote:
I noticed the use of private and final, I am guessing you are being more explicit here for easy reading purposes, or is there another reason

Your code will still run if you remove private and final identifiers.
I use final when I assume the variables will not change while running the program. So they are ... final.

I use private when I do not want a function to be called from outside the class. Again, you can just remove it, and it will still work. It's just a matter of readability in this case. You don't have to go start searching outside the class to see where the function is being used because the only place it can be used, is inside the class.

Quote:
I see you used the a draw function inside a class, I am guessing this is the best way to make a loop inside a class

I did not use the draw function inside the class. I just used a function called draw. Because that's what it does. It draws the text engine. You still need to call the function from within processing's draw function. But you can rename the function to something else, and it will still work.

Quote:
Also what is happening in this bit of code, is this recursive, I am really puzzeled. And what is max determining here, why do you want to know what the max value is between 0 and the size of excess lines (), perhaps trying to prevent a null pointer error
 void draw()
 {
   draw(max(0, lineBuffer.size()-maxLines));
 }

No, it's not a recursive call. It calls a function with the same name, but with a different parameter. I made a private draw function which starts the drawing at a specific index, so that it can be used in both the draw() and the scrollText() function. The max(number1, number2) determines the maximum of 0 and a number that might possibly be negative. This happens when the size of the buffer is smaller than the maximum number of lines which can be shown on the screen. In this case the textengine should start drawing from the start of the buffer (0) and not buffersize-maxlines.
Quote:
What is happening here, especially the one with the arrow confuses me. I can only find get in the refrence in conjecture with pVectors, and I don't think we're working with those. I am guessing it gets the difference between the excess and the buffer size one at a time or something How does the % work in this
   for(int i = start; i < start+nbLines ; i++)
   {
     String line = (String)lineBuffer.get(i%lineBuffer.size()); <==
     text(line,leftMargin,((i-start)*fontSize)+fontSize);
   }

All lines are being drawn from the startindex to startindex+nblines. This last index might very well be larger than the number of items in our linebuffer, so we need to wrap the index around back to the start of the buffer.
lineBuffer.get(index) : get the string at the specified index
i%lineBuffer.size() : wrap the index around
The a%b operation (a modulo b) gives you the remainder after dividing a by b. So if you divide 7 by 4, the remainder is 3.
So if your index reaches the size of the buffer it goes back to 0.

Quote:
Again the draw(); function weirds me out. I suppose this is the bit that does the timing, I can't get around how though.

   draw(scrollIndex);
   if(lineBuffer.size() - scrollIndex > maxLines)
   {
    scrollIndex = (scrollIndex + 1)%lineBuffer.size();
   }

Also linkedList() and removeFirst() (just removes the first item of an array maybe) seem to be things in the processing library you call, but I don't really know what they are, and I can't find them in the reference.

It used the private draw function as explained above.
Processing is built on top of java, so you can use stuff which is not in the processing reference, but which is in java api: LinkedList
I recommend using the stuff documented in processing though. As the java api stuff is a little bit harder to use. However, there just isn't a decent alternative for the LinkedList (it's the most efficient way to remove the first item in a list, which is done a lot in this code). If I'm wrong, please someone correct me!

I hope it helps.
Re: Scrolling text timed
Reply #7 - Dec 12th, 2009, 3:49am
 
Thanks that was very helpful, I'll be cracking on now trying to make my database terminal like program in processing, this sure is some cool stuff : D.

Deurbel
Page Index Toggle Pages: 1