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 "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"
|
2013-06-02 10:11:43 +08:00
|
|
|
#include "keyboard_dispatcher/CCKeyboardDispatcher.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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// Layer
|
|
|
|
Layer::Layer()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _touchEnabled(false)
|
|
|
|
, _accelerometerEnabled(false)
|
|
|
|
, _keyboardEnabled(false)
|
|
|
|
, _keypadEnabled(false)
|
|
|
|
, _touchPriority(0)
|
2013-06-20 14:13:12 +08:00
|
|
|
, _touchMode(kTouchesAllAtOnce)
|
2013-06-15 14:03:30 +08:00
|
|
|
{
|
|
|
|
_ignoreAnchorPointForPosition = true;
|
2013-07-12 14:11:55 +08:00
|
|
|
setAnchorPoint(Point(0.5f, 0.5f));
|
2010-08-02 10:58:00 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Layer::~Layer()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Layer::init()
|
2010-10-01 23:04:12 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool bRet = false;
|
|
|
|
do
|
2012-10-27 08:11:43 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Director * pDirector;
|
2013-07-12 06:24:23 +08:00
|
|
|
CC_BREAK_IF(!(pDirector = Director::getInstance()));
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setContentSize(pDirector->getWinSize());
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchEnabled = false;
|
|
|
|
_accelerometerEnabled = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
// success
|
|
|
|
bRet = true;
|
|
|
|
} while(0);
|
|
|
|
return bRet;
|
2010-10-01 23:04:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Layer *Layer::create()
|
2010-10-01 23:04:12 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Layer *pRet = new Layer();
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::registerWithTouchDispatcher()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
TouchDispatcher* pDispatcher = Director::getInstance()->getTouchDispatcher();
|
2012-08-31 21:23:23 +08:00
|
|
|
|
2013-07-16 09:55:06 +08:00
|
|
|
if( _touchMode == kTouchesAllAtOnce ) {
|
|
|
|
pDispatcher->addStandardDelegate(this, 0);
|
|
|
|
} else {
|
|
|
|
pDispatcher->addTargetedDelegate(this, _touchPriority, true);
|
2012-10-29 15:38:44 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int Layer::excuteScriptTouchHandler(int nEventType, Touch *pTouch)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
2013-07-02 15:23:51 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
Set touches;
|
|
|
|
touches.addObject((Object*)pTouch);
|
2013-07-16 09:55:06 +08:00
|
|
|
TouchesScriptData data(nEventType,(void*)this,&touches);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent event(kTouchesEvent,&data);
|
|
|
|
return ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
|
|
|
else if(kScriptTypeJavascript == _scriptType)
|
|
|
|
{
|
|
|
|
return ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchEvent(this, nEventType, pTouch);
|
|
|
|
}
|
|
|
|
//can not reach it
|
|
|
|
return 0;
|
2012-02-02 10:38:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int Layer::excuteScriptTouchHandler(int nEventType, Set *pTouches)
|
2012-02-02 10:38:26 +08:00
|
|
|
{
|
2013-07-02 15:23:51 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
TouchesScriptData data(nEventType,(void*)this,pTouches);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent event(kTouchesEvent,&data);
|
|
|
|
return ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
|
|
|
else if(kScriptTypeJavascript == _scriptType)
|
|
|
|
{
|
|
|
|
return ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerTouchesEvent(this, nEventType, pTouches);
|
|
|
|
}
|
|
|
|
return 0;
|
2012-02-02 10:38:26 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isTouchEnabled getter
|
2013-07-04 08:22:15 +08:00
|
|
|
bool Layer::isTouchEnabled() const
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _touchEnabled;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
/// isTouchEnabled setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::setTouchEnabled(bool enabled)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchEnabled != enabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchEnabled = enabled;
|
|
|
|
if (_running)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
this->registerWithTouchDispatcher();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// have problems?
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->getTouchDispatcher()->removeDelegate(this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::setTouchMode(ccTouchesMode mode)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_touchMode != mode)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchMode = mode;
|
2012-10-27 08:11:43 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _touchEnabled)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2012-10-27 08:11:43 +08:00
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::setTouchPriority(int priority)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchPriority != priority)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchPriority = priority;
|
2012-10-27 08:11:43 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _touchEnabled)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2012-10-27 08:11:43 +08:00
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
int Layer::getTouchPriority() const
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _touchPriority;
|
2012-10-27 08:11:43 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
int Layer::getTouchMode() const
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _touchMode;
|
2012-10-27 08:11:43 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isAccelerometerEnabled getter
|
2013-07-04 08:22:15 +08:00
|
|
|
bool Layer::isAccelerometerEnabled() const
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _accelerometerEnabled;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
/// isAccelerometerEnabled setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::setAccelerometerEnabled(bool enabled)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (enabled != _accelerometerEnabled)
|
2010-11-10 16:29:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_accelerometerEnabled = enabled;
|
2010-11-10 16:29:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_running)
|
2010-11-10 16:29:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director* pDirector = Director::getInstance();
|
2010-11-10 16:29:52 +08:00
|
|
|
if (enabled)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(Layer::didAccelerate, this));
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-19 16:10:53 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(nullptr);
|
2010-11-10 16:29:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2012-10-27 08:11:43 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::setAccelerometerInterval(double interval) {
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_accelerometerEnabled)
|
2012-10-27 08:11:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_running)
|
2012-10-27 08:11:43 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director* pDirector = Director::getInstance();
|
2012-11-14 06:57:05 +08:00
|
|
|
pDirector->getAccelerometer()->setAccelerometerInterval(interval);
|
2012-10-27 08:11:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::didAccelerate(Acceleration* pAccelerationValue)
|
2012-11-06 14:18:13 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pAccelerationValue);
|
2013-07-02 15:23:51 +08:00
|
|
|
if (kScriptTypeJavascript == _scriptType)
|
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEngineManager::sharedManager()->getScriptEngine()->executeAccelerometerEvent(this, pAccelerationValue);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
|
|
|
else if(kScriptTypeLua == _scriptType)
|
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
BasicScriptData data((void*)this,(void*)pAccelerationValue);
|
|
|
|
ScriptEvent event(kAccelerometerEvent,&data);
|
|
|
|
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
2012-10-27 08:11:43 +08:00
|
|
|
}
|
|
|
|
|
2013-05-31 20:29:32 +08:00
|
|
|
/// isKeyboardEnabled getter
|
2013-07-04 08:22:15 +08:00
|
|
|
bool Layer::isKeyboardEnabled() const
|
2013-05-31 20:29:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _keyboardEnabled;
|
2013-05-31 20:29:32 +08:00
|
|
|
}
|
|
|
|
/// isKeyboardEnabled setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::setKeyboardEnabled(bool enabled)
|
2013-05-31 20:29:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (enabled != _keyboardEnabled)
|
2013-05-31 20:29:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_keyboardEnabled = enabled;
|
2013-05-31 20:29:32 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director* pDirector = Director::getInstance();
|
2013-05-31 20:29:32 +08:00
|
|
|
if (enabled)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pDirector->getKeyboardDispatcher()->setKeyPressDelegate( CC_CALLBACK_1(Layer::keyPressed, this) );
|
|
|
|
pDirector->getKeyboardDispatcher()->setKeyReleaseDelegate( CC_CALLBACK_1(Layer::keyReleased, this) );
|
2013-05-31 20:29:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-01 18:22:09 +08:00
|
|
|
pDirector->getKeyboardDispatcher()->setKeyPressDelegate(nullptr);
|
|
|
|
pDirector->getKeyboardDispatcher()->setKeyReleaseDelegate(nullptr);
|
2013-05-31 20:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-13 14:10:39 +08:00
|
|
|
/// isKeypadEnabled getter
|
2013-07-04 08:22:15 +08:00
|
|
|
bool Layer::isKeypadEnabled() const
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _keypadEnabled;
|
2010-12-13 14:10:39 +08:00
|
|
|
}
|
|
|
|
/// isKeypadEnabled setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::setKeypadEnabled(bool enabled)
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (enabled != _keypadEnabled)
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_keypadEnabled = enabled;
|
2010-12-13 14:10:39 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_running)
|
2010-12-13 14:10:39 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director* pDirector = Director::getInstance();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::keyBackClicked(void)
|
2012-12-02 15:17:34 +08:00
|
|
|
{
|
2013-07-15 10:38:16 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
KeypadScriptData data(kTypeBackClicked,(void*)this);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent event(kKeypadEvent,(void*)&data);
|
|
|
|
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
|
|
|
else if(kScriptTypeJavascript == _scriptType)
|
2012-12-02 15:17:34 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ScriptEngineManager::sharedManager()->getScriptEngine()->executeLayerKeypadEvent(this, kTypeBackClicked);
|
2012-12-02 15:17:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::keyMenuClicked(void)
|
2012-12-02 15:17:34 +08:00
|
|
|
{
|
2013-07-15 10:38:16 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2012-12-02 15:17:34 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
KeypadScriptData data(kTypeMenuClicked,(void*)this);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent event(kKeypadEvent,(void*)&data);
|
|
|
|
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&event);
|
2012-12-02 15:17:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// Callbacks
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::onEnter()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director* pDirector = Director::getInstance();
|
2012-04-19 14:35:52 +08:00
|
|
|
// register 'parent' nodes first
|
|
|
|
// since events are propagated in reverse order
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchEnabled)
|
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
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::onEnter();
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
// add this layer to concern the Accelerometer Sensor
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_accelerometerEnabled)
|
2010-11-10 16:29:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(Layer::didAccelerate, 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
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_keypadEnabled)
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::onExit()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director* pDirector = Director::getInstance();
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _touchEnabled )
|
2012-09-20 11:32:02 +08:00
|
|
|
{
|
|
|
|
pDirector->getTouchDispatcher()->removeDelegate(this);
|
|
|
|
}
|
2010-11-10 16:29:52 +08:00
|
|
|
|
|
|
|
// remove this layer from the delegates who concern Accelerometer Sensor
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_accelerometerEnabled)
|
2010-11-10 16:29:52 +08:00
|
|
|
{
|
2013-06-20 18:01:53 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(nullptr);
|
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
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_keypadEnabled)
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::onExit();
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::onEnterTransitionDidFinish()
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_accelerometerEnabled)
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director* pDirector = Director::getInstance();
|
2013-06-20 14:13:12 +08:00
|
|
|
pDirector->getAccelerometer()->setDelegate(CC_CALLBACK_1(Layer::didAccelerate, this));
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::onEnterTransitionDidFinish();
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Layer::ccTouchBegan(Touch *pTouch, Event *pEvent)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (kScriptTypeNone != _scriptType)
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::ccTouchMoved(Touch *pTouch, Event *pEvent)
|
2012-08-31 21:23:23 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (kScriptTypeNone != _scriptType)
|
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);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::ccTouchEnded(Touch *pTouch, Event *pEvent)
|
2012-08-31 21:23:23 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (kScriptTypeNone != _scriptType)
|
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);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::ccTouchCancelled(Touch *pTouch, Event *pEvent)
|
2012-08-31 21:23:23 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (kScriptTypeNone != _scriptType)
|
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);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::ccTouchesBegan(Set *pTouches, Event *pEvent)
|
2011-06-20 17:31:38 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (kScriptTypeNone != _scriptType)
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::ccTouchesMoved(Set *pTouches, Event *pEvent)
|
2011-06-20 17:31:38 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (kScriptTypeNone != _scriptType)
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::ccTouchesEnded(Set *pTouches, Event *pEvent)
|
2011-06-20 17:31:38 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (kScriptTypeNone != _scriptType)
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Layer::ccTouchesCancelled(Set *pTouches, Event *pEvent)
|
2011-06-20 17:31:38 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (kScriptTypeNone != _scriptType)
|
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
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerRGBA::LayerRGBA()
|
2013-02-28 11:55:36 +08:00
|
|
|
: _displayedOpacity(255)
|
|
|
|
, _realOpacity (255)
|
2013-07-08 18:11:32 +08:00
|
|
|
, _displayedColor(Color3B::WHITE)
|
|
|
|
, _realColor(Color3B::WHITE)
|
2013-02-28 11:55:36 +08:00
|
|
|
, _cascadeOpacityEnabled(false)
|
2013-03-03 10:32:09 +08:00
|
|
|
, _cascadeColorEnabled(false)
|
2013-02-28 11:55:36 +08:00
|
|
|
{}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerRGBA::~LayerRGBA() {}
|
2013-02-27 09:38:30 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LayerRGBA::init()
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Layer::init())
|
2013-02-27 18:21:35 +08:00
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
_displayedOpacity = _realOpacity = 255;
|
2013-07-08 18:11:32 +08:00
|
|
|
_displayedColor = _realColor = Color3B::WHITE;
|
2013-02-27 18:21:35 +08:00
|
|
|
setCascadeOpacityEnabled(false);
|
|
|
|
setCascadeColorEnabled(false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
GLubyte LayerRGBA::getOpacity() const
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
return _realOpacity;
|
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
GLubyte LayerRGBA::getDisplayedOpacity() const
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
return _displayedOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Override synthesized setOpacity to recurse items */
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerRGBA::setOpacity(GLubyte opacity)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity = opacity;
|
|
|
|
|
2013-02-27 18:21:35 +08:00
|
|
|
if( _cascadeOpacityEnabled )
|
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
GLubyte parentOpacity = 255;
|
2013-06-20 14:13:12 +08:00
|
|
|
RGBAProtocol *parent = dynamic_cast<RGBAProtocol*>(_parent);
|
2013-02-27 18:21:35 +08:00
|
|
|
if (parent && parent->isCascadeOpacityEnabled())
|
|
|
|
{
|
|
|
|
parentOpacity = parent->getDisplayedOpacity();
|
|
|
|
}
|
2013-02-28 11:55:36 +08:00
|
|
|
updateDisplayedOpacity(parentOpacity);
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
const Color3B& LayerRGBA::getColor() const
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
return _realColor;
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
const Color3B& LayerRGBA::getDisplayedColor() const
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
return _displayedColor;
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void LayerRGBA::setColor(const Color3B& color)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
_displayedColor = _realColor = color;
|
|
|
|
|
2013-02-27 18:21:35 +08:00
|
|
|
if (_cascadeColorEnabled)
|
|
|
|
{
|
2013-07-08 18:11:32 +08:00
|
|
|
Color3B parentColor = Color3B::WHITE;
|
2013-06-20 14:13:12 +08:00
|
|
|
RGBAProtocol* parent = dynamic_cast<RGBAProtocol*>(_parent);
|
2013-02-27 18:21:35 +08:00
|
|
|
if (parent && parent->isCascadeColorEnabled())
|
|
|
|
{
|
|
|
|
parentColor = parent->getDisplayedColor();
|
|
|
|
}
|
|
|
|
|
2013-02-28 11:55:36 +08:00
|
|
|
updateDisplayedColor(parentColor);
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerRGBA::updateDisplayedOpacity(GLubyte parentOpacity)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity * parentOpacity/255.0;
|
|
|
|
|
2013-02-27 18:21:35 +08:00
|
|
|
if (_cascadeOpacityEnabled)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, obj)
|
2013-02-27 18:21:35 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
RGBAProtocol *item = dynamic_cast<RGBAProtocol*>(obj);
|
2013-02-27 18:21:35 +08:00
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
item->updateDisplayedOpacity(_displayedOpacity);
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void LayerRGBA::updateDisplayedColor(const Color3B& parentColor)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
_displayedColor.r = _realColor.r * parentColor.r/255.0;
|
|
|
|
_displayedColor.g = _realColor.g * parentColor.g/255.0;
|
|
|
|
_displayedColor.b = _realColor.b * parentColor.b/255.0;
|
|
|
|
|
2013-02-27 18:21:35 +08:00
|
|
|
if (_cascadeColorEnabled)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *obj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, obj)
|
2013-02-27 18:21:35 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
RGBAProtocol *item = dynamic_cast<RGBAProtocol*>(obj);
|
2013-02-27 18:21:35 +08:00
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
item->updateDisplayedColor(_displayedColor);
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
bool LayerRGBA::isCascadeOpacityEnabled() const
|
2013-02-28 11:55:36 +08:00
|
|
|
{
|
|
|
|
return _cascadeOpacityEnabled;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerRGBA::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
|
2013-02-28 11:55:36 +08:00
|
|
|
{
|
|
|
|
_cascadeOpacityEnabled = cascadeOpacityEnabled;
|
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
bool LayerRGBA::isCascadeColorEnabled() const
|
2013-02-28 11:55:36 +08:00
|
|
|
{
|
|
|
|
return _cascadeColorEnabled;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerRGBA::setCascadeColorEnabled(bool cascadeColorEnabled)
|
2013-02-28 11:55:36 +08:00
|
|
|
{
|
|
|
|
_cascadeColorEnabled = cascadeColorEnabled;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/// LayerColor
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerColor::LayerColor()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2011-07-05 15:39:50 +08:00
|
|
|
// default blend function
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc.src = CC_BLEND_SRC;
|
|
|
|
_blendFunc.dst = CC_BLEND_DST;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2011-07-05 15:39:50 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerColor::~LayerColor()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// blendFunc getter
|
2013-07-05 16:49:22 +08:00
|
|
|
const BlendFunc &LayerColor::getBlendFunc() const
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _blendFunc;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
/// blendFunc setter
|
2013-07-05 16:49:22 +08:00
|
|
|
void LayerColor::setBlendFunc(const BlendFunc &var)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc = var;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerColor* LayerColor::create()
|
2012-11-01 22:17:12 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerColor* pRet = new LayerColor();
|
2012-11-01 22:17:12 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
LayerColor * LayerColor::create(const Color4B& color, GLfloat width, GLfloat height)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerColor * pLayer = new LayerColor();
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
LayerColor * LayerColor::create(const Color4B& color)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerColor * pLayer = new LayerColor();
|
2012-04-19 14:35:52 +08:00
|
|
|
if(pLayer && pLayer->initWithColor(color))
|
|
|
|
{
|
|
|
|
pLayer->autorelease();
|
|
|
|
return pLayer;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pLayer);
|
|
|
|
return NULL;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LayerColor::init()
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Size s = Director::getInstance()->getWinSize();
|
2013-07-05 16:49:22 +08:00
|
|
|
return initWithColor(Color4B(0,0,0,0), s.width, s.height);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
bool LayerColor::initWithColor(const Color4B& color, GLfloat w, GLfloat h)
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Layer::init())
|
2013-02-27 18:21:35 +08:00
|
|
|
{
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// default blend function
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc.src = GL_SRC_ALPHA;
|
|
|
|
_blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2013-02-27 18:21:35 +08:00
|
|
|
_displayedColor.r = _realColor.r = color.r;
|
|
|
|
_displayedColor.g = _realColor.g = color.g;
|
|
|
|
_displayedColor.b = _realColor.b = color.b;
|
2013-04-23 23:02:39 +08:00
|
|
|
_displayedOpacity = _realOpacity = color.a;
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (size_t i = 0; i<sizeof(_squareVertices) / sizeof( _squareVertices[0]); i++ )
|
2013-02-27 18:21:35 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_squareVertices[i].x = 0.0f;
|
|
|
|
_squareVertices[i].y = 0.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
updateColor();
|
2013-07-12 14:30:26 +08:00
|
|
|
setContentSize(Size(w, h));
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->programForKey(kShader_PositionColor));
|
2013-07-02 14:45:06 +08:00
|
|
|
return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-02 14:45:06 +08:00
|
|
|
return false;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
bool LayerColor::initWithColor(const Color4B& color)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Size s = Director::getInstance()->getWinSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
this->initWithColor(color, s.width, s.height);
|
|
|
|
return true;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// override contentSize
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerColor::setContentSize(const Size & size)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_squareVertices[1].x = size.width;
|
|
|
|
_squareVertices[2].y = size.height;
|
|
|
|
_squareVertices[3].x = size.width;
|
|
|
|
_squareVertices[3].y = size.height;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Layer::setContentSize(size);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerColor::changeWidthAndHeight(GLfloat w ,GLfloat h)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
this->setContentSize(Size(w, h));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerColor::changeWidth(GLfloat w)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
this->setContentSize(Size(w, _contentSize.height));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerColor::changeHeight(GLfloat h)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
this->setContentSize(Size(_contentSize.width, h));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerColor::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++ )
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_squareColors[i].r = _displayedColor.r / 255.0f;
|
|
|
|
_squareColors[i].g = _displayedColor.g / 255.0f;
|
|
|
|
_squareColors[i].b = _displayedColor.b / 255.0f;
|
|
|
|
_squareColors[i].a = _displayedOpacity / 255.0f;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerColor::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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ccGLEnableVertexAttribs( kVertexAttribFlag_Position | kVertexAttribFlag_Color );
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
2013-04-09 08:51:30 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
2013-07-05 16:49:22 +08:00
|
|
|
setGLBufferData(_squareVertices, 4 * sizeof(Vertex2F), 0);
|
2013-06-20 14:13:12 +08:00
|
|
|
glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-09 08:51:30 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
setGLBufferData(_squareColors, 4 * sizeof(Color4F), 1);
|
2013-06-20 14:13:12 +08:00
|
|
|
glVertexAttribPointer(kVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, 0);
|
2013-04-09 08:51:30 +08:00
|
|
|
#else
|
2013-06-20 14:13:12 +08:00
|
|
|
glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, _squareVertices);
|
|
|
|
glVertexAttribPointer(kVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
|
2013-04-09 08:51:30 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-08 14:16:29 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccGLBlendFunc( _blendFunc.src, _blendFunc.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
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void LayerColor::setColor(const Color3B &color)
|
2013-02-27 18:21:35 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerRGBA::setColor(color);
|
2013-02-27 18:21:35 +08:00
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerColor::setOpacity(GLubyte opacity)
|
2013-02-27 18:21:35 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerRGBA::setOpacity(opacity);
|
2013-02-27 18:21:35 +08:00
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// LayerGradient
|
2010-12-27 09:40:45 +08:00
|
|
|
//
|
2013-07-05 16:49:22 +08:00
|
|
|
LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerGradient * pLayer = new LayerGradient();
|
2010-12-27 09:40:45 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end, const Point& v)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerGradient * pLayer = new LayerGradient();
|
2010-12-27 09:40:45 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerGradient* LayerGradient::create()
|
2012-11-01 22:17:12 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerGradient* pRet = new LayerGradient();
|
2012-11-01 22:17:12 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LayerGradient::init()
|
2012-10-31 18:00:51 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
return initWithColor(Color4B(0, 0, 0, 255), Color4B(0, 0, 0, 255));
|
2012-10-31 18:00:51 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
return initWithColor(start, end, Point(0, -1));
|
2011-04-27 22:45:54 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end, const Point& v)
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_endColor.r = end.r;
|
|
|
|
_endColor.g = end.g;
|
|
|
|
_endColor.b = end.b;
|
2011-04-27 22:45:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_endOpacity = end.a;
|
|
|
|
_startOpacity = start.a;
|
|
|
|
_alongVector = v;
|
2011-04-27 22:45:54 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_compressedInterpolation = true;
|
2011-07-01 11:26:26 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
return LayerColor::initWithColor(Color4B(start.r, start.g, start.b, 255));
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerGradient::updateColor()
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerColor::updateColor();
|
2010-12-27 09:40:45 +08:00
|
|
|
|
2013-07-11 16:38:58 +08:00
|
|
|
float h = _alongVector.getLength();
|
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);
|
2013-07-12 14:11:55 +08:00
|
|
|
Point u = Point(_alongVector.x / h, _alongVector.y / h);
|
2011-04-27 22:45:54 +08:00
|
|
|
|
2011-07-05 15:39:50 +08:00
|
|
|
// Compressed Interpolation mode
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_compressedInterpolation)
|
2011-07-05 15:39:50 +08:00
|
|
|
{
|
|
|
|
float h2 = 1 / ( fabsf(u.x) + fabsf(u.y) );
|
2013-07-11 16:38:58 +08:00
|
|
|
u = u * (h2 * (float)c);
|
2011-07-01 11:26:26 +08:00
|
|
|
}
|
|
|
|
|
2013-02-27 18:21:35 +08:00
|
|
|
float opacityf = (float)_displayedOpacity / 255.0f;
|
2011-04-27 22:45:54 +08:00
|
|
|
|
2013-07-07 21:08:14 +08:00
|
|
|
Color4F S(
|
2013-02-27 18:21:35 +08:00
|
|
|
_displayedColor.r / 255.0f,
|
|
|
|
_displayedColor.g / 255.0f,
|
|
|
|
_displayedColor.b / 255.0f,
|
2013-06-15 14:03:30 +08:00
|
|
|
_startOpacity * opacityf / 255.0f
|
2013-07-07 21:08:14 +08:00
|
|
|
);
|
2011-04-27 22:45:54 +08:00
|
|
|
|
2013-07-07 21:08:14 +08:00
|
|
|
Color4F E(
|
2013-06-15 14:03:30 +08:00
|
|
|
_endColor.r / 255.0f,
|
|
|
|
_endColor.g / 255.0f,
|
|
|
|
_endColor.b / 255.0f,
|
|
|
|
_endOpacity * opacityf / 255.0f
|
2013-07-07 21:08:14 +08:00
|
|
|
);
|
2011-04-27 22:45:54 +08:00
|
|
|
|
|
|
|
// (-1, -1)
|
2013-06-15 14:03:30 +08:00
|
|
|
_squareColors[0].r = E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0f * c));
|
|
|
|
_squareColors[0].g = E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0f * c));
|
|
|
|
_squareColors[0].b = E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0f * c));
|
|
|
|
_squareColors[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)
|
2013-06-15 14:03:30 +08:00
|
|
|
_squareColors[1].r = E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0f * c));
|
|
|
|
_squareColors[1].g = E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0f * c));
|
|
|
|
_squareColors[1].b = E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0f * c));
|
|
|
|
_squareColors[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)
|
2013-06-15 14:03:30 +08:00
|
|
|
_squareColors[2].r = E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0f * c));
|
|
|
|
_squareColors[2].g = E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0f * c));
|
|
|
|
_squareColors[2].b = E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0f * c));
|
|
|
|
_squareColors[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)
|
2013-06-15 14:03:30 +08:00
|
|
|
_squareColors[3].r = E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0f * c));
|
|
|
|
_squareColors[3].g = E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0f * c));
|
|
|
|
_squareColors[3].b = E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0f * c));
|
|
|
|
_squareColors[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-07-05 16:49:22 +08:00
|
|
|
const Color3B& LayerGradient::getStartColor() const
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-02-27 18:21:35 +08:00
|
|
|
return _realColor;
|
2011-04-27 22:45:54 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void LayerGradient::setStartColor(const Color3B& 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
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void LayerGradient::setEndColor(const Color3B& color)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_endColor = color;
|
2011-04-27 22:45:54 +08:00
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
const Color3B& LayerGradient::getEndColor() const
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _endColor;
|
2011-04-27 22:45:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerGradient::setStartOpacity(GLubyte o)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_startOpacity = o;
|
2011-04-27 22:45:54 +08:00
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
GLubyte LayerGradient::getStartOpacity()
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _startOpacity;
|
2011-04-27 22:45:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerGradient::setEndOpacity(GLubyte o)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_endOpacity = o;
|
2011-04-27 22:45:54 +08:00
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
GLubyte LayerGradient::getEndOpacity()
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _endOpacity;
|
2011-04-27 22:45:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerGradient::setVector(const Point& var)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_alongVector = var;
|
2011-04-27 22:45:54 +08:00
|
|
|
updateColor();
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
const Point& LayerGradient::getVector() const
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _alongVector;
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-07-04 12:50:17 +08:00
|
|
|
bool LayerGradient::isCompressedInterpolation() const
|
2011-07-05 15:39:50 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _compressedInterpolation;
|
2011-07-05 15:39:50 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerGradient::setCompressedInterpolation(bool compress)
|
2011-07-05 15:39:50 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_compressedInterpolation = compress;
|
2011-07-05 15:39:50 +08:00
|
|
|
updateColor();
|
2011-07-01 11:26:26 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// MultiplexLayer
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex::LayerMultiplex()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _enabledLayer(0)
|
|
|
|
, _layers(NULL)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex::~LayerMultiplex()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_layers);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex * LayerMultiplex::create(Layer * 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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex * pMultiplexLayer = new LayerMultiplex();
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex * LayerMultiplex::createWithLayer(Layer* layer)
|
2011-05-31 14:04:14 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return LayerMultiplex::create(layer, NULL);
|
2011-05-31 14:04:14 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex* LayerMultiplex::create()
|
2012-11-01 22:17:12 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex* pRet = new LayerMultiplex();
|
2012-11-01 22:17:12 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex* LayerMultiplex::createWithArray(Array* arrayOfLayers)
|
2012-11-30 21:13:25 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex* pRet = new LayerMultiplex();
|
2012-11-30 21:13:25 +08:00
|
|
|
if (pRet && pRet->initWithArray(arrayOfLayers))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-11-01 22:17:12 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerMultiplex::addLayer(Layer* layer)
|
2011-05-31 14:04:14 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCAssert(_layers, "");
|
|
|
|
_layers->addObject(layer);
|
2011-05-31 14:04:14 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LayerMultiplex::initWithLayers(Layer *layer, va_list params)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Layer::init())
|
2012-11-30 21:13:25 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_layers = Array::createWithCapacity(5);
|
2013-06-15 14:03:30 +08:00
|
|
|
_layers->retain();
|
|
|
|
_layers->addObject(layer);
|
2012-12-04 10:36:13 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Layer *l = va_arg(params,Layer*);
|
2012-11-30 21:13:25 +08:00
|
|
|
while( l ) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_layers->addObject(l);
|
2013-06-20 14:13:12 +08:00
|
|
|
l = va_arg(params,Layer*);
|
2012-11-30 21:13:25 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabledLayer = 0;
|
2013-06-20 14:13:12 +08:00
|
|
|
this->addChild((Node*)_layers->objectAtIndex(_enabledLayer));
|
2012-11-30 21:13:25 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LayerMultiplex::initWithArray(Array* arrayOfLayers)
|
2012-11-30 21:13:25 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Layer::init())
|
2012-12-04 10:36:13 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_layers = Array::createWithCapacity(arrayOfLayers->count());
|
2013-06-15 14:03:30 +08:00
|
|
|
_layers->addObjectsFromArray(arrayOfLayers);
|
|
|
|
_layers->retain();
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabledLayer = 0;
|
2013-06-20 14:13:12 +08:00
|
|
|
this->addChild((Node*)_layers->objectAtIndex(_enabledLayer));
|
2012-12-04 10:36:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerMultiplex::switchTo(unsigned int n)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCAssert( n < _layers->count(), "Invalid index in MultiplexLayer switchTo message" );
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
this->removeChild((Node*)_layers->objectAtIndex(_enabledLayer), true);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabledLayer = n;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
this->addChild((Node*)_layers->objectAtIndex(n));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void LayerMultiplex::switchToAndReleaseMe(unsigned int n)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCAssert( n < _layers->count(), "Invalid index in MultiplexLayer switchTo message" );
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
this->removeChild((Node*)_layers->objectAtIndex(_enabledLayer), true);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//[layers replaceObjectAtIndex:enabledLayer withObject:[NSNull null]];
|
2013-06-15 14:03:30 +08:00
|
|
|
_layers->replaceObjectAtIndex(_enabledLayer, NULL);
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabledLayer = n;
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
this->addChild((Node*)_layers->objectAtIndex(n));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
NS_CC_END
|