Changing Touch API in cocos2d-x. Also change ScriptEngine callbacks for targetted Touches

This commit is contained in:
Rohan Kuruvilla 2012-10-26 17:11:43 -07:00 committed by James Chen
parent 16d5659b17
commit a2ee6c3761
2 changed files with 110 additions and 19 deletions

View File

@ -50,6 +50,8 @@ CCLayer::CCLayer()
{
setAnchorPoint(ccp(0.5f, 0.5f));
m_bIgnoreAnchorPointForPosition = true;
m_bTouchMode = kCCTouchesAllAtOnce;
m_bTouchPriority = 0;
}
CCLayer::~CCLayer()
@ -61,7 +63,7 @@ bool CCLayer::init()
{
bool bRet = false;
do
{
{
CCDirector * pDirector;
CC_BREAK_IF(!(pDirector = CCDirector::sharedDirector()));
this->setContentSize(pDirector->getWinSize());
@ -99,24 +101,36 @@ void CCLayer::registerWithTouchDispatcher()
{
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
if (m_pScriptHandlerEntry)
{
if (m_pScriptHandlerEntry->isMultiTouches())
{
pDispatcher->addStandardDelegate(this, 0);
LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptHandlerEntry->getHandler());
}
else
{
pDispatcher->addTargetedDelegate(this,
m_pScriptHandlerEntry->getPriority(),
m_pScriptHandlerEntry->getSwallowsTouches());
LUALOG("[LUA] Add touch event handler: %d", m_pScriptHandlerEntry->getHandler());
}
return;
}
if (m_pScriptHandlerEntry)
{
CCScriptEngineProtocol* pEngine = CCScriptEngineManager::sharedManager()->getScriptEngine();
if(pEngine->getScriptType() == kScriptTypeJavascript) {
pDispatcher->addStandardDelegate(this, 0);
if( m_bTouchMode == kCCTouchesAllAtOnce ) {
pDispatcher->addStandardDelegate(this, 0);
} else {
pDispatcher->addTargetedDelegate(this, m_bTouchPriority, true);
}
return;
} else {
if (m_pScriptHandlerEntry->isMultiTouches())
{
pDispatcher->addStandardDelegate(this, 0);
LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptHandlerEntry->getHandler());
}
else
{
pDispatcher->addTargetedDelegate(this,
m_pScriptHandlerEntry->getPriority(),
m_pScriptHandlerEntry->getSwallowsTouches());
LUALOG("[LUA] Add touch event handler: %d", m_pScriptHandlerEntry->getHandler());
}
return;
}
}
pDispatcher->addStandardDelegate(this, 0);
}
void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches)
@ -167,6 +181,40 @@ void CCLayer::setTouchEnabled(bool enabled)
}
}
void CCLayer::setTouchMode(ccTouchesMode mode) {
if(m_bTouchMode != mode) {
m_bTouchMode = mode;
if( m_bIsTouchEnabled) {
setTouchEnabled(false);
setTouchEnabled(true);
}
}
}
void CCLayer::setTouchPriority(int priority) {
if(m_bTouchPriority != priority) {
m_bTouchPriority = priority;
if( m_bIsTouchEnabled) {
setTouchEnabled(false);
setTouchEnabled(true);
}
}
}
int CCLayer::getTouchPriority() {
return m_bTouchPriority;
}
int CCLayer::getTouchMode() {
return m_bTouchMode;
}
/// isAccelerometerEnabled getter
bool CCLayer::isAccelerometerEnabled()
{
@ -194,6 +242,30 @@ void CCLayer::setAccelerometerEnabled(bool enabled)
}
}
void CCLayer::setAccelerometerInterval(double interval) {
if (m_bIsAccelerometerEnabled)
{
if (m_bIsRunning)
{
CCDirector* pDirector = CCDirector::sharedDirector();
// pDirector->getAccelerometer()->setAccelerometerInterval(interval);
}
}
}
void CCLayer::didAccelerate(CCAcceleration* pAccelerationValue) {
CC_UNUSED_PARAM(pAccelerationValue);
//
// if ( m_eScriptType != kScriptTypeNone)
// {
// CCScriptEngineManager::sharedManager()->getScriptEngine()->executeAccelerometerEvent(this, pAccelerationValue);
// }
}
/// isKeypadEnabled getter
bool CCLayer::isKeypadEnabled()
{

View File

@ -36,6 +36,12 @@ THE SOFTWARE.
NS_CC_BEGIN
typedef enum {
kCCTouchesAllAtOnce,
kCCTouchesOneByOne,
} ccTouchesMode;
/**
* @addtogroup layer
* @{
@ -80,7 +86,7 @@ public:
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);
virtual void didAccelerate(CCAcceleration* pAccelerationValue) {CC_UNUSED_PARAM(pAccelerationValue);}
virtual void didAccelerate(CCAcceleration* pAccelerationValue);
/** If isTouchEnabled, this method is called onEnter. Override it to change the
way CCLayer receives touch events.
@ -106,12 +112,21 @@ public:
*/
bool isTouchEnabled();
void setTouchEnabled(bool value);
void setTouchMode(ccTouchesMode mode);
void setTouchPriority(int priority);
int getTouchPriority();
int getTouchMode();
/** whether or not it will receive Accelerometer events
You can enable / disable accelerometer events with this property.
@since v0.8.1
*/
bool isAccelerometerEnabled();
void setAccelerometerEnabled(bool value);
void setAccelerometerInterval(double interval);
/** whether or not it will receive keypad events
You can enable / disable accelerometer events with this property.
it's new in cocos2d-x
@ -128,6 +143,10 @@ protected:
private:
// Script touch events handler
CCTouchScriptHandlerEntry* m_pScriptHandlerEntry;
int m_bTouchPriority;
ccTouchesMode m_bTouchMode;
int excuteScriptTouchHandler(int nEventType, CCTouch *pTouch);
int excuteScriptTouchHandler(int nEventType, CCSet *pTouches);
};