ControlP5: Multiple Toggles - how to make it so that if one toggle is true, the rest are false?

edited May 2017 in Library Questions

I have six toggles: I, II, III, IV, V, VI. If toggle I is true, it changes the variable cycleNumber to 1. If toggle II is true, it changes the variable cycleNumber, and so on. However, at the moment, if I click toggle I, it becomes true and cycleNumber changes to 1, but if I were to then click toggle II, cycleNumber changes to 2, but toggle I still remains true, etc.

Basically I want to create a menu that if an element was clicked, it would have an active state until another element is clicked in the menu which I couldn't figure out how to get to work with a button from the ControlP5 library.

Here's the code I'm currently using:

    import controlP5.*;
    ControlP5 cp52;
    int cycleNumber = 0;

    void setup() {
    cp52 = new ControlP5(this);
    ControlFont cf24 = new ControlFont(createFont("Arial",24));
      cp52.setControlFont(cf24);
      cp52.setColorForeground(color(230));
      cp52.setColorBackground(color(215));
      cp52.setColorLabel(linkColor);
      cp52.setColorActive(color(255));  

      for(int i=0; i<6; i++) {
      int w; if(i==0) { w=15; } else if(i==1) { w=23; } else if(i==4) { w=25; } else { w=30; };
      Toggle t = cp52.addToggle(cycleButtons[i], false, i*80+125, 67, w, 20);
      t.setLabel(cycleButtons[i]);
      controlP5.Label l = t.captionLabel();
      l.style().marginTop = -28; //move upwards (relative to button size)
      l.style().marginLeft = 4; //move to the right   
      }
    }

    void draw() { 

    // Other stuff....

    }

    void controlEvent(ControlEvent theEvent) {
      if(theEvent.isController()) { 

        if(theEvent.controller().name()=="I") {
          if(theEvent.controller().value()==1) {  cycleNumber = 1;  } else { cycleNumber = 0; }
        }

        if(theEvent.controller().name()=="II") {
          if(theEvent.controller().value()==1) {  cycleNumber = 2;  } else { cycleNumber = 0; }
        }

        if(theEvent.controller().name()=="III") {
          if(theEvent.controller().value()==1) {  cycleNumber = 3;  } else { cycleNumber = 0; }
        }

        if(theEvent.controller().name()=="IV") {
          if(theEvent.controller().value()==1) {  cycleNumber = 4;  } else { cycleNumber = 0; }
        }

        if(theEvent.controller().name()=="V") {
          if(theEvent.controller().value()==1) {  cycleNumber = 5;  } else { cycleNumber = 0; }
        }

        if(theEvent.controller().name()=="VI") {
          if(theEvent.controller().value()==1) {  cycleNumber = 6;  } else { cycleNumber = 0; }
        }
      }
    }

Thanks in advance!!!

Tagged:

Answers

  • The thing you want is a RadioButton. ControlP5 has an example sketch.

  • @colouredmirrorball Thank you. How silly of me for not noticing RadioButton!

    Side Note: There seems to be a bug with the forum, things keep double posting.

  • The forum appears to be a bit slow today. I got an error saying something went wrong so I clicked on Post Comment again. Looks like the message got through the first time anyway.

  • Is there any way to work with radiobutton in the same way as toggle button so that creating a Boolean variable will simplify the assignment of it? It is understood? Radio button works with array of results and ToggleButton I think it is simpler since just creating a variable one can handle it in a more direct but simple way. I would like to do the same but with the radioButton of the magnificent ControlP5 library. Thanks in advance!

  • Is there any way to work with radiobutton in the same way as toggle button so that creating a Boolean variable will simplify the assignment of it?

    Can you provide the example code that you are referencing? We can start from there..

    Kf

Sign In or Register to comment.