ControlP5 Radio Button multiple images

edited May 2014 in Library Questions

Hi everyone, I'm trying to implement a radio button controller but with a different image for each .addItem I created inside of it. I saw the references but I found nothing. Is there a way to do it?

here's the code (basically the example a little edited)

import controlP5.*;

ControlP5 cp5;

int myColorBackground = color(0,0,0);

RadioButton r;

PImage[] imgs;

void setup() {
  size(700,400);

  PImage[] imgs = {loadImage("button_a.png"),loadImage("button_b.png"),loadImage("button_c.png")};

  cp5 = new ControlP5(this);
  r = cp5.addRadioButton("radioButton")
         .setPosition(width/2,30)
         .setSize(imgs[1])
         .setColorForeground(color(120))
         .setColorActive(color(255))
         .setColorLabel(color(255))
         .setItemsPerRow(1)
         .setSpacingColumn(10)
         .setSpacingRow(10)
         .addItem("50",1)
         .setImages(imgs[0],imgs[1],imgs[2])
         .addItem("100",2)
         .setImages(imgs[1],imgs[0],imgs[0])
         .addItem("150",3)
         .setImages(imgs[1],imgs[2],imgs[0])
         .addItem("200",4)
         .setImages(imgs[0],imgs[2],imgs[1])
         .addItem("250",5)
         .setImages(imgs[1],imgs[2],imgs[0])
         .addItem("300",6)
         .setImages(imgs[0],imgs[1],imgs[2])
         .hideLabels() 
         ;

     for(Toggle t:r.getItems()) {
       t.captionLabel().setColorBackground(color(255,80));
       t.captionLabel().style().moveMargin(-7,0,0,-3);
       t.captionLabel().style().movePadding(7,0,0,3);
       t.captionLabel().style().backgroundWidth = 45;
       t.captionLabel().style().backgroundHeight = 13;
     }
}


void draw() {
  background(myColorBackground);
}


void keyPressed() {
  switch(key) {
    case('0'): r.deactivateAll(); break;
    case('1'): r.activate(0); break;
    case('2'): r.activate(1); break;
    case('3'): r.activate(2); break;
    case('4'): r.activate(3); break;
    case('5'): r.activate(4); break;
  }

}

void controlEvent(ControlEvent theEvent) {
  if(theEvent.isFrom(r)) {
    print("got an event from "+theEvent.getName()+"\t");
    for(int i=0;i<theEvent.getGroup().getArrayValue().length;i++) {
      print(int(theEvent.getGroup().getArrayValue()[i]));
    }
    println("\t "+theEvent.getValue());
    myColorBackground = color(int(theEvent.group().value()*50),0,0);
  }
}

void radioButton(int a) {
  println("a radio Button event: "+a);
}
Sign In or Register to comment.