How to use ControlP5 inside classes?

edited November 2015 in Library Questions

Hello.

I want to make a simple gui application for controlling my Chuck programms (http://chuck.cs.princeton.edu/). My current goal is writing a class which can load and run Chuck sketch (ClipLauncher), but I got some problems:

1 - I can't make a unique class instance of ControlP5 inside my ClipLauncher class, so I adding it manualy in main sketch (ControlP5 gui, ControlP5 gui2, ... ControlP5 guiN).

2 - I can't call functions, binded to gui elements

import controlP5.*;

ControlP5 gui;
ControlP5 gui2;
ClipLauncher clip;
ClipLauncher clip2;
void setup(){
  size(480, 320);
  background(0);
  gui = new ControlP5(this);
  gui2 = new ControlP5(this);
  clip = new ClipLauncher(gui, 10, 10);
  clip2 = new ClipLauncher(gui2, 10, 75);
}

void draw(){

}

class ClipLauncher {
  ClipLauncher(ControlP5 gui, int x, int y) {
    gui.addToggle("start clip")
     .setPosition(x,y)
     .setSize(50,50)
     .setId(1)
     .setLabel("");

    gui.addButton("select")
     .setPosition(x + 55,y)
     .setSize(100,50)
     .setId(2)
     .setLabel("no file selected");

  }

  public void controlEvent(ControlEvent theEvent) {  //don't work
    switch(theEvent.getController().getId()) {
      case(1):
        print("checkbox");
        break;
      case(2):
        print("button");
        break;  
    }
  }

  public void select() {  //don't work too
    println("Button from function");
  }
}
Tagged:

Answers

  • You need a ControlP5 attribute inside the class and create it in the constructor. To do that you need to pass a PApplet instnace in the ctor parameters.

    Just like this -

    import controlP5.*;
    
    ClipLauncher clip;
    ClipLauncher clip2;
    
    void setup() {
      size(480, 320);
      background(0);
      clip = new ClipLauncher(this, 10, 10);
      clip2 = new ClipLauncher(this, 10, 75);
    }
    
    void draw() {
    }
    
    class ClipLauncher {
      ControlP5 gui;
    
      ClipLauncher(PApplet papp, int x, int y) {
        gui = new ControlP5(papp);
    
        gui.addToggle("start clip")
          .setPosition(x, y)
          .setSize(50, 50)
          .setId(1)
          .setLabel("");
    
        gui.addButton("select")
          .setPosition(x + 55, y)
          .setSize(100, 50)
          .setId(2)
          .setLabel("no file selected");
      }
    
      public void controlEvent(ControlEvent theEvent) {  //don't work
        switch(theEvent.getController().getId()) {
          case(1):
          print("checkbox");
          break;
          case(2):
          print("button");
          break;
        }
      }
    
      public void select() {  //don't work too
        println("Button from function");
      }
    }
    
  • Thanks, Quark! Your tip is great, but class methods still not works.

  • Thanks, Quark! Your tip is great, but class methods still not works.

  • Ok. I solve problem with addCallback(), but now I have a problem with openFile() function visibility.

    class ClipLauncher {
      ControlP5 gui;
    
      ClipLauncher(PApplet papp, int x, int y) {
        gui = new ControlP5(papp);
    
        gui.addToggle("start clip")
         .setPosition(x,y)
         .setSize(50,50)
         .setLabel("");
    
        Button b = gui.addButton("select")
         .setPosition(x + 55,y)
         .setSize(100,50)
         .setLabel("no file selected");
    
        b.addCallback(new CallbackListener() {
          void controlEvent(CallbackEvent theEvent) {
            if (theEvent.getAction() == ControlP5.ACTION_BROADCAST) {
              selectInput("Select a file...", "openFile");
            }
          }
        });
      }
    
      public void openFile(File selection) {
        if (selection != null) {
          println(selection.getName());
        }
      }
    }
    
  • If you want to access Processing methods inside a class you need a reference to a PApplet object. Like this.

    class ClipLauncher {
      PApplet app;
      ControlP5 gui;
    
      ClipLauncher(PApplet papp, int x, int y) {
        app = papp;
        gui = new ControlP5(papp);
    
        gui.addToggle("start clip")
          .setPosition(x, y)
          .setSize(50, 50)
          .setLabel("");
    
        Button b = gui.addButton("select")
          .setPosition(x + 55, y)
          .setSize(100, 50)
          .setLabel("no file selected");
    
        b.addCallback(new CallbackListener() {
          void controlEvent(CallbackEvent theEvent) {
            if (theEvent.getAction() == ControlP5.ACTION_BROADCAST) {
              app.selectInput("Select a file...", "openFile");
            }
          }
        }
        );
      }
    
      public void openFile(File selection) {
        if (selection != null) {
          println(selection.getName());
        }
      }
    }
    
Sign In or Register to comment.