Making a back button

edited April 2017 in Library Questions

My code is a little long so I'm sorry about that but I've tried to condense it here and just explain the bare minimum of my problem. I have a state machine that incorporates both mousePressed and button functions. I want to create a back button starting in state 2, so I called it in one of my state 2 options, void showQuestion2Palestinian(). The problem is, it only shows up if I click the Palestinian button first. Additionally, I had to put a set of conditions on the button more specific than state--; because I don't always just want it to go back to the last state. I've given it direct assignments, but a couple of them glitch and don't go back to the right state. I don't know if this is a flaw in my processing application or in the code... Please help!

import controlP5.*;
ControlP5 cp5session1;
ControlP5 cp5session2;
ControlP5 cp5session3;
ControlP5 cp5;

int state;

void setup() { 
  size(800, 600);
  background(255);  
}

void mousePressed() {

  if(state == 3) {
    if (200<mouseX && mouseX<400 && 100<mouseY && mouseY<400){
    state = 4;  
    }
  }

/*  if(state == 4) {
    if (200<mouseX && mouseX<400 && 100<mouseY && mouseY<400){
    state = 5;  
    }
  } */

  if(state == 6) {
    if (200<mouseX && mouseX<400 && 100<mouseY && mouseY<400){
    state = 7;  
    }
  }
   if(state == 8) {
    if (0<mouseX && mouseX<800 && 0<mouseY && mouseY<600){
    state = 9;  
    }
  }
  if(state == 10) {
    //should be before
    if (200<mouseX && mouseX<400 && 100<mouseY && mouseY<400){
    state = 12;  
    }
  }

  /*if(state == 11) {
    if (200<mouseX && mouseX<400 && 100<mouseY && mouseY<400){
    state = 12;  
    }
  } */

  if(state == 13) {
    if (200<mouseX && mouseX<400 && 100<mouseY && mouseY<400){
    state = 14;  
    }
  }
   if(state == 15) {
    if (0<mouseX && mouseX<800 && 0<mouseY && mouseY<600){
    state = 16;  
    }
  } 
}


public void Next (int theValue) {
  println("a button event from Next: "+theValue);
  showQuestion1();
  state++;
}

void showQuestion1() {
  background(255);
  video.stop();
  cp5.hide();

  cp5session1 = new ControlP5(this); 

  cp5session1.addButton("Palestinian")
    .setPosition(width/2 - 200, 285)
    .updateSize()
    .setSize(100, 50)
    ;

  cp5session1.addButton("Israeli")
    .setPosition(width/2 + 100, 285)
    .updateSize()
    .setSize(100, 50)
    ;

}

public void controlEvent(ControlEvent theEvent) {
  println(theEvent.getController().getName());
}

public void Palestinian (int theValue) {
  println("a button event from Palestinian: "+theValue);
  showQuestion2Palestinian();
  state = 2;
}

public void Israeli (int theValue) {
  println("a button event from Israeli: "+theValue);
  showQuestion2Israeli();
  state = 2;

}


void showQuestion2Palestinian() {
  background(0);
  cp5session1.hide();

  //Date buttons created 
  cp5session2 = new ControlP5(this);
  cp5session2.addButton("Palestine1945")
    .setPosition(width/3 - 100, 285)
    .updateSize()
    .setSize(100, 50)
    ;

  cp5session2.addButton("Palestine1967")
    .setPosition(width/3 + 100, 285)
    .updateSize()
    .setSize(100, 50)
    ;

  cp5session2.addButton("Palestine2017")
    .setPosition(width/3 + 300, 285)
    .updateSize()
    .setSize(100, 50)
    ;

cp5session3 = new ControlP5(this); 

  cp5session3.addButton("back")
    .setPosition(600, 500)
    .updateSize()
    .setSize(100, 50)
    ;
}


public void back (int theValue) {
  println("a button event from Palestine 1945: "+theValue);

  if (state == 2) {
    state--;
    cp5session1.show();
    cp5session2.hide();
  }
   if (state == 3) {
    state = 2;
  }
   if (state == 4) {
    state = 3;
  }
   if (state == 5) {
    state = 4;
  }
   if (state == 6) {
    state = 2;
  }
   if (state == 7) {
    state = 6;
  }
   if (state == 8) {
    state = 2;
  }

   if (state == 9) {
    state = 8;
  }
   if (state == 10) {
    state = 2;
  }

   if (state == 11) {
    state = 10;
  }

   if (state == 12) {
    state = 11;
  }

   if (state == 13) {
    state = 2;
  }
   if (state == 14) {
    state = 13;
  }

   if (state == 15) {
    state = 2;
  }

   if (state == 16) {
    state = 15;
  }
}

public void Palestine1945 (int theValue) {
  println("a button event from Palestine 1945: "+theValue);
  state = 3;
  cp5session2.hide();
  cp5session1.hide();

}

public void Palestine1967 (int theValue) {
  println("a button event from Palestine1967: "+theValue);
  state = 6;
  cp5session2.hide();
  cp5session1.hide();

}


public void Palestine2017 (int theValue) {
  println("a button event from Palestine2017: "+theValue);
  state = 8; 
  cp5session2.hide();
  cp5session1.hide();

}

void showQuestion2Israeli() {
  background(255);
  cp5session1.hide();
  //cp5session3.show(); <== when I run this, the buttons don't show up at all 

  cp5session2 = new ControlP5(this);

  cp5session2.addButton("Israel1945")
    .setPosition(width/3 - 100, 285)
    .updateSize()
    .setSize(100, 50)
    ;

  cp5session2.addButton("Israel1967")
    .setPosition(width/3 + 100, 285)
    .updateSize()
    .setSize(100, 50)
    ;

  cp5session2.addButton("Israel2017")
    .setPosition(width/3 + 300, 285)
    .updateSize()
    .setSize(100, 50)
    ;
}
Sign In or Register to comment.