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