ControlP5 is a feature-packed GUI library, full of options for building and customising user interfaces. It provides an extensive set of control widgets that include buttons, sliders, knobs, toggles, textfields, checkboxes, accordions, charts, timers, drop-downs, tab- and window-interfaces, and more.
To begin using ControlP5, one must first install it. From the Processing menu bar, select Sketch > Import Library… > Add Library…
This raises the Contribution Manager window; under the Libraries tab, locate and install ControlP5.
Once you’ve the library installed, create a new sketch named “identikit”. An identikit – or facial composite – is a portrait image reconstructed from the memory of one or many eyewitnesses. You’ve probably seen criminal identikits in police stations or on the news. These are rendered in various ways, namely: by sketch artists; or using a system of overlaid transparencies; or with computer software. Our identikit program will be no use for any real-world application, but fun to play with, nonetheless. To get started, add the following code.
Processing requires the
add_library() line for loading in ControlP5. In the
setup block, a new ControlP5 instance is assigned to a variable named
cp5. From here on, one can access ContolP5 features with a
cp5. prefix; this will make sense a little further along when we begin to add controllers. The
axis coordinate runs through the centre of the
ellipse; in other words, it marks the horizontal centre of the circular ‘face’. The face is positioned to the left of the display to make room for control widgets on the right.
The first widget will be a textfield. The controller is added to the
The brackets surrounding the
cp5.addTextfield() lines may look odd, but this is necessary to break the chain of methods over multiple lines. Alternatively, you could write this all on a single line but likely won’t find it as readable. Whenever you create a new controller, specify a name in the first argument – in this case, I have used
'alias'. This name is used to reference the controller further along and also serves as the default label for the field.
draw function, retrieve the captured input and render it using a
text() function. You use the
getController() method to access the properties of any
cp5 controller (by its name), and chain a
getText() onto this to isolate the text value.
Test out the input field. The alias you enter will appear beneath the face.
Next, we’ll add widgets for controlling the eyes. The additional methods,
setValue(), set the lower/upper value range and in the initial value, respectively.
The problem is that the default position for any slider label is to the right of the widget, which doesn’t fit nicely in this layout. Add some code that adjusts the alignment and padding to reposition the label at the bottom left.
Before drawing the eyes on the face, add three more control widgets – a knob and two toggles:
To draw the eyes, add the following lines to your
draw function. You’ll notice that a
getController() is used to retrieve each of the controller properties – but, unlike the textfield – there are
getValue() methods in place of
Run the sketch a have a play with the various eye features.
For the nose, we’ll add a 2D slider; for the mouth, a standard slider but with tick marks for set increments.
The 2D slider holds two values in a list, hence the
.getArrayValue() white square brackets. If you are confused about what the different methods control, try adjusting the arguments to see what effect this has.
We’ll add one final button widget that will save the image as a TIFF file. Using the
.addButton() method, place a new button at the lower right of the display window.
One can attach event handlers like any other method. These require a Python lambda – but for now, all you need to know is where to write the lambda. We’ll not review lambdas in these lessons, but if you wish to explore them further, wait until after lesson 8 on functions (they’ll make far more sense, after that).
e variable serves the same role as in the mouse event examples from earlierThat’s, it holds all of the properties related to the event (in this case, an
.onClick). You may also name
e whatever you wish. To provide some insight into what these properties are, we’ll print them to the console.
Of course, we wish to save an image, so change the lambda line, replacing everything after the colon with a
save() function that uses the alias input for a filename.
.onClick( lambda e: save(cp5.getController('alias').getText()) )
that’s as far as we’ll venture into ControlP5. There’s plenty more to explore, though. For instance, while pressing the Alt key and click-and-dragging, controllers can be moved about the display. You can also hide all of the controllers using Alt+Shift+H. To activate these shortcut features, add a
cp5.enableShortcuts() line somewhere in the setup block.
For more examples of how to use ControlP5, refer to the File > Examples… menu. In the window that pops-up, you’ll find an extensive selection of sample sketches. Be warned though: almost all of these are written in Processing’s Java language. In spite of this, it should be similar enough for you to understand and translate to Python.
That’s all for this lesson. Feel welcome to experiment with and add additional features to the tasks you’ve completed.
You’ll often find that you repeat the same, or very similar, lines of code within a single sketch. Moreover, as your programs grow more complex, repetition tends to creep in. For more modular and reusable code, one can employ functions. In the next chapter, you’ll look at how to define and work with functions. As a concept, you should grasp functions without much trouble, especially considering what you managed thus far. On the other hand, I’ll still be throwing in some crunchy tasks to keep you challenged!