ControlP5: controller group in canvas?

edited May 2015 in Library Questions

Hi all, I was wondering if it was possible to use a ControlP5 controller group within a canvas? I have been trying without much success. The group appears when I open the canvas, but when I close the canvas the controllers are still visible. Thanks!

import controlP5.*;

ControlP5 cp5;

Canvas cc;

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

  cp5 = new ControlP5(this);

  Button btn = cp5.addButton("Add_Canvas")
    .setPosition(10, 30)
      .setSize(80, 25)
        ;

  Button btn2 = cp5.addButton("Remove_Canvas")
    .setPosition(10, 65)
      .setSize(80, 25)
        ;
}         

void draw() {
  background(80);
}



class MyCanvas extends Canvas {

  int y;

  public void setup(PApplet theApplet) {
    y = 200;
    Group g1 = cp5.addGroup("g1")
      .setPosition(200, 100)
        .setBackgroundHeight(100)
          .setBackgroundColor(color(255, 50))
            ;

    cp5.addSlider("S-1")
      .setPosition(20, 20)
        .setSize(80, 30)
          .setGroup(g1)
            ;
  }  

  public void draw(PApplet p) {
    p.fill(150);
    p.rect(120, 50, 550, 320);
  }
}


void controlEvent(ControlEvent theEvent) {
  if (theEvent.getName() == "Add_Canvas") {
    cc = new MyCanvas();
    cc.pre();
    cp5.addCanvas(cc);
  }

  if (theEvent.getName() == "Remove_Canvas") {
    cp5.removeCanvas(cc);
  }

  if (theEvent.isGroup()) {
    println("got an event from group "
      +theEvent.getGroup().getName()
      +", isOpen? "+theEvent.getGroup().isOpen()
      );
  } else if (theEvent.isController()) {
    println("got something from a controller "
      +theEvent.getController().getName()
      );
  }
}
Sign In or Register to comment.