Correctly handling controlEvents is arguably the most challenging aspect of controlP5. It will require some tweaking, some considering of the options and some trial-and-error. So the best way is to incrementally build it, checking if it's functioning along the way.
The more controllers etc. you have, the bigger and perhaps more compex the controlEvent method will be. Not all classes send events by default. For example tabs don't. If you want those to send events, you have to set it to true. But suppose you have stuff sending events, then you have to handle it in the controlEvent method. Otherwise the redness of your console will increase.
There is no one-size-fits-all solution. The handling has to be tailored to your setup. At the first level you have to check from which TYPE it came. So at the first level you'll have things like IF it's from a controller, controlgroup, tab, isFrom(specific controller) etc. This segments the different TYPES of controllers. At the second level, you can distinguish between controllers. For example you may have 10 controllers (of the TYPE controller), you can distinguish between them based on different things such as an id, a name etc. You have different options, choose the one that fits you. At the third level, you do something with it. At this point the route has already gone through the type and distinguishing the specific controller. So now you for example get the value and set another variable with it and/or trigger a function.
A small simple sketch has a small simple controlEvent method, because you will need less routing. A bigger more complex sketch has relatively a bigger more complex controlEvent method. It's not something that will be clear after one day, it'll take some time to get right. And in this case, more than ever, the examples and javadocs are your allie.
Here is an example of an actual (working ;-) controlEvent method of a sketch I'm working on right now.
- void controlEvent(ControlEvent theControlEvent) {
- if (frameCount > 0) {
- String eventName = "";
- if (theControlEvent.isTab()) {
- activeTab = theControlEvent.getTab().getId();
- } else if (theControlEvent.isFrom(radioSortMethod)) {
- sortMethod = int(theControlEvent.getValue());
- cm.sortColorList();
- cm.putColorsInTexture();
- } else if (theControlEvent.isFrom(radioColorMode)) {
- colorMode = int(theControlEvent.getValue());
- cm.updateColors();
- } else {
- eventName = theControlEvent.controller().name();
- if (eventName.equals("physicsDrag")) { physics.setDrag(physicsDrag); }
- else if (eventName.equals("particleRadius")) { physics.setParticleRadius(); }
- else if (eventName.equals("particleStrength")) { physics.setParticleStrength(); }
- else if (eventName.equals("particleJitter")) { physics.setParticleJitter(); }
- else if (eventName.equals("metaBallRadius")) { physics.setMetaBallRadius(); }
- else if (eventName.equals("gravity")) {
- if (mouseButton == RIGHT) {
- sliderGravity.setArrayValue(0, 0);
- sliderGravity.setArrayValue(1, 0);
- physicsGravityX = 0;
- physicsGravityY = 0;
- } else {
- physicsGravityX = theControlEvent.controller().arrayValue()[0];
- physicsGravityY = theControlEvent.controller().arrayValue()[1];
- }
- setGravity();
- } else if (eventName.equals("numColors") || eventName.equals("gradientSteps")) {
- cm.updateColors();
- } else if (eventName.equals("minThreshold")
- || eventName.equals("maxThreshold")
- || eventName.equals("alphaMinThreshold")
- || eventName.equals("alphaMaxThreshold")) {
- cm.putColorsInTexture();
- }
- }
- }
- }