CCEventListenerCustom.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __cocos2d_libs__CCCustomEventListener__
  22. #define __cocos2d_libs__CCCustomEventListener__
  23. #include "base/CCEventListener.h"
  24. /**
  25. * @addtogroup base
  26. * @{
  27. */
  28. NS_CC_BEGIN
  29. class EventCustom;
  30. /** @class EventListenerCustom
  31. * @brief Custom event listener.
  32. * @code Usage:
  33. * auto dispatcher = Director::getInstance()->getEventDispatcher();
  34. * Adds a listener:
  35. *
  36. * auto callback = [](EventCustom* event){ do_some_thing(); };
  37. * auto listener = EventListenerCustom::create(callback);
  38. * dispatcher->addEventListenerWithSceneGraphPriority(listener, one_node);
  39. *
  40. * Dispatches a custom event:
  41. *
  42. * EventCustom event("your_event_type");
  43. * dispatcher->dispatchEvent(&event);
  44. *
  45. * Removes a listener
  46. *
  47. * dispatcher->removeEventListener(listener);
  48. * \endcode
  49. * @js cc._EventListenerCustom
  50. */
  51. class CC_DLL EventListenerCustom : public EventListener
  52. {
  53. public:
  54. /** Creates an event listener with type and callback.
  55. * @param eventName The type of the event.
  56. * @param callback The callback function when the specified event was emitted.
  57. * @return An autoreleased EventListenerCustom object.
  58. */
  59. static EventListenerCustom* create(const std::string& eventName, const std::function<void(EventCustom*)>& callback);
  60. /// Overrides
  61. virtual bool checkAvailable() override;
  62. virtual EventListenerCustom* clone() override;
  63. CC_CONSTRUCTOR_ACCESS:
  64. /** Constructor */
  65. EventListenerCustom();
  66. /** Initializes event with type and callback function */
  67. bool init(const ListenerID& listenerId, const std::function<void(EventCustom*)>& callback);
  68. protected:
  69. std::function<void(EventCustom*)> _onCustomEvent;
  70. friend class LuaEventListenerCustom;
  71. };
  72. NS_CC_END
  73. // end of base group
  74. /// @}
  75. #endif /* defined(__cocos2d_libs__CCCustomEventListener__) */