ControlP5 Button from Class doesn't work when click on it

edited January 2017 in Library Questions

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?

Tagged:

Answers

  • Answer ✓

    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:

    CAToolBox catbx;
    
    void setup(){
        catbx = new CAToolBox(this, font, 0, 0);
    }
    
    void draw(){
    
    }
    
    void PlayPause(){
       catbx.PlayPause();
    }
    

    If anyone has a better and or optimal solution please share!!

  • Answer ✓

    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

Sign In or Register to comment.