Show/Hide CP5 Button

edited March 2017 in Library Questions

Hi! I'm new to coding so sorry if this question is dumb but I can't figure out how to make a CP5 button hide once it's been clicked. I want the two buttons A and B to be the first to show then once they are clicked I want the buttons 1945, 1960, and 2015 to show and A and B to hide. Please help!

import controlP5.*;

ControlP5 cp5;

String myResponse1;
int myResponse2;
PImage show1947Israel, show1960Israel, show2015Israel, question;
boolean display = false;

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

  cp5 = new ControlP5(this);

  cp5.addButton("A")
     .setPosition(210,300)
     .updateSize()
     ;

  cp5.addButton("B")
  .setPosition(310,300)
     .updateSize()
     ;

  cp5.addButton("1945")
     .setPosition(150,400)
     .updateSize()
     ;

cp5.addButton("1945").setSwitch(true);

  cp5.addButton("1960")
     .setPosition(250,400)
     .updateSize()
     ;

cp5.addButton("1960").setSwitch(true);

  cp5.addButton("2015")
     .setPosition(350,400)
     .updateSize()
     ;

cp5.addButton("2015").setSwitch(true);

question = loadImage("when.png");
show1947Israel = loadImage("1947map.png");
show1960Israel = loadImage("1960map.png");
show2015Israel = loadImage("2015map.png");

}

void draw(){
  hideButton();

}  

void hideButton() {
 if (display == false) {
    cp5.addButton("1945").hide();
    cp5.addButton("1960").hide();
    cp5.addButton("2015").hide();
  }
  else {
   cp5.addButton("1945").show();
   cp5.addButton("1960").show();
   cp5.addButton("2015").show();
  }
}
  public void controlEvent(ControlEvent theEvent) {
  println(theEvent.getController().getName());
  }

// function A will receive changes from 
// controller with name A
  public void A(int theValue) {
  println("a button event from A: "+theValue);
 showQuestion2();


  }
// function B will receive changes from 
// controller with name B
  public void B(int theValue) {
  println("a button event from B: "+theValue);
showQuestion2();
  }

  void showQuestion2() {
   image(question, 0, 0, width, height);
   display = true;
  }

Answers

  • Answer ✓
    cp5session1 = new ControlP5(this);
    cp5session2 = new ControlP5(this);
    

    Associate buttons A and b to session 1. Associate 1945 and 1960 to session2. Now to hide the first two buttons, you need to call: cp5session1.hide(); and to show them: cp5session1.show();

    This is one way to do it.

    Kf

  • I tried to do that but the code won't run when I comment out //cp5 = new ControlP5(this); but when it's not commented out the show and hide feature doesn't work. Could you tell me what I'm doing wrong? Thanks!

    import controlP5.*;
        ControlP5 cp5;
    
        ControlP5 cp5session1;
        ControlP5 cp5session2;
    
        String myResponse1;
        int myResponse2;
        PImage show1947Israel, show1960Israel, show2015Israel, question;
    
        void setup() {
          size(600, 600);
          background(255);
    
          //cp5 = new ControlP5(this);
    
          cp5session1 = new ControlP5(this);
    
          cp5.addButton("A")
             .setPosition(210,300)
             .updateSize()
             ;
    
          cp5.addButton("B")
          .setPosition(310,300)
             .updateSize()
             ;
    
         cp5session2 = new ControlP5(this);
    
          cp5.addButton("1945")
             .setPosition(150,400)
             .updateSize()
             ;
    
    
          cp5.addButton("1960")
             .setPosition(250,400)
             .updateSize()
             ;
    
    
          cp5.addButton("2015")
             .setPosition(350,400)
             .updateSize()
             ;
    
        question = loadImage("when.png");
        show1947Israel = loadImage("1947map.png");
        show1960Israel = loadImage("1960map.png");
        show2015Israel = loadImage("2015map.png");
    
        }
    
        void draw(){
    
        }  
    
          public void controlEvent(ControlEvent theEvent) {
          println(theEvent.getController().getName());
          }
    
          public void A (int theValue) {
          println("a button event from A: "+theValue);
         showQuestion2();
    
    
          }
    
          public void B (int theValue) {
          println("a button event from B: "+theValue);
        showQuestion2();
          }
    
          void showQuestion2() {
           image(question, 0, 0, width, height);
             cp5session1.hide();
             cp5session2.show();
    
          }
    
  • I realized my mistake! I hadn't changed to cp5.addButton to cp5session1.addButton. Thanks for your help!

  • Perfect!

    Kf

Sign In or Register to comment.