We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi! I'm creating a simple program with a class for creating all the controlP5 GUI, inside that class are buttons and other stuff but i'd like to manage the state of a button.
import controlP5.*;
class CAToolBox{
public ControlP5 cp5;
PFont font;
int caX;
int caY;
int caWidth;
int caHeight;
boolean play = false;
boolean generated = false;
CAToolBox(PApplet applet, PFont _font, int _x, int _y, int _caWidth, int _caHeight){
caX = _x;
caY = _y;
caWidth = _caWidth;
caHeight = _caHeight;
font = _font;
cp5 = new ControlP5(applet);
this.drawCAToolBox(caX + 10, caY + 10);
}
void drawCAToolBox(int _x, int _y){
//Generate Button
cp5.addButton("Generate")
.setPosition(_x +3 , _y + 65)
.setSize(50, 22);
//Play & Pause Button
cp5.addButton("PlayPause")
.setLabel("Pause")
.setPosition(_x + 3 + 60 , _y + 65)
.setSize(50, 22);
}
void PlayPause(){
play = !play;
checkLabelState();
}
void checkLabelState(){
if(play == false){
cp5.get(controlP5.Button.class, "PlayPause").setLabel("play");
}else{
cp5.get(controlP5.Button.class, "PlayPause").setLabel("pause");
}
}
}
Any idea on how can i call the button in this class for working with my main window?
Answers
I've managed to solve it, but i don't think is the most optimal way.
In my main class i've created a PlayPause() function wich calls the class PlayPause Function :-/ but that is really confusing ill put my code bellow:
If anyone has a better and or optimal solution please share!!
For sake to avoid confusing code, try not to use the same name for different things.
Please note that is a convention that class names start with a capital: CAToolBox and objects, arrays, functions start with lowercase / small first letter. For instance, playPause().
Related to your question, it seems ok. I mean, your class contains the different widgets/controls and there are certain ways you can access it. Within your draw() function for example, you can interact with your playPause button either by calling playPause() or catbx.PlayPause(). I would use the former to keep the code modular.
Kf