2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
2020-10-17 16:32:16 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CCInputDelegate.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#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;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
InputDelegate::InputDelegate(void)
|
|
|
|
: _touchEnabled(false)
|
|
|
|
, _touchListener(nullptr)
|
|
|
|
, _accelerometerEnabled(false)
|
|
|
|
, _accelerometerListener(nullptr)
|
|
|
|
, _keypadEnabled(false)
|
|
|
|
, _keyboardListener(nullptr)
|
|
|
|
, _touchPriority(-1)
|
|
|
|
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-10-17 16:32:16 +08:00
|
|
|
InputDelegate::~InputDelegate(void)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->removeEventListener(_touchListener);
|
|
|
|
dispatcher->removeEventListener(_keyboardListener);
|
|
|
|
dispatcher->removeEventListener(_accelerometerListener);
|
|
|
|
Device::setAccelerometerEnabled(false);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void InputDelegate::didAccelerate(cocos2d::Acceleration* /*accelerationValue*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool InputDelegate::ccTouchBegan(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::ccTouchMoved(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::ccTouchEnded(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::ccTouchCancelled(cocos2d::Touch* /*touch*/, cocos2d::Event* /*event*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::ccTouchesBegan(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::ccTouchesMoved(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::ccTouchesEnded(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::ccTouchesCancelled(cocos2d::__Set* /*touches*/, cocos2d::Event* /*event*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::onAcceleration(cocos2d::Acceleration* /*acc*/, cocos2d::Event* /*event*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::onKeyPressed(cocos2d::EventKeyboard::KeyCode /*keyCode*/, cocos2d::Event* /*event*/) {}
|
|
|
|
|
|
|
|
void InputDelegate::onKeyReleased(cocos2d::EventKeyboard::KeyCode /*keyCode*/, cocos2d::Event* /*event*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool InputDelegate::onTouchBegan(Touch* /*pTouch*/, Event* /*pEvent*/)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::onTouchMoved(Touch* /*pTouch*/, Event* /*pEvent*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::onTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::onTouchCancelled(Touch* /*pTouch*/, Event* /*pEvent*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::onTouchesBegan(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::onTouchesMoved(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void InputDelegate::onTouchesEnded(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/) {}
|
|
|
|
|
|
|
|
void InputDelegate::onTouchesCancelled(const std::vector<Touch*>& /*pTouches*/, Event* /*pEvent*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool InputDelegate::isTouchEnabled() const
|
|
|
|
{
|
|
|
|
return _touchEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDelegate::setTouchEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (_touchEnabled != enabled)
|
|
|
|
{
|
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
2021-12-25 10:04:45 +08:00
|
|
|
_touchEnabled = enabled;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (enabled)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (_touchMode == Touch::DispatchMode::ALL_AT_ONCE)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// Register Touch Event
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2021-12-25 10:04:45 +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);
|
2019-11-23 20:27:39 +08:00
|
|
|
listener->onTouchesCancelled = CC_CALLBACK_2(InputDelegate::onTouchesCancelled, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority);
|
|
|
|
_touchListener = listener;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// Register Touch Event
|
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->setSwallowTouches(true);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(InputDelegate::onTouchBegan, this);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(InputDelegate::onTouchMoved, this);
|
|
|
|
listener->onTouchEnded = CC_CALLBACK_2(InputDelegate::onTouchEnded, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
listener->onTouchCancelled = CC_CALLBACK_2(InputDelegate::onTouchCancelled, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
dispatcher->addEventListenerWithFixedPriority(listener, _touchPriority);
|
|
|
|
_touchListener = listener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dispatcher->removeEventListener(_touchListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDelegate::setTouchMode(Touch::DispatchMode mode)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_touchMode != mode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_touchMode = mode;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_touchEnabled)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDelegate::setTouchPriority(int priority)
|
|
|
|
{
|
|
|
|
if (_touchPriority != priority)
|
|
|
|
{
|
|
|
|
_touchPriority = priority;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_touchEnabled)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Device::setAccelerometerEnabled(enabled);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (enabled)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto listener = EventListenerKeyboard::create();
|
|
|
|
listener->onKeyPressed = CC_CALLBACK_2(InputDelegate::onKeyPressed, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
listener->onKeyReleased = CC_CALLBACK_2(InputDelegate::onKeyReleased, this);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
dispatcher->addEventListenerWithFixedPriority(listener, -1);
|
|
|
|
_keyboardListener = listener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|