Controller 001 alpha : Reference

Creating controllers

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:

keywordminimummaximum
Horizontal Slidershsliders04
Vertical Slidersvsliders04
Arcballarcballs01
Spinnersspinners04
Toggle Buttonstogglebuttons08

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.

Component default states

Once a controller has been created, its components are still in their default state, as in the following table:

values rangevaluelegendselected# of ticksticks paintedsnap to ticks
Slider (horizontal & vertical)0 ... 10none---10truefalse
Arcball---{0°, 0, 0, 0}none------------
Spinner0 ... 2π0none------------
Toggle Button------nonefalse---------

Accessing Components

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 Slidershsliders.get(n)
Vertical Slidersvsliders.get(n)
Arcballarcball
Spinnersspinners.get(n)
Toggle Buttonstogglebuttons.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.

Component reference

Working with events

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;
  }
}

launch example

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.

Advanced features

top  |  home  |  bagel papa poule : tookit