fbxeventhandler.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /****************************************************************************************
  2. Copyright (C) 2015 Autodesk, Inc.
  3. All rights reserved.
  4. Use of this software is subject to the terms of the Autodesk license agreement
  5. provided at the time of installation or download, or which otherwise accompanies
  6. this software in either electronic or hard copy form.
  7. ****************************************************************************************/
  8. //! \file fbxeventhandler.h
  9. #ifndef _FBXSDK_CORE_EVENT_HANDLER_H_
  10. #define _FBXSDK_CORE_EVENT_HANDLER_H_
  11. #include <fbxsdk/fbxsdk_def.h>
  12. #include <fbxsdk/core/fbxevent.h>
  13. #include <fbxsdk/core/base/fbxintrusivelist.h>
  14. #include <fbxsdk/fbxsdk_nsbegin.h>
  15. class FbxListener;
  16. /** Event handler class contains a listener and a callback function.
  17. * Event handler is used to bind emitter and listener together. Its callback function can process event data.
  18. * To generate a valid event handler, you can create an event emitter and event listener first and then call FbxListener::Bind().
  19. * It will create an event handler automatically and bind the handler to the listener and the created emitter.
  20. * After that, the emitter and listener are bound together via event handler.
  21. * \remarks An object(emitter) can emit a certain type of event, the object(listener) who are listening to that type of event,
  22. * will receive a signal and take action to process the event data.
  23. * \par The whole process of event is:
  24. * \li 1. Create an emitter and a listener, then bind them together via the same event handler.
  25. * \li 2. Emitter can emit an event at certain conditions. The event could be handled by event handler.
  26. * \li 3. Once an event is emitted, the listener to this event will receive a signal.
  27. * \li 4. And then the listener could process the event data according to the types of event, by calling event handler.
  28. * \note The event data is process by the callback function of event handler.
  29. * \nosubgrouping
  30. * \see FbxListener FbxEventBase FbxEvent FbxEmitter
  31. */
  32. class FbxEventHandler
  33. {
  34. public:
  35. //! Event handler base type.
  36. enum EType
  37. {
  38. eListener, //!< Listener event handler type.
  39. eEmitter, //!< Emitter event handler type.
  40. eCount //!< Count of different event handler types.
  41. };
  42. /** Get event type of current handler.
  43. * \return The type ID of event. */
  44. virtual int GetHandlerEventType()=0;
  45. /** Call function that process event data.
  46. * \param pEvent specify the event type. pEvent could be a specific class which derived from FbxEventBase.
  47. * \see FbxEventBase */
  48. virtual void FunctionCall(const FbxEventBase& pEvent)=0;
  49. /** Get listener of current handler.
  50. * \return A pointer to the listener object. */
  51. virtual FbxListener* GetListener()=0;
  52. /*****************************************************************************************************************************
  53. ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
  54. *****************************************************************************************************************************/
  55. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  56. FbxEventHandler(){}
  57. virtual ~FbxEventHandler(){}
  58. FBXSDK_INTRUSIVE_LIST_NODE(FbxEventHandler, eCount);
  59. #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
  60. };
  61. /*****************************************************************************************************************************
  62. ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
  63. *****************************************************************************************************************************/
  64. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  65. template <typename EventType, typename ListenerType> class FbxMemberFuncEventHandler : public FbxEventHandler
  66. {
  67. typedef void (ListenerType::*CallbackFnc)(const EventType*);
  68. public:
  69. FbxMemberFuncEventHandler(ListenerType* pListenerInstance, CallbackFnc pFunction) : mListener(pListenerInstance), mFunction(pFunction){}
  70. virtual int GetHandlerEventType(){ return EventType::GetStaticTypeId(); }
  71. virtual void FunctionCall(const FbxEventBase& pEvent){ (*mListener.*mFunction)(reinterpret_cast<const EventType*>(&pEvent)); }
  72. virtual FbxListener* GetListener(){ return mListener; }
  73. private:
  74. ListenerType* mListener;
  75. CallbackFnc mFunction;
  76. };
  77. template <typename EventType, typename ListenerType> class FbxConstMemberFuncEventHandler : public FbxEventHandler
  78. {
  79. typedef void (ListenerType::*CallbackFnc)(const EventType*) const;
  80. public:
  81. FbxConstMemberFuncEventHandler(ListenerType* pListenerInstance, CallbackFnc pFunction) : mListener(pListenerInstance), mFunction(pFunction){}
  82. virtual int GetHandlerEventType(){ return EventType::GetStaticTypeId(); }
  83. virtual void FunctionCall(const FbxEventBase& pEvent){ (*mListener.*mFunction)(reinterpret_cast<const EventType*>(&pEvent)); }
  84. virtual FbxListener* GetListener(){ return mListener; }
  85. private:
  86. ListenerType* mListener;
  87. CallbackFnc mFunction;
  88. };
  89. template <typename EventType> class FbxFuncEventHandler : public FbxEventHandler
  90. {
  91. typedef void (*CallbackFnc)(const EventType*, FbxListener*);
  92. public:
  93. FbxFuncEventHandler(FbxListener* pListener, CallbackFnc pFunction) : mListener(pListener), mFunction(pFunction){}
  94. virtual int GetHandlerEventType(){ return EventType::GetStaticTypeId(); }
  95. virtual void FunctionCall(const FbxEventBase& pEvent){ (*mFunction)(reinterpret_cast<const EventType*>(&pEvent), mListener); }
  96. virtual FbxListener* GetListener(){ return mListener; }
  97. private:
  98. FbxListener* mListener;
  99. CallbackFnc mFunction;
  100. };
  101. #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
  102. #include <fbxsdk/fbxsdk_nsend.h>
  103. #endif /* _FBXSDK_CORE_EVENT_HANDLER_H_ */