[ControlP5] passing array to CallbackListener

Hi everybody,

I am using Processing 3.5.4 and ControlP5
i was wondering if there is a way to pass any variables or in my case an array to a callback function.
In detail: I want sort of a tab function but with vertical tabs and the builtin addTab() doenst suit my needs. The actual tabs will be organized as groups of controllers and these groups will be saved in an array. The .hide() and .show() methods will be accessed via .onClick() and some CallbackListener. In my opinion, the most elegant way would be, to pass the array with all groups directly to the CallbackListener, but i dont know how, not even where to start.

import controlP5.*;

ControlP5 cp5;
Group g0;
Group g1;
Group[] groupArray = {g0,g1};

  //the Callback that should take the Array from the Button
CallbackListener doSomething = new CallbackListener(){
  public void controlEvent(CallbackEvent theEvent) {
    int j = ((Button)theEvent.getController()).getId();
      for (int i = 0; i<groupArray.length; i++){
          if (j == i) groupArray[i].setVisible(true);
          else groupArray[i].setVisible(false);
       }
  }
};

void setup(){
  size(400,400);
  
  cp5 = new ControlP5(this);
  
  g1 = cp5.addGroup("g1").setPosition(120,10)
    .setSize(200,200)
    .setBackgroundColor(color(255,100))
    ;
  g0 = cp5.addGroup("g0").setPosition(120,10)
    .setSize(200,200)
    .setBackgroundColor(color(255,100))
    ;
  
//add togglebutton
cp5.addButton("togglebuttonA")
      .setPosition(10,10)
      .setSize(100,20)
      .onClick(doSomething)
       //.setArray(groupArray )
      .setId(0)
      ;
cp5.addButton("togglebuttonB")
      .setPosition(10,10)
      .setSize(100,20)
      .onClick(doSomething)
       //.setArray(groupArray )
      .setId(0)
      ;

//add dummy elements
cp5.addButton("a1").setPosition(0,10).setSize(100,20).setGroup("g0");
cp5.addButton("a2").setPosition(0,10).setSize(100,20).setGroup("g0");
cp5.addButton("b1").setPosition(0,10).setSize(100,20).setGroup("g1");
cp5.addButton("b2").setPosition(0,10).setSize(100,20).setGroup("g1");

//hide second group
g1.setVisible(false);
}

void draw(){
  background(250);
}

If i missed any threads on my research, please point me to them. I am aware, that neither my english nor my code is top shelf, please excuse this.

thanks in advance,
ForgottenCat

I am not 100% sure of your use case. I suggest you explore the docs for ControlP5. I will start by checking the Button first. As it does not define setArrayValue, I will check its parent here

Just to note that setArray is not defined for Button, so I assume you are referring to setArrayValue.

Kf

Hi,
thanks for the reply, seems like i did not make my needs 100% clear. there is no defined attribute called setArray. but i would need one. the question is: how to do this.

i have a method, that draws a button without the need to manually set all the same options again and again.

one of the overloaded methods of my gui class should assign a button that will act as a switch for a group of other gui elements. so i assigned a callback to that kind of buttons.

the callback at the moment takes the predifined Group[] array and if button.getId() = Group[i], then it show() s the group, else it hide() s it. Since i have to declare the callback specificly for every group, the question is:

1. can i assign the Group[] array directly to the .addButton()?
2. and how can i, if 1) is possible, retrieve that array in the callback?

i have a guess how this could be possible (guesses as in “i dont know anything”): maybe through somehow extending the cp5 class with a .addButtonWithArray() method?

sadly im still a beginner in java, processing and cp5, so i dont have the slightest clue where even to start