ControlP5: Multiple sketch/frame/windows and controls on each?
in
Contributed Library Questions
•
2 years ago
Hello!
I have multiple sketches designed to have their own toolbar using ControlP5. Individually, they run fine, but when I try to put each in its own frame, the controls only render on the last window created.
I have seen the ControlWindow example ( http://www.sojamo.de/libraries/controlP5/reference/controlP5/ControlWindow.html). But I am trying to have multiple windows with their own controls, rather than one separate control window.
Here is the code in question:
CP5_manyWindows.pde (this is the "main" sketch that ties the others together)
View2.pde
I have multiple sketches designed to have their own toolbar using ControlP5. Individually, they run fine, but when I try to put each in its own frame, the controls only render on the last window created.
I have seen the ControlWindow example ( http://www.sojamo.de/libraries/controlP5/reference/controlP5/ControlWindow.html). But I am trying to have multiple windows with their own controls, rather than one separate control window.
Here is the code in question:
CP5_manyWindows.pde (this is the "main" sketch that ties the others together)
- //CP5_manyWindows.pde
- View1 v1;
View2 v2;
void setup(){
PFrame v1_frame = new PFrame(v1);
PFrame v2_frame = new PFrame(v2);
}
void draw(){
}
public class PFrame extends Frame {
public PFrame(View1 app) {
setBounds(100,100,400,300);
setTitle("View1");
app = new View1();
add(app);
app.init();
show();
}
public PFrame(View2 app) {
setBounds(100,100,400,300);
setTitle("View2");
app = new View2();
add(app);
app.init();
show();
}
}
- //View1
import controlP5.*;
class View1 extends PApplet{
ControlP5 cp5;
ListBox axisbox;
Button resetbutton;
void setup(){
size(400, 300);
//frame.setResizable(true);
smooth();
// controlP5 configuration
cp5 = new ControlP5(this);
//cp5.setAutoDraw(false);
// ControlGroup sc_gui = cp5.addGroup("sc_gui", 0, 0);
axisbox = cp5.addListBox("items",5,30,60,60);//(name, x, y, w, h)
// axisbox.setGroup(sc_gui);
axisbox.setBarHeight(25);
axisbox.setItemHeight(15);
axisbox.captionLabel().style().marginTop = 7;
axisbox.valueLabel().style().marginTop = 10; // the +/- sign
axisbox.addItem("item "+1,1);
axisbox.addItem("item "+2,2);
// controlP5 buttons
resetbutton = cp5.addButton("reset_view1",0f,width-70,5,60,25);
// resetbutton.setGroup(sc_gui);//(name, val, x, y, w, h)
}
void draw(){
//cp5.draw();
}
//BEGIN GUI CONTROLLERS
/**
* controlP5 event controller
*/
public void controlEvent(ControlEvent event) {
if (event.isGroup()) { // an event from a group e.g. scrollList
if( event.group().name().equalsIgnoreCase("items") ){
int listindex = int( event.group().value() );
switch(listindex){
case 1: println(listindex); break;
case 2: println(listindex); break;
default: println("function not yet defined");
}
} else {
println("interface element not defined");
}
} else {
println(event.controller().name());
}
}
//END GUI CONTROLLERS
}// end class
View2.pde
- //View2
import controlP5.*;
class View2 extends PApplet{
ControlP5 cp5;
ListBox axisbox;
Button resetbutton;
void setup(){
size(400, 300);
//frame.setResizable(true);
smooth();
// controlP5 configuration
cp5 = new ControlP5(this);
cp5.setAutoDraw(false);
// ControlGroup th_gui = cp5.addGroup("th_gui", 0, 0);
axisbox = cp5.addListBox("options",5,130,60,60);
// axisbox.setGroup(th_gui);//(name, x, y, w, h)
axisbox.setBarHeight(25);
axisbox.setItemHeight(15);
axisbox.captionLabel().style().marginTop = 7;
axisbox.valueLabel().style().marginTop = 10; // the +/- sign
axisbox.addItem("option 1",1);
axisbox.addItem("option 2",2);
// controlP5 buttons
resetbutton = cp5.addButton("reset_view2",0f,width-70,105,60,25);
// resetbutton.setGroup(th_gui);//(name, val, x, y, w, h)
}
void draw(){
cp5.draw();
}
//BEGIN GUI CONTROLLERS
/**
* controlP5 event controller
*/
public void controlEvent(ControlEvent event) {
if (event.isGroup()) { // an event from a group e.g. scrollList
if( event.group().name().equalsIgnoreCase("options") ){
int listindex = int( event.group().value() );
switch(listindex){
case 1: println(listindex); break;
case 2: println(listindex); break;
default: println("function not yet defined");
}
} else {
println("interface element not defined");
}
} else {
println(event.controller().name());
}
}
//END GUI CONTROLLERS
} // end class
1