Class for creating custom events. Also used inside oF for its own events (see ofCoreEvents).
ie: To create a new event:
To notify an event of that type:
float value = 10.0f; ofNotifyEvent(onVolumeChange, value);
To add a listener to that event, if the event is for example in an object called mySoundObject, and you want to register ofApp as a listener for that event:
ofAddListener(mySoundObject.onVolumeChange, this, &ofApp::onVolumeChange);
where ofApp::onVolumeChange is a function with the following signature:
void onVolumeChange(float & volume);
See the advancedEventsExample to see a complete example of how to work with events in oF.
Be careful when using events, it's a powerful structure but can also lead to really confusing code as there's no direct calls to an object.
It's important to unregister events in the destruction of listeners, if not an object that has already been destroyed can get called and the application will crash.
ofRemoveListener(mySoundObject.onVolumeChange, this, &ofApp:onVolumeChange);
Events have a private copy constructor to avoid that a copy of an object with an event gets all the registered listeners of the original. To be able to create a copy of an object that contains an event, use a pointer to an event instead of a normal var. This also means that you cannot store events directly in a vector or any other collection, the solution is the same, just use a pointer to an event.
vector< ofEvent<int> > events;
vector< ofEvent<int>* > events;
void ofEvent::add(TFunction function, int priority)
void ofEvent::add(TObj *listener, TMethod method, int priority)
unique_ptr< of::priv::AbstractEventToken > ofEvent::newListener(TFunction function, int priority)
unique_ptr< of::priv::AbstractEventToken > ofEvent::newListener(TObj *listener, TMethod method, int priority)
bool ofEvent::notify(T ¶m)
bool ofEvent::notify(const void *sender, T ¶m)
void ofEvent::remove(TFunction function, int priority)
void ofEvent::remove(TObj *listener, TMethod method, int priority)
Last updated 星期五, 15 九月 2023 21:22:44 UTC - 89b7d17faa77e847b7de814f7dcea89eeca38653