2013-06-04 17:38:43 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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 "CCInputDelegate.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
InputDelegate::InputDelegate(void)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _touchEnabled(false)
|
2013-09-14 09:02:49 +08:00
|
|
|
, _touchListener(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _accelerometerEnabled(false)
|
2013-09-14 09:02:49 +08:00
|
|
|
, _accelerometerListener(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _keypadEnabled(false)
|
2013-09-14 09:02:49 +08:00
|
|
|
, _keyboardListener(nullptr)
|
2013-09-19 09:24:11 +08:00
|
|
|
, _touchPriority(-1)
|
2013-07-26 07:27:24 +08:00
|
|
|
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
InputDelegate::~InputDelegate(void)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
auto dispatcher = EventDispatcher::getInstance();
|
2013-09-14 09:02:49 +08:00
|
|
|
dispatcher->removeEventListener(_touchListener);
|
|
|
|
dispatcher->removeEventListener(_keyboardListener);
|
|
|
|
dispatcher->removeEventListener(_accelerometerListener);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool InputDelegate::onTouchBegan(Touch *pTouch, Event *pEvent)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pTouch);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void InputDelegate::onTouchMoved(Touch *pTouch, Event *pEvent)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pTouch);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void InputDelegate::onTouchEnded(Touch *pTouch, Event *pEvent)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pTouch);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void InputDelegate::onTouchCancelled(Touch *pTouch, Event *pEvent)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pTouch);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void InputDelegate::onTouchesBegan(const std::vector<Touch*>& pTouches, Event *pEvent)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pTouches);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void InputDelegate::onTouchesMoved(const std::vector<Touch*>& pTouches, Event *pEvent)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pTouches);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void InputDelegate::onTouchesEnded(const std::vector<Touch*>& pTouches, Event *pEvent)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pTouches);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void InputDelegate::onTouchesCancelled(const std::vector<Touch*>& pTouches, Event *pEvent)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pTouches);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
bool InputDelegate::isTouchEnabled() const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _touchEnabled;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void InputDelegate::setTouchEnabled(bool enabled)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchEnabled != enabled)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchEnabled = enabled;
|
2013-06-04 17:38:43 +08:00
|
|
|
if (enabled)
|
2013-09-14 09:02:49 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE ) {
|
|
|
|
// Register Touch Event
|
2013-09-20 19:19:31 +08:00
|
|
|
auto listener = EventListenerTouch::create(Touch::DispatchMode::ALL_AT_ONCE);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-09-19 09:19:03 +08:00
|
|
|
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, _touchPriority);
|
2013-09-14 09:02:49 +08:00
|
|
|
_touchListener = listener;
|
2013-09-03 18:22:03 +08:00
|
|
|
} else {
|
|
|
|
// Register Touch Event
|
2013-09-20 19:19:31 +08:00
|
|
|
auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE);
|
2013-09-03 18:22:03 +08:00
|
|
|
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);
|
|
|
|
|
2013-09-14 09:02:49 +08:00
|
|
|
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, _touchPriority);
|
|
|
|
_touchListener = listener;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-14 09:02:49 +08:00
|
|
|
EventDispatcher::getInstance()->removeEventListener(_touchListener);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
void InputDelegate::setTouchMode(Touch::DispatchMode mode)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_touchMode != mode)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchMode = mode;
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _touchEnabled)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void InputDelegate::setTouchPriority(int priority)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchPriority != priority)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchPriority = priority;
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _touchEnabled)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
int InputDelegate::getTouchPriority() const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _touchPriority;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
Touch::DispatchMode InputDelegate::getTouchMode() const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _touchMode;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
bool InputDelegate::isAccelerometerEnabled() const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _accelerometerEnabled;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void InputDelegate::setAccelerometerEnabled(bool enabled)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (enabled != _accelerometerEnabled)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_accelerometerEnabled = enabled;
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
auto dispatcher = EventDispatcher::getInstance();
|
2013-09-14 09:02:49 +08:00
|
|
|
dispatcher->removeEventListener(_accelerometerListener);
|
|
|
|
_accelerometerListener = nullptr;
|
|
|
|
|
2013-06-04 17:38:43 +08:00
|
|
|
if (enabled)
|
|
|
|
{
|
2013-09-20 19:19:31 +08:00
|
|
|
auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(InputDelegate::onAcceleration, this));
|
2013-09-19 09:19:03 +08:00
|
|
|
dispatcher->addEventListenerWithFixedPriority(listener, -1);
|
2013-09-14 09:02:49 +08:00
|
|
|
_accelerometerListener = listener;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
bool InputDelegate::isKeypadEnabled() const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _keypadEnabled;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void InputDelegate::setKeypadEnabled(bool enabled)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (enabled != _keypadEnabled)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_keypadEnabled = enabled;
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-09-14 09:02:49 +08:00
|
|
|
EventDispatcher::getInstance()->removeEventListener(_keyboardListener);
|
|
|
|
|
2013-06-04 17:38:43 +08:00
|
|
|
if (enabled)
|
|
|
|
{
|
2013-09-20 19:19:31 +08:00
|
|
|
auto listener = EventListenerKeyboard::create();
|
2013-09-03 18:22:03 +08:00
|
|
|
listener->onKeyPressed = CC_CALLBACK_2(InputDelegate::onKeyPressed, this);
|
|
|
|
listener->onKeyReleased = CC_CALLBACK_2(InputDelegate::onKeyReleased, this);
|
|
|
|
|
2013-09-19 09:19:03 +08:00
|
|
|
EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, -1);
|
2013-09-14 09:02:49 +08:00
|
|
|
_keyboardListener = listener;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|