How to switch currentScreen after time delay?

edited April 2015 in How To...

Hi folks,

New to Processing and coding in general. Have looked but can't find the answer to this one. I have three different screens/cases - 0, 1 and 2 in my sketch. At the moment hitting the control key cycles through the screens.

In screen 0, text is displayed to begin with until the user inputs their own text. Hitting control saves the user's text to a text file and moves to the next screen (screen1).

Screen 1 animates the text on a loop.** I would like to insert a timer here for about 7 seconds after which it switches to the next screen (screen 2).**

Eventually, I would like it to return to the first screen where the text displayed is the last line of whatever the user inputted, the next user inputs some more text and the cycle begins again....I haven't gotten to this part yet.

I'm sure there's a lot of inefficient code in here. I've basically been stumbling around in a very dark room, just turning things off and on, and moving them around until something looks like it's working without breaking something else. So, if you see anyway to improve it, please let me know.

Thanks in advance.

PS: Bonus question - anyone know how I can introduce colour hue (or why I can't) into the ellipses in Screen 2?

//Code adapted from examples by Amnon available at: https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/ 
//and Daniel Shiffman available at: http://www.learningprocessing.com/examples/chapter-18/example-18-1/
//and John Park, University Oregon available at: http://pages.uoregon.edu/park/Processing/AdvancedSplit/applet/AdvancedSplit.pde
//and Kyuha Shim, available at: http://visualizing-data.org/index.php?/class-code/52-stringcharatascii-/

import java.util.Calendar;

int currentScreen;

int savedTime;
int totalTime = 6000;

int x=0;
float rotater = 0;

String myText = "Your story...";
String yourText = ""; // Variable to store text currently being typed
String savedText = ""; // Variable to store saved text when Control is hit
String [] rotateText = split (savedText, " "); // array of words contained in savedText
//String[] vizText = splitTokens (yourText, ".?!");//text for visualization, split into lines.
String newText = ("data/story.txt");//variable to store content of new txt file (user input)
//String nextText = loadStrings (newText [newText.length - 1]); //start with last line of last story
//String nextText = ""; //variable to hold last line of latest story

PrintWriter textFile;

// timestamp
String timestamp() {
Calendar now = Calendar.getInstance();
return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

void setup() {
  size(800, 600);
  smooth();

  savedTime = millis();

  PFont s = createFont("AGaramondPro-Regular", 140); 
  textFont(s);
  smooth();
  fill (0);
  textAlign(CENTER, CENTER);



}

void draw() {
  switch(currentScreen) {
  case 0: drawScreenZero(); break;
  case 1: drawScreenOne(); break;
  case 2: drawScreenTwo(); break;
  default: background(0); break;
  }

}

void drawScreenZero() {
  background(255);
  textSize (30);
  text (myText, 0, 0, width, height);
  text (yourText, 0, 0, width, height);
  //text (nextText, 0, 0, width, height);

  // Create a new file in the sketch directory
  //textFile = createWriter(timestamp()+".txt");
  //textFile.println(savedText);

} 


void drawScreenOne() {

  int savedTime;
  int totalTime = 6000;
  savedTime = millis();

  int passedTime = millis() - savedTime;
  // Has 10 seconds passed?sa
  if (passedTime > totalTime) {
      currentScreen++;}
  if (currentScreen > 2) { currentScreen = 0; } //switches to next screen
    savedTime = millis(); // Save the current time to restart the timer!


    String [] rotateText = split (savedText, " ");
    textSize (140);
    fill(255,20);
    rotater += 1;
  x++; 
  if(x> (rotateText.length-1)){
    x=0;
  }  

  translate(width/2, height/2);
  rotate (rotater);
  text(rotateText[x],0,0);
  delay(40); // in milliseconds


}

void drawScreenTwo() { 

  background (255);

  savedText = yourText;
  String[] list = splitTokens(savedText, ".?!"); //splits inputted text into lines.
      noStroke();
      colorMode(HSB);
      translate(10, height/2);
      //translates each character into an elipse colour coded by its ASCII number
      for (int i=0; i<list.length; i++) {
      for (int j=0; j<list[i].length(); j++) {
      char num = list[i].charAt(j);
      fill(map(int(num), 0, 128, 0, 255)); 
      ellipse(j*20, i*20, 20, 20);
      }
      }


      saveFrame("dot_images/"+timestamp()+"_##.png"); //creates a PNG in the sketch directory
      noLoop();
}

void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (yourText.length() > 0) {
      yourText = yourText.substring(0, yourText.length()-1);
    }
  } else if (keyCode == DELETE) {
    yourText = "";
  } else if (keyCode != CONTROL && keyCode != ALT) {
    myText = "";
    yourText = yourText + key;
  }

  // If the Control key is pressed save the String and write it to text file
  if (key == CODED) 
  {
    if (keyCode == CONTROL) {
      savedText = yourText;
      textFile = createWriter("stories/"+timestamp()+"_##.txt");
      textFile.println(savedText);
      textFile.flush();
      textFile.close();
      rect (0,0,width, height); //PROBLEM visible when screen is switched.
      noStroke ();
      currentScreen++;
  if (currentScreen > 2) { currentScreen = 0; } //switches to next screen


      } 

 else {
      // Otherwise, concatenate the String
      // Each character typed by the user is added to the end of the String variable.
      yourText = yourText + key;
    }
  }
}
Tagged:

