SlideShare a Scribd company logo
1 of 26
openFrameworks
     events
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)
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.
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)
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.
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
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;
      
 
      }
      }
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.
 
 );
 }
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
            
 );
            }
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()
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)
Event listening
class 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) {

 }

};
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)
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)
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)
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!
Simple messaging


 void testApp::setup(){
 
 ofSendMessage("ready");
 }

 void testApp::gotMessage(ofMessage msg){
 
 cout << "got message: " << msg.message << endl;
 }
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;
              
 }
              }
Common events
Mouse events      Touch events

• mouseMoved      • touchDown
• mouseDragged    • touchUp
• mousePressed    • touchMoved
• mouseReleased   • touchDoubleTap
                  • touchCancelled
Key events        Audio events

• keyPressed      • audioReceived
• keyReleased     • audioRequested
Common events

Application events   Simple messages

• setup              • messageEvent
• update
• draw               File drag events

• exit               • fileDragEvent
• windowResized
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
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
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.
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
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;

 
      }
}
roxlu
www.roxlu.com

More Related Content

What's hot

オブジェクト指向できていますか?
オブジェクト指向できていますか?オブジェクト指向できていますか?
オブジェクト指向できていますか?
Moriharu Ohzu
 
Rにおける大規模データ解析(第10回TokyoWebMining)
Rにおける大規模データ解析(第10回TokyoWebMining)Rにおける大規模データ解析(第10回TokyoWebMining)
Rにおける大規模データ解析(第10回TokyoWebMining)
Shintaro Fukushima
 
今さら聞けないカーネル法とサポートベクターマシン
今さら聞けないカーネル法とサポートベクターマシン今さら聞けないカーネル法とサポートベクターマシン
今さら聞けないカーネル法とサポートベクターマシン
Shinya Shimizu
 
異常行動検出入門(改)
異常行動検出入門(改)異常行動検出入門(改)
異常行動検出入門(改)
Yohei Sato
 

What's hot (20)

Control as Inference (強化学習とベイズ統計)
Control as Inference (強化学習とベイズ統計)Control as Inference (強化学習とベイズ統計)
Control as Inference (強化学習とベイズ統計)
 
2023-03-23_Spiral.AI
2023-03-23_Spiral.AI2023-03-23_Spiral.AI
2023-03-23_Spiral.AI
 
「機械学習:技術的負債の高利子クレジットカード」のまとめ
「機械学習:技術的負債の高利子クレジットカード」のまとめ「機械学習:技術的負債の高利子クレジットカード」のまとめ
「機械学習:技術的負債の高利子クレジットカード」のまとめ
 
高速な倍精度指数関数expの実装
高速な倍精度指数関数expの実装高速な倍精度指数関数expの実装
高速な倍精度指数関数expの実装
 
オブジェクト指向できていますか?
オブジェクト指向できていますか?オブジェクト指向できていますか?
オブジェクト指向できていますか?
 
Rにおける大規模データ解析(第10回TokyoWebMining)
Rにおける大規模データ解析(第10回TokyoWebMining)Rにおける大規模データ解析(第10回TokyoWebMining)
Rにおける大規模データ解析(第10回TokyoWebMining)
 
今さら聞けないカーネル法とサポートベクターマシン
今さら聞けないカーネル法とサポートベクターマシン今さら聞けないカーネル法とサポートベクターマシン
今さら聞けないカーネル法とサポートベクターマシン
 
【DL輪読会】Toolformer: Language Models Can Teach Themselves to Use Tools
【DL輪読会】Toolformer: Language Models Can Teach Themselves to Use Tools【DL輪読会】Toolformer: Language Models Can Teach Themselves to Use Tools
【DL輪読会】Toolformer: Language Models Can Teach Themselves to Use Tools
 
[DL輪読会]VoxelPose: Towards Multi-Camera 3D Human Pose Estimation in Wild Envir...
[DL輪読会]VoxelPose: Towards Multi-Camera 3D Human Pose Estimation in Wild Envir...[DL輪読会]VoxelPose: Towards Multi-Camera 3D Human Pose Estimation in Wild Envir...
[DL輪読会]VoxelPose: Towards Multi-Camera 3D Human Pose Estimation in Wild Envir...
 
時系列予測にTransformerを使うのは有効か?
時系列予測にTransformerを使うのは有効か?時系列予測にTransformerを使うのは有効か?
時系列予測にTransformerを使うのは有効か?
 
