1
0

CCInputDelegate.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/CCInputDelegate.h"
  21. #include "base/CCDirector.h"
  22. #include "platform/CCDevice.h"
  23. #include "base/CCEventListenerTouch.h"
  24. #include "base/CCEventListenerAcceleration.h"
  25. #include "base/CCEventListenerKeyboard.h"
  26. #include "base/CCEventDispatcher.h"
  27. using namespace cocos2d;
  28. namespace cocostudio {
  29. InputDelegate::InputDelegate(void)
  30. : _touchEnabled(false)
  31. , _touchListener(nullptr)
  32. , _accelerometerEnabled(false)
  33. , _accelerometerListener(nullptr)
  34. , _keypadEnabled(false)
  35. , _keyboardListener(nullptr)
  36. , _touchPriority(-1)
  37. , _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
  38. {
  39. }
  40. InputDelegate::~InputDelegate(void)
  41. {
  42. auto dispatcher = Director::getInstance()->getEventDispatcher();
  43. dispatcher->removeEventListener(_touchListener);
  44. dispatcher->removeEventListener(_keyboardListener);
  45. dispatcher->removeEventListener(_accelerometerListener);
  46. Device::setAccelerometerEnabled(false);
  47. }
  48. void InputDelegate::didAccelerate(cocos2d::Acceleration* /*accelerationValue*/)
  49. {}
  50. bool InputDelegate::ccTouchBegan(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/)
  51. {
  52. return false;
  53. }
  54. void InputDelegate::ccTouchMoved(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/)
  55. {}
  56. void InputDelegate::ccTouchEnded(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/)
  57. {}
  58. void InputDelegate::ccTouchCancelled(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/)
  59. {}
  60. void InputDelegate::ccTouchesBegan(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/)
  61. {}
  62. void InputDelegate::ccTouchesMoved(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/)
  63. {}
  64. void InputDelegate::ccTouchesEnded(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/)
  65. {}
  66. void InputDelegate::ccTouchesCancelled(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/)
  67. {}
  68. void InputDelegate::onAcceleration(cocos2d::Acceleration* /*acc*/, cocos2d::Event* /*event*/)
  69. {}
  70. void InputDelegate::onKeyPressed(cocos2d::EventKeyboard::KeyCode /*keyCode*/, cocos2d::Event* /*event*/)
  71. {}
  72. void InputDelegate::onKeyReleased(cocos2d::EventKeyboard::KeyCode /*keyCode*/, cocos2d::Event* /*event*/)
  73. {}
  74. bool InputDelegate::onTouchBegan(Touch* /*pTouch*/, Event* /*pEvent*/)
  75. {
  76. return true;
  77. }
  78. void InputDelegate::onTouchMoved(Touch* /*pTouch*/, Event* /*pEvent*/)
  79. {
  80. }
  81. void InputDelegate::onTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/)
  82. {
  83. }
  84. void InputDelegate::onTouchCancelled(Touch* /*pTouch*/, Event* /*pEvent*/)
  85. {
  86. }
  87. void InputDelegate::onTouchesBegan(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/)
  88. {
  89. }
  90. void InputDelegate::onTouchesMoved(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/)
  91. {
  92. }
  93. void InputDelegate::onTouchesEnded(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/)
  94. {
  95. }
  96. void InputDelegate::onTouchesCancelled(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/)
  97. {
  98. }
  99. bool InputDelegate::isTouchEnabled() const
  100. {
  101. return _touchEnabled;
  102. }
  103. void InputDelegate::setTouchEnabled(bool enabled)
  104. {
  105. if (_touchEnabled != enabled)
  106. {
  107. auto dispatcher = Director::getInstance()->getEventDispatcher();
  108. _touchEnabled = enabled;
  109. if (enabled)
  110. {
  111. if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) {
  112. // Register Touch Event
  113. auto listener = EventListenerTouchAllAtOnce::create();
  114. listener->onTouchesBegan = CC_CALLBACK_2(InputDelegate::onTouchesBegan, this);
  115. listener->onTouchesMoved = CC_CALLBACK_2(InputDelegate::onTouchesMoved, this);
  116. listener->onTouchesEnded = CC_CALLBACK_2(InputDelegate::onTouchesEnded, this);
  117. listener->onTouchesCancelled = CC_CALLBACK_2(InputDelegate::onTouchesCancelled, this);
  118. dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority);
  119. _touchListener = listener;
  120. } else {
  121. // Register Touch Event
  122. auto listener = EventListenerTouchOneByOne::create();
  123. listener->setSwallowTouches(true);
  124. listener->onTouchBegan = CC_CALLBACK_2(InputDelegate::onTouchBegan, this);
  125. listener->onTouchMoved = CC_CALLBACK_2(InputDelegate::onTouchMoved, this);
  126. listener->onTouchEnded = CC_CALLBACK_2(InputDelegate::onTouchEnded, this);
  127. listener->onTouchCancelled = CC_CALLBACK_2(InputDelegate::onTouchCancelled, this);
  128. dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority);
  129. _touchListener = listener;
  130. }
  131. }
  132. else
  133. {
  134. dispatcher->removeEventListener(_touchListener);
  135. }
  136. }
  137. }
  138. void InputDelegate::setTouchMode(Touch::DispatchMode mode)
  139. {
  140. if(_touchMode != mode)
  141. {
  142. _touchMode = mode;
  143. if( _touchEnabled)
  144. {
  145. setTouchEnabled(false);
  146. setTouchEnabled(true);
  147. }
  148. }
  149. }
  150. void InputDelegate::setTouchPriority(int priority)
  151. {
  152. if (_touchPriority != priority)
  153. {
  154. _touchPriority = priority;
  155. if( _touchEnabled)
  156. {
  157. setTouchEnabled(false);
  158. setTouchEnabled(true);
  159. }
  160. }
  161. }
  162. int InputDelegate::getTouchPriority() const
  163. {
  164. return _touchPriority;
  165. }
  166. Touch::DispatchMode InputDelegate::getTouchMode() const
  167. {
  168. return _touchMode;
  169. }
  170. bool InputDelegate::isAccelerometerEnabled() const
  171. {
  172. return _accelerometerEnabled;
  173. }
  174. void InputDelegate::setAccelerometerEnabled(bool enabled)
  175. {
  176. if (enabled != _accelerometerEnabled)
  177. {
  178. _accelerometerEnabled = enabled;
  179. auto dispatcher = Director::getInstance()->getEventDispatcher();
  180. dispatcher->removeEventListener(_accelerometerListener);
  181. _accelerometerListener = nullptr;
  182. Device::setAccelerometerEnabled(enabled);
  183. if (enabled)
  184. {
  185. auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(InputDelegate::onAcceleration, this));
  186. dispatcher->addEventListenerWithFixedPriority(listener, -1);
  187. _accelerometerListener = listener;
  188. }
  189. }
  190. }
  191. bool InputDelegate::isKeypadEnabled() const
  192. {
  193. return _keypadEnabled;
  194. }
  195. void InputDelegate::setKeypadEnabled(bool enabled)
  196. {
  197. if (enabled != _keypadEnabled)
  198. {
  199. _keypadEnabled = enabled;
  200. auto dispatcher = Director::getInstance()->getEventDispatcher();
  201. dispatcher->removeEventListener(_keyboardListener);
  202. if (enabled)
  203. {
  204. auto listener = EventListenerKeyboard::create();
  205. listener->onKeyPressed = CC_CALLBACK_2(InputDelegate::onKeyPressed, this);
  206. listener->onKeyReleased = CC_CALLBACK_2(InputDelegate::onKeyReleased, this);
  207. dispatcher->addEventListenerWithFixedPriority(listener, -1);
  208. _keyboardListener = listener;
  209. }
  210. }
  211. }
  212. }