
The simplest way to add a controller to your application is to instantiate a new Controller object as follow:
new Controller(this)
will launch a java popup window, with a predefined set of components: 4 horizontal sliders, 1 vertical slider, 1 arcball, 3 spinners and 6 toggle-buttons.
It is also possible to define the set of components that will come with the controller. For example:
new Controller(this, "hsliders: 3; spinners: 2")
will launch a controller with 3 horizontal sliders and 2 spinners. For each type of component, there is a proper keyword to use and a maximum number of instances allowed, as in follow:
| keyword | minimum | maximum | |
| Horizontal Sliders | hsliders | 0 | 4 |
| Vertical Sliders | vsliders | 0 | 4 |
| Arcball | arcballs | 0 | 1 |
| Spinners | spinners | 0 | 4 |
| Toggle Buttons | togglebuttons | 0 | 8 |
When you instantiate a new Controller object, your application will "block" until the controller's popup window has been created and populated with its components.
Once a controller has been created, its components are still in their default state, as in the following table:
| values range | value | legend | selected | # of ticks | ticks painted | snap to ticks | |
| Slider (horizontal & vertical) | 0 ... 1 | 0 | none | --- | 10 | true | false |
| Arcball | --- | {0°, 0, 0, 0} | none | --- | --- | --- | --- |
| Spinner | 0 ... 2π | 0 | none | --- | --- | --- | --- |
| Toggle Button | --- | --- | none | false | --- | --- | --- |
Consider the following example:
controller = new Controller(this, "hsliders: 1; spinners: 2");
controller.hsliders.get(0).setLegend("controls the background color");
println(controller.spinners.get(0).getValue());
controller.spinners.get(1).setValue(PI);
- A controller is created with one horizontal slider and two spinners.
- The slider is given a legend text that will be displayed when rolled-over.
- The current value of the first spinner is printed.
- The value of the second spinner is set to PI.
Some of the components are coming in group (horizontal sliders, vertical sliders, spinners, toggle buttons), while others (arcball) are standalone. The different types of components are accessible as follow:
| Horizontal Sliders | hsliders.get(n) |
| Vertical Sliders | vsliders.get(n) |
| Arcball | arcball |
| Spinners | spinners.get(n) |
| Toggle Buttons | togglebuttons.get(n) |
where n is the index of a component within its group (sorted either from left-to-right or top-to-bottom). For example hsliders.get(0) will access the first horizontal slider, and hsliders.get(2) will access the third horizontal slider, providing a controller has been created with at least 3 horizontal sliders.
Instead of constantly grabbing values returned by components from within loop(), it's possible to use event-based programming as in the following example:
Controller controller;
float bgcolor;
void setup()
{
controller = new Controller(this, "hsliders: 1");
}
void loop()
{
background(bgcolor);
}
void controllerUpdated(ControllerEvent e)
{
if (e.getSource() == controller.hsliders.get(0) && e.getMessage() == "VALUE")
{
bgcolor = controller.hsliders.get(0).getValue() * 255;
}
}
The key is to include the controllerUpdated() method in your code and to examine the e parameter: e.getSource() contains a reference to the component that generated the event and e.getMessage() contains a string that reflects the type of event.
For more details about the different types of events generated by each component and for additional code examples dealing with event-handling, see the Component reference.