For your class to provide custom events you need a `CallbackMgr` member variable for each event, usually private:
- CallbackMgr<bool(ThingEvent)> mCallbacksThingHappened;
And a public convenience function for registering callbacks is helpful:
- template<typename T>
- CallbackId registerThingHappened( T *obj, bool (T::*callback)(ThingEvent) ){
- return mCallbacksThingHappened.registerCb(std::bind1st(std::mem_fun(callback), obj));
- }
`bool(ThingEvent)` means that callbacks will return `bool` and accept `ThingEvent` as an argument. Here, `ThingEvent` is a custom class that extends `Event` and keeps track of Thing variables (see `TouchEvent` or `MouseEvent` in the Cinder source for an example).
To register event listeners on an instance of your class called `myObj` you would call the following in your app's `setup` function:
- myObj.registerThingHappened(this, &MyApp::thingHappened);
Where `thingHappened` is a method on your Cinder app that looks just like `mouseDown` and friends, returning `bool` and accepting `ThingEvent` as described above:
- bool MyApp::thingHappened(ThingEvent event) {
- /* handle event!
- * return true to cancel all other listeners
- * return false otherwise */
- }
Finally in the class the provides the custom events, when the thing happens you'll call:
- mCallbacksThingHappened.call(ThingEvent(/* special event params go here */));
This will notify all the listeners in the order they were added until one of them returns true. If you followed along so far that's good, I just learned all this tonight from reading Cinder's `App` class.
Oh, and if all this works you'll want to implement a corresponding `unregisterThingHappened` that takes the CallbackId returned by `myObj.registerThingHappened` and tidies up after itself. Phewf!