import controlP5.*;
//************************************************************************************************************
//* How to use ControlP5 Events & Callbacks
//*
//* This program shows 4 differnt ControlP5 Textlabel widgets displayed in 4 different ways:
//* 1) Textlabel blueLabel = cp5.addTextlabel("blueLabel");
(as single line label using ControlP5Legacy command)
//* 2) Textlabel redLabel = new Textlabel(cp5, "redLabel", 0, 0, 0, 0);
(as single line label using PGraphic off-screen display)
//* 3) Textlabel greenLabel = new Textlabel(cp5, "greenLabel", 10, 100, 400, 100);
(as multiline label)
//* 4) MyLabel yellowLabel = new MyLabel(cp5, "yellowLabel", 10, 250, 400, 100);
(as class that extends Textlabel)
//*
//* Problems:
//* 1) in the setup() I attempt to println the name of each label but only the
blueLabel prints successfully? Why?
//* 2) how can I register each Textlabel widget so that mouse events are sent to the
controlEvent(ControlEvent event) method?
//* 3) how can I register each Textlabel widget to call its callback method.
//* void blueLabel(in value) { println ("blueLabel clicked " + value); };
//* void redLabel(in value) { println ("redLabel clicked " + value); };
//* void greenLabel(in value) { println ("greenLabel clicked " + value); };
//* void yellowLabel(in value) { println ("yellowLabel clicked " + value); };
//* 4) in the draw() method I have declared c1, c2, c3, and c4 Controllers; why
does c1 work OK, but c2, c3, and c4 have null pointers?
//**********************************************************************************************************
ControlP5 cp5;
Textlabel redLabel;
Textlabel blueLabel;
Textlabel greenLabel;
MyLabel yellowLabel;
PGraphics pg;
PFont f1;
PFont f2;
CallbackListener cb;
void setup() {
size(600, 600);
background(100, 100, 100);
cp5 = new ControlP5(this);
pg = createGraphics(200, 50); // size of Red Label's background
f1 = createFont("Arial", 20);
f2 = createFont("Arial", 15);
blueLabel = cp5.addTextlabel("blueLabel")
.setText("Blue Label")
.setPosition(7, 5)
.setColorValue(color(0, 0, 255))
.setFont(createFont("Arial", 20));
blueLabel.get().setColorBackground(color(0, 0, 150));
blueLabel.getStyle().setPadding(5, 5, 5, 5);
redLabel = new Textlabel(cp5, "redLabel", 0, 0, 0, 0);
greenLabel = new Textlabel(cp5, "greenLabel", 10, 100, 400, 100);
greenLabel.setWidth(400);
greenLabel.setHeight(100);
greenLabel.setPosition(10, 100);
greenLabel.setMultiline(true);
greenLabel.setColor(color(0, 255, 0));
greenLabel.setFont(f2);
greenLabel.setText("Green Label.\n1) Now is the time for all good men to come to the aid of their country!\n2) The brown cow jumped over the moon!\n3) a b c d e f g h i j k l m n o p q r s t u v w x y z");
greenLabel.get().enableColorBackground();
greenLabel.get().setColorBackground(color(0, 55, 0));
greenLabel.getStyle().setPadding(5, 5, 5, 5);
yellowLabel = new MyLabel(cp5, "yellowLabel", 10, 250, 400, 100);
yellowLabel.setMultiline(true);
yellowLabel.setColor(color(255, 255, 0));
yellowLabel.setFont(f2);
yellowLabel.setText("Yellow Label.\n1) Now is the time for all good men to come to the aid of their country!\n2) The brown cow jumped over the moon!\n3) a b c d e f g h i j k l m n o p q r s t u v w x y z");
yellowLabel.get().enableColorBackground();
yellowLabel.get().setColorBackground(color(55, 55, 0));
yellowLabel.getStyle().setPadding(5, 5, 5, 5);
println("blue name = " + blueLabel.getName()); // this prints OK
println("red name = " + redLabel.getName()); // this fails to print, name is empty... why?
println("green name = " + greenLabel.getName()); // this fails to print, name is empty... why?
println("yellow name = " + yellowLabel.getName()); // this fails to print, name is empty... why?
}
void controlEvent(ControlEvent event) {
if (event.isFrom(blueLabel)) {
println("blueLabel Event");
} else {
if (event.isFrom(redLabel)) {
println("redLabel Event");
} else {
if (event.isFrom(greenLabel)) {
println("greenLabel Event");
} else {
if (event.isFrom(yellowLabel)) {
println("yellowLabel Event");
}
}
}
}
}
void redLabelDraw() {
// start drawing to the off-screen buffer
pg.beginDraw();
pg.background(200, 0, 0);
redLabel.setFont(f1);
redLabel.draw(pg);
redLabel.setColorValue(color(255, 0, 0));
redLabel.get().enableColorBackground();
redLabel.get().setColorBackground(color(55, 0, 0));
redLabel.setText("Red Label");
redLabel.getStyle().setPadding(5, 5, 5, 5);
redLabel.getStyle().setMargin(5, 5, 5, 5);
redLabel.setHeight(30);
redLabel.setWidth(50);
pg.endDraw();
// put the off-screen buffer onto the main application's window
image(pg, 10, 40);
}
int[] mouse = new int[4];
void draw() {
redLabelDraw();
greenLabel.draw(this);
yellowLabel.draw(this);
Controller c1 = cp5.getController("blueLabel");
Controller c2 = cp5.getController("greenLabel");
Controller c3 = cp5.getController("redLabel");
Controller c4 = cp5.getController("yellowLabel");
if (c1 != null) {
if (c1.isMouseOver()) {
println("mouse is over blueLabel: " + (++mouse[0]));
}
}
if (c2 != null) {
if (c2.isMouseOver()) {
println("mouse is over greenLabel" + (++mouse[1]));
}
}
if (c3 != null) {
if (c3.isMouseOver()) {
println("mouse is over redLabel" + (++mouse[2]));
}
}
if (c4 != null) {
if (c4.isMouseOver()) {
println("mouse is over greenLabel" + (++mouse[3]));
}
}
}
class MyLabel extends Textlabel {
MyLabel(ControlP5 theController, String theName, int theX, int theY, int theW, int theH) {
super (theController, theName, theX, theY, theW, theH);
}
}
My questions for this code are in the comments at the top. Thanks for the help!