calling cp5 elements from inside a class

edited September 2016 in Library Questions

Hi all, I'm a little stuck with this controlP5 problem. I went through some code to solve it but had no success with it. What I aim to do is to map these gui elements/controls to data but I think because they're in their own class there's another way to call them though couldn't figure out how. I placed the controlEvent in the main window and it succeeds to read the values but somehow I'm not able to map them. I would appreciate if someone could point me in the right direction here. Should I be adding callbacks to the individual sliders within the class? what's the proper way to do this?

thanks _ö

PS: I just patched the interface into a scene, the content is basically just for me to be able to visualise it.

int margin;
Top [] tops = new Top[100];
Interface gui;
String s;
float val;
color col;

void setup() {
  size(600, 400);
  margin = 200;
  col = 127;

  gui  = new Interface(this);
  gui.build();

  for (int i = 0; i < tops.length; i++) {
    tops[i] = new Top(margin);
  }
}


void draw() {
  background(80);
  fill(col);
  rectMode(CENTER);
  rect(width/2, height/2, width - margin, height - margin);


  for (int i = 0; i < tops.length; i++) {
    tops[i].display();
    //tops[i].update();
    //tops[i].borders();
  }
}

// to be mapped: col, margin, tops[i].maxSpeed, tops[i].maxForce

void controlEvent(CallbackEvent theEvent) {
  s = theEvent.getController().getName();
  val = theEvent.getController().getValue();
  println(s, val);
  margin = (int)map(val, 100, 500, 100, 300);
  for (int i = 0; i < tops.length; i++) {
    tops[i].r = (int)map(val, 100, 500, 5, 20);
  }
}


class Top {
  int margin, r;
  float x, y;
  PVector location, velocity, acceleration;
  float maxSpeed, maxForce;
  PVector noff;

  Top(int margin_) {
    margin = margin_;
    r = 5;
    int side = int(random(0, 3.9));
    switch(side) {
    case 0: 
      x= random(width);
      y= random(margin/2);
      break;
    case 1: 
      x= random(width);
      y= random(height-margin/2, height);
      break;
    case 2: 
      x= random(margin/2);
      y= random(height);
      break;
    case 3: 
      x= random(width-margin/2, width);
      y= random(height);
      break;
    }


    location = new PVector(x, y);
    velocity = new PVector();
    acceleration = new PVector(0, 0);
    noff = new PVector (random(1000), random(1000), random(1000));
    maxSpeed = 3;
    maxForce = 0.15;
  }
  void update() {
    acceleration.x = map(noise(noff.x), 0, 1, -1, 1);
    acceleration.y = map(noise(noff.y), 0, 1, -1, 1);
    //acceleration.z = map(noise(noff.z), 0, 1, -1, 1);
    noff.add(0.001, 0.001, 0.001);


    velocity.add(acceleration);
    velocity.limit(maxSpeed);
    location.add(velocity);
    acceleration.mult(0);

    if (location.x<0||location.x>width) velocity.x *=-1;
    if (location.y<0||location.y>height) velocity.y *=-1;

    /*
    boolean A,B,C,D;

     if(location.y<margin/2) A = true;
     else A = false;
     if(location.x>width-margin/2) B = true;
     else B = false;
     if(location.y>height-margin/2) C = true;
     else C = false;
     if(location.x<margin/2) D = true;
     else D = false;

     if(location.y>margin/2 && !B && !C && !D || y<height-margin/2 && !A && !B && !D) velocity.y *= -1;
     if(location.x>margin/2 && !A && !B && !C || x<width-margin/2 && !A && !C && !D) velocity.x *= -1;
     */
  }
  void display() {
    stroke(0);
    fill(50);
    ellipse(location.x, location.y, r, r);
  }
  /*void borders(){
   if(location.x<0||location.x>width) x *=-1;
   if(location.y<0||location.y>height) y *=-1;

   }*/
}


import controlP5.*;

class Interface {

