Add / remove button

PerPer
edited December 2015 in Library Questions

Ive created the following example for adding buttons with "+" and removing buttons with "-". But I cant find out how to remove a button. Only adding. It would be perfect if something like removeButton() existed but it doesnt so need another solution. Any suggestion is very much apreciated!

import controlP5.*;
ControlP5 cp5;

int listNr = 0;
StringList list;

void setup() {
  size(400, 600);
  cp5 = new ControlP5(this);

  list = new StringList(); 
  list.append(str(listNr));

  cp5.addButton("listNumber: " + listNr)
    .setValue(0) // detta värde skickas när den trycks ner
    .setPosition(100, 100+(listNr*30))
    .setSize(200, 19)
    ;
}

void draw() {
}

void keyReleased() {
  if (key == '+') {
    listNr++;
  } else if (key == '-') {
    listNr--;
  }

  // if the button doesnt exist - add a new one
  for (int i = 0; i < list.size(); i++) {
    if (listNr == list.size()-1) {
      println("DO NOTHING");
      break; // leave loop
    } else if (listNr > list.size()-1) {
      println("ADD BUTTON");
      cp5.addButton("listNumber: " + listNr)
        .setValue(0) 
        .setPosition(100, 100+(listNr*30))
        .setSize(200, 19)
        ;
      list.append(str(listNr));
      break; // leave loop
    } else if (listNr < list.size()-1) {
      println("REMOVE BUTTON");

      /* HERE I WANT TO DO SIMILIAR THING AS ABOVE BUT REMOVE A BUTTON
       cp5.removeButton("listNumber: " + listNr); // BUT THIS DOESNT WORK
       */

      list.remove(listNr);
      break; // leave loop
    }
  }

  println("listNr: " + listNr + " LISTSIZE: " + list.size());
  println("LIST: " + list);
  println("====================");
}

Answers

  • If controlP5 doesn't allow you to remove the button just make it invisible.

  • PerPer
    edited December 2015

    Its an option. Would prefer to remove it completely though if its possible even though I guess it takes minimum power to create an invisible button. Otherwise I also have to handle the listNr so it only counts the visible buttons. Otherwise if I remove a number from the list and then add a new one again I get this errormessage:

    VARNING: Controller with name "/listNumber: 2" already exists. overwriting reference of existing controller.

  • Rather than add a new one make it visible again. You can re-position and resize it.

Sign In or Register to comment.