2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-01 11:26:26 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
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>
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCLayer.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCScriptSupport.h"
|
|
|
|
#include "platform/CCDevice.h"
|
2014-05-10 12:29:24 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "renderer/CCGLProgramState.h"
|
2014-05-10 12:29:24 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventListenerTouch.h"
|
|
|
|
#include "base/CCEventTouch.h"
|
|
|
|
#include "base/CCEventKeyboard.h"
|
|
|
|
#include "base/CCEventListenerKeyboard.h"
|
|
|
|
#include "base/CCEventAcceleration.h"
|
|
|
|
#include "base/CCEventListenerAcceleration.h"
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2017-05-22 13:42:00 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
#include "platform/desktop/CCGLViewImpl-desktop.h"
|
|
|
|
#endif
|
|
|
|
|
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-11-19 13:56:30 +08:00
|
|
|
: _touchEnabled(false)
|
|
|
|
, _accelerometerEnabled(false)
|
|
|
|
, _keyboardEnabled(false)
|
|
|
|
, _touchListener(nullptr)
|
|
|
|
, _keyboardListener(nullptr)
|
|
|
|
, _accelerationListener(nullptr)
|
2013-12-27 15:06:16 +08:00
|
|
|
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
|
|
|
|
, _swallowsTouches(true)
|
2013-06-15 14:03:30 +08:00
|
|
|
{
|
|
|
|
_ignoreAnchorPointForPosition = true;
|
2014-05-15 01:07:09 +08:00
|
|
|
setAnchorPoint(Vec2(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
|
|
|
{
|
2014-05-02 07:42:35 +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
|
|
|
{
|
2014-05-31 07:42:05 +08:00
|
|
|
Director * director = Director::getInstance();
|
|
|
|
setContentSize(director->getWinSize());
|
|
|
|
return true;
|
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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Layer *ret = new (std::nothrow) Layer();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->init())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-10-01 23:04:12 +08:00
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
2010-10-01 23:04:12 +08:00
|
|
|
}
|
2010-08-03 14:50:41 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
int Layer::executeScriptTouchHandler(EventTouch::EventCode eventType, Touch* touch, Event* event)
|
2013-11-19 13:56:30 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-19 13:56:30 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
TouchScriptData data(eventType, this, touch, event);
|
|
|
|
ScriptEvent scriptEvent(kTouchEvent, &data);
|
|
|
|
return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(eventType);
|
|
|
|
CC_UNUSED_PARAM(touch);
|
|
|
|
CC_UNUSED_PARAM(event);
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2013-11-20 10:26:00 +08:00
|
|
|
return 0;
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType, const std::vector<Touch*>& touches, Event* event)
|
2013-11-19 13:56:30 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
TouchesScriptData data(eventType, this, touches, event);
|
|
|
|
ScriptEvent scriptEvent(kTouchesEvent, &data);
|
|
|
|
return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
|
2013-11-20 10:26:00 +08:00
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(eventType);
|
|
|
|
CC_UNUSED_PARAM(touches);
|
|
|
|
CC_UNUSED_PARAM(event);
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2013-11-20 10:26:00 +08:00
|
|
|
return 0;
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
bool Layer::ccTouchBegan(Touch* /*pTouch*/, Event* /*pEvent*/) {return false;};
|
|
|
|
void Layer::ccTouchMoved(Touch* /*pTouch*/, Event* /*pEvent*/) {}
|
|
|
|
void Layer::ccTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/) {}
|
|
|
|
void Layer::ccTouchCancelled(Touch* /*pTouch*/, Event* /*pEvent*/) {}
|
|
|
|
void Layer::ccTouchesBegan(__Set* /*pTouches*/, Event* /*pEvent*/) {}
|
|
|
|
void Layer::ccTouchesMoved(__Set* /*pTouches*/, Event* /*pEvent*/) {}
|
|
|
|
void Layer::ccTouchesEnded(__Set* /*pTouches*/, Event* /*pEvent*/) {}
|
|
|
|
void Layer::ccTouchesCancelled(__Set* /*pTouches*/, Event* /*pEvent*/) {}
|
|
|
|
|
2013-11-20 10:26:00 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (push)
|
|
|
|
#pragma warning (disable: 4996)
|
|
|
|
#endif
|
|
|
|
|
2013-11-20 10:40:43 +08:00
|
|
|
/// isTouchEnabled getter
|
|
|
|
bool Layer::isTouchEnabled() const
|
|
|
|
{
|
|
|
|
return _touchEnabled;
|
|
|
|
}
|
|
|
|
|
2013-11-19 13:56:30 +08:00
|
|
|
/// isTouchEnabled setter
|
|
|
|
void Layer::setTouchEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (_touchEnabled != enabled)
|
|
|
|
{
|
|
|
|
_touchEnabled = enabled;
|
2013-11-19 15:41:22 +08:00
|
|
|
if (enabled)
|
2013-11-19 13:56:30 +08:00
|
|
|
{
|
2013-11-20 10:26:00 +08:00
|
|
|
if (_touchListener != nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE )
|
|
|
|
{
|
|
|
|
// Register Touch Event
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
|
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(Layer::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(Layer::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(Layer::onTouchesEnded, this);
|
|
|
|
listener->onTouchesCancelled = CC_CALLBACK_2(Layer::onTouchesCancelled, this);
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
_touchListener = listener;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Register Touch Event
|
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->setSwallowTouches(_swallowsTouches);
|
|
|
|
|
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(Layer::onTouchBegan, this);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(Layer::onTouchMoved, this);
|
|
|
|
listener->onTouchEnded = CC_CALLBACK_2(Layer::onTouchEnded, this);
|
|
|
|
listener->onTouchCancelled = CC_CALLBACK_2(Layer::onTouchCancelled, this);
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
_touchListener = listener;
|
|
|
|
}
|
2013-11-19 15:41:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
|
|
|
_touchListener = nullptr;
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layer::setTouchMode(Touch::DispatchMode mode)
|
|
|
|
{
|
|
|
|
if(_touchMode != mode)
|
|
|
|
{
|
|
|
|
_touchMode = mode;
|
|
|
|
|
|
|
|
if( _touchEnabled)
|
|
|
|
{
|
2013-11-20 10:26:00 +08:00
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layer::setSwallowsTouches(bool swallowsTouches)
|
|
|
|
{
|
|
|
|
if (_swallowsTouches != swallowsTouches)
|
|
|
|
{
|
|
|
|
_swallowsTouches = swallowsTouches;
|
|
|
|
|
|
|
|
if( _touchEnabled)
|
|
|
|
{
|
2013-11-20 10:26:00 +08:00
|
|
|
setTouchEnabled(false);
|
|
|
|
setTouchEnabled(true);
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Touch::DispatchMode Layer::getTouchMode() const
|
|
|
|
{
|
|
|
|
return _touchMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Layer::isSwallowsTouches() const
|
|
|
|
{
|
|
|
|
return _swallowsTouches;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isAccelerometerEnabled getter
|
|
|
|
bool Layer::isAccelerometerEnabled() const
|
|
|
|
{
|
|
|
|
return _accelerometerEnabled;
|
|
|
|
}
|
|
|
|
/// isAccelerometerEnabled setter
|
|
|
|
void Layer::setAccelerometerEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled != _accelerometerEnabled)
|
|
|
|
{
|
|
|
|
_accelerometerEnabled = enabled;
|
|
|
|
|
|
|
|
Device::setAccelerometerEnabled(enabled);
|
|
|
|
|
2013-11-20 10:26:00 +08:00
|
|
|
_eventDispatcher->removeEventListener(_accelerationListener);
|
|
|
|
_accelerationListener = nullptr;
|
2013-11-19 13:56:30 +08:00
|
|
|
|
2013-11-20 10:26:00 +08:00
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
_accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(Layer::onAcceleration, this));
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layer::setAccelerometerInterval(double interval) {
|
|
|
|
if (_accelerometerEnabled)
|
|
|
|
{
|
|
|
|
if (_running)
|
|
|
|
{
|
|
|
|
Device::setAccelerometerInterval(interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void Layer::onAcceleration(Acceleration* acc, Event* /*unused_event*/)
|
2013-11-19 13:56:30 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-11-19 13:56:30 +08:00
|
|
|
if(kScriptTypeNone != _scriptType)
|
|
|
|
{
|
2013-11-20 10:26:00 +08:00
|
|
|
BasicScriptData data(this,(void*)acc);
|
2013-11-19 13:56:30 +08:00
|
|
|
ScriptEvent event(kAccelerometerEvent,&data);
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(acc);
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void Layer::onKeyPressed(EventKeyboard::KeyCode /*keyCode*/, Event* /*unused_event*/)
|
2013-11-19 13:56:30 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void Layer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* /*unused_event*/)
|
2013-11-19 13:56:30 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-11-19 13:56:30 +08:00
|
|
|
if(kScriptTypeNone != _scriptType)
|
|
|
|
{
|
|
|
|
KeypadScriptData data(keyCode, this);
|
|
|
|
ScriptEvent event(kKeypadEvent,&data);
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(keyCode);
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isKeyboardEnabled getter
|
|
|
|
bool Layer::isKeyboardEnabled() const
|
|
|
|
{
|
|
|
|
return _keyboardEnabled;
|
|
|
|
}
|
|
|
|
/// isKeyboardEnabled setter
|
|
|
|
void Layer::setKeyboardEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled != _keyboardEnabled)
|
|
|
|
{
|
|
|
|
_keyboardEnabled = enabled;
|
|
|
|
|
|
|
|
_eventDispatcher->removeEventListener(_keyboardListener);
|
|
|
|
_keyboardListener = nullptr;
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
auto listener = EventListenerKeyboard::create();
|
2013-11-20 10:26:00 +08:00
|
|
|
listener->onKeyPressed = CC_CALLBACK_2(Layer::onKeyPressed, this);
|
|
|
|
listener->onKeyReleased = CC_CALLBACK_2(Layer::onKeyReleased, this);
|
2013-11-19 13:56:30 +08:00
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
_keyboardListener = listener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Layer::setKeypadEnabled(bool enabled)
|
|
|
|
{
|
2013-11-20 10:26:00 +08:00
|
|
|
setKeyboardEnabled(enabled);
|
|
|
|
}
|
|
|
|
/// Callbacks
|
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
bool Layer::onTouchBegan(Touch *touch, Event *event)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-19 13:56:30 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
return executeScriptTouchHandler(EventTouch::EventCode::BEGAN, touch, event) == 0 ? false : true;
|
2013-11-20 10:26:00 +08:00
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(touch);
|
2014-03-04 13:42:06 +08:00
|
|
|
CC_UNUSED_PARAM(event);
|
2016-11-16 09:48:37 +08:00
|
|
|
#endif
|
2013-11-20 10:26:00 +08:00
|
|
|
CCASSERT(false, "Layer#ccTouchBegan override me");
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-19 13:56:30 +08:00
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
void Layer::onTouchMoved(Touch *touch, Event *event)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
executeScriptTouchHandler(EventTouch::EventCode::MOVED, touch, event);
|
2013-11-20 10:26:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(touch);
|
2014-03-04 13:42:06 +08:00
|
|
|
CC_UNUSED_PARAM(event);
|
2016-11-16 09:48:37 +08:00
|
|
|
#endif
|
2013-11-20 10:26:00 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
void Layer::onTouchEnded(Touch *touch, Event *event)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
executeScriptTouchHandler(EventTouch::EventCode::ENDED, touch, event);
|
2013-11-20 10:26:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(touch);
|
2014-03-04 13:42:06 +08:00
|
|
|
CC_UNUSED_PARAM(event);
|
2016-11-16 09:48:37 +08:00
|
|
|
#endif
|
2013-11-20 10:26:00 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
void Layer::onTouchCancelled(Touch *touch, Event *event)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
executeScriptTouchHandler(EventTouch::EventCode::CANCELLED, touch, event);
|
2013-11-20 10:26:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(touch);
|
2014-03-04 13:42:06 +08:00
|
|
|
CC_UNUSED_PARAM(event);
|
2016-11-16 09:48:37 +08:00
|
|
|
#endif
|
2013-11-20 10:26:00 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
void Layer::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
executeScriptTouchesHandler(EventTouch::EventCode::BEGAN, touches, event);
|
2013-11-20 10:26:00 +08:00
|
|
|
return;
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(touches);
|
2014-03-04 13:42:06 +08:00
|
|
|
CC_UNUSED_PARAM(event);
|
2016-11-16 09:48:37 +08:00
|
|
|
#endif
|
2013-11-20 10:26:00 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
void Layer::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
executeScriptTouchesHandler(EventTouch::EventCode::MOVED, touches, event);
|
2013-11-20 10:26:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(touches);
|
2014-03-04 13:42:06 +08:00
|
|
|
CC_UNUSED_PARAM(event);
|
2016-11-16 09:48:37 +08:00
|
|
|
#endif
|
2013-11-20 10:26:00 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
void Layer::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
executeScriptTouchesHandler(EventTouch::EventCode::ENDED, touches, event);
|
2013-11-20 10:26:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(touches);
|
2014-03-04 13:42:06 +08:00
|
|
|
CC_UNUSED_PARAM(event);
|
2016-11-16 09:48:37 +08:00
|
|
|
#endif
|
2013-11-20 10:26:00 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:42:06 +08:00
|
|
|
void Layer::onTouchesCancelled(const std::vector<Touch*>& touches, Event *event)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2014-03-04 13:42:06 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
2013-11-20 10:26:00 +08:00
|
|
|
{
|
2014-03-04 13:42:06 +08:00
|
|
|
executeScriptTouchesHandler(EventTouch::EventCode::CANCELLED, touches, event);
|
2013-11-20 10:26:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-11-16 09:48:37 +08:00
|
|
|
#else
|
|
|
|
CC_UNUSED_PARAM(touches);
|
2014-03-04 13:42:06 +08:00
|
|
|
CC_UNUSED_PARAM(event);
|
2016-11-16 09:48:37 +08:00
|
|
|
#endif
|
2013-11-19 13:56:30 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string Layer::getDescription() const
|
|
|
|
{
|
|
|
|
return StringUtils::format("<Layer | Tag = %d>", _tag);
|
|
|
|
}
|
|
|
|
|
2013-12-16 03:42:54 +08:00
|
|
|
__LayerRGBA::__LayerRGBA()
|
|
|
|
{
|
|
|
|
CCLOG("LayerRGBA deprecated.");
|
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
|
2013-11-20 10:26:00 +08:00
|
|
|
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
|
|
|
|
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
|
|
|
|
#elif _MSC_VER >= 1400 //vs 2005 or higher
|
|
|
|
#pragma warning (pop)
|
|
|
|
#endif
|
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-07-26 08:47:42 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerColor* ret = new (std::nothrow) LayerColor();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->init())
|
2012-11-01 22:17:12 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-11-01 22:17:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-11-01 22:17:12 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-11-01 22:17:12 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerColor * layer = new (std::nothrow) LayerColor();
|
2013-12-18 17:47:20 +08:00
|
|
|
if( layer && layer->initWithColor(color,width,height))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
layer->autorelease();
|
|
|
|
return layer;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(layer);
|
|
|
|
return nullptr;
|
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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerColor * layer = new (std::nothrow) LayerColor();
|
2013-12-18 17:47:20 +08:00
|
|
|
if(layer && layer->initWithColor(color))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
layer->autorelease();
|
|
|
|
return layer;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(layer);
|
|
|
|
return nullptr;
|
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-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
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
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
|
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();
|
2016-02-18 10:23:54 +08:00
|
|
|
return initWithColor(color, s.width, s.height);
|
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
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void LayerColor::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2011-07-12 11:20:41 +08:00
|
|
|
{
|
2015-01-16 06:00:49 +08:00
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
2014-05-31 07:42:05 +08:00
|
|
|
_customCommand.func = CC_CALLBACK_0(LayerColor::onDraw, this, transform, flags);
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2014-01-07 22:08:00 +08:00
|
|
|
|
|
|
|
for(int i = 0; i < 4; ++i)
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec4 pos;
|
2014-02-23 11:16:42 +08:00
|
|
|
pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ;
|
2014-04-10 22:38:57 +08:00
|
|
|
pos.w = 1;
|
|
|
|
_modelViewTransform.transformVector(&pos);
|
2014-05-15 01:07:09 +08:00
|
|
|
_noMVPVertices[i] = Vec3(pos.x,pos.y,pos.z)/pos.w;
|
2014-01-07 22:08:00 +08:00
|
|
|
}
|
2013-11-23 09:14:24 +08:00
|
|
|
}
|
|
|
|
|
2016-11-09 10:34:50 +08:00
|
|
|
void LayerColor::onDraw(const Mat4& transform, uint32_t /*flags*/)
|
2011-07-12 11:20:41 +08:00
|
|
|
{
|
2014-05-09 07:42:36 +08:00
|
|
|
getGLProgram()->use();
|
|
|
|
getGLProgram()->setUniformsForBuiltins(transform);
|
2015-01-23 18:19:03 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR );
|
2015-01-23 18:19:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
2015-01-23 18:19:03 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2014-01-07 22:08:00 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
|
2015-07-16 14:15:41 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::blendFunc( _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
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4);
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string LayerColor::getDescription() const
|
|
|
|
{
|
|
|
|
return StringUtils::format("<LayerColor | Tag = %d>", _tag);
|
|
|
|
}
|
2014-03-27 10:01:46 +08:00
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// LayerGradient
|
2014-03-26 18:45:08 +08:00
|
|
|
//
|
|
|
|
LayerGradient::LayerGradient()
|
2014-03-27 10:01:46 +08:00
|
|
|
: _startColor(Color4B::BLACK)
|
|
|
|
, _endColor(Color4B::BLACK)
|
2014-03-26 18:45:08 +08:00
|
|
|
, _startOpacity(255)
|
|
|
|
, _endOpacity(255)
|
2014-05-15 01:07:09 +08:00
|
|
|
, _alongVector(Vec2(0, -1))
|
2014-03-26 18:45:08 +08:00
|
|
|
, _compressedInterpolation(true)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2014-03-27 10:01:46 +08:00
|
|
|
|
|
|
|
LayerGradient::~LayerGradient()
|
|
|
|
{
|
2014-03-26 18:45:08 +08:00
|
|
|
}
|
2014-03-27 10:01:46 +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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerGradient * layer = new (std::nothrow) LayerGradient();
|
2013-12-18 17:47:20 +08:00
|
|
|
if( layer && layer->initWithColor(start, end))
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
layer->autorelease();
|
|
|
|
return layer;
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(layer);
|
|
|
|
return nullptr;
|
2011-04-27 22:45:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end, const Vec2& v)
|
2011-04-27 22:45:54 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerGradient * layer = new (std::nothrow) LayerGradient();
|
2013-12-18 17:47:20 +08:00
|
|
|
if( layer && layer->initWithColor(start, end, v))
|
2010-12-27 09:40:45 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
layer->autorelease();
|
|
|
|
return layer;
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(layer);
|
|
|
|
return nullptr;
|
2011-04-27 22:45:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerGradient* LayerGradient::create()
|
2012-11-01 22:17:12 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerGradient* ret = new (std::nothrow) LayerGradient();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->init())
|
2012-11-01 22:17:12 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-11-01 22:17:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-11-01 22:17:12 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-11-01 22:17:12 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool LayerGradient::init()
|
2012-10-31 18:00:51 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +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
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
return initWithColor(start, end, Vec2(0, -1));
|
2011-04-27 22:45:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end, const Vec2& 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
|
|
|
|
2014-03-26 18:45:08 +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);
|
2015-04-20 01:40:52 +08:00
|
|
|
Vec2 u(_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-07-20 04:16:38 +08:00
|
|
|
GLubyte LayerGradient::getStartOpacity() const
|
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-07-20 04:16:38 +08:00
|
|
|
GLubyte LayerGradient::getEndOpacity() const
|
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
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void LayerGradient::setVector(const Vec2& 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
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
const Vec2& 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
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string LayerGradient::getDescription() const
|
|
|
|
{
|
|
|
|
return StringUtils::format("<LayerGradient | Tag = %d>", _tag);
|
|
|
|
}
|
|
|
|
|
2017-05-22 13:42:00 +08:00
|
|
|
/**
|
|
|
|
* LayerRadialGradient
|
|
|
|
*/
|
|
|
|
LayerRadialGradient* LayerRadialGradient::create(const Color4B& startColor, const Color4B& endColor, float radius, const Vec2& center, float expand)
|
|
|
|
{
|
|
|
|
auto layerGradient = new LayerRadialGradient();
|
|
|
|
if (layerGradient && layerGradient->initWithColor(startColor, endColor, radius, center, expand))
|
|
|
|
{
|
|
|
|
layerGradient->autorelease();
|
|
|
|
return layerGradient;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete layerGradient;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerRadialGradient* LayerRadialGradient::create()
|
|
|
|
{
|
|
|
|
auto layerGradient = new LayerRadialGradient();
|
|
|
|
if (layerGradient && layerGradient->initWithColor(Color4B::BLACK, Color4B::BLACK, 0, Vec2(0,0), 0))
|
|
|
|
{
|
|
|
|
layerGradient->autorelease();
|
|
|
|
return layerGradient;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete layerGradient;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerRadialGradient::LayerRadialGradient()
|
|
|
|
: _startColor(Color4B::BLACK)
|
|
|
|
, _startColorRend(Color4F::BLACK)
|
|
|
|
, _endColor(Color4B::BLACK)
|
|
|
|
, _endColorRend(Color4F::BLACK)
|
2018-05-14 10:34:56 +08:00
|
|
|
, _center(Vec2(0,0))
|
2017-05-22 13:42:00 +08:00
|
|
|
, _radius(0.f)
|
|
|
|
, _expand(0.f)
|
2018-05-14 10:34:56 +08:00
|
|
|
, _uniformLocationStartColor(0)
|
|
|
|
, _uniformLocationEndColor(0)
|
2017-05-22 13:42:00 +08:00
|
|
|
, _uniformLocationCenter(0)
|
|
|
|
, _uniformLocationRadius(0)
|
|
|
|
, _uniformLocationExpand(0)
|
|
|
|
, _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
LayerRadialGradient::~LayerRadialGradient()
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool LayerRadialGradient::initWithColor(const cocos2d::Color4B &startColor, const cocos2d::Color4B &endColor, float radius, const Vec2& center, float expand)
|
|
|
|
{
|
|
|
|
// should do it before Layer::init()
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
_vertices[i] = {0.0f, 0.0f};
|
|
|
|
|
|
|
|
if (Layer::init())
|
|
|
|
{
|
|
|
|
convertColor4B24F(_startColorRend, startColor);
|
|
|
|
_startColor = startColor;
|
|
|
|
|
|
|
|
convertColor4B24F(_endColorRend, endColor);
|
|
|
|
_endColor = endColor;
|
|
|
|
|
|
|
|
_expand = expand;
|
|
|
|
|
|
|
|
setRadius(radius);
|
|
|
|
setCenter(center);
|
|
|
|
|
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_LAYER_RADIAL_GRADIENT));
|
|
|
|
auto program = getGLProgram();
|
|
|
|
_uniformLocationStartColor = program->getUniformLocation("u_startColor");
|
|
|
|
_uniformLocationEndColor = program->getUniformLocation("u_endColor");
|
|
|
|
_uniformLocationExpand = program->getUniformLocation("u_expand");
|
|
|
|
_uniformLocationRadius = program->getUniformLocation("u_radius");
|
|
|
|
_uniformLocationCenter = program->getUniformLocation("u_center");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
|
|
|
{
|
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
|
|
|
_customCommand.func = CC_CALLBACK_0(LayerRadialGradient::onDraw, this, transform, flags);
|
|
|
|
renderer->addCommand(&_customCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::onDraw(const Mat4& transform, uint32_t /*flags*/)
|
|
|
|
{
|
|
|
|
auto program = getGLProgram();
|
|
|
|
program->use();
|
|
|
|
program->setUniformsForBuiltins(transform);
|
|
|
|
program->setUniformLocationWith4f(_uniformLocationStartColor, _startColorRend.r,
|
|
|
|
_startColorRend.g, _startColorRend.b, _startColorRend.a);
|
|
|
|
program->setUniformLocationWith4f(_uniformLocationEndColor, _endColorRend.r,
|
|
|
|
_endColorRend.g, _endColorRend.b, _endColorRend.a);
|
|
|
|
program->setUniformLocationWith2f(_uniformLocationCenter, _center.x, _center.y);
|
|
|
|
program->setUniformLocationWith1f(_uniformLocationRadius, _radius);
|
|
|
|
program->setUniformLocationWith1f(_uniformLocationExpand, _expand);
|
|
|
|
|
|
|
|
|
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
|
|
|
|
|
|
|
|
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setContentSize(const Size& size)
|
|
|
|
{
|
|
|
|
_vertices[1].x = size.width;
|
|
|
|
_vertices[2].y = size.height;
|
|
|
|
_vertices[3].x = size.width;
|
|
|
|
_vertices[3].y = size.height;
|
|
|
|
Layer::setContentSize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setStartOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
_startColorRend.a = opacity / 255.0f;
|
|
|
|
_startColor.a = opacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLubyte LayerRadialGradient::getStartOpacity() const
|
|
|
|
{
|
|
|
|
return _startColor.a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setEndOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
_endColorRend.a = opacity / 255.0f;
|
|
|
|
_endColor.a = opacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLubyte LayerRadialGradient::getEndOpacity() const
|
|
|
|
{
|
|
|
|
return _endColor.a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setRadius(float radius)
|
|
|
|
{
|
|
|
|
_radius = radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
float LayerRadialGradient::getRadius() const
|
|
|
|
{
|
|
|
|
return _radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setCenter(const Vec2& center)
|
|
|
|
{
|
|
|
|
_center = center;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 LayerRadialGradient::getCenter() const
|
|
|
|
{
|
|
|
|
return _center;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setExpand(float expand)
|
|
|
|
{
|
|
|
|
_expand = expand;
|
|
|
|
}
|
|
|
|
|
|
|
|
float LayerRadialGradient::getExpand() const
|
|
|
|
{
|
|
|
|
return _expand;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setStartColor(const Color3B& color)
|
|
|
|
{
|
|
|
|
setStartColor(Color4B(color));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setStartColor(const cocos2d::Color4B &color)
|
|
|
|
{
|
|
|
|
_startColor = color;
|
|
|
|
convertColor4B24F(_startColorRend, _startColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color4B LayerRadialGradient::getStartColor() const
|
|
|
|
{
|
|
|
|
return _startColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color3B LayerRadialGradient::getStartColor3B() const
|
|
|
|
{
|
|
|
|
return Color3B(_startColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setEndColor(const Color3B& color)
|
|
|
|
{
|
|
|
|
setEndColor(Color4B(color));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setEndColor(const cocos2d::Color4B &color)
|
|
|
|
{
|
|
|
|
_endColor = color;
|
|
|
|
convertColor4B24F(_endColorRend, _endColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color4B LayerRadialGradient::getEndColor() const
|
|
|
|
{
|
|
|
|
return _endColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color3B LayerRadialGradient::getEndColor3B() const
|
|
|
|
{
|
|
|
|
return Color3B(_endColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::setBlendFunc(const BlendFunc& blendFunc)
|
|
|
|
{
|
|
|
|
_blendFunc = blendFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlendFunc LayerRadialGradient::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _blendFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerRadialGradient::convertColor4B24F(Color4F& outColor, const Color4B& inColor)
|
|
|
|
{
|
|
|
|
outColor.r = inColor.r / 255.0f;
|
|
|
|
outColor.g = inColor.g / 255.0f;
|
|
|
|
outColor.b = inColor.b / 255.0f;
|
|
|
|
outColor.a = inColor.a / 255.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
|
|
|
}
|
2013-11-29 11:36:42 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LayerMultiplex::~LayerMultiplex()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &layer : _layers) {
|
2013-11-29 11:36:42 +08:00
|
|
|
layer->cleanup();
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-03-29 02:25:39 +08:00
|
|
|
LayerMultiplex * LayerMultiplex::createVariadic(Layer * layer, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args,layer);
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerMultiplex * multiplexLayer = new (std::nothrow) LayerMultiplex();
|
2014-03-29 02:25:39 +08:00
|
|
|
if(multiplexLayer && multiplexLayer->initWithLayers(layer, args))
|
|
|
|
{
|
|
|
|
multiplexLayer->autorelease();
|
|
|
|
va_end(args);
|
|
|
|
return multiplexLayer;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
CC_SAFE_DELETE(multiplexLayer);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
#else
|
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
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerMultiplex * multiplexLayer = new (std::nothrow) LayerMultiplex();
|
2013-12-18 17:47:20 +08:00
|
|
|
if(multiplexLayer && multiplexLayer->initWithLayers(layer, args))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
multiplexLayer->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
va_end(args);
|
2013-12-18 17:47:20 +08:00
|
|
|
return multiplexLayer;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
va_end(args);
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(multiplexLayer);
|
|
|
|
return nullptr;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2014-03-29 02:25:39 +08:00
|
|
|
#endif
|
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
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
return LayerMultiplex::create(layer, nullptr);
|
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
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerMultiplex* ret = new (std::nothrow) LayerMultiplex();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->init())
|
2012-11-01 22:17:12 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-11-01 22:17:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-11-01 22:17:12 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-11-01 22:17:12 +08:00
|
|
|
}
|
|
|
|
|
2013-11-29 11:36:42 +08:00
|
|
|
LayerMultiplex* LayerMultiplex::createWithArray(const Vector<Layer*>& arrayOfLayers)
|
2012-11-30 21:13:25 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LayerMultiplex* ret = new (std::nothrow) LayerMultiplex();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret && ret->initWithArray(arrayOfLayers))
|
2012-11-30 21:13:25 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-11-30 21:13:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-11-30 21:13:25 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-11-30 21:13:25 +08:00
|
|
|
}
|
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
|
|
|
{
|
2015-12-17 21:55:47 +08:00
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2016-02-16 13:43:11 +08:00
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (sEngine)
|
|
|
|
{
|
|
|
|
sEngine->retainScriptObject(this, layer);
|
|
|
|
}
|
2015-12-17 21:55:47 +08:00
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-12-05 10:35:10 +08:00
|
|
|
_layers.pushBack(layer);
|
2011-05-31 14:04:14 +08:00
|
|
|
}
|
|
|
|
|
2013-08-26 19:38:23 +08:00
|
|
|
bool LayerMultiplex::init()
|
2013-08-26 19:42:41 +08:00
|
|
|
{
|
|
|
|
if (Layer::init())
|
|
|
|
{
|
2013-08-26 19:38:23 +08:00
|
|
|
_enabledLayer = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-26 19:42:41 +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-12-05 10:35:10 +08:00
|
|
|
_layers.reserve(5);
|
2015-12-17 21:55:47 +08:00
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2016-02-16 13:43:11 +08:00
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (sEngine)
|
|
|
|
{
|
|
|
|
sEngine->retainScriptObject(this, layer);
|
|
|
|
}
|
2015-12-17 21:55:47 +08:00
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-12-05 10:35:10 +08:00
|
|
|
_layers.pushBack(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 ) {
|
2015-12-17 21:55:47 +08:00
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2016-02-16 13:43:11 +08:00
|
|
|
if (sEngine)
|
|
|
|
{
|
|
|
|
sEngine->retainScriptObject(this, l);
|
|
|
|
}
|
2015-12-17 21:55:47 +08:00
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-12-05 10:35:10 +08:00
|
|
|
_layers.pushBack(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-12-05 10:35:10 +08:00
|
|
|
this->addChild(_layers.at(_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-11-29 11:36:42 +08:00
|
|
|
bool LayerMultiplex::initWithArray(const Vector<Layer*>& 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
|
|
|
{
|
2015-12-17 21:55:47 +08:00
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2016-02-16 13:43:11 +08:00
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (sEngine)
|
|
|
|
{
|
|
|
|
for (const auto &layer : arrayOfLayers)
|
|
|
|
{
|
|
|
|
if (layer)
|
|
|
|
{
|
|
|
|
sEngine->retainScriptObject(this, layer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-17 21:55:47 +08:00
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-12-05 10:35:10 +08:00
|
|
|
_layers.reserve(arrayOfLayers.size());
|
2013-12-17 18:09:07 +08:00
|
|
|
_layers.pushBack(arrayOfLayers);
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabledLayer = 0;
|
2013-12-05 10:35:10 +08:00
|
|
|
this->addChild(_layers.at(_enabledLayer));
|
2012-12-04 10:36:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-08 11:26:38 +08:00
|
|
|
void LayerMultiplex::switchTo(int n)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
|
|
|
|
switchTo(n, true);
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2017-09-08 09:28:57 +08:00
|
|
|
void LayerMultiplex::switchTo(int n, bool cleanup)
|
|
|
|
{
|
|
|
|
CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
|
|
|
|
|
|
|
|
this->removeChild(_layers.at(_enabledLayer), cleanup);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabledLayer = n;
|
2017-09-08 09:28:57 +08:00
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
this->addChild(_layers.at(n));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-08 11:26:38 +08:00
|
|
|
void LayerMultiplex::switchToAndReleaseMe(int n)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
this->removeChild(_layers.at(_enabledLayer), true);
|
2015-12-17 21:55:47 +08:00
|
|
|
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2016-02-16 13:43:11 +08:00
|
|
|
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (sEngine)
|
|
|
|
{
|
|
|
|
sEngine->releaseScriptObject(this, _layers.at(_enabledLayer));
|
|
|
|
}
|
2015-12-17 21:55:47 +08:00
|
|
|
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
_layers.replace(_enabledLayer, nullptr);
|
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-12-05 10:35:10 +08:00
|
|
|
this->addChild(_layers.at(n));
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string LayerMultiplex::getDescription() const
|
|
|
|
{
|
2014-01-05 11:59:32 +08:00
|
|
|
return StringUtils::format("<LayerMultiplex | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
|
2013-12-13 06:30:22 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|