  ControlP5 cp5;
  Group g1, g2, g3;
  Accordion accordion;
  DropdownList d2;
  int env_H, bo_H, ou_H;
  int sliderPosX, sliderPosY, sliderGap;
  int sliderSizeX, sliderSizeY;
  int groupWidth;


  //String [] sliderNames;
  //String [] groupNames;
  //String slider, group;
  //Group gCreate;


  Interface(PApplet this_) {
    cp5  = new ControlP5(this_);
    //s1 = new CallbackListener(this);
    sliderPosX = 10;
    sliderPosY = 10;
    sliderGap = 25;
    sliderSizeX = 160;
    sliderSizeY = 10;
    groupWidth = 180;
    env_H = sliderSizeY*2 + sliderGap*1;
    bo_H = sliderSizeY*2 + sliderGap*3;
    ou_H = sliderSizeY*2 + sliderGap*5;

    //sliderNames = new String [] {"TOLERANCE", "BOUNDARY VISIBILITY", "INFLUENCE", "FALLOFF", "QUALITY", "POPULATION", "OBEDIENCE", "INFLUENCE "};
    //groupNames = new String [] {"ENVIRONMENT", "OURO", "BOROS"};
  }

  void build() {
    cp5.addCallback();

    color gFg = color(200, 50, 100, 100);
    color gLabel = color(255);
    color gBg = color(50, 200, 100, 120);
    color sFg = color(10, 150);
    color sAct = color(50);
    color sBg = color(50, 120);
    color rBg = sBg;
    color rAct = sAct;

    String g1_slider1 = "g1_slider1";
    String g1_slider2 = "g1_slider2";
    String g2_slider1 = "g2_slider1";
    String g2_slider2 = "g2_slider2";
    String g2_slider3 = "g2_slider3";
    String g3_slider1 = "g3_slider1";
    String g3_slider2 = "g3_slider2";
    String g3_slider3 = "g3_slider3 "; // added a space to fix the identical name problem!--------



    g1 = cp5.addGroup("g1")
      .setPosition(100, 50)
      .setBarHeight(10)
      .setColorForeground(gFg)
      .setColorLabel(gLabel)
      .setColorBackground(gBg)
      .setColorValue(color(255, 126, 240))
      .setBackgroundHeight(env_H)
      .setBackgroundColor(color(100, 100))
      ;

    g2 = cp5.addGroup("g2")
      .setPosition(100, 150)
      .setBarHeight(10)
      .setColorForeground(gFg)
      .setColorLabel(gLabel)
      .setColorBackground(gBg)
      .setBackgroundHeight(ou_H)
      .setBackgroundColor(color(100, 100))
      ;

    g3 = cp5.addGroup("g3")
      .setPosition(100, 270)
      .setBarHeight(10)
      .setColorForeground(gFg)
      .setColorLabel(gLabel)
      .setColorBackground(gBg)
      .setBackgroundHeight(bo_H)
      .setBackgroundColor(color(100, 100))
      ;

    // environment sliders
    cp5.addSlider(g1_slider1)
      .setPosition(sliderPosX, sliderPosY)
      .setSize(sliderSizeX, sliderSizeY)
      .setColorForeground(sFg)
      .setColorBackground(sBg)
      .setColorActive(sAct)
      .setRange(100, 500)
      .setValue(100)
      .setGroup(g1)
      ;

    cp5.addSlider(g1_slider2)
      .setPosition(sliderPosX, sliderPosY+sliderGap)
      .setSize(sliderSizeX, sliderSizeY)
      .setColorForeground(sFg)
      .setColorBackground(sBg)
      .setColorActive(sAct)
      .setGroup(g1)
      ;

    // g2 sliders
    cp5.addSlider(g2_slider1)
      .setPosition(sliderPosX, sliderPosY)
      .setSize(sliderSizeX, sliderSizeY)
      .setColorForeground(sFg)
      .setColorBackground(sBg)
      .setColorActive(sAct)
      .setGroup(g2)
      ;

    cp5.addSlider(g2_slider2)
      .setPosition(sliderPosX, sliderPosY+sliderGap)
      .setSize(sliderSizeX, sliderSizeY)
      .setColorForeground(sFg)
      .setColorBackground(sBg)
      .setColorActive(sAct)
      .setGroup(g2)
      ;

    cp5.addSlider(g2_slider3)
      .setSliderMode(Slider.FLEXIBLE)
      .setRange(-100, 100)
      .setDefaultValue(0)
      .setHandleSize(20)
      .setPosition(sliderPosX, sliderPosY+sliderGap*2)
      .setSize(sliderSizeX, sliderSizeY)
      .setColorForeground(sFg)
      .setColorBackground(sBg)
      .setColorActive(sAct)
      .setGroup(g2)
      ;

    // CHECKBOX g2

    cp5.addCheckBox("check1")
      .setPosition(10, 90)
      .setSize(20, 20)
      .setColorActive(rAct)
      .setColorLabel(color(255))
      .setColorBackground(rBg)
      .setItemsPerRow(1)
      .setSpacingColumn(60)
      .addItem("check1", 0)
      .moveTo(g2)
      ;

    cp5.addCheckBox("check2")
      .setPosition(10, 112)
      .setSize(20, 20)
      .setColorActive(rAct)
      .setColorLabel(color(255))
      .setColorBackground(rBg)
      .setItemsPerRow(1)
      .setSpacingColumn(60)
      .addItem("check2", 0)
      .moveTo(g2)
      ;

    //g3 sliders
    cp5.addSlider(g3_slider1)
      .setPosition(sliderPosX, sliderPosY)
      .setSize(sliderSizeX, sliderSizeY)
      .setColorForeground(sFg)
      .setColorBackground(sBg)
      .setColorActive(sAct)
      .setGroup(g3)
      ;

    cp5.addSlider(g3_slider2)
      .setPosition(sliderPosX, sliderPosY+sliderGap)
      .setSize(sliderSizeX, sliderSizeY)
      .setColorForeground(sFg)
      .setColorBackground(sBg)
      .setColorActive(sAct)
      .setGroup(g3)
      ;

    cp5.addSlider(g3_slider3)
      .setPosition(sliderPosX, sliderPosY+sliderGap*2)
      .setSize(sliderSizeX, sliderSizeY)
      .setColorForeground(sFg)
      .setColorBackground(sBg)
      .setColorActive(sAct)
      .setGroup(g3)
      ;




    // caption alignment
    cp5.getController(g1_slider1).getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
    cp5.getController(g1_slider2).getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
    cp5.getController(g2_slider1).getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
    cp5.getController(g2_slider2).getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
    cp5.getController(g2_slider3).getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
    cp5.getController(g3_slider1).getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
    cp5.getController(g3_slider2).getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
    cp5.getController(g3_slider3).getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);


    accordion = cp5.addAccordion("acc")
      .setPosition(0, 0)
      .setWidth(180)
      .addItem(g1)
      .addItem(g2)
      .addItem(g3)
      ;

    accordion.setCollapseMode(Accordion.MULTI);



    // unsuccesful looping efforts ----------------------------------------

    /*
        for(int i = 0; i < groupNames.length; i++){
                group = "g" + i;
                for(int j = 0; j < groupNames.length; j++){
                    String groupTitle = groupNames[j];
                    gCreate = cp5.addGroup(groupTitle)
                                .setPosition(100,50)
                                .setWidth(groupWidth)
                                .setBackgroundHeight(100)
                                .setBackgroundColor(color(100))
                                ;
                }

            } 
            for(int i = 0; i < sliderNames.length; i++){
                String sliderTitle = sliderNames[i];
                            cp5.addSlider(sliderTitle)
                            .setPosition(sliderPosX,sliderPosY)
                            .setSize(sliderSizeX,sliderSizeY)
                            .setGroup(group)
                            ;
                            cp5.getController(sliderTitle).getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);

                }
            */
  }
}


// add the above callback to controlP5


// add another callback to slider s1, callback event will only be invoked for this 
// particular controller.
Sign In or Register to comment.