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

Weight & balance control
Weight & balance controlWeight & balance control
Weight & balance controlMohamed Tayfour
 
סיפור הקופים פרדיגמות
סיפור הקופים פרדיגמותסיפור הקופים פרדיגמות
סיפור הקופים פרדיגמותaokrat
 
Preliminary Design of 100 Seater Aircraft
Preliminary Design of 100 Seater AircraftPreliminary Design of 100 Seater Aircraft
Preliminary Design of 100 Seater AircraftROSHAN SAH
 
History of Aviation by Robert Andrei
History of Aviation by Robert Andrei History of Aviation by Robert Andrei
History of Aviation by Robert Andrei andreimonica76
 
Презентація на тему: ПАТ "Укрнафта"
Презентація на тему: ПАТ "Укрнафта"Презентація на тему: ПАТ "Укрнафта"
Презентація на тему: ПАТ "Укрнафта"Roman "Seth" Nazarchuk
 
ICAO Rules and Regulations in Airline Industry And ICAO Annexes
ICAO Rules and Regulations in Airline Industry And ICAO AnnexesICAO Rules and Regulations in Airline Industry And ICAO Annexes
ICAO Rules and Regulations in Airline Industry And ICAO AnnexesJetline Marvel
 
Aircraft Engine
Aircraft Engine Aircraft Engine
Aircraft Engine tamer999
 

What's hot (8)

Weight & balance control
Weight & balance controlWeight & balance control
Weight & balance control
 
סיפור הקופים פרדיגמות
סיפור הקופים פרדיגמותסיפור הקופים פרדיגמות
סיפור הקופים פרדיגמות
 
Preliminary Design of 100 Seater Aircraft
Preliminary Design of 100 Seater AircraftPreliminary Design of 100 Seater Aircraft
Preliminary Design of 100 Seater Aircraft
 
Documento_Diaz_de_Argandona_Ignacio
Documento_Diaz_de_Argandona_IgnacioDocumento_Diaz_de_Argandona_Ignacio
Documento_Diaz_de_Argandona_Ignacio
 
History of Aviation by Robert Andrei
History of Aviation by Robert Andrei History of Aviation by Robert Andrei
History of Aviation by Robert Andrei
 
Презентація на тему: ПАТ "Укрнафта"
Презентація на тему: ПАТ "Укрнафта"Презентація на тему: ПАТ "Укрнафта"
Презентація на тему: ПАТ "Укрнафта"
 
ICAO Rules and Regulations in Airline Industry And ICAO Annexes
ICAO Rules and Regulations in Airline Industry And ICAO AnnexesICAO Rules and Regulations in Airline Industry And ICAO Annexes
ICAO Rules and Regulations in Airline Industry And ICAO Annexes
 
Aircraft Engine
Aircraft Engine Aircraft Engine
Aircraft Engine
 

Viewers also liked

openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - videoroxlu
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utilsroxlu
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphicsroxlu
 
openFrameworks 007 - GL
openFrameworks 007 - GL openFrameworks 007 - GL
openFrameworks 007 - GL roxlu
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3Droxlu
 
Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013Dynamic C++ ACCU 2013
Dynamic C++ ACCU 2013aleks-f
 
Computer vision techniques for interactive art
Computer vision techniques for interactive artComputer vision techniques for interactive art
Computer vision techniques for interactive artJorge Cardoso
 
Open frameworks 101_fitc
Open frameworks 101_fitcOpen frameworks 101_fitc
Open frameworks 101_fitcbenDesigning
 
openFrameworks 007 - sound
openFrameworks 007 - soundopenFrameworks 007 - sound
openFrameworks 007 - soundroxlu
 
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を使用するAtsushi Tadokoro
 

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

Event handling
Event handlingEvent handling
Event handlingswapnac12
 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handlingteach4uin
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handlingAmol Gaikwad
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxGood657694
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Androidgreenrobot
 
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)jammiashok123
 
File Handling
File HandlingFile Handling
File HandlingSohanur63
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2PRN USM
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVASrajan Shukla
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events WebStackAcademy
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 appletsraksharao
 

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

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 

Recently uploaded (20)

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 

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