Hiding a slider - How?

edited November 2017 in Library Questions

Hey! I have been trying for quite some time now to figure out how to hide a slider after it has been shown. I've tried numerous ways but I have not found a solution yet. I would like to archive a code where the slider simply disappear if I hit the green area.

import controlP5.*;

ControlP5 cp5;

void setup() {
  size(400,400);


}

void draw() {
  fill(255,0,0);
  rect(0,0,200,100);
  fill(0,255,0);
  rect(200,0,200,100);
     if (mousePressed) {
      if (mouseX > 0 && mouseX < 200 && mouseY > 0 && mouseY < 100) {
          cp5 = new ControlP5(this);
  cp5.addSlider("slider")
  .setPosition(100,150)
  .setSize(200,30);
      }
    } 

         if (mousePressed) {
      if (mouseX > 200 && mouseX < 400 && mouseY > 0 && mouseY < 100) {

      }
    } 
}

Answers

  • Te following link should help:

    https://forum.processing.org/one/topic/controlp5-show-and-hide.html

    Here below is the modified version of your code. Notice There are two ways to do this. I have commented out the second way.

    Lastly, you should create your cp5 object inside the draw() function and there is a potential this object could be created 30 times per second. This is the reason I moved it to setup(), which is run once. I encourage you to explore the examples that comes with the library. In the Processing IDE, go to Files>>Examples and then go to Contributed Libraries>>ControlP5 and you can see the list of examples associated to this library.

    Kf

    import controlP5.*;
    
    ControlP5 cp5;
    
    void setup() {
      size(400, 400);
    
      cp5 = new ControlP5(this);
      cp5.addSlider("slider")
        .setPosition(100, 150)
        .setSize(200, 30);
    }
    
    void draw() {
      background(0);
      fill(255, 0, 0);
      rect(0, 0, 200, 100);
      fill(0, 255, 0);
      rect(200, 0, 200, 100);
      if (mousePressed) {
        if (mouseX > 0 && mouseX < 200 && mouseY > 0 && mouseY < 100) {
          cp5.hide();
        }
    
        if (mouseX > 200 && mouseX < 400 && mouseY > 0 && mouseY < 100) {
          cp5.show();
        }
      }
    }
    
    //void mousePressed() {
    //  cp5.getController("slider").setVisible(false);
    //}
    
    //void mouseReleased() {
    //  cp5.getController("slider").setVisible(true);
    //}
    
Sign In or Register to comment.