Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

openFrameworks 007 - events

Related Books

Free with a 30 day trial from Scribd

See all
  • Login to see the comments

openFrameworks 007 - events

  1. 1. openFrameworks events
  2. 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. 3. EventsDelegates• 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. 4. EventsListen for OF-eventsCreate a function which is called when the eventnotification is sent. This function must have oneargument of the correct event type.Use ofAddListener(event object, callback object,callback function) to tell the event system you wantto listen for events.You can remove your regiseterd listener by callingofRemoveListener(event object, callback object,callback function)
  5. 5. Register to an event1. Decide to what event you want to listenSee events/ofEvents.h for the available OF eventsin the ofCoreEvent class.For example:ofEvent<ofMouseEventArgs> mouseMovedThe name of the event is mouseMoved and passesam ofMouseEventArgs to the callback.
  6. 6. Register to an event Name of event isclass 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. 7. Register to an event2. Create the callback functionCreate a function which receives the event type asfirst 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. 8. Register to an event3. Tell the event object you want to listenUse the global function ofAddListener to registeryour new function as a callback for the event. Notethat 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. 9. UnRegister from eventTo unregister from an event, you simply callofRemoveListener() with the same parameterswhen you added the listener.You can re-enable the listener again bycalling ofAddListener() again. void testApp::setup(){ ofRemoveListener( ofEvents.mouseReleased ,this ,&testApp::myCustomMouseReleased ); }
  10. 10. Recap event listeningListen for OF-events1. Create listener function2. Find the event you want to listen to.3. Call ofAddListener() to registerRemove listener1. Call ofRemoveListener()
  11. 11. Event listeningRegister to all mouse eventsCreate these functions:void mouseDragged(ofMouseEventArgs& args)void mouseMoved(ofMouseEventArgs& args)void mousePressed(ofMouseEventArgs& args)void mouseReleased(ofMouseEventArgs& args)Call ofRegisterMouseEvents(this) toregister to all mouse events. Instead ofregistering for each one separately. To unregistercall ofUnregisterMouseEvents(this)
  12. 12. Event listeningclass MyWorker {public: MyWorker() { } void setup() { ofRegisterMouseEvents(this); } void mouseMoved(ofMouseEventArgs& args) { cout << "Moved: " << args.x << ", " << args.y << endl; } void mouseDragged(ofMouseEventArgs& args) { } void mousePressed(ofMouseEventArgs& args) { } void mouseReleased(ofMouseEventArgs& args) { }};
  13. 13. Event listeningRegister to all keyboard eventsCreate these functions:void keyPressed(ofKeyEventArgs& args)void keyReleased(ofKeyEventArgs& args)Call ofRegisterKeyEvents(this) toregister to all key events. To unregistercall ofUnregisterKeyEvents(this)
  14. 14. Event listeningRegister to all touch eventsCreate 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) toregister to all touch events. Unregister usingofUnregisterTouchEvents(this)
  15. 15. Event listeningRegister to all drag eventsCreate these functions:void dragEvent(ofDragInfo& args)Call ofRegisterDragEvents(this) toregister to all drag events. Unregisterwith ofUnregisterDragEvents(this)
  16. 16. Simple messagingopenFrameworks adds a very simple way to sendmessages between objects using the new functionofSendMessage(string).When you call ofSendMessage, the functiontestApp::gotMessage(ofMessage msg) is called by theevent system.You can use this as an easy way to notifyyour application about certain simple events.Works everywhere!
  17. 17. Simple messaging void testApp::setup(){ ofSendMessage("ready"); } void testApp::gotMessage(ofMessage msg){ cout << "got message: " << msg.message << endl; }
  18. 18. Register for messagesWhen you create your own class and you want toreceive messages, you create a function called voidgotMessage(ofMessage& msg) and register usingofRegisterGetMessages(this) class MyMessageReciever { public: MyMessageReciever() { } void setup() { ofRegisterGetMessages(this); } void gotMessage(ofMessage& msg) { cout << "got message: " << msg.message << endl; } }
  19. 19. Common eventsMouse events Touch events• mouseMoved • touchDown• mouseDragged • touchUp• mousePressed • touchMoved• mouseReleased • touchDoubleTap • touchCancelledKey events Audio events• keyPressed • audioReceived• keyReleased • audioRequested
  20. 20. Common eventsApplication events Simple messages• setup • messageEvent• update• draw File drag events• exit • fileDragEvent• windowResized
  21. 21. Event argument objectsofKeyEventArgs ofAudioEventArgsint key float* buffer int bufferSizeofMouseEventArgs int nChannelsint xint y ofResizeEventArgsint button int width int height
  22. 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. 23. Custom eventsIn your header file (.h) create an extern event objectwhich will be used as a storage for listeners andwhich notifies them.“extern” tells your compiler the object is declaredsomewhere else. This will be done in your .cpp file.Also, define your custom event data type.
  24. 24. Custom eventsStep 1. create your custom event data typeclass 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. 25. Custom eventsStep 3. in your .cpp define the event dispatcher object.ofEvent<MyCustomEventData> myCustomEventDispatcher;To listen to this myCustomerEventDispatcher youcreate a event listener function and callofAddListener 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: " << << endl; }}
  26. 26.