CCEventListenerTouch.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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__CCTouchEventListener__
  22. #define __cocos2d_libs__CCTouchEventListener__
  23. #include "base/CCEventListener.h"
  24. #include <vector>
  25. /**
  26. * @addtogroup base
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. class Touch;
  31. /** @class EventListenerTouchOneByOne
  32. * @brief Single touch event listener.
  33. * @js cc._EventListenerTouchOneByOne
  34. */
  35. class CC_DLL EventListenerTouchOneByOne : public EventListener
  36. {
  37. public:
  38. static const std::string LISTENER_ID;
  39. /** Create a one by one touch event listener.
  40. */
  41. static EventListenerTouchOneByOne* create();
  42. /**
  43. * Destructor.
  44. * @js NA
  45. */
  46. virtual ~EventListenerTouchOneByOne();
  47. /** Whether or not to swall touches.
  48. *
  49. * @param needSwallow True if needs to swall touches.
  50. */
  51. void setSwallowTouches(bool needSwallow);
  52. /** Is swall touches or not.
  53. *
  54. * @return True if needs to swall touches.
  55. */
  56. bool isSwallowTouches();
  57. /// Overrides
  58. virtual EventListenerTouchOneByOne* clone() override;
  59. virtual bool checkAvailable() override;
  60. //
  61. public:
  62. typedef std::function<bool(Touch*, Event*)> ccTouchBeganCallback;
  63. typedef std::function<void(Touch*, Event*)> ccTouchCallback;
  64. ccTouchBeganCallback onTouchBegan;
  65. ccTouchCallback onTouchMoved;
  66. ccTouchCallback onTouchEnded;
  67. ccTouchCallback onTouchCancelled;
  68. CC_CONSTRUCTOR_ACCESS:
  69. EventListenerTouchOneByOne();
  70. bool init();
  71. private:
  72. std::vector<Touch*> _claimedTouches;
  73. bool _needSwallow;
  74. friend class EventDispatcher;
  75. };
  76. /** @class EventListenerTouchAllAtOnce
  77. * @brief Multiple touches event listener.
  78. */
  79. class CC_DLL EventListenerTouchAllAtOnce : public EventListener
  80. {
  81. public:
  82. static const std::string LISTENER_ID;
  83. /** Create a all at once event listener.
  84. *
  85. * @return An autoreleased EventListenerTouchAllAtOnce object.
  86. */
  87. static EventListenerTouchAllAtOnce* create();
  88. /** Destructor.
  89. * @js NA
  90. */
  91. virtual ~EventListenerTouchAllAtOnce();
  92. /// Overrides
  93. virtual EventListenerTouchAllAtOnce* clone() override;
  94. virtual bool checkAvailable() override;
  95. //
  96. public:
  97. typedef std::function<void(const std::vector<Touch*>&, Event*)> ccTouchesCallback;
  98. ccTouchesCallback onTouchesBegan;
  99. ccTouchesCallback onTouchesMoved;
  100. ccTouchesCallback onTouchesEnded;
  101. ccTouchesCallback onTouchesCancelled;
  102. CC_CONSTRUCTOR_ACCESS:
  103. EventListenerTouchAllAtOnce();
  104. bool init();
  105. private:
  106. friend class EventDispatcher;
  107. };
  108. NS_CC_END
  109. // end of base group
  110. /// @}
  111. #endif /* defined(__cocos2d_libs__CCTouchEventListener__) */