123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- /****************************************************************************
- Copyright (c) 2013-2017 Chukong Technologies Inc.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "editor-support/cocostudio/CCInputDelegate.h"
- #include "base/CCDirector.h"
- #include "platform/CCDevice.h"
- #include "base/CCEventListenerTouch.h"
- #include "base/CCEventListenerAcceleration.h"
- #include "base/CCEventListenerKeyboard.h"
- #include "base/CCEventDispatcher.h"
- using namespace cocos2d;
- namespace cocostudio {
- InputDelegate::InputDelegate(void)
- : _touchEnabled(false)
- , _touchListener(nullptr)
- , _accelerometerEnabled(false)
- , _accelerometerListener(nullptr)
- , _keypadEnabled(false)
- , _keyboardListener(nullptr)
- , _touchPriority(-1)
- , _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
- {
- }
- InputDelegate::~InputDelegate(void)
- {
- auto dispatcher = Director::getInstance()->getEventDispatcher();
- dispatcher->removeEventListener(_touchListener);
- dispatcher->removeEventListener(_keyboardListener);
- dispatcher->removeEventListener(_accelerometerListener);
- Device::setAccelerometerEnabled(false);
- }
-
- void InputDelegate::didAccelerate(cocos2d::Acceleration* /*accelerationValue*/)
- {}
- bool InputDelegate::ccTouchBegan(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/)
- {
- return false;
- }
- void InputDelegate::ccTouchMoved(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/)
- {}
- void InputDelegate::ccTouchEnded(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/)
- {}
- void InputDelegate::ccTouchCancelled(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/)
- {}
- void InputDelegate::ccTouchesBegan(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/)
- {}
- void InputDelegate::ccTouchesMoved(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/)
- {}
- void InputDelegate::ccTouchesEnded(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/)
- {}
- void InputDelegate::ccTouchesCancelled(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/)
- {}
- void InputDelegate::onAcceleration(cocos2d::Acceleration* /*acc*/, cocos2d::Event* /*event*/)
- {}
- void InputDelegate::onKeyPressed(cocos2d::EventKeyboard::KeyCode /*keyCode*/, cocos2d::Event* /*event*/)
- {}
- void InputDelegate::onKeyReleased(cocos2d::EventKeyboard::KeyCode /*keyCode*/, cocos2d::Event* /*event*/)
- {}
-
- bool InputDelegate::onTouchBegan(Touch* /*pTouch*/, Event* /*pEvent*/)
- {
- return true;
- }
- void InputDelegate::onTouchMoved(Touch* /*pTouch*/, Event* /*pEvent*/)
- {
- }
-
- void InputDelegate::onTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/)
- {
- }
- void InputDelegate::onTouchCancelled(Touch* /*pTouch*/, Event* /*pEvent*/)
- {
- }
- void InputDelegate::onTouchesBegan(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/)
- {
- }
- void InputDelegate::onTouchesMoved(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/)
- {
- }
- void InputDelegate::onTouchesEnded(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/)
- {
- }
- void InputDelegate::onTouchesCancelled(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/)
- {
- }
- bool InputDelegate::isTouchEnabled() const
- {
- return _touchEnabled;
- }
- void InputDelegate::setTouchEnabled(bool enabled)
- {
- if (_touchEnabled != enabled)
- {
- auto dispatcher = Director::getInstance()->getEventDispatcher();
- _touchEnabled = enabled;
- if (enabled)
- {
- if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) {
- // Register Touch Event
- auto listener = EventListenerTouchAllAtOnce::create();
-
- listener->onTouchesBegan = CC_CALLBACK_2(InputDelegate::onTouchesBegan, this);
- listener->onTouchesMoved = CC_CALLBACK_2(InputDelegate::onTouchesMoved, this);
- listener->onTouchesEnded = CC_CALLBACK_2(InputDelegate::onTouchesEnded, this);
- listener->onTouchesCancelled = CC_CALLBACK_2(InputDelegate::onTouchesCancelled, this);
-
- dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority);
- _touchListener = listener;
- } else {
- // Register Touch Event
- auto listener = EventListenerTouchOneByOne::create();
- listener->setSwallowTouches(true);
-
- listener->onTouchBegan = CC_CALLBACK_2(InputDelegate::onTouchBegan, this);
- listener->onTouchMoved = CC_CALLBACK_2(InputDelegate::onTouchMoved, this);
- listener->onTouchEnded = CC_CALLBACK_2(InputDelegate::onTouchEnded, this);
- listener->onTouchCancelled = CC_CALLBACK_2(InputDelegate::onTouchCancelled, this);
-
- dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority);
- _touchListener = listener;
- }
- }
- else
- {
- dispatcher->removeEventListener(_touchListener);
- }
- }
- }
- void InputDelegate::setTouchMode(Touch::DispatchMode mode)
- {
- if(_touchMode != mode)
- {
- _touchMode = mode;
-
- if( _touchEnabled)
- {
- setTouchEnabled(false);
- setTouchEnabled(true);
- }
- }
- }
- void InputDelegate::setTouchPriority(int priority)
- {
- if (_touchPriority != priority)
- {
- _touchPriority = priority;
-
- if( _touchEnabled)
- {
- setTouchEnabled(false);
- setTouchEnabled(true);
- }
- }
- }
- int InputDelegate::getTouchPriority() const
- {
- return _touchPriority;
- }
- Touch::DispatchMode InputDelegate::getTouchMode() const
- {
- return _touchMode;
- }
- bool InputDelegate::isAccelerometerEnabled() const
- {
- return _accelerometerEnabled;
- }
- void InputDelegate::setAccelerometerEnabled(bool enabled)
- {
- if (enabled != _accelerometerEnabled)
- {
- _accelerometerEnabled = enabled;
- auto dispatcher = Director::getInstance()->getEventDispatcher();
- dispatcher->removeEventListener(_accelerometerListener);
- _accelerometerListener = nullptr;
-
- Device::setAccelerometerEnabled(enabled);
-
- if (enabled)
- {
- auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(InputDelegate::onAcceleration, this));
- dispatcher->addEventListenerWithFixedPriority(listener, -1);
- _accelerometerListener = listener;
- }
- }
- }
- bool InputDelegate::isKeypadEnabled() const
- {
- return _keypadEnabled;
- }
- void InputDelegate::setKeypadEnabled(bool enabled)
- {
- if (enabled != _keypadEnabled)
- {
- _keypadEnabled = enabled;
- auto dispatcher = Director::getInstance()->getEventDispatcher();
- dispatcher->removeEventListener(_keyboardListener);
-
- if (enabled)
- {
- auto listener = EventListenerKeyboard::create();
- listener->onKeyPressed = CC_CALLBACK_2(InputDelegate::onKeyPressed, this);
- listener->onKeyReleased = CC_CALLBACK_2(InputDelegate::onKeyReleased, this);
-
- dispatcher->addEventListenerWithFixedPriority(listener, -1);
- _keyboardListener = listener;
- }
- }
- }
- }
|