機械学習による統計的実験計画(ベイズ最適化を中心に)
機械学習による統計的実験計画(ベイズ最適化を中心に)機械学習による統計的実験計画(ベイズ最適化を中心に)
機械学習による統計的実験計画(ベイズ最適化を中心に)
 
Superpixel Sampling Networks
Superpixel Sampling NetworksSuperpixel Sampling Networks
Superpixel Sampling Networks
 
OSS強化学習向けゲーム環境の動向
OSS強化学習向けゲーム環境の動向OSS強化学習向けゲーム環境の動向
OSS強化学習向けゲーム環境の動向
 
MIRU2013チュートリアル:SIFTとそれ以降のアプローチ
MIRU2013チュートリアル:SIFTとそれ以降のアプローチMIRU2013チュートリアル:SIFTとそれ以降のアプローチ
MIRU2013チュートリアル:SIFTとそれ以降のアプローチ
 
ガウス過程入門
ガウス過程入門ガウス過程入門
ガウス過程入門
 
【DL輪読会】Reward Design with Language Models
【DL輪読会】Reward Design with Language Models【DL輪読会】Reward Design with Language Models
【DL輪読会】Reward Design with Language Models
 
[GTCJ2018]CuPy -NumPy互換GPUライブラリによるPythonでの高速計算- PFN奥田遼介
[GTCJ2018]CuPy -NumPy互換GPUライブラリによるPythonでの高速計算- PFN奥田遼介[GTCJ2018]CuPy -NumPy互換GPUライブラリによるPythonでの高速計算- PFN奥田遼介
[GTCJ2018]CuPy -NumPy互換GPUライブラリによるPythonでの高速計算- PFN奥田遼介
 
【Unite Tokyo 2019】Understanding C# Struct All Things
【Unite Tokyo 2019】Understanding C# Struct All Things【Unite Tokyo 2019】Understanding C# Struct All Things
【Unite Tokyo 2019】Understanding C# Struct All Things
 
異常行動検出入門(改)
異常行動検出入門(改)異常行動検出入門(改)
異常行動検出入門(改)
 
時系列問題に対するCNNの有用性検証
時系列問題に対するCNNの有用性検証時系列問題に対するCNNの有用性検証
時系列問題に対するCNNの有用性検証
 

Viewers also liked (10)

openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - video
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utils
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphics
 
openFrameworks 007 - GL
openFrameworks 007 - GL openFrameworks 007 - GL
openFrameworks 007 - GL
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3D
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013
 
Computer vision techniques for interactive art
Computer vision techniques for interactive artComputer vision techniques for interactive art
Computer vision techniques for interactive art
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitc
 
openFrameworks 007 - sound
openFrameworks 007 - soundopenFrameworks 007 - sound
openFrameworks 007 - sound
 
Media Art II 2013 第5回:openFrameworks Addonを使用する
Media Art II 2013 第5回:openFrameworks Addonを使用するMedia Art II 2013 第5回:openFrameworks Addonを使用する
Media Art II 2013 第5回:openFrameworks Addonを使用する
 

Similar to openFrameworks 007 - events

Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
sotlsoc
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
PRN USM
 

Similar to openFrameworks 007 - events (20)

Event handling
Event handlingEvent handling
Event handling
 
Event handling
Event handlingEvent handling
Event handling
 
Java gui event
Java gui eventJava gui event
Java gui event
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
What is Event
What is EventWhat is Event
What is Event
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
Java-Events
Java-EventsJava-Events
Java-Events
 
Events1
Events1Events1
Events1
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 
File Handling
File HandlingFile Handling
File Handling
 
Androd Listeners
Androd ListenersAndrod Listeners
Androd Listeners
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

openFrameworks 007 - events

  • 1. openFrameworks events
  • 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)
  • 12. Event listening class 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. 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!
  • 17. Simple messaging void testApp::setup(){ ofSendMessage("ready"); } void testApp::gotMessage(ofMessage msg){ cout << "got message: " << msg.message << endl; }
  • 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; } }
  • 19. Common events Mouse events Touch events • mouseMoved • touchDown • mouseDragged • touchUp • mousePressed • touchMoved • mouseReleased • touchDoubleTap • touchCancelled Key events Audio events • keyPressed • audioReceived • keyReleased • audioRequested
  • 20. Common events Application events Simple messages • setup • messageEvent • update • draw File drag events • exit • fileDragEvent • windowResized
  • 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; } }

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n