[ControlP5] Getting the reference of a text label

Hi there,

I'm Ignazio and I'm having great fun using Processing in conjunction with ControlP5. I have a small question, that got me stuck for a little while: I have the name of a textlabel stored in a string and I want to change an attribute of that label. I did something very similar with a button:

cp5.getController(buttonName).unlock();

In the example above I was unlocking the button which had the name stored in the string buttonName.

Any help?

Thank you very much, Ignazio

Answers

  • I took this example code from: http://www.sojamo.de/libraries/controlP5/examples/controllers/ControlP5button/ControlP5button.pde

    Instructions:

    Press the space bar to lock or unlock the wicked button.

    Kf

    import controlP5.*;
    
    ControlP5 cp5;
    
    int myColor = color(255);
    
    int c1, c2;
    
    float n, n1;
    
    String buttonName="wicked";
    
    
    void setup() {
      size(400, 600);
      noStroke();
      cp5 = new ControlP5(this);
    
      // create a new button with name 'buttonA'
      cp5.addButton(buttonName)
        .setValue(0)
        .setPosition(100, 100)
        .setSize(200, 19)
        ;
    
      // and add another 2 buttons
      cp5.addButton("colorB")
        .setValue(100)
        .setPosition(100, 120)
        .setSize(200, 19)
        ;
    
      cp5.addButton("colorC")
        .setPosition(100, 140)
        .setSize(200, 19)
        .setValue(0)
        ;
    
      PImage[] imgs = {loadImage("button_a.png"), loadImage("button_b.png"), loadImage("button_c.png")};
      cp5.addButton("play")
        .setValue(128)
        .setPosition(140, 300)
        .setImages(imgs)
        .updateSize()
        ;
    
      cp5.addButton("playAgain")
        .setValue(128)
        .setPosition(210, 300)
        .setImages(imgs)
        .updateSize()
        ;
    }
    
    void draw() {
      background(myColor);
      myColor = lerpColor(c1, c2, n);
      n += (1-n)* 0.1;
    }
    
    void keyPressed() {
    
      if (keyCode!=' ')
        return;
    
      println("Status now: "+cp5.getController(buttonName).isLock());
    
      if (cp5.getController(buttonName).isLock()==true)
        cp5.getController(buttonName).unlock();
      else
        cp5.getController(buttonName).lock();
    
      //ALTERNATIVE:
      //cp5.getController(buttonName).setLock(!cp5.getController(buttonName).isLock());
    }
    
    public void controlEvent(ControlEvent theEvent) {
      println(theEvent.getController().getName());
      n = 0;
    }
    
    // function colorA will receive changes from 
    // controller with name colorA
    public void wicked(int theValue) {
      println("a button event from colorA: "+theValue);
      c1 = c2;
      c2 = color(0, 160, 100);
    }
    
    // function colorB will receive changes from 
    // controller with name colorB
    public void colorB(int theValue) {
      println("a button event from colorB: "+theValue);
      c1 = c2;
      c2 = color(150, 0, 0);
    }
    
    // function colorC will receive changes from 
    // controller with name colorC
    public void colorC(int theValue) {
      println("a button event from colorC: "+theValue);
      c1 = c2;
      c2 = color(255, 255, 0);
    }
    
    public void play(int theValue) {
      println("a button event from buttonB: "+theValue);
      c1 = c2;
      c2 = color(0, 0, 0);
    }
    
  • Hey kfrajer,

    thank you for the answer, I appreciated it. So I can use the getController method also in the textlabel class?

    I'll check it out soon, Ignazio

  • The object textLabel can be accessed using getController but not sure if it can be accessed in the same way as button. A button can be accessed when an event is emitted. On the other hand, I am not sure if textLabel emits an event at all.

    http://www.sojamo.de/libraries/controlP5/examples/use/ControlP5controlEvent/ControlP5controlEvent.pde

    Consider the code below.

    Kf

    import controlP5.*;
    
    ControlP5 cp5;
    
    Textlabel myTextlabelA;
    Textlabel myTextlabelB;
    
    String lblName="wicked";
    
    void setup() {
      size(700, 400);
      cp5 = new ControlP5(this);
    
      myTextlabelA = cp5.addTextlabel(lblName)
        .setText("A single ControlP5 textlabel, in yellow.")
        .setPosition(100, 50)
        .setColorValue(0xffffff00)
        .setFont(createFont("Georgia", 20))
        ;
    
      myTextlabelB = new Textlabel(cp5, "Another textlabel, not created through ControlP5 needs to be rendered separately by calling Textlabel.draw(PApplet).", 100, 100, 400, 200);
    }
    
    
    
    void draw() {
      background(0);
      myTextlabelB.draw(this);
    }
    
    
    void keyPressed() {
    
      if (keyCode!=' ')
        return;
    
      println("Status now: "+cp5.getController(lblName).isLock());
    
      cp5.getController(lblName).setLock(!cp5.getController(lblName).isLock());
    }
    
Sign In or Register to comment.