We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › ControlP5 Events Handling
Page Index Toggle Pages: 1
ControlP5 Events Handling (Read 1135 times)
ControlP5 Events Handling
May 17th, 2009, 9:52am
 
Hi guys,
I'm quite a n00b with Processing, I'm a building a graphic interface to control an Arduino connected to a LynxMotion SSC-32.
The GUI is built with ControlP5 library (fantastic, great work), I've almost finished it but I'm still feeling like I'm missing something.....
What I basically don't understand it's the event handling.
When creating some controllers, the linking between the Controller and the function "controlEvent" is automatic? Every controller in my sketch is firing that function....
I've noticed that, if I declare a function with the same name of the controller, using that controller automatically fires that function too (and the controlEvent function). Is this normal?
Is this the way to associate different Event Handlers Functions for every controller object?
Sorry for the confusion, but as I said I feel like I'm really missing something Sad
Thanks a lot

Re: ControlP5 Events Handling
Reply #1 - May 21st, 2009, 6:29am
 
hi sr.richie -
your suspicions are correct. when you declare a new controller, you give it a name, and controlP5 "magically" - that is, using the java reflection API - accesses the code you're actually writing to tell it about the event.

there are three ways to access information about "events" such as moving a slider.

1.) when you name a slider "mrSlider" in the addSlider method, then create a method "void mrSlider( float theAmt )" or "mrSlider( int theAmt )", that method will be called each time the slider generates an event detected by the library.

2.) controlp5 has the additional ability to update a public variable, so, using the previous example of a slider named "mrSlider", you could declare a variable outside of a method named "public int mrSlider" or "public float mrSlider", and that variable will have the updated value stored in it, bypassing the event notification, but assuring that the variable has the latest value in it.

3.) you can declare a method "void controlEvent( ControlEvent theEvent )", which gives you access to all the information associated with every event, such as the event generator's name, the event generator's label, the event generator's value, and the event generator's id number, if it has one. this last option is the "heavyweight" way to handle events - the most complex and is clogged with the most information. see the api for more information about methods that can be called on a ControlEvent object.

Quote:
import controlP5.*;


ControlP5 controlP5;

int myColorBackground = color(0,0,0);

int sliderValue = 100;

void setup() {
  size(400,400);
  controlP5 = new ControlP5(this);
 Slider s = controlP5.addSlider("slider",100,167,128,100,160,10,100);
  s = controlP5.addSlider("sliderValue",0,255,128,200,200,10,100);
}

void draw() {
  background(myColorBackground);
  fill(sliderValue);
  rect(0,0,width,100);
}

void slider(float theColor) {
  myColorBackground = color(theColor);
  println("a slider event. setting background to "+theColor);
}

Re: ControlP5 Events Handling
Reply #2 - May 21st, 2009, 9:32pm
 
there is one more. you can use the ControlListener interface with your own class as an event listener to one or more controllers. there is the  ControlP5listener sketch in the examples that comes with the download.
best,
andreas
Page Index Toggle Pages: 1