2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2012-06-08 14:50:41 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-01 11:26:26 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2010-08-02 10:58:00 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "CCLayer.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "touch_dispatcher/CCTouchDispatcher.h"
|
|
|
|
#include "keypad_dispatcher/CCKeypadDispatcher.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCAccelerometer.h"
|
2010-08-02 10:58:00 +08:00
|
|
|
#include "CCDirector.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "support/CCPointExtension.h"
|
|
|
|
#include "script_support/CCScriptSupport.h"
|
|
|
|
#include "shaders/CCShaderCache.h"
|
|
|
|
#include "shaders/CCGLProgram.h"
|
|
|
|
#include "shaders/ccGLStateCache.h"
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "support/TransformUtils.h"
|
2012-04-08 14:16:29 +08:00
|
|
|
// extern
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "kazmath/GL/matrix.h"
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-08-02 10:58:00 +08:00
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
// CCLayer
|
|
|
|
CCLayer::CCLayer()
|
2012-11-14 18:05:15 +08:00
|
|
|
: m_bTouchEnabled(false)
|
|
|
|
, m_bAccelerometerEnabled(false)
|
|
|
|
, m_bKeypadEnabled(false)
|
2012-12-02 15:17:34 +08:00
|
|
|
,m_pScriptTouchHandlerEntry(NULL)
|
|
|
|
,m_pScriptKeypadHandlerEntry(NULL)
|
|
|
|
,m_pScriptAccelerateHandlerEntry(NULL)
|
2012-11-14 18:05:15 +08:00
|
|
|
, m_eTouchMode(kCCTouchesAllAtOnce)
|
|
|
|
, m_nTouchPriority(0)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
m_bIgnoreAnchorPointForPosition = true;
|
2012-11-14 18:05:15 +08:00
|
|
|
setAnchorPoint(ccp(0.5f, 0.5f));
|
2010-08-02 10:58:00 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
|
|
|
CCLayer::~CCLayer()
|
|
|
|
{
|
2012-02-07 11:43:29 +08:00
|
|
|
unregisterScriptTouchHandler();
|
2012-12-02 15:17:34 +08:00
|
|
|
unregisterScriptKeypadHandler();
|
|
|
|
unregisterScriptAccelerateHandler();
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-10-01 23:04:12 +08:00
|
|
|
bool CCLayer::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool bRet = false;
|
|
|
|
do
|
2012-10-27 08:11:43 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector * pDirector;
|
|
|
|
CC_BREAK_IF(!(pDirector = CCDirector::sharedDirector()));
|
|
|
|
this->setContentSize(pDirector->getWinSize());
|
2012-11-14 18:05:15 +08:00
|
|
|
m_bTouchEnabled = false;
|
|
|
|
m_bAccelerometerEnabled = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
// success
|
|
|
|
bRet = true;
|
|
|
|
} while(0);
|
|
|
|
return bRet;
|
2010-10-01 23:04:12 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLayer *CCLayer::create()
|
2010-10-01 23:04:12 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayer *pRet = new CCLayer();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2010-10-01 23:04:12 +08:00
|
|
|
else
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2010-10-01 23:04:12 +08:00
|
|
|
}
|
2010-08-03 14:50:41 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
|
|
|
/// Touch and Accelerometer related
|
|
|
|
|
|
|
|
void CCLayer::registerWithTouchDispatcher()
|
|
|
|
{
|
2012-09-21 00:21:24 +08:00
|
|
|
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2012-10-29 15:38:44 +08:00
|
|
|
// Using LuaBindings
|
2012-12-02 15:17:34 +08:00
|
|
|
if (m_pScriptTouchHandlerEntry)
|
2012-10-29 15:38:44 +08:00
|
|
|
{
|
2012-12-02 15:17:34 +08:00
|
|
|
if (m_pScriptTouchHandlerEntry->isMultiTouches())
|
2012-10-29 15:38:44 +08:00
|
|
|
{
|
2012-10-27 08:11:43 +08:00
|
|
|
pDispatcher->addStandardDelegate(this, 0);
|
2012-12-02 15:17:34 +08:00
|
|
|
LUALOG("[LUA] Add multi-touches event handler: %d", m_pScriptTouchHandlerEntry->getHandler());
|
2012-10-29 15:38:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-27 08:11:43 +08:00
|
|
|
pDispatcher->addTargetedDelegate(this,
|
2012-12-02 15:17:34 +08:00
|
|
|
m_pScriptTouchHandlerEntry->getPriority(),
|
|
|
|
m_pScriptTouchHandlerEntry->getSwallowsTouches());
|
|
|
|
LUALOG("[LUA] Add touch event handler: %d", m_pScriptTouchHandlerEntry->getHandler());
|
2012-10-29 15:38:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
if( m_eTouchMode == kCCTouchesAllAtOnce ) {
|
2012-10-29 15:38:44 +08:00
|
|
|
pDispatcher->addStandardDelegate(this, 0);
|
|
|
|
} else {
|
2012-11-14 18:05:15 +08:00
|
|
|
pDispatcher->addTargetedDelegate(this, m_nTouchPriority, true);
|
2012-10-29 15:38:44 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
void CCLayer::registerScriptTouchHandler(int nHandler, bool bIsMultiTouches, int nPriority, bool bSwallowsTouches)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
|
|
|
unregisterScriptTouchHandler();
|
2012-12-02 15:17:34 +08:00
|
|
|
m_pScriptTouchHandlerEntry = CCTouchScriptHandlerEntry::create(nHandler, bIsMultiTouches, nPriority, bSwallowsTouches);
|
|
|
|
m_pScriptTouchHandlerEntry->retain();
|
2012-02-02 10:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::unregisterScriptTouchHandler(void)
|
|
|
|
{
|
2012-12-02 15:17:34 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(m_pScriptTouchHandlerEntry);
|
2012-02-02 10:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CCLayer::excuteScriptTouchHandler(int nEventType, CCTouch *pTouch)
|
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
return CCScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchEvent(this, nEventType, pTouch);
|
2012-02-02 10:38:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CCLayer::excuteScriptTouchHandler(int nEventType, CCSet *pTouches)
|
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
return CCScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchesEvent(this, nEventType, pTouches);
|
2012-02-02 10:38:26 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isTouchEnabled getter
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCLayer::isTouchEnabled()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
return m_bTouchEnabled;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
/// isTouchEnabled setter
|
2012-06-15 15:10:40 +08:00
|
|
|
void CCLayer::setTouchEnabled(bool enabled)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bTouchEnabled != enabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
m_bTouchEnabled = enabled;
|
|
|
|
if (m_bRunning)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
this->registerWithTouchDispatcher();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// have problems?
|
2012-09-21 00:21:24 +08:00
|
|
|
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
void CCLayer::setTouchMode(ccTouchesMode mode)
|
|
|
|
{
|
|
|
|
if(m_eTouchMode != mode)
|
|
|
|
{
|
|
|
|
m_eTouchMode = mode;
|
2012-10-27 08:11:43 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
if( m_bTouchEnabled)
|
|
|
|
{
|
2012-10-27 08:11:43 +08:00
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
void CCLayer::setTouchPriority(int priority)
|
|
|
|
{
|
|
|
|
if (m_nTouchPriority != priority)
|
|
|
|
{
|
|
|
|
m_nTouchPriority = priority;
|
2012-10-27 08:11:43 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
if( m_bTouchEnabled)
|
|
|
|
{
|
2012-10-27 08:11:43 +08:00
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
int CCLayer::getTouchPriority()
|
|
|
|
{
|
|
|
|
return m_nTouchPriority;
|
2012-10-27 08:11:43 +08:00
|
|
|
}
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
int CCLayer::getTouchMode()
|
|
|
|
{
|
|
|
|
return m_eTouchMode;
|
2012-10-27 08:11:43 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isAccelerometerEnabled getter
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCLayer::isAccelerometerEnabled()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
return m_bAccelerometerEnabled;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
/// isAccelerometerEnabled setter
|
2012-06-15 15:10:40 +08:00
|
|
|
void CCLayer::setAccelerometerEnabled(bool enabled)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
if (enabled != m_bAccelerometerEnabled)
|
2010-11-10 16:29:52 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
m_bAccelerometerEnabled = enabled;
|
2010-11-10 16:29:52 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bRunning)
|
2010-11-10 16:29:52 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
2010-11-10 16:29:52 +08:00
|
|
|
if (enabled)
|
|
|
|
{
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(this);
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(NULL);
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2012-10-27 08:11:43 +08:00
|
|
|
|
|
|
|
void CCLayer::setAccelerometerInterval(double interval) {
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bAccelerometerEnabled)
|
2012-10-27 08:11:43 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bRunning)
|
2012-10-27 08:11:43 +08:00
|
|
|
{
|
2012-11-16 17:08:34 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
2012-11-14 06:57:05 +08:00
|
|
|
pDirector->getAccelerometer()->setAccelerometerInterval(interval);
|
2012-10-27 08:11:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-06 14:18:13 +08:00
|
|
|
void CCLayer::didAccelerate(CCAcceleration* pAccelerationValue)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pAccelerationValue);
|
|
|
|
if ( m_eScriptType != kScriptTypeNone)
|
|
|
|
{
|
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeAccelerometerEvent(this, pAccelerationValue);
|
|
|
|
}
|
2012-10-27 08:11:43 +08:00
|
|
|
}
|
|
|
|
|
2012-12-02 15:17:34 +08:00
|
|
|
void CCLayer::registerScriptAccelerateHandler(int nHandler)
|
|
|
|
{
|
|
|
|
unregisterScriptAccelerateHandler();
|
|
|
|
m_pScriptAccelerateHandlerEntry = CCScriptHandlerEntry::create(nHandler);
|
|
|
|
m_pScriptAccelerateHandlerEntry->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::unregisterScriptAccelerateHandler(void)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(m_pScriptAccelerateHandlerEntry);
|
|
|
|
}
|
2012-10-27 08:11:43 +08:00
|
|
|
|
2010-12-13 14:10:39 +08:00
|
|
|
/// isKeypadEnabled getter
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCLayer::isKeypadEnabled()
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
return m_bKeypadEnabled;
|
2010-12-13 14:10:39 +08:00
|
|
|
}
|
|
|
|
/// isKeypadEnabled setter
|
2012-06-15 15:10:40 +08:00
|
|
|
void CCLayer::setKeypadEnabled(bool enabled)
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
if (enabled != m_bKeypadEnabled)
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
m_bKeypadEnabled = enabled;
|
2010-12-13 14:10:39 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bRunning)
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
2010-12-13 14:10:39 +08:00
|
|
|
if (enabled)
|
|
|
|
{
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getKeypadDispatcher()->addDelegate(this);
|
2010-12-13 14:10:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getKeypadDispatcher()->removeDelegate(this);
|
2010-12-13 14:10:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 15:17:34 +08:00
|
|
|
void CCLayer::registerScriptKeypadHandler(int nHandler)
|
|
|
|
{
|
|
|
|
unregisterScriptKeypadHandler();
|
|
|
|
m_pScriptKeypadHandlerEntry = CCScriptHandlerEntry::create(nHandler);
|
|
|
|
m_pScriptKeypadHandlerEntry->retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::unregisterScriptKeypadHandler(void)
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(m_pScriptKeypadHandlerEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::keyBackClicked(void)
|
|
|
|
{
|
|
|
|
if (m_pScriptKeypadHandlerEntry)
|
|
|
|
{
|
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeBackClicked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::keyMenuClicked(void)
|
|
|
|
{
|
|
|
|
if (m_pScriptKeypadHandlerEntry)
|
|
|
|
{
|
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeMenuClicked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// Callbacks
|
|
|
|
void CCLayer::onEnter()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
|
|
|
// register 'parent' nodes first
|
|
|
|
// since events are propagated in reverse order
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bTouchEnabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->registerWithTouchDispatcher();
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// then iterate over all the children
|
|
|
|
CCNode::onEnter();
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
// add this layer to concern the Accelerometer Sensor
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bAccelerometerEnabled)
|
2010-11-10 16:29:52 +08:00
|
|
|
{
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(this);
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
2010-12-13 14:10:39 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// add this layer to concern the keypad msg
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bKeypadEnabled)
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getKeypadDispatcher()->addDelegate(this);
|
2010-12-13 14:10:39 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::onExit()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
2012-11-14 18:05:15 +08:00
|
|
|
if( m_bTouchEnabled )
|
2012-09-20 11:32:02 +08:00
|
|
|
{
|
|
|
|
pDirector->getTouchDispatcher()->removeDelegate(this);
|
2012-09-20 16:15:00 +08:00
|
|
|
// [lua]:don't unregister script touch handler, or the handler will be destroyed
|
2012-09-20 11:48:34 +08:00
|
|
|
// unregisterScriptTouchHandler();
|
2012-09-20 11:32:02 +08:00
|
|
|
}
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
// remove this layer from the delegates who concern Accelerometer Sensor
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bAccelerometerEnabled)
|
2010-11-10 16:29:52 +08:00
|
|
|
{
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(NULL);
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// remove this layer from the delegates who concern the keypad msg
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bKeypadEnabled)
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getKeypadDispatcher()->removeDelegate(this);
|
2010-12-13 14:10:39 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCNode::onExit();
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayer::onEnterTransitionDidFinish()
|
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
if (m_bAccelerometerEnabled)
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(this);
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCNode::onEnterTransitionDidFinish();
|
|
|
|
}
|
|
|
|
|
2011-03-18 09:39:34 +08:00
|
|
|
bool CCLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
if (kScriptTypeNone != m_eScriptType)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
2012-05-29 17:11:33 +08:00
|
|
|
return excuteScriptTouchHandler(CCTOUCHBEGAN, pTouch) == 0 ? false : true;
|
2012-02-02 10:38:26 +08:00
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2011-06-10 17:51:37 +08:00
|
|
|
CC_UNUSED_PARAM(pTouch);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(false, "Layer#ccTouchBegan override me");
|
|
|
|
return true;
|
2010-07-30 17:18:20 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-08-31 21:23:23 +08:00
|
|
|
void CCLayer::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
if (kScriptTypeNone != m_eScriptType)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
|
|
|
excuteScriptTouchHandler(CCTOUCHMOVED, pTouch);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2012-02-02 10:38:26 +08:00
|
|
|
CC_UNUSED_PARAM(pTouch);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:23:23 +08:00
|
|
|
void CCLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
if (kScriptTypeNone != m_eScriptType)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
|
|
|
excuteScriptTouchHandler(CCTOUCHENDED, pTouch);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2012-02-02 10:38:26 +08:00
|
|
|
CC_UNUSED_PARAM(pTouch);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:23:23 +08:00
|
|
|
void CCLayer::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
if (kScriptTypeNone != m_eScriptType)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
|
|
|
excuteScriptTouchHandler(CCTOUCHCANCELLED, pTouch);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2012-02-02 10:38:26 +08:00
|
|
|
CC_UNUSED_PARAM(pTouch);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
|
2011-06-20 17:31:38 +08:00
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
if (kScriptTypeNone != m_eScriptType)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
|
|
|
excuteScriptTouchHandler(CCTOUCHBEGAN, pTouches);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2012-02-02 10:38:26 +08:00
|
|
|
CC_UNUSED_PARAM(pTouches);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
2011-06-20 17:31:38 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 10:38:26 +08:00
|
|
|
void CCLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
|
2011-06-20 17:31:38 +08:00
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
if (kScriptTypeNone != m_eScriptType)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
|
|
|
excuteScriptTouchHandler(CCTOUCHMOVED, pTouches);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2012-02-02 10:38:26 +08:00
|
|
|
CC_UNUSED_PARAM(pTouches);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
2011-06-20 17:31:38 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 10:38:26 +08:00
|
|
|
void CCLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
|
2011-06-20 17:31:38 +08:00
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
if (kScriptTypeNone != m_eScriptType)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
|
|
|
excuteScriptTouchHandler(CCTOUCHENDED, pTouches);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2012-02-02 10:38:26 +08:00
|
|
|
CC_UNUSED_PARAM(pTouches);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
2011-06-20 17:31:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
if (kScriptTypeNone != m_eScriptType)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
|
|
|
excuteScriptTouchHandler(CCTOUCHCANCELLED, pTouches);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2012-02-02 10:38:26 +08:00
|
|
|
CC_UNUSED_PARAM(pTouches);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
2011-06-20 17:31:38 +08:00
|
|
|
}
|
|
|
|
|
2013-02-27 09:38:30 +08:00
|
|
|
// LayerRGBA
|
|
|
|
|
|
|
|
bool CCLayerRGBA::init()
|
|
|
|
{
|
|
|
|
if ( (self=[super init]) ) {
|
|
|
|
_displayedOpacity = _realOpacity = 255;
|
|
|
|
_displayedColor = _realColor = ccWHITE;
|
|
|
|
self.cascadeOpacityEnabled = NO;
|
|
|
|
self.cascadeColorEnabled = NO;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(GLubyte) opacity
|
|
|
|
{
|
|
|
|
return _realOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(GLubyte) displayedOpacity
|
|
|
|
{
|
|
|
|
return _displayedOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Override synthesized setOpacity to recurse items */
|
|
|
|
- (void) setOpacity:(GLubyte)opacity
|
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity = opacity;
|
|
|
|
|
|
|
|
if( _cascadeOpacityEnabled ) {
|
|
|
|
GLubyte parentOpacity = 255;
|
|
|
|
if( [_parent conformsToProtocol:@protocol(CCRGBAProtocol)] && [(id<CCRGBAProtocol>)_parent isCascadeOpacityEnabled] )
|
|
|
|
parentOpacity = [(id<CCRGBAProtocol>)_parent displayedOpacity];
|
|
|
|
[self updateDisplayedOpacity:parentOpacity];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
-(ccColor3B) color
|
|
|
|
{
|
|
|
|
return _realColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(ccColor3B) displayedColor
|
|
|
|
{
|
|
|
|
return _displayedColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setColor:(ccColor3B)color
|
|
|
|
{
|
|
|
|
_displayedColor = _realColor = color;
|
|
|
|
|
|
|
|
if( _cascadeColorEnabled ) {
|
|
|
|
ccColor3B parentColor = ccWHITE;
|
|
|
|
if( [_parent conformsToProtocol:@protocol(CCRGBAProtocol)] && [(id<CCRGBAProtocol>)_parent isCascadeColorEnabled] )
|
|
|
|
parentColor = [(id<CCRGBAProtocol>)_parent displayedColor];
|
|
|
|
[self updateDisplayedColor:parentColor];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateDisplayedOpacity:(GLubyte)parentOpacity
|
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity * parentOpacity/255.0;
|
|
|
|
|
|
|
|
if (_cascadeOpacityEnabled) {
|
|
|
|
id<CCRGBAProtocol> item;
|
|
|
|
CCARRAY_FOREACH(_children, item) {
|
|
|
|
if ([item conformsToProtocol:@protocol(CCRGBAProtocol)]) {
|
|
|
|
[item updateDisplayedOpacity:_displayedOpacity];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateDisplayedColor:(ccColor3B)parentColor
|
|
|
|
{
|
|
|
|
_displayedColor.r = _realColor.r * parentColor.r/255.0;
|
|
|
|
_displayedColor.g = _realColor.g * parentColor.g/255.0;
|
|
|
|
_displayedColor.b = _realColor.b * parentColor.b/255.0;
|
|
|
|
|
|
|
|
if (_cascadeColorEnabled) {
|
|
|
|
id<CCRGBAProtocol> item;
|
|
|
|
CCARRAY_FOREACH(_children, item) {
|
|
|
|
if ([item conformsToProtocol:@protocol(CCRGBAProtocol)]) {
|
|
|
|
[item updateDisplayedColor:_displayedColor];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CCLayerColor
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
CCLayerColor::CCLayerColor()
|
2011-04-01 16:06:53 +08:00
|
|
|
: m_cOpacity(0)
|
2011-07-05 15:39:50 +08:00
|
|
|
, m_tColor( ccc3(0,0,0) )
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2011-07-05 15:39:50 +08:00
|
|
|
// default blend function
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tBlendFunc.src = CC_BLEND_SRC;
|
|
|
|
m_tBlendFunc.dst = CC_BLEND_DST;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2011-07-05 15:39:50 +08:00
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
CCLayerColor::~CCLayerColor()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opacity and RGB color protocol
|
|
|
|
/// opacity getter
|
2010-12-27 09:40:45 +08:00
|
|
|
GLubyte CCLayerColor::getOpacity()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_cOpacity;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
/// opacity setter
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayerColor::setOpacity(GLubyte var)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_cOpacity = var;
|
|
|
|
updateColor();
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// color getter
|
2013-01-14 15:51:53 +08:00
|
|
|
const ccColor3B& CCLayerColor::getColor()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tColor;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// color setter
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCLayerColor::setColor(const ccColor3B& var)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tColor = var;
|
|
|
|
updateColor();
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// blendFunc getter
|
2010-12-27 09:40:45 +08:00
|
|
|
ccBlendFunc CCLayerColor::getBlendFunc()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tBlendFunc;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
/// blendFunc setter
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayerColor::setBlendFunc(ccBlendFunc var)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tBlendFunc = var;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2012-11-01 22:17:12 +08:00
|
|
|
CCLayerColor* CCLayerColor::node()
|
|
|
|
{
|
|
|
|
return CCLayerColor::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLayerColor* CCLayerColor::create()
|
|
|
|
{
|
|
|
|
CCLayerColor* pRet = new CCLayerColor();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLayerColor * CCLayerColor::create(const ccColor4B& color, GLfloat width, GLfloat height)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayerColor * pLayer = new CCLayerColor();
|
|
|
|
if( pLayer && pLayer->initWithColor(color,width,height))
|
|
|
|
{
|
|
|
|
pLayer->autorelease();
|
|
|
|
return pLayer;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pLayer);
|
|
|
|
return NULL;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCLayerColor * CCLayerColor::create(const ccColor4B& color)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayerColor * pLayer = new CCLayerColor();
|
|
|
|
if(pLayer && pLayer->initWithColor(color))
|
|
|
|
{
|
|
|
|
pLayer->autorelease();
|
|
|
|
return pLayer;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pLayer);
|
|
|
|
return NULL;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
bool CCLayerColor::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
return initWithColor(ccc4(0,0,0,0), s.width, s.height);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCLayerColor::initWithColor(const ccColor4B& color, GLfloat w, GLfloat h)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( CCLayer::init() ) {
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// default blend function
|
|
|
|
m_tBlendFunc.src = GL_SRC_ALPHA;
|
|
|
|
m_tBlendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tColor.r = color.r;
|
|
|
|
m_tColor.g = color.g;
|
|
|
|
m_tColor.b = color.b;
|
|
|
|
m_cOpacity = color.a;
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
for (int i = 0; i<sizeof(m_pSquareVertices) / sizeof( m_pSquareVertices[0]); i++ ) {
|
|
|
|
m_pSquareVertices[i].x = 0.0f;
|
|
|
|
m_pSquareVertices[i].y = 0.0f;
|
|
|
|
}
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
updateColor();
|
|
|
|
setContentSize(CCSizeMake(w, h));
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
|
|
|
|
}
|
|
|
|
return true;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
bool CCLayerColor::initWithColor(const ccColor4B& color)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
this->initWithColor(color, s.width, s.height);
|
|
|
|
return true;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// override contentSize
|
2012-07-27 16:57:56 +08:00
|
|
|
void CCLayerColor::setContentSize(const CCSize & size)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pSquareVertices[1].x = size.width;
|
|
|
|
m_pSquareVertices[2].y = size.height;
|
|
|
|
m_pSquareVertices[3].x = size.width;
|
|
|
|
m_pSquareVertices[3].y = size.height;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayer::setContentSize(size);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayerColor::changeWidthAndHeight(GLfloat w ,GLfloat h)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setContentSize(CCSizeMake(w, h));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayerColor::changeWidth(GLfloat w)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
this->setContentSize(CCSizeMake(w, m_obContentSize.height));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayerColor::changeHeight(GLfloat h)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
this->setContentSize(CCSizeMake(m_obContentSize.width, h));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayerColor::updateColor()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
for( unsigned int i=0; i < 4; i++ )
|
|
|
|
{
|
|
|
|
m_pSquareColors[i].r = m_tColor.r / 255.0f;
|
|
|
|
m_pSquareColors[i].g = m_tColor.g / 255.0f;
|
|
|
|
m_pSquareColors[i].b = m_tColor.b / 255.0f;
|
|
|
|
m_pSquareColors[i].a = m_cOpacity / 255.0f;
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayerColor::draw()
|
2011-07-12 11:20:41 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_NODE_DRAW_SETUP();
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color );
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, m_pSquareVertices);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, m_pSquareColors);
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_INCREMENT_GL_DRAWS(1);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
//
|
|
|
|
// CCLayerGradient
|
|
|
|
//
|
2012-07-19 05:40:09 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLayerGradient* CCLayerGradient::create(const ccColor4B& start, const ccColor4B& end)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2010-12-27 09:40:45 +08:00
|
|
|
CCLayerGradient * pLayer = new CCLayerGradient();
|
|
|
|
if( pLayer && pLayer->initWithColor(start, end))
|
|
|
|
{
|
|
|
|
pLayer->autorelease();
|
|
|
|
return pLayer;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pLayer);
|
2011-04-27 22:45:54 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLayerGradient* CCLayerGradient::create(const ccColor4B& start, const ccColor4B& end, const CCPoint& v)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2010-12-27 09:40:45 +08:00
|
|
|
CCLayerGradient * pLayer = new CCLayerGradient();
|
|
|
|
if( pLayer && pLayer->initWithColor(start, end, v))
|
|
|
|
{
|
|
|
|
pLayer->autorelease();
|
|
|
|
return pLayer;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pLayer);
|
2011-04-27 22:45:54 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-01 22:17:12 +08:00
|
|
|
CCLayerGradient* CCLayerGradient::node()
|
|
|
|
{
|
|
|
|
return CCLayerGradient::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLayerGradient* CCLayerGradient::create()
|
|
|
|
{
|
|
|
|
CCLayerGradient* pRet = new CCLayerGradient();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-10-31 18:00:51 +08:00
|
|
|
bool CCLayerGradient::init()
|
|
|
|
{
|
|
|
|
return initWithColor(ccc4(0, 0, 0, 255), ccc4(0, 0, 0, 255));
|
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
bool CCLayerGradient::initWithColor(const ccColor4B& start, const ccColor4B& end)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
|
|
|
return initWithColor(start, end, ccp(0, -1));
|
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
bool CCLayerGradient::initWithColor(const ccColor4B& start, const ccColor4B& end, const CCPoint& v)
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2011-04-27 22:45:54 +08:00
|
|
|
m_endColor.r = end.r;
|
|
|
|
m_endColor.g = end.g;
|
|
|
|
m_endColor.b = end.b;
|
|
|
|
|
|
|
|
m_cEndOpacity = end.a;
|
2012-04-19 14:35:52 +08:00
|
|
|
m_cStartOpacity = start.a;
|
2011-04-27 22:45:54 +08:00
|
|
|
m_AlongVector = v;
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
m_bCompressedInterpolation = true;
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
return CCLayerColor::initWithColor(ccc4(start.r, start.g, start.b, 255));
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayerGradient::updateColor()
|
|
|
|
{
|
|
|
|
CCLayerColor::updateColor();
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
float h = ccpLength(m_AlongVector);
|
2011-04-27 22:45:54 +08:00
|
|
|
if (h == 0)
|
|
|
|
return;
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
float c = sqrtf(2.0f);
|
2011-04-27 22:45:54 +08:00
|
|
|
CCPoint u = ccp(m_AlongVector.x / h, m_AlongVector.y / h);
|
|
|
|
|
2011-07-05 15:39:50 +08:00
|
|
|
// Compressed Interpolation mode
|
|
|
|
if (m_bCompressedInterpolation)
|
|
|
|
{
|
|
|
|
float h2 = 1 / ( fabsf(u.x) + fabsf(u.y) );
|
|
|
|
u = ccpMult(u, h2 * (float)c);
|
2011-07-01 11:26:26 +08:00
|
|
|
}
|
|
|
|
|
2011-04-27 22:45:54 +08:00
|
|
|
float opacityf = (float)m_cOpacity / 255.0f;
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
ccColor4F S = {
|
|
|
|
m_tColor.r / 255.0f,
|
|
|
|
m_tColor.g / 255.0f,
|
|
|
|
m_tColor.b / 255.0f,
|
|
|
|
m_cStartOpacity * opacityf / 255.0f
|
2011-04-27 22:45:54 +08:00
|
|
|
};
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
ccColor4F E = {
|
|
|
|
m_endColor.r / 255.0f,
|
|
|
|
m_endColor.g / 255.0f,
|
|
|
|
m_endColor.b / 255.0f,
|
|
|
|
m_cEndOpacity * opacityf / 255.0f
|
2011-04-27 22:45:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// (-1, -1)
|
2012-05-07 17:15:32 +08:00
|
|
|
m_pSquareColors[0].r = E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[0].g = E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[0].b = E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[0].a = E.a + (S.a - E.a) * ((c + u.x + u.y) / (2.0f * c));
|
2011-04-27 22:45:54 +08:00
|
|
|
// (1, -1)
|
2012-05-07 17:15:32 +08:00
|
|
|
m_pSquareColors[1].r = E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[1].g = E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[1].b = E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[1].a = E.a + (S.a - E.a) * ((c - u.x + u.y) / (2.0f * c));
|
2011-04-27 22:45:54 +08:00
|
|
|
// (-1, 1)
|
2012-05-07 17:15:32 +08:00
|
|
|
m_pSquareColors[2].r = E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[2].g = E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[2].b = E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[2].a = E.a + (S.a - E.a) * ((c + u.x - u.y) / (2.0f * c));
|
2011-04-27 22:45:54 +08:00
|
|
|
// (1, 1)
|
2012-05-07 17:15:32 +08:00
|
|
|
m_pSquareColors[3].r = E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[3].g = E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[3].b = E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0f * c));
|
|
|
|
m_pSquareColors[3].a = E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0f * c));
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 15:51:53 +08:00
|
|
|
const ccColor3B& CCLayerGradient::getStartColor()
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
|
|
|
return m_tColor;
|
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCLayerGradient::setStartColor(const ccColor3B& color)
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2011-08-17 21:19:57 +08:00
|
|
|
setColor(color);
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCLayerGradient::setEndColor(const ccColor3B& color)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2011-08-17 21:19:57 +08:00
|
|
|
m_endColor = color;
|
2011-04-27 22:45:54 +08:00
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
2013-01-14 15:51:53 +08:00
|
|
|
const ccColor3B& CCLayerGradient::getEndColor()
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
|
|
|
return m_endColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayerGradient::setStartOpacity(GLubyte o)
|
|
|
|
{
|
|
|
|
m_cStartOpacity = o;
|
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLubyte CCLayerGradient::getStartOpacity()
|
|
|
|
{
|
|
|
|
return m_cStartOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayerGradient::setEndOpacity(GLubyte o)
|
|
|
|
{
|
|
|
|
m_cEndOpacity = o;
|
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
GLubyte CCLayerGradient::getEndOpacity()
|
|
|
|
{
|
|
|
|
return m_cEndOpacity;
|
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCLayerGradient::setVector(const CCPoint& var)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
|
|
|
m_AlongVector = var;
|
|
|
|
updateColor();
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 15:51:53 +08:00
|
|
|
const CCPoint& CCLayerGradient::getVector()
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
|
|
|
return m_AlongVector;
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
bool CCLayerGradient::isCompressedInterpolation()
|
2011-07-05 15:39:50 +08:00
|
|
|
{
|
|
|
|
return m_bCompressedInterpolation;
|
|
|
|
}
|
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
void CCLayerGradient::setCompressedInterpolation(bool compress)
|
2011-07-05 15:39:50 +08:00
|
|
|
{
|
|
|
|
m_bCompressedInterpolation = compress;
|
|
|
|
updateColor();
|
2011-07-01 11:26:26 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// MultiplexLayer
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
CCLayerMultiplex::CCLayerMultiplex()
|
2011-04-01 16:06:53 +08:00
|
|
|
: m_nEnabledLayer(0)
|
|
|
|
, m_pLayers(NULL)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
}
|
2011-07-01 11:26:26 +08:00
|
|
|
CCLayerMultiplex::~CCLayerMultiplex()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE(m_pLayers);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLayerMultiplex * CCLayerMultiplex::create(CCLayer * layer, ...)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args,layer);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayerMultiplex * pMultiplexLayer = new CCLayerMultiplex();
|
|
|
|
if(pMultiplexLayer && pMultiplexLayer->initWithLayers(layer, args))
|
|
|
|
{
|
|
|
|
pMultiplexLayer->autorelease();
|
|
|
|
va_end(args);
|
|
|
|
return pMultiplexLayer;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
CC_SAFE_DELETE(pMultiplexLayer);
|
|
|
|
return NULL;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCLayerMultiplex * CCLayerMultiplex::createWithLayer(CCLayer* layer)
|
2011-05-31 14:04:14 +08:00
|
|
|
{
|
2012-06-27 14:21:29 +08:00
|
|
|
return CCLayerMultiplex::create(layer, NULL);
|
2011-05-31 14:04:14 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-11-01 22:17:12 +08:00
|
|
|
CCLayerMultiplex* CCLayerMultiplex::node()
|
|
|
|
{
|
|
|
|
return CCLayerMultiplex::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLayerMultiplex* CCLayerMultiplex::create()
|
|
|
|
{
|
|
|
|
CCLayerMultiplex* pRet = new CCLayerMultiplex();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-11-30 21:13:25 +08:00
|
|
|
CCLayerMultiplex* CCLayerMultiplex::createWithArray(CCArray* arrayOfLayers)
|
|
|
|
{
|
|
|
|
CCLayerMultiplex* pRet = new CCLayerMultiplex();
|
|
|
|
if (pRet && pRet->initWithArray(arrayOfLayers))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-11-01 22:17:12 +08:00
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
void CCLayerMultiplex::addLayer(CCLayer* layer)
|
2011-05-31 14:04:14 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(m_pLayers, "");
|
|
|
|
m_pLayers->addObject(layer);
|
2011-05-31 14:04:14 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
bool CCLayerMultiplex::initWithLayers(CCLayer *layer, va_list params)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-11-30 21:13:25 +08:00
|
|
|
if (CCLayer::init())
|
|
|
|
{
|
2012-12-04 10:36:13 +08:00
|
|
|
m_pLayers = CCArray::createWithCapacity(5);
|
|
|
|
m_pLayers->retain();
|
|
|
|
m_pLayers->addObject(layer);
|
|
|
|
|
2012-11-30 21:13:25 +08:00
|
|
|
CCLayer *l = va_arg(params,CCLayer*);
|
|
|
|
while( l ) {
|
|
|
|
m_pLayers->addObject(l);
|
|
|
|
l = va_arg(params,CCLayer*);
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-11-30 21:13:25 +08:00
|
|
|
m_nEnabledLayer = 0;
|
|
|
|
this->addChild((CCNode*)m_pLayers->objectAtIndex(m_nEnabledLayer));
|
|
|
|
return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-11-30 21:13:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCLayerMultiplex::initWithArray(CCArray* arrayOfLayers)
|
|
|
|
{
|
2012-12-04 10:36:13 +08:00
|
|
|
if (CCLayer::init())
|
|
|
|
{
|
|
|
|
m_pLayers = CCArray::createWithCapacity(arrayOfLayers->count());
|
|
|
|
m_pLayers->addObjectsFromArray(arrayOfLayers);
|
|
|
|
m_pLayers->retain();
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2012-12-04 10:36:13 +08:00
|
|
|
m_nEnabledLayer = 0;
|
|
|
|
this->addChild((CCNode*)m_pLayers->objectAtIndex(m_nEnabledLayer));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
void CCLayerMultiplex::switchTo(unsigned int n)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( n < m_pLayers->count(), "Invalid index in MultiplexLayer switchTo message" );
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->removeChild((CCNode*)m_pLayers->objectAtIndex(m_nEnabledLayer), true);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_nEnabledLayer = n;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->addChild((CCNode*)m_pLayers->objectAtIndex(n));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
void CCLayerMultiplex::switchToAndReleaseMe(unsigned int n)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( n < m_pLayers->count(), "Invalid index in MultiplexLayer switchTo message" );
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->removeChild((CCNode*)m_pLayers->objectAtIndex(m_nEnabledLayer), true);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//[layers replaceObjectAtIndex:enabledLayer withObject:[NSNull null]];
|
|
|
|
m_pLayers->replaceObjectAtIndex(m_nEnabledLayer, NULL);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_nEnabledLayer = n;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->addChild((CCNode*)m_pLayers->objectAtIndex(n));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
NS_CC_END
|