Hello,
I am quite new in Processing. I am working on a project where I use a CheckBox from ControlP5 library to control random rotation of a shape. I have 4 switches each for one angle (0/90/180/270 degrees) and I need to detect which switch was toggled and caused an event to keep at least one ‘ON’.
Is there any other simple way how to detect it? Currently I store last state of all switches and compare it with new state in case of the event.
Here is simple example of my code
// Test of swich toggle detection
import controlP5.*;
ControlP5 cp5;
CheckBox checkbox;
float b[] = new float [4];
void setup() {
size(500, 280);
background(0);
cp5 = new ControlP5(this);
checkbox = cp5.addCheckBox("checkBox")
.setPosition(100, 100)
.setSize(20, 20)
.setItemsPerRow(2)
.setSpacingColumn(35)
.setSpacingRow(25)
.addItem("0", 0)
.addItem("90", 1)
.addItem("180", 2)
.addItem("270", 3)
;
}
void draw() {
}
void checkBox(float[] a) {
for (int i=0; i<4; i++) {
if (a[i]!=b[i]) {
println("Switch ",i*90," was toggled");
break;
}
}
arrayCopy(a,b);
}
The shape can be in 1 to 4 different positions (determined by activated checkboxes), so I need to detect all the combinations. The reason is to avoid the case when user wants to deactivate the last “ON” checkbox, so I need to detect it and toggle it back “ON”.
To get the state of particular checkbox in the radioButton example you have to check all of them one by one (like I do now in my sketch). I want to know if there is more simple way like to get an index of the checkbox which caused the event when user toggled it.