ControlP5 Icon Class - Switch mode does not call controlEvent function

So I'm working with controlP5 and the Icon class which works great except when I turn on "setSwitch(true)". The moment I add this line of code it does not reach the controlEvent function anymore. I've solved it before by checking isPressed on the specific controller and then setting things with iconObject.isOn(), but I've got quite a few more buttons now so I'd love to be able to use the controlEvent function.

Has anyone encountered or fixed this before?

Thanks

Answers

  • setting the behaviour of an icon to a switch using setSwitch(true), triggers a controlEvent for me, though getting the state of the switch requires to call the getBooleanValue() on the controller that triggered the event. To do so you need to cast the Controller linked to the event (theEvent.getController()) to an Icon which allows you to then call getBooleanValue:

    void controlEvent(ControlEvent theEvent) {
       // make sure we are dealing with an Icon controller
      if(theEvent.isAssignableFrom(Icon.class)) {
        Icon icon = (Icon)theEvent.getController();
        println("icon event", icon.getBooleanValue());
      }
    }
    

    another option is to use a CallbackListener

      CallbackListener callback = new CallbackListener() {
        public void controlEvent(CallbackEvent theEvent) {
          if(theEvent.getAction()==ControlP5.ACTION_BROADCAST) {
            Icon icon = (Icon)theEvent.getController();
            println("icon callback", icon.getBooleanValue());
          }
        }
      };
    

    which you can add to an icon with addCallback(callback)

  • Thanks for the reply, I'll look into using a callback if I can't get the controlEvent to work. I've looked into it a bit more and found the following: - If I add setSwitch(true) to an icon, the controlEvent still receives it's events - If I then also plug the icon to an object '.plugTo(object)'. The controlEvent inside that object does not receive the events.

    > `import controlP5.*;
    
    ControlP5 cp5;
    Thing thing;
    
    void setup() {
      size(800,400);
      thing = new Thing(); 
      cp5 = new ControlP5(this);
    
      cp5.addIcon("icon",10)
         .setPosition(100,100)
         .setSize(70,50)
         .plugTo(thing)
         .setFont(createFont("fontawesome-webfont.ttf", 40))
         .setFontIcons(#00f205,#00f204)
         .showBackground()
         .setSwitch(true)
         ;  
    }
    
    void draw() {
      background(220);
    }
    
    class Thing {
    
      Thing() {
      }
    
      void controlEvent(ControlEvent theEvent) {
        // make sure we are dealing with an Icon controller
        if (theEvent.isAssignableFrom(Icon.class)) {
          Icon icon = (Icon)theEvent.getController();
          println("icon event", icon.getBooleanValue());
        }
      }
    
    }` 
    

    Actually, if I have a controlEvent method both inside the object class and in the global scope. The global one is triggered.

  • Answer ✓

    plugTo() looks for members with the same name as given to the Controller but ignores controlEvent(ControlEvent). In order to trigger the controlEvent your class should implement the ControlListener interface and use addListener(thing)instead of plugTo(thing).

    import controlP5.*;
    
    ControlP5 cp5;
    Thing thing;
    
    void setup() {
      size(800, 400);
      thing = new Thing(); 
      cp5 = new ControlP5(this);
    
      cp5.addIcon("icon", 10)
        .setPosition(100, 100)
        .setSize(70, 50)
        .addListener(thing)
        .setFont(createFont("fontawesome-webfont.ttf", 40))
        .setFontIcons(#00f205, #00f204)
        .showBackground()
        .setSwitch(true)
        ;
    }
    
    void draw() {
      background(220);
    }
    
    class Thing implements ControlListener {
    
      Thing() {}
    
      void controlEvent(ControlEvent theEvent) {
        // make sure we are dealing with an Icon controller
        if (theEvent.isAssignableFrom(Icon.class)) {
          Icon icon = (Icon)theEvent.getController();
          println("icon event", icon.getBooleanValue());
        }
      }
    }
    
  • Ah that makes a lot more sense, I've been using .plugTo(object) and controlEvent in other places to with mixed success so that explains why.

    Thanks for these quick answers!

Sign In or Register to comment.