A carousel for graphs

click mouse to go to next screen

you mustn’t use background() at the begin of a state but use fill(.....); rect(0,0,width,height); I think

Chrisir :wink:

// carousel with a transition between the screens (states)

//
// states
final int stateGame  = 0; // consts
final int statePause = 1;
final int stateC     = 2;
int state = stateGame;    // current

// for transition 
int statePrevious=stateGame; 
boolean transitionFlag=false;
float transitionAmt=0; // for lerp

// -------------------------------------------------

void setup() {
  // init (runs only once)
  size(800, 600);
} // func 

void draw() { 
  // runs on and on 

  if (transitionFlag) {
    // show the transition 
    manageTransition();
  }//if
  else {
    // show the screen (transition is over)
    showState(state); 
    text("press mouse to go on ", 120, 16);
    //
  }//else 
  //
} // func 

// functions for states - called from draw() -------------------------------

void manageTransition() {

  float xprevious=0, xcurrent=0;

  xprevious = lerp( 0, -width, transitionAmt); 
  xcurrent = lerp( width, 0, transitionAmt); 

  pushMatrix(); 
  translate(xprevious, 0); 
  showState(statePrevious); 
  popMatrix(); 

  pushMatrix(); 
  translate(xcurrent, 0); 
  showState(state); 
  popMatrix();
  //
  transitionAmt+=0.025; // for lerp

  // Is the transition finished?
  if (transitionAmt>=1) {
    // yes, reset 
    transitionAmt=0; 
    transitionFlag=false;
  }
}

void showState(int state) {
  switch (state) {

  case stateGame: 
    // Game
    handleStateGame();
    break; 

  case statePause:
    // Pause
    handleStatePause(); 
    break;

  case stateC:
    // Pause
    handleStateC(); 
    break;

  default:
    // error
    println("Error number 939; unknown state : " 
      + state
      + ".");
    exit();
    break;
  } // switch
}//func 

void handleStateGame() {
  // Game
  fill(11);
  rect(0, 0, width, height); 
  fill(244, 3, 3); // red 

  //
  noStroke();
  fill(255, 2, 2) ;
  rect(100, 100, 100, 100);
  fill(255, 2, 255) ;
  // rect(300, 100, 100, 100);
} // func 

void handleStatePause() {
  // Pause
  fill(255);
  rect(0, 0, width, height); 
  fill(244, 3, 3); // red 
  text ("Pause....  " 
    + "   ...   ", 210, 313);
} // func 

void handleStateC() {
  //
  fill(255);
  rect(0, 0, width, height); 
  fill(0);
  text("state C", 111, 122);
}

// ----------------------------------------
// keyboard input 

void keyPressed() {

  switch (state) {

  case stateGame: 
    // Game
    keyPressedForStateGame();
    break; 

  case statePause:
    // Pause
    keyPressedForStatePause(); 
    break;

  case stateC:
    // 
    // 
    break;

  default:
    // error
    println("Error number 1039; unknown state : " 
      + state 
      + ".");
    exit();
    break;
  } // switch
} // func 

// ----

void keyPressedForStateGame() { 
  state++;
} // func

void keyPressedForStatePause() { 
  if (key == CODED) 
  {
    if (keyCode == UP) {
      //
    } else if (keyCode == DOWN) {
      // none
    }

    if (keyCode == LEFT) {
      //
    } else if (keyCode == RIGHT) {
      //
    } else {
      // do nothing
    } // else
  } //  if (key == CODED) {
  else 
  {
    // not CODED ---------------------- 
    if (key == 'p') {
      //
      state = stateGame;
    } else {
      // do nothing
    } // else
  } // else not CODED
} // func

// -------------------------------------------------------
// Mouse input

void mousePressed() {
  //

  statePrevious=state; 
  transitionFlag=true; 
  transitionAmt=0; // for lerp

  switch (state) {

  case stateGame: 
    // Game
    mousePressedForStateGame();
    break; 

  case statePause:
    // Pause
    mousePressedForStatePause(); 
    break;

  case stateC:
    // C
    mousePressedForStateC(); 
    break;

  default:
    // error
    println("Error number 1139; unknown state : " 
      + state 
      + ".");
    exit();
    break;
  } // switch
  //
} // func 

void mousePressedForStateGame() {
  //
  state++;
  //
} // func 

void mousePressedForStatePause() {
  //
  state++;
} // func 

void mousePressedForStateC() {
  //
  state=0;
} // func 

// =========================================
3 Likes