2013-09-03 18:22:03 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-21 14:40:54 +08:00
|
|
|
#include "CCEventListenerTouch.h"
|
2013-09-03 18:22:03 +08:00
|
|
|
#include "CCEventDispatcher.h"
|
2013-09-21 14:40:54 +08:00
|
|
|
#include "CCEventTouch.h"
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-12 17:31:37 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
EventListenerTouch::EventListenerTouch()
|
2013-09-13 18:00:56 +08:00
|
|
|
: onTouchBegan(nullptr)
|
2013-09-03 18:22:03 +08:00
|
|
|
, onTouchMoved(nullptr)
|
|
|
|
, onTouchEnded(nullptr)
|
|
|
|
, onTouchCancelled(nullptr)
|
|
|
|
, onTouchesBegan(nullptr)
|
|
|
|
, onTouchesMoved(nullptr)
|
|
|
|
, onTouchesEnded(nullptr)
|
|
|
|
, onTouchesCancelled(nullptr)
|
|
|
|
, _needSwallow(false)
|
2013-09-13 18:00:56 +08:00
|
|
|
, _dispatchMode(Touch::DispatchMode::ALL_AT_ONCE)
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
EventListenerTouch::~EventListenerTouch()
|
2013-09-13 18:00:56 +08:00
|
|
|
{
|
|
|
|
CCLOGINFO("In the destructor of TouchEventListener, %p", this);
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
bool EventListenerTouch::init(Touch::DispatchMode mode)
|
2013-09-13 18:00:56 +08:00
|
|
|
{
|
2013-09-20 19:19:31 +08:00
|
|
|
if (EventListener::init(EventTouch::EVENT_TYPE, nullptr))
|
2013-09-13 18:00:56 +08:00
|
|
|
{
|
|
|
|
_dispatchMode = mode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
void EventListenerTouch::setSwallowTouches(bool needSwallow)
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
|
|
|
CCASSERT(_dispatchMode == Touch::DispatchMode::ONE_BY_ONE, "Swallow touches only available in OneByOne mode.");
|
|
|
|
_needSwallow = needSwallow;
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
EventListenerTouch* EventListenerTouch::create(Touch::DispatchMode mode)
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
2013-09-20 19:19:31 +08:00
|
|
|
auto ret = new EventListenerTouch();
|
2013-09-13 18:00:56 +08:00
|
|
|
if (ret && ret->init(mode))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
bool EventListenerTouch::checkAvaiable()
|
2013-09-03 18:22:03 +08:00
|
|
|
{
|
|
|
|
if (_dispatchMode == Touch::DispatchMode::ALL_AT_ONCE)
|
|
|
|
{
|
|
|
|
if (onTouchesBegan == nullptr && onTouchesMoved == nullptr
|
|
|
|
&& onTouchesEnded == nullptr && onTouchesCancelled == nullptr)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "Invalid TouchEventListener.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_dispatchMode == Touch::DispatchMode::ONE_BY_ONE)
|
|
|
|
{
|
|
|
|
if (onTouchBegan == nullptr && onTouchMoved == nullptr
|
|
|
|
&& onTouchEnded == nullptr && onTouchCancelled == nullptr)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "Invalid TouchEventListener.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
EventListenerTouch* EventListenerTouch::clone()
|
2013-09-13 16:36:25 +08:00
|
|
|
{
|
2013-09-20 19:19:31 +08:00
|
|
|
auto ret = new EventListenerTouch();
|
2013-09-13 18:00:56 +08:00
|
|
|
if (ret && ret->init(_dispatchMode))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
|
|
|
|
ret->onTouchBegan = onTouchBegan;
|
|
|
|
ret->onTouchMoved = onTouchMoved;
|
|
|
|
ret->onTouchEnded = onTouchEnded;
|
|
|
|
ret->onTouchCancelled = onTouchCancelled;
|
|
|
|
ret->onTouchesBegan = onTouchesBegan;
|
|
|
|
ret->onTouchesMoved = onTouchesMoved;
|
|
|
|
ret->onTouchesEnded = onTouchesEnded;
|
|
|
|
ret->onTouchesCancelled = onTouchesCancelled;
|
|
|
|
|
|
|
|
ret->_claimedTouches = _claimedTouches;
|
|
|
|
ret->_dispatchMode = _dispatchMode;
|
|
|
|
ret->_needSwallow = _needSwallow;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
2013-09-13 16:36:25 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
NS_CC_END
|