Loading...
Logo
Processing Forum
I've got some control p5 toggle boxes.. When they are clicked, I want to change a boolean. Do I have to do this for every toggle box with a void toggle1, void toggle2 and so on or can I combine this like it is possible with springGUI ?
 
thank you for your answers.

Replies(5)

Hey,

I have a similar question:

I have a sketch with a couple of toggle boxes. When I click a few, I want it to call 1 function. Can someone explain how to do this?
Hope someone has a solution.


Kind regards,

Joshua
Not sure why you'd want several toggles to change the same boolean and not also do something else...  But looking at the Library examples it didn't take long to find one possible solution...
No what I mean is:


toggle1 + toggle2 = round

toggle1 = square

toggle 3 = flower


SO... void toggle1  + void toggle 2 = round



OK - that at least explains your use-case... and I'll admit that some functionality of controlP5 seems a little obtuse at first - for instance a toggle which you'd expect to return a boolean actually returns a float - but the docs are pretty thorough and there are some really useful examples.  With just a little tinkering I came up with this solution using the controlEvent method I suggested before:


Copy code
  1. // amended controlP5 Toggle example...
  2. import controlP5.*;
  3. ControlP5 controlP5;

  4. void setup() {
  5.   size(400,400);
  6.   smooth();
  7.   controlP5 = new ControlP5(this);
  8.   controlP5.addToggle("toggle1",false,100,160,20,20);
  9.   controlP5.addToggle("toggle2",true,100,240,20,20);
  10. }
  11.  
  12. void draw() {
  13.   background(0);
  14. }

  15. // fired whenever there is an event on any controlP5 element...
  16. // That means the test will return a result when another control is used - but, if necessary,  it should be
  17. // simple enough to handle that situation.
  18. void controlEvent(ControlEvent theEvent) {
  19.    // toggle.value() actually returns a float...
  20.    float valueToggle1 = controlP5.controller("toggle1").value();
  21.    float valueToggle2 = controlP5.controller("toggle2").value();

  22.    if(valueToggle1 == 1.0 && valueToggle2 == 1.0) {
  23.      println("both true");
  24.    }
  25.    else if(valueToggle1 == 1.0 || valueToggle2 == 1.0) {
  26.      println("one true, one false");
  27.    }
  28.    else {
  29.      println("both false");
  30.    }
  31. }
Thanks for the help.