Answers

  • you wrote

    I would like to insert a timer here for about 7 seconds after which it switches to the next screen (screen 2).**

    you mean from screen #0 to #1 ?

  • Answer ✓
    //Code adapted from examples by Amnon available at: <a href="https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/" target="_blank" rel="nofollow">https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/</a>; 
    //and Daniel Shiffman available at: <a href="http://www.learningprocessing.com/examples/chapter-18/example-18-1/" target="_blank" rel="nofollow">http://www.learningprocessing.com/examples/chapter-18/example-18-1/</a>;
    //and John Park, University Oregon available at: <a href="http://pages.uoregon.edu/park/Processing/AdvancedSplit/applet/AdvancedSplit.pde" target="_blank" rel="nofollow">http://pages.uoregon.edu/park/Processing/AdvancedSplit/applet/AdvancedSplit.pde</a>;
    //and Kyuha Shim, available at: <a href="http://visualizing-data.org/index.php?/class-code/52-stringcharatascii-/" target="_blank" rel="nofollow">http://visualizing-data.org/index.php?/class-code/52-stringcharatascii-/</a>;
    
    import java.util.Calendar;
    
    int currentScreen = 0;
    
    
    int totalTime = 6000;
    
    int x=0;
    float rotater = 0;
    
    String myText = "Your story...";
    String yourText = ""; // Variable to store text currently being typed
    String savedText = ""; // Variable to store saved text when Control is hit
    String [] rotateText = split (savedText, " "); // array of words contained in savedText
    //String[] vizText = splitTokens (yourText, ".?!");//text for visualization, split into lines.
    String newText = ("data/story.txt");//variable to store content of new txt file (user input)
    //String nextText = loadStrings (newText [newText.length - 1]); //start with last line of last story
    //String nextText = ""; //variable to hold last line of latest story
    
    PrintWriter textFile;
    
    int savedTime=0;
    
    boolean verticalLineIsVisible = true;
    
    // ------------------------------------------------------
    
    void setup() {
      size(800, 600);
      smooth();
    
      savedTime = millis();
    
      PFont s = createFont("AGaramondPro-Regular", 140); 
      textFont(s);
      smooth();
      fill (0);
      textAlign(CENTER, CENTER);
      savedTime = millis();
    }
    
    void draw() {
      switch(currentScreen) {
      case 0: 
        drawScreenZero(); 
        break;
      case 1: 
        drawScreenOne(); 
        break;
      case 2: 
        drawScreenTwo(); 
        break;
      default: 
        background(0);
        // ERROR
        // should never get here!!!!
        println ("// should never get here!!!!"); 
        exit(); 
        break;
      } // switch
    } // func 
    
    // ------------------------------------------------------
    
    void drawScreenZero() {
      background(255);
      textSize (30);
      text (myText, 0, 0, width, height);
      text (yourText, 0, 0, width, height);
      //text (nextText, 0, 0, width, height);
    
      // Create a new file in the sketch directory
      //textFile = createWriter(timestamp()+".txt");
      //textFile.println(savedText);
    
    
      int passedTime = millis() - savedTime;
      // Has 6 seconds passed?
      if (passedTime > totalTime) {
        currentScreen=1;
        println ("screen 1");
      }
    } 
    
    void drawScreenOne() {
    
      background(255);
    
      fill(0);
      textSize (30);
      //  println ("screen one");
      text ("screen one", 10, 55, width, height);
    
      textAlign(LEFT, CENTER);
      text (yourText+verticalLine(), 330, 155, width, height);
      textAlign(CENTER, CENTER);
    
      if (frameCount%20==0)
        verticalLineIsVisible=!verticalLineIsVisible;
    
      //  if (currentScreen > 2) { 
      //    currentScreen = 0;
      //  } //switches to next screen
    
      String [] rotateText = split (savedText, " ");
      textSize (140);
      fill(255, 20);
      rotater += 1;
      x++; 
      if (x> (rotateText.length-1)) {
        x=0;
      }  
    
      translate(width/2, height/2);
      rotate (rotater);
      text(rotateText[x], 0, 0);
      delay(4); // in milliseconds / 40
    }
    
    void drawScreenTwo() { 
    
      background (255);
    
      savedText = yourText;
      String[] list = splitTokens(savedText, ".?!\n"); //splits inputted text into lines.
      // noStroke();
      stroke(90);
      // colorMode(HSB);
      translate(40, height/2);
      //translates each character into an ellipse colour coded by its ASCII number
      for (int i=0; i<list.length; i++) {
        for (int j=0; j<list[i].length (); j++) {
          char num = list[i].charAt(j);
          //  println(num); 
          // fill(map(int(num), 0, 128, 0, 255));
          fill(map(int(num), 0, 255, 0, 255)); 
          ellipse(j*20, i*20, 10, 10);
        }
      }
      saveFrame("dot_images/"+timestamp()+"_##.png"); //creates a PNG in the sketch directory
      // noLoop();
    }
    
    // ----------------------------------------------------------
    
    void keyPressed() {
    
      switch(currentScreen) {
      case 0:
        // any key: move on (and timer) 
        currentScreen=1;
        break;
    
      case 1: 
        keyPressedForScreen1(); 
        break;
    
      case 2: 
        // any key: move on  
        currentScreen=0;
        savedTime = millis();
        break;
    
      default: 
        background(0);
        // ERROR
        // should never get here!!!!
        println ("// should never get here!!!!   Error # 165 "); 
        exit(); 
        break;
      } // switch
    }
    
    // -------------------------------------------------------------
    
    void keyPressedForScreen1() {
    
      if (keyCode == BACKSPACE) {
        if (yourText.length() > 0) {
          yourText = yourText.substring(0, yourText.length()-1);
        }
      } else if (keyCode == DELETE) {
        yourText = "";
      } else if (keyCode != CONTROL && keyCode != ALT) {
        myText = "";
        yourText = yourText + key;
      }
    
      // If the Control key is pressed save the String and write it to text file
      if (key == CODED) 
      {
        if (keyCode == CONTROL) {
          savedText = yourText;
          textFile = createWriter("stories/"+timestamp()+"_##.txt");
          textFile.println(savedText);
          textFile.flush();
          textFile.close();
          rect (0, 0, width, height); //PROBLEM visible when screen is switched.
          noStroke ();
          // move on 
          currentScreen=2;
        }
      }
    } // func 
    
    // minor tools 
    
    
    // timestamp
    String timestamp() {
      Calendar now = Calendar.getInstance();
      return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
    }
    
    String verticalLine() {
      // our cursor 
      if (verticalLineIsVisible) 
        return "|";
      else 
        return "";
    }
    //
    
  • screen 0 goes to screen 1 after 6 seconds

    in screen 1 you can enter text (only here)

    CTRL brings you to screen 2

    here the ellipses are shown

    any key brings you back to zero

    ;-)

  • HI @Chrisir. I mean switch from Screen #1 to Screen #2 after a delay. At the moment user hits Control to switch from Screen #0 to Screen #1. Screen #1 animates the text on an infinite loop. Instead I would like it to animate for about 7 seconds then automatically switch to Screen #2 without any user input.

    Updated version of code below. Still working on it...

    //Code adapted from examples by Amnon available at: https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/ 
    //and Daniel Shiffman available at: http://www.learningprocessing.com/examples/chapter-18/example-18-1/
    //and John Park, University Oregon available at: http://pages.uoregon.edu/park/Processing/AdvancedSplit/applet/AdvancedSplit.pde
    //and Kyuha Shim, available at: http://visualizing-data.org/index.php?/class-code/52-stringcharatascii-/
    
    import java.util.Calendar;
    
    int currentScreen;
    
    int savedTime; //for Screen 1
    int totalTime = 6000;
    
    int x=0; //for Screen 1
    float rotater = 0;
    
    String myText = "Your story...";
    String yourText = ""; // Variable to store text currently being typed
    String savedText = ""; // Variable to store saved text when Control is hit
    String [] rotateText = split (savedText, " "); // array of words contained in savedText
    String lowText;
    
    int[] letterCounts = new int[26]; //for Screen 2
    int maxCount = 0;
    String joinedText; //for Screen 2
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ,.;:!? ";
    int[] counters = new int[alphabet.length()];
    boolean[] drawLetters = new boolean[alphabet.length()];
    
    float charSize; //for Screen 2
    color charColor = 0;
    int posX = 20;
    int posY = 50;
    boolean drawText = true;
    
    PrintWriter textFile;
    
    // timestamp
    String timestamp() {
    Calendar now = Calendar.getInstance();
    return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
    }
    
    void setup() {
      size(800, 600);
      smooth();
    
      savedTime = millis();
    
      PFont f = createFont("AGaramondPro-Regular", 140); 
      textFont(f);
      smooth();
      fill (0);
      textAlign(CENTER, CENTER);
    
      for (int t = 0; t < alphabet.length(); t++) {
      drawLetters[t] = true;
      }
    
      countCharacters();
    
    }
    
    void draw() {
      switch(currentScreen) {
      case 0: drawScreenZero(); break;
      case 1: drawScreenOne(); break;
      case 2: drawScreenTwo(); break;
      default: background(255); break;
      }
    
    }
    
    void drawScreenZero() {
      background(255);
      textSize (30);
      fill (0);
      text (myText, 0, 0, width, height);
      text (yourText, 0, 0, width, height);
    } 
    
    
    void drawScreenOne() {  
        String [] rotateText = split (savedText, " ");
        textSize (140);
        fill(255,20);
        rotater += 1;
        x++; 
        if(x> (rotateText.length-1)){
          x=0;
        }  
    
        translate(width/2, height/2);
        rotate (rotater);
        text(rotateText[x],0,0);
        delay(40); // in milliseconds
    
        int savedTime;
        int totalTime = 6000;
        savedTime = millis();
    
        int passedTime = millis() - savedTime;
        // Has 10 seconds passed?sa
        if (passedTime > totalTime) {
            currentScreen++;}
        if (currentScreen > 2) { currentScreen = 0; } //switches to next screen
          savedTime = millis(); // Save the current time to restart the timer!
    }
    
    void drawScreenTwo() { 
      for (int t = 0; t < alphabet.length(); t++) {
      drawLetters[t] = true;
      }
    
      countCharacters();  
    
      background (0); 
      fill (255);
    
      for (int t = 0; t < alphabet.length(); t++) {
        drawLetters[t] = true;
      }
    
     countCharacters();
    
    
      posX = 20;
      posY = 200;
      float oldX = 0;
      float oldY = 0;
    
      // go through all characters in the text to draw them  
      for (int t = 0; t < savedText.length(); t++) {
        // again, find the index of the current letter in the alphabet
        String u = str(savedText.charAt(t)).toUpperCase();
        char uppercaseChar = u.charAt(0);
        int index = alphabet.indexOf(uppercaseChar);
        if (index < 0) continue;
    
        textSize(20);
        smooth ();
    
        float sortY = index*20+40;
        float m = map(mouseX, 50,width-50, 0,1);
        m = constrain(m, 0, 1);
        float interY = lerp(posY, sortY, m);
    
        if (drawLetters[index]) {
            oldX = posX;
            oldY = interY;
          }
    
    if (drawText) text(savedText.charAt(t), posX, interY);
    
        else {
          oldX = 0;
          oldY = 0;
        }
    
    
    posX += textWidth(savedText.charAt(t));
    if (posX >= width-200 && uppercaseChar == ' ') {
    posY += 40;
    posX = 20;
        }
    }
          String[] list = splitTokens(savedText, ".?!"); //splits inputted text into lines.
          colorMode(HSB);
          translate(10, height/2);
          //translates each character into an elipse colour coded by its ASCII number
          for (int i=0; i<list.length; i++) {
          for (int j=0; j<list[i].length(); j++) {
          char num = list[i].charAt(j);
          fill(map(int(num), 0, 128, 0, 255)); 
          ellipse(j*15, i*15, 15, 15);
          }
          saveFrame("dot_images/"+timestamp()+".png"); //creates a PNG in the sketch directory
          //noLoop();
      }
    }
    
    void keyPressed() {
      if (keyCode == BACKSPACE) {
        if (yourText.length() > 0) {
          yourText = yourText.substring(0, yourText.length()-1);
        }
      } else if (keyCode == DELETE) {
        yourText = "";
      } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
        myText = "";
        yourText = yourText + key;
      }
    
      // If the Control key is pressed save the String and write it to text file
      if (key == CODED) 
      {
        if (keyCode == CONTROL) {
          savedText = yourText;
          textFile = createWriter("stories/"+timestamp()+".txt");
          textFile.println(savedText);
          textFile.flush();
          textFile.close();
          rect (0,0,width, height); //PROBLEM visable when screen is switched.
          noStroke ();
          currentScreen++;
      if (currentScreen > 2) { currentScreen = 0; } //switches to next screen
    
      } 
    
     else {
          // Otherwise, concatenate the String
          // Each character typed by the user is added to the end of the String variable.
          yourText = yourText + key;
        }
      }
    }
    
    void countCharacters(){
      for (int t = 0; t < savedText.length(); t++) {
        // get one char from the text, convert it to a string and turn it to uppercase
        String u = str(savedText.charAt(t)).toUpperCase();
        // convert it back to a char
        char uppercaseChar = u.charAt(0);
        // get the position of this char inside the alphabet string
        int index = alphabet.indexOf(uppercaseChar);
        // increase the respective counter
        if (index >= 0) counters[index]++;
      }
    }
    
  • HI @Chrisir. Our posts must have crossed. Just saw your code now. Thanks so much for doing that. Will have a play around with it. Thanks again for taking the time with this. :)

Sign In or Register to comment.