DropdownList "network"

edited September 2014 in Library Questions

Hey guys,

I have 4 DropdownList, and would like them to behave "relative to one another", so to say. Here's how it goes: the total of all my lists' selected values should never exceed 100. So when the program starts, and list ddl1 has value 20, then values in other lists should be set not to exceed 80 (i.e. values 100 and 90 should be removed from the list), so that's it's never possible to have more than 100 in total. Now if one selects 60 as the next value in, say, ddl2, then the remaining lists should only display values up to 20, because [list1's 20 + list2's 60 = 80], and [100 - 80 = 20]. Hope I'm being clear... 8-}

Maybe you can help, because I haven't very well grasped the structure of the ControlP5 classes (why is DropdownList of type ControlGroup, what does this imply?...). I came up to the point of method updateDDL(DropdownList ddl) below, but I don't know if this is the most elegant way to go.

import controlP5.*;

ControlP5 cp5;
DropdownList ddl1;
DropdownList ddl2;
DropdownList ddl3;
DropdownList ddl4;

void setup() {
  size(640, 480);

  cp5 = new ControlP5(this);

  ddl4 = cp5.addDropdownList("ddl4").setPosition(10, 40);
  ddl3 = cp5.addDropdownList("ddl3").setPosition(10, 30);
  ddl2 = cp5.addDropdownList("ddl2").setPosition(10, 20);
  ddl1 = cp5.addDropdownList("ddl1").setPosition(10, 10);

  customizeDDL(ddl4);
  customizeDDL(ddl3);
  customizeDDL(ddl2);
  customizeDDL(ddl1);

  ddl1.setIndex(2);
}

void draw() {
  background(0);
}

void customizeDDL(DropdownList ddl) {
  for (int i=0; i<11; i++) {
    ddl.addItem(str(i*10), i);
  }
  ddl.setIndex(0);
}

void updateDDL(DropdownList ddl) {
  ////////////////////////
  // HOW TO WRITE THIS? //
  ////////////////////////
}

void controlEvent(ControlEvent theEvent) {
  if (theEvent.isGroup()) {
    if (theEvent.getGroup() == ddl1) {
      updateDDL(ddl2);
      updateDDL(ddl3);
      updateDDL(ddl4);
    } 
    else if (theEvent.getGroup() == ddl2) {
      updateDDL(ddl1);
      updateDDL(ddl3);
      updateDDL(ddl4);
    } 
    else if (theEvent.getGroup() == ddl3) {
      updateDDL(ddl1);
      updateDDL(ddl2);
      updateDDL(ddl4);
    }
    else if (theEvent.getGroup() == ddl4) {
      updateDDL(ddl1);
      updateDDL(ddl2);
      updateDDL(ddl3);
    }
  }

  println((ddl1.getValue() + ddl2.getValue() + ddl3.getValue() + ddl4.getValue())*10);
}
Sign In or Register to comment.