2. Events
• Send notifications on certain events or create
your own.
• Wraps around Poco Events system
• You register for the events you want to receive.
• testApp receives events automatically ( ,
keyPressed
keyReleased, mouseMoved)
3. Events
Delegates
• A target subscribes to an event by registering a
delegate.
• You can see the delegate as an “Event listener”
• An event has always one argument.
• There is one global ofEvents object which holds
the OF related, event instances to which you can
subscribe.
• OF uses a First In First Out (FIFO) event queue.
4. Events
Listen for OF-events
Create a function which is called when the event
notification is sent. This function must have one
argument of the correct event type.
Use ofAddListener(event object, callback object,
callback function) to tell the event system you want
to listen for events.
You can remove your regiseterd listener by calling
ofRemoveListener(event object, callback object,
callback function)
5. Register to an event
1. Decide to what event you want to listen
See events/ofEvents.h for the available OF events
in the ofCoreEvent class.
For example:
ofEvent<ofMouseEventArgs> mouseMoved
The name of the event is mouseMoved and passes
am ofMouseEventArgs to the callback.
6. Register to an event
Name of event is
class ofCoreEvents {
public: mouseMoved which
ofEvent<ofEventArgs>
ofEvent<ofEventArgs>
setup;
update; stores the listeners
ofEvent<ofEventArgs>
draw;
ofEvent<ofEventArgs>
exit;
ofEvent<ofResizeEventArgs>
indowResized;
w
ofEvent<ofKeyEventArgs>
keyPressed;
ofEvent<ofKeyEventArgs>
keyReleased;
ofEvent<ofMouseEventArgs>
mouseMoved;
ofEvent<ofMouseEventArgs>
mouseDragged;
ofEvent<ofMouseEventArgs>
mousePressed; ofMouseEventArgs is
ofEvent<ofMouseEventArgs>
mouseReleased;
... passed as parameter to
}
the callback
7. Register to an event
2. Create the callback function
Create a function which receives the event type as
first and only argument.
ofEvent<ofMouseEventArgs> mouseMoved
class testApp : public ofBaseApp{
public:
void myCustomMouseReleased(ofMouseEventArgs& args) {
cout << "received a mouse released event" << endl;
cout << "mouse x:" << args.x << endl;
cout << "mouse y:" << args.y << endl;
cout << "mouse button:" << args.button << endl;
}
}
8. Register to an event
3. Tell the event object you want to listen
Use the global function ofAddListener to register
your new function as a callback for the event. Note
that ofEvents is the global instance of ofCoreEvents
void testApp::setup(){
ofAddListener(
ofEvents.mouseReleased
// the event object
,this
// testApp pointer
,&testApp::myCustomMouseReleased
// the callback function.
);
}
9. UnRegister from event
To unregister from an event, you simply call
ofRemoveListener() with the same parameters
when you added the listener.
You can re-enable the listener again by
calling ofAddListener() again.
void testApp::setup(){
ofRemoveListener(
ofEvents.mouseReleased
,this
,&testApp::myCustomMouseReleased
);
}
10. Recap event listening
Listen for OF-events
1. Create listener function
2. Find the event you want to listen to.
3. Call ofAddListener() to register
Remove listener
1. Call ofRemoveListener()
11. Event listening
Register to all mouse events
Create these functions:
void mouseDragged(ofMouseEventArgs& args)
void mouseMoved(ofMouseEventArgs& args)
void mousePressed(ofMouseEventArgs& args)
void mouseReleased(ofMouseEventArgs& args)
Call ofRegisterMouseEvents(this) to
register to all mouse events. Instead of
registering for each one separately. To unregister
call ofUnregisterMouseEvents(this)
13. Event listening
Register to all keyboard events
Create these functions:
void keyPressed(ofKeyEventArgs& args)
void keyReleased(ofKeyEventArgs& args)
Call ofRegisterKeyEvents(this) to
register to all key events. To unregister
call ofUnregisterKeyEvents(this)
14. Event listening
Register to all touch events
Create these functions:
void touchDoubleTap(ofTouchEventArgs& args)
void touchDown(ofTouchEventArgs& args)
void touchMoved(ofTouchEventArgs& args)
void touchUp(ofTouchEventArgs& args)
void touchCancelled(ofTouchEventArgs& args)
Call ofRegisterTouchEvents(this) to
register to all touch events. Unregister using
ofUnregisterTouchEvents(this)
15. Event listening
Register to all drag events
Create these functions:
void dragEvent(ofDragInfo& args)
Call ofRegisterDragEvents(this) to
register to all drag events. Unregister
with ofUnregisterDragEvents(this)
16. Simple messaging
openFrameworks adds a very simple way to send
messages between objects using the new function
ofSendMessage(string).
When you call ofSendMessage, the function
testApp::gotMessage(ofMessage msg) is called by the
event system.
You can use this as an easy way to notify
your application about certain simple events.
Works everywhere!
18. Register for messages
When you create your own class and you want to
receive messages, you create a function called void
gotMessage(ofMessage& msg) and register using
ofRegisterGetMessages(this)
class MyMessageReciever {
public:
MyMessageReciever() {
}
void setup() {
ofRegisterGetMessages(this);
}
void gotMessage(ofMessage& msg) {
cout << "got message: " << msg.message << endl;
}
}
21. Event argument objects
ofKeyEventArgs ofAudioEventArgs
int key float* buffer
int bufferSize
ofMouseEventArgs int nChannels
int x
int y ofResizeEventArgs
int button int width
int height
22. Event argument objects
ofTouchEventArgs
int id float minoraxis
int time float majoraxis
float x float pressure
float y float xspeed
int numTouches float yspeed
float width float xaccel
float height float yaccel
float angle
23. Custom events
In your header file (.h) create an extern event object
which will be used as a storage for listeners and
which notifies them.
“extern” tells your compiler the object is declared
somewhere else. This will be done in your .cpp file.
Also, define your custom event data type.
24. Custom events
Step 1. create your custom event data type
class MyCustomEventData {
public:
MyCustomEventData(string someData):data(someData) {
}
string data;
};
Step 2. create a extern declared event dispatcher object.
extern ofEvent<MyCustomEventData> myCustomEventDispatcher;
The event dispatcher
The data type you will pass to listener
25. Custom events
Step 3. in your .cpp define the event dispatcher object.
ofEvent<MyCustomEventData> myCustomEventDispatcher;
To listen to this myCustomerEventDispatcher you
create a event listener function and call
ofAddListener like this:
ofAddListener(myCustomEventDispatcher, this, &testApp::myCustomEventListener);
This is how the event listener function looks.
class testApp : public ofBaseApp{
public:
void myCustomEventListener(MyCustomEventData& args) {
cout << "Received a custom event: " << args.data << endl;
}
}