2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
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"
|
2010-08-25 10:19:20 +08:00
|
|
|
#include "CCTouchDispatcher.h"
|
2010-12-13 14:10:39 +08:00
|
|
|
#include "CCKeypadDispatcher.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCAccelerometer.h"
|
2010-08-02 10:58:00 +08:00
|
|
|
#include "CCDirector.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCPointExtension.h"
|
2010-10-01 23:04:12 +08:00
|
|
|
namespace cocos2d {
|
2010-08-02 10:58:00 +08:00
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
// CCLayer
|
|
|
|
CCLayer::CCLayer()
|
2010-08-03 14:50:41 +08:00
|
|
|
:m_bIsTouchEnabled(false)
|
|
|
|
,m_bIsAccelerometerEnabled(false)
|
2010-12-13 14:10:39 +08:00
|
|
|
,m_bIsKeypadEnabled(false)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2011-04-27 22:45:54 +08:00
|
|
|
m_eTouchDelegateType = ccTouchDelegateAllBit;
|
2011-08-02 11:02:02 +08:00
|
|
|
setAnchorPoint(ccp(0.5f, 0.5f));
|
2010-08-02 10:58:00 +08:00
|
|
|
m_bIsRelativeAnchorPoint = false;
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
|
|
|
CCLayer::~CCLayer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-10-01 23:04:12 +08:00
|
|
|
bool CCLayer::init()
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CCDirector * pDirector;
|
2011-06-10 17:51:37 +08:00
|
|
|
CC_BREAK_IF(!(pDirector = CCDirector::sharedDirector()));
|
2010-10-01 23:04:12 +08:00
|
|
|
this->setContentSize(pDirector->getWinSize());
|
2011-07-01 11:26:26 +08:00
|
|
|
m_bIsTouchEnabled = false;
|
|
|
|
m_bIsAccelerometerEnabled = false;
|
2010-10-01 23:04:12 +08:00
|
|
|
// success
|
|
|
|
bRet = true;
|
2011-06-10 17:51:37 +08:00
|
|
|
} while(0);
|
2010-10-01 23:04:12 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLayer *CCLayer::node()
|
|
|
|
{
|
|
|
|
CCLayer *pRet = new CCLayer();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pRet)
|
2010-10-01 23:04:12 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-03 14:50:41 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
|
|
|
/// Touch and Accelerometer related
|
|
|
|
|
|
|
|
void CCLayer::registerWithTouchDispatcher()
|
|
|
|
{
|
2010-11-16 15:12:09 +08:00
|
|
|
CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-08-27 14:13:32 +08:00
|
|
|
void CCLayer::destroy(void)
|
|
|
|
{
|
|
|
|
this->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::keep(void)
|
|
|
|
{
|
|
|
|
this->retain();
|
|
|
|
}
|
|
|
|
|
2011-04-27 22:45:54 +08:00
|
|
|
void CCLayer::KeypadDestroy()
|
|
|
|
{
|
|
|
|
this->release();
|
|
|
|
}
|
|
|
|
|
2010-12-13 14:10:39 +08:00
|
|
|
void CCLayer::KeypadKeep()
|
|
|
|
{
|
|
|
|
this->retain();
|
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isTouchEnabled getter
|
|
|
|
bool CCLayer::getIsTouchEnabled()
|
|
|
|
{
|
|
|
|
return m_bIsTouchEnabled;
|
|
|
|
}
|
|
|
|
/// isTouchEnabled setter
|
|
|
|
void CCLayer::setIsTouchEnabled(bool enabled)
|
|
|
|
{
|
2010-08-02 10:58:00 +08:00
|
|
|
if (m_bIsTouchEnabled != enabled)
|
|
|
|
{
|
|
|
|
m_bIsTouchEnabled = enabled;
|
|
|
|
if (m_bIsRunning)
|
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
this->registerWithTouchDispatcher();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// have problems?
|
2010-11-16 15:12:09 +08:00
|
|
|
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
|
2010-08-02 10:58:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isAccelerometerEnabled getter
|
|
|
|
bool CCLayer::getIsAccelerometerEnabled()
|
|
|
|
{
|
|
|
|
return m_bIsAccelerometerEnabled;
|
|
|
|
}
|
|
|
|
/// isAccelerometerEnabled setter
|
|
|
|
void CCLayer::setIsAccelerometerEnabled(bool enabled)
|
|
|
|
{
|
2010-11-10 16:29:52 +08:00
|
|
|
if (enabled != m_bIsAccelerometerEnabled)
|
|
|
|
{
|
|
|
|
m_bIsAccelerometerEnabled = enabled;
|
|
|
|
|
|
|
|
if (m_bIsRunning)
|
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
2011-03-07 18:20:48 +08:00
|
|
|
CCAccelerometer::sharedAccelerometer()->addDelegate(this);
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-07 18:20:48 +08:00
|
|
|
CCAccelerometer::sharedAccelerometer()->removeDelegate(this);
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-13 14:10:39 +08:00
|
|
|
/// isKeypadEnabled getter
|
|
|
|
bool CCLayer::getIsKeypadEnabled()
|
|
|
|
{
|
|
|
|
return m_bIsKeypadEnabled;
|
|
|
|
}
|
|
|
|
/// isKeypadEnabled setter
|
|
|
|
void CCLayer::setIsKeypadEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled != m_bIsKeypadEnabled)
|
|
|
|
{
|
|
|
|
m_bIsKeypadEnabled = enabled;
|
|
|
|
|
|
|
|
if (m_bIsRunning)
|
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
CCKeypadDispatcher::sharedDispatcher()->addDelegate(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCKeypadDispatcher::sharedDispatcher()->removeDelegate(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// Callbacks
|
|
|
|
void CCLayer::onEnter()
|
|
|
|
{
|
|
|
|
// register 'parent' nodes first
|
|
|
|
// since events are propagated in reverse order
|
2010-08-05 18:07:14 +08:00
|
|
|
if (m_bIsTouchEnabled)
|
|
|
|
{
|
|
|
|
this->registerWithTouchDispatcher();
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
|
|
|
// then iterate over all the children
|
2010-09-02 14:54:42 +08:00
|
|
|
CCNode::onEnter();
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
// add this layer to concern the Accelerometer Sensor
|
|
|
|
if (m_bIsAccelerometerEnabled)
|
|
|
|
{
|
2011-03-07 18:20:48 +08:00
|
|
|
CCAccelerometer::sharedAccelerometer()->addDelegate(this);
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
2010-12-13 14:10:39 +08:00
|
|
|
|
|
|
|
// add this layer to concern the kaypad msg
|
|
|
|
if (m_bIsKeypadEnabled)
|
|
|
|
{
|
|
|
|
CCKeypadDispatcher::sharedDispatcher()->addDelegate(this);
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::onExit()
|
|
|
|
{
|
2010-08-05 18:07:14 +08:00
|
|
|
if( m_bIsTouchEnabled )
|
|
|
|
{
|
2010-11-16 15:12:09 +08:00
|
|
|
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this);
|
2010-08-05 18:07:14 +08:00
|
|
|
}
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
// remove this layer from the delegates who concern Accelerometer Sensor
|
|
|
|
if (m_bIsAccelerometerEnabled)
|
|
|
|
{
|
2011-03-07 18:20:48 +08:00
|
|
|
CCAccelerometer::sharedAccelerometer()->removeDelegate(this);
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
|
|
|
|
2010-12-13 14:10:39 +08:00
|
|
|
// remove this layer from the delegates who concern the kaypad msg
|
|
|
|
if (m_bIsKeypadEnabled)
|
|
|
|
{
|
|
|
|
CCKeypadDispatcher::sharedDispatcher()->removeDelegate(this);
|
|
|
|
}
|
|
|
|
|
2010-09-02 14:54:42 +08:00
|
|
|
CCNode::onExit();
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayer::onEnterTransitionDidFinish()
|
|
|
|
{
|
|
|
|
if (m_bIsAccelerometerEnabled)
|
|
|
|
{
|
2011-03-07 18:20:48 +08:00
|
|
|
CCAccelerometer::sharedAccelerometer()->addDelegate(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
|
|
|
{
|
2011-06-10 17:51:37 +08:00
|
|
|
CC_UNUSED_PARAM(pTouch);
|
|
|
|
CC_UNUSED_PARAM(pEvent);
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(false, "Layer#ccTouchBegan override me");
|
2010-07-30 17:18:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
|
2011-06-20 17:31:38 +08:00
|
|
|
{
|
|
|
|
if (isScriptHandlerExist(CCTOUCHBEGAN))
|
|
|
|
{
|
|
|
|
excuteScriptTouchesHandler(CCTOUCHBEGAN, pTouches);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
if (isScriptHandlerExist(CCTOUCHENDED))
|
|
|
|
{
|
|
|
|
excuteScriptTouchesHandler(CCTOUCHENDED, pTouches);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
if (isScriptHandlerExist(CCTOUCHMOVED))
|
|
|
|
{
|
|
|
|
excuteScriptTouchesHandler(CCTOUCHMOVED, pTouches);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayer::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
if (isScriptHandlerExist(CCTOUCHCANCELLED))
|
|
|
|
{
|
|
|
|
excuteScriptTouchesHandler(CCTOUCHCANCELLED, pTouches);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// ColorLayer
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
|
|
|
return m_cOpacity;
|
|
|
|
}
|
|
|
|
/// opacity setter
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayerColor::setOpacity(GLubyte var)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
m_cOpacity = var;
|
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// color getter
|
2011-08-17 21:19:57 +08:00
|
|
|
const ccColor3B& CCLayerColor::getColor()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
return m_tColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// color setter
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCLayerColor::setColor(const ccColor3B& var)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
m_tColor = var;
|
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// blendFunc getter
|
2010-12-27 09:40:45 +08:00
|
|
|
ccBlendFunc CCLayerColor::getBlendFunc()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
return m_tBlendFunc;
|
|
|
|
}
|
|
|
|
/// blendFunc setter
|
2010-12-27 09:40:45 +08:00
|
|
|
void CCLayerColor::setBlendFunc(ccBlendFunc var)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
m_tBlendFunc = var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
CCLayerColor * CCLayerColor::layerWithColorWidthHeight(const ccColor4B& color, GLfloat width, GLfloat height)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2010-12-27 09:40:45 +08:00
|
|
|
CCLayerColor * pLayer = new CCLayerColor();
|
2010-09-04 12:02:52 +08:00
|
|
|
if( pLayer && pLayer->initWithColorWidthHeight(color,width,height))
|
|
|
|
{
|
|
|
|
pLayer->autorelease();
|
|
|
|
return pLayer;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pLayer);
|
2010-09-04 12:02:52 +08:00
|
|
|
return NULL;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2011-08-17 21:19:57 +08:00
|
|
|
CCLayerColor * CCLayerColor::layerWithColor(const ccColor4B& color)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2010-12-27 09:40:45 +08:00
|
|
|
CCLayerColor * pLayer = new CCLayerColor();
|
2010-09-04 12:02:52 +08:00
|
|
|
if(pLayer && pLayer->initWithColor(color))
|
|
|
|
{
|
|
|
|
pLayer->autorelease();
|
|
|
|
return pLayer;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pLayer);
|
2010-09-04 12:02:52 +08:00
|
|
|
return NULL;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
bool CCLayerColor::initWithColorWidthHeight(const ccColor4B& color, GLfloat width, GLfloat height)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2010-07-21 11:13:32 +08:00
|
|
|
// default blend function
|
|
|
|
m_tBlendFunc.src = CC_BLEND_SRC;
|
|
|
|
m_tBlendFunc.dst = CC_BLEND_DST;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
m_tColor.r = color.r;
|
|
|
|
m_tColor.g = color.g;
|
|
|
|
m_tColor.b = color.b;
|
|
|
|
m_cOpacity = color.a;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
for (unsigned int i=0; i<sizeof(m_pSquareVertices) / sizeof(m_pSquareVertices[0]); i++ )
|
2010-09-04 12:02:52 +08:00
|
|
|
{
|
2011-07-01 11:26:26 +08:00
|
|
|
m_pSquareVertices[i].x = 0.0f;
|
|
|
|
m_pSquareVertices[i].y = 0.0f;
|
2010-09-04 12:02:52 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
this->updateColor();
|
2011-03-07 17:11:57 +08:00
|
|
|
this->setContentSize(CCSizeMake(width,height));
|
2010-09-04 12:02:52 +08:00
|
|
|
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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
2010-09-04 12:02:52 +08:00
|
|
|
this->initWithColorWidthHeight(color, s.width, s.height);
|
|
|
|
return true;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// override contentSize
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCLayerColor::setContentSize(const CCSize& size)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2011-07-01 11:26:26 +08:00
|
|
|
m_pSquareVertices[1].x = size.width * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
m_pSquareVertices[2].y = size.height * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
m_pSquareVertices[3].x = size.width * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
m_pSquareVertices[3].y = size.height * CC_CONTENT_SCALE_FACTOR();
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-09-02 14:54:42 +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
|
|
|
{
|
2011-03-07 17:11:57 +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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
this->setContentSize(CCSizeMake(w, m_tContentSize.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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
this->setContentSize(CCSizeMake(m_tContentSize.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
|
|
|
{
|
2010-12-27 09:40:45 +08:00
|
|
|
for( unsigned int i=0; i < 4; i++ )
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2011-07-01 11:26:26 +08:00
|
|
|
m_pSquareColors[i].r = m_tColor.r;
|
|
|
|
m_pSquareColors[i].g = m_tColor.g;
|
|
|
|
m_pSquareColors[i].b = m_tColor.b;
|
|
|
|
m_pSquareColors[i].a = m_cOpacity;
|
2010-07-21 11:13:32 +08:00
|
|
|
}
|
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
|
|
|
{
|
|
|
|
CCLayer::draw();
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
|
|
|
|
// Needed states: GL_VERTEX_ARRAY, GL_COLOR_ARRAY
|
|
|
|
// Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
glVertexPointer(2, GL_FLOAT, 0, m_pSquareVertices);
|
|
|
|
glColorPointer(4, GL_UNSIGNED_BYTE, 0, m_pSquareColors);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
bool newBlend = false;
|
|
|
|
if( m_tBlendFunc.src != CC_BLEND_SRC || m_tBlendFunc.dst != CC_BLEND_DST ) {
|
|
|
|
newBlend = true;
|
|
|
|
glBlendFunc(m_tBlendFunc.src, m_tBlendFunc.dst);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2010-07-21 11:13:32 +08:00
|
|
|
else if( m_cOpacity != 255 ) {
|
|
|
|
newBlend = true;
|
2010-07-12 17:56:06 +08:00
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
}
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
|
|
|
if( newBlend )
|
|
|
|
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
|
|
|
|
|
|
|
|
// restore default GL state
|
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
2010-07-21 11:13:32 +08:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
//
|
|
|
|
// CCLayerGradient
|
|
|
|
//
|
2011-08-17 21:19:57 +08:00
|
|
|
CCLayerGradient* CCLayerGradient::layerWithColor(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;
|
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
CCLayerGradient* CCLayerGradient::layerWithColor(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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
m_cStartOpacity = start.a;
|
|
|
|
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;
|
|
|
|
|
|
|
|
double c = sqrt(2.0);
|
|
|
|
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;
|
|
|
|
|
|
|
|
ccColor4B S = {
|
|
|
|
(unsigned char) m_tColor.r,
|
|
|
|
(unsigned char) m_tColor.g,
|
|
|
|
(unsigned char) m_tColor.b,
|
|
|
|
(unsigned char) (m_cStartOpacity * opacityf)
|
|
|
|
};
|
|
|
|
|
|
|
|
ccColor4B E = {
|
|
|
|
(unsigned char) m_endColor.r,
|
|
|
|
(unsigned char) m_endColor.g,
|
|
|
|
(unsigned char) m_endColor.b,
|
|
|
|
(unsigned char) (m_cEndOpacity * opacityf)
|
|
|
|
};
|
|
|
|
|
|
|
|
// (-1, -1)
|
2011-07-01 11:26:26 +08:00
|
|
|
m_pSquareColors[0].r = (GLubyte) (E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[0].g = (GLubyte) (E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[0].b = (GLubyte) (E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[0].a = (GLubyte) (E.a + (S.a - E.a) * ((c + u.x + u.y) / (2.0f * c)));
|
2011-04-27 22:45:54 +08:00
|
|
|
// (1, -1)
|
2011-07-01 11:26:26 +08:00
|
|
|
m_pSquareColors[1].r = (GLubyte) (E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[1].g = (GLubyte) (E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[1].b = (GLubyte) (E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[1].a = (GLubyte) (E.a + (S.a - E.a) * ((c - u.x + u.y) / (2.0f * c)));
|
2011-04-27 22:45:54 +08:00
|
|
|
// (-1, 1)
|
2011-07-01 11:26:26 +08:00
|
|
|
m_pSquareColors[2].r = (GLubyte) (E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[2].g = (GLubyte) (E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[2].b = (GLubyte) (E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[2].a = (GLubyte) (E.a + (S.a - E.a) * ((c + u.x - u.y) / (2.0f * c)));
|
2011-04-27 22:45:54 +08:00
|
|
|
// (1, 1)
|
2011-07-01 11:26:26 +08:00
|
|
|
m_pSquareColors[3].r = (GLubyte) (E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[3].g = (GLubyte) (E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[3].b = (GLubyte) (E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0f * c)));
|
|
|
|
m_pSquareColors[3].a = (GLubyte) (E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0f * c)));
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +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();
|
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +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
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +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
|
|
|
|
2011-07-05 15:39:50 +08:00
|
|
|
bool CCLayerGradient::getIsCompressedInterpolation()
|
|
|
|
{
|
|
|
|
return m_bCompressedInterpolation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLayerGradient::setIsCompressedInterpolation(bool compress)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2011-04-01 16:06:53 +08:00
|
|
|
CC_SAFE_RELEASE(m_pLayers);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
CCLayerMultiplex * CCLayerMultiplex::layerWithLayers(CCLayer * layer, ...)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2010-07-21 11:13:32 +08:00
|
|
|
va_list args;
|
2010-07-12 17:56:06 +08:00
|
|
|
va_start(args,layer);
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
CCLayerMultiplex * pMultiplexLayer = new CCLayerMultiplex();
|
2010-09-04 12:02:52 +08:00
|
|
|
if(pMultiplexLayer && pMultiplexLayer->initWithLayers(layer, args))
|
|
|
|
{
|
|
|
|
pMultiplexLayer->autorelease();
|
|
|
|
va_end(args);
|
|
|
|
return pMultiplexLayer;
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
va_end(args);
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pMultiplexLayer);
|
2010-09-04 12:02:52 +08:00
|
|
|
return NULL;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
CCLayerMultiplex * CCLayerMultiplex::layerWithLayer(CCLayer* layer)
|
2011-05-31 14:04:14 +08:00
|
|
|
{
|
2011-07-01 11:26:26 +08:00
|
|
|
CCLayerMultiplex * pMultiplexLayer = new CCLayerMultiplex();
|
2011-05-31 14:04:14 +08:00
|
|
|
pMultiplexLayer->initWithLayer(layer);
|
|
|
|
pMultiplexLayer->autorelease();
|
|
|
|
return pMultiplexLayer;
|
|
|
|
}
|
2011-07-01 11:26:26 +08:00
|
|
|
void CCLayerMultiplex::addLayer(CCLayer* layer)
|
2011-05-31 14:04:14 +08:00
|
|
|
{
|
|
|
|
assert(m_pLayers);
|
|
|
|
m_pLayers->addObject(layer);
|
|
|
|
}
|
|
|
|
|
2011-07-01 11:26:26 +08:00
|
|
|
bool CCLayerMultiplex::initWithLayer(CCLayer* layer)
|
2011-05-31 14:04:14 +08:00
|
|
|
{
|
|
|
|
m_pLayers = new CCMutableArray<CCLayer*>(1);
|
|
|
|
m_pLayers->addObject(layer);
|
|
|
|
m_nEnabledLayer = 0;
|
|
|
|
this->addChild(layer);
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-20 17:31:38 +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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
m_pLayers = new CCMutableArray<CCLayer*>(5);
|
2010-09-16 17:19:18 +08:00
|
|
|
//m_pLayers->retain();
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
m_pLayers->addObject(layer);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +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
|
|
|
}
|
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
m_nEnabledLayer = 0;
|
|
|
|
this->addChild(m_pLayers->getObjectAtIndex(m_nEnabledLayer));
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( n < m_pLayers->count(), "Invalid index in MultiplexLayer switchTo message" );
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
this->removeChild(m_pLayers->getObjectAtIndex(m_nEnabledLayer), true);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
m_nEnabledLayer = n;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
this->addChild(m_pLayers->getObjectAtIndex(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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert( n < m_pLayers->count(), "Invalid index in MultiplexLayer switchTo message" );
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
this->removeChild(m_pLayers->getObjectAtIndex(m_nEnabledLayer), true);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
//[layers replaceObjectAtIndex:enabledLayer withObject:[NSNull null]];
|
|
|
|
m_pLayers->replaceObjectAtIndex(m_nEnabledLayer, NULL);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
m_nEnabledLayer = n;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-21 11:13:32 +08:00
|
|
|
this->addChild(m_pLayers->getObjectAtIndex(n));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2010-10-01 23:04:12 +08:00
|
|
|
}//namespace cocos2d
|