axmol/cocos/2d/CCLayer.cpp

1050 lines
27 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-08-30 10:47:07 +08:00
http://www.cocos2d-x.org
2019-11-23 20:27:39 +08:00
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 "2d/CCLayer.h"
#include "base/CCScriptSupport.h"
#include "base/ccUtils.h"
#include "platform/CCDevice.h"
#include "renderer/CCRenderer.h"
#include "base/CCDirector.h"
#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"
#include "base/ccUTF8.h"
#include "renderer/backend/Buffer.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/ProgramState.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
2021-08-30 10:47:07 +08:00
#include "platform/desktop/CCGLViewImpl-desktop.h"
2019-11-23 20:27:39 +08:00
#endif
NS_CC_BEGIN
// Layer
Layer::Layer()
2021-08-30 10:47:07 +08:00
: _touchEnabled(false)
, _accelerometerEnabled(false)
, _keyboardEnabled(false)
, _touchListener(nullptr)
, _keyboardListener(nullptr)
, _accelerationListener(nullptr)
, _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
, _swallowsTouches(true)
2019-11-23 20:27:39 +08:00
{
_ignoreAnchorPointForPosition = true;
setAnchorPoint(Vec2(0.5f, 0.5f));
}
2021-08-30 10:47:07 +08:00
Layer::~Layer()
{
}
2019-11-23 20:27:39 +08:00
bool Layer::init()
{
setContentSize(_director->getWinSize());
2019-11-23 20:27:39 +08:00
return true;
}
2021-08-30 10:47:07 +08:00
Layer *Layer::create()
2019-11-23 20:27:39 +08:00
{
2021-08-30 10:47:07 +08:00
Layer *ret = new (std::nothrow) Layer();
2019-11-23 20:27:39 +08:00
if (ret && ret->init())
{
ret->autorelease();
return ret;
}
else
{
CC_SAFE_DELETE(ret);
return nullptr;
}
}
int Layer::executeScriptTouchHandler(EventTouch::EventCode eventType, Touch* touch, Event* event)
{
#if CC_ENABLE_SCRIPT_BINDING
TouchScriptData data(eventType, this, touch, event);
ScriptEvent scriptEvent(kTouchEvent, &data);
return ScriptEngineManager::sendEventToLua(scriptEvent);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(eventType);
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
return 0;
#endif
2019-11-23 20:27:39 +08:00
}
2021-08-30 10:47:07 +08:00
int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType, const std::vector<Touch*>& touches, Event* event)
2019-11-23 20:27:39 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
TouchesScriptData data(eventType, this, touches, event);
ScriptEvent scriptEvent(kTouchesEvent, &data);
return ScriptEngineManager::sendEventToLua(scriptEvent);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(eventType);
CC_UNUSED_PARAM(touches);
CC_UNUSED_PARAM(event);
return 0;
#endif
2019-11-23 20:27:39 +08:00
}
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
void Layer::onAcceleration(Acceleration* acc, Event* /*unused_event*/)
{
#if CC_ENABLE_SCRIPT_BINDING
2021-08-30 10:47:07 +08:00
BasicScriptData data(this,(void*)acc);
ScriptEvent event(kAccelerometerEvent,&data);
ScriptEngineManager::sendEventToLua(event);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(acc);
#endif
}
2021-08-30 10:47:07 +08:00
void Layer::onKeyPressed(EventKeyboard::KeyCode /*keyCode*/, Event* /*unused_event*/)
{
}
2019-11-23 20:27:39 +08:00
void Layer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* /*unused_event*/)
{
#if CC_ENABLE_SCRIPT_BINDING
KeypadScriptData data(keyCode, this);
2021-08-30 10:47:07 +08:00
ScriptEvent event(kKeypadEvent,&data);
ScriptEngineManager::sendEventToLua(event);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(keyCode);
#endif
}
/// Callbacks
2021-08-30 10:47:07 +08:00
bool Layer::onTouchBegan(Touch *touch, Event *event)
2019-11-23 20:27:39 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
return executeScriptTouchHandler(EventTouch::EventCode::BEGAN, touch, event) == 0 ? false : true;
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
CCASSERT(false, "Layer#ccTouchBegan override me");
return true;
#endif
2019-11-23 20:27:39 +08:00
}
2021-08-30 10:47:07 +08:00
void Layer::onTouchMoved(Touch *touch, Event *event)
2019-11-23 20:27:39 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
executeScriptTouchHandler(EventTouch::EventCode::MOVED, touch, event);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
#endif
}
2021-08-30 10:47:07 +08:00
void Layer::onTouchEnded(Touch *touch, Event *event)
2019-11-23 20:27:39 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
executeScriptTouchHandler(EventTouch::EventCode::ENDED, touch, event);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
#endif
}
2021-08-30 10:47:07 +08:00
void Layer::onTouchCancelled(Touch *touch, Event *event)
2019-11-23 20:27:39 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
executeScriptTouchHandler(EventTouch::EventCode::CANCELLED, touch, event);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
#endif
2021-08-30 10:47:07 +08:00
}
2019-11-23 20:27:39 +08:00
2021-08-30 10:47:07 +08:00
void Layer::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
2019-11-23 20:27:39 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
executeScriptTouchesHandler(EventTouch::EventCode::BEGAN, touches, event);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(touches);
CC_UNUSED_PARAM(event);
#endif
}
2021-08-30 10:47:07 +08:00
void Layer::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
2019-11-23 20:27:39 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
executeScriptTouchesHandler(EventTouch::EventCode::MOVED, touches, event);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(touches);
CC_UNUSED_PARAM(event);
#endif
}
2021-08-30 10:47:07 +08:00
void Layer::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
2019-11-23 20:27:39 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
executeScriptTouchesHandler(EventTouch::EventCode::ENDED, touches, event);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(touches);
CC_UNUSED_PARAM(event);
#endif
}
2021-08-30 10:47:07 +08:00
void Layer::onTouchesCancelled(const std::vector<Touch*>& touches, Event *event)
2019-11-23 20:27:39 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
executeScriptTouchesHandler(EventTouch::EventCode::CANCELLED, touches, event);
2019-11-23 20:27:39 +08:00
#else
CC_UNUSED_PARAM(touches);
CC_UNUSED_PARAM(event);
#endif
}
std::string Layer::getDescription() const
{
return StringUtils::format("<Layer | Tag = %d>", _tag);
}
/// LayerColor
2021-08-30 10:47:07 +08:00
LayerColor::LayerColor()
{
// default blend function
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
auto& pipelinePS = _customCommand.getPipelineDescriptor().programState;
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR);
//!!! LayerColor private programState don't want affect by Node::_programState, so store at _customCommand
pipelinePS = new backend::ProgramState(program);
auto vertexLayout = pipelinePS->getVertexLayout();
const auto& attributeInfo = pipelinePS->getProgram()->getActiveAttributes();
auto iter = attributeInfo.find("a_position");
if(iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3, 0, false);
}
iter = attributeInfo.find("a_color");
if(iter != attributeInfo.end())
{
vertexLayout->setAttribute("a_color", iter->second.location, backend::VertexFormat::FLOAT4, sizeof(_vertexData[0].vertices), false);
}
vertexLayout->setLayout(sizeof(_vertexData[0]));
_mvpMatrixLocation = pipelinePS->getUniformLocation("u_MVPMatrix");
_customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, 6, CustomCommand::BufferUsage::STATIC);
unsigned short indices[] = {0, 1, 2, 2, 1, 3};
_customCommand.updateIndexBuffer(indices, sizeof(indices));
_customCommand.createVertexBuffer(sizeof(_vertexData[0]), 4, CustomCommand::BufferUsage::DYNAMIC);
_customCommand.setDrawType(CustomCommand::DrawType::ELEMENT);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
}
LayerColor::~LayerColor()
{
CC_SAFE_RELEASE_NULL(_customCommand.getPipelineDescriptor().programState);
}
/// blendFunc getter
const BlendFunc &LayerColor::getBlendFunc() const
{
return _blendFunc;
}
/// blendFunc setter
void LayerColor::setBlendFunc(const BlendFunc &var)
{
_blendFunc = var;
}
2019-11-23 20:27:39 +08:00
LayerColor* LayerColor::create()
{
LayerColor* ret = new (std::nothrow) LayerColor();
if (ret && ret->init())
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
2021-08-30 10:47:07 +08:00
LayerColor * LayerColor::create(const Color4B& color, float width, float height)
2019-11-23 20:27:39 +08:00
{
2021-08-30 10:47:07 +08:00
LayerColor * layer = new (std::nothrow) LayerColor();
if( layer && layer->initWithColor(color,width,height))
2019-11-23 20:27:39 +08:00
{
layer->autorelease();
return layer;
}
CC_SAFE_DELETE(layer);
return nullptr;
}
2021-08-30 10:47:07 +08:00
LayerColor * LayerColor::create(const Color4B& color)
2019-11-23 20:27:39 +08:00
{
2021-08-30 10:47:07 +08:00
LayerColor * layer = new (std::nothrow) LayerColor();
if(layer && layer->initWithColor(color))
2019-11-23 20:27:39 +08:00
{
layer->autorelease();
return layer;
}
CC_SAFE_DELETE(layer);
return nullptr;
}
bool LayerColor::init()
{
Size s = _director->getWinSize();
2021-08-30 10:47:07 +08:00
return initWithColor(Color4B(0,0,0,0), s.width, s.height);
2019-11-23 20:27:39 +08:00
}
bool LayerColor::initWithColor(const Color4B& color, float w, float h)
{
2021-08-30 10:47:07 +08:00
if (Layer::init())
2019-11-23 20:27:39 +08:00
{
2021-08-30 10:47:07 +08:00
// default blend function
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
_displayedColor.r = _realColor.r = color.r;
_displayedColor.g = _realColor.g = color.g;
_displayedColor.b = _realColor.b = color.b;
_displayedOpacity = _realOpacity = color.a;
for (size_t i = 0; i<sizeof(_squareVertices) / sizeof( _squareVertices[0]); i++ )
{
_squareVertices[i].x = 0.0f;
_squareVertices[i].y = 0.0f;
}
updateColor();
2019-11-23 20:27:39 +08:00
setContentSize(Size(w, h));
return true;
}
return false;
}
bool LayerColor::initWithColor(const Color4B& color)
{
Size s = _director->getWinSize();
2019-11-23 20:27:39 +08:00
return initWithColor(color, s.width, s.height);
}
2021-08-30 10:47:07 +08:00
/// override contentSize
void LayerColor::setContentSize(const Size & size)
{
_squareVertices[1].x = size.width;
_squareVertices[2].y = size.height;
_squareVertices[3].x = size.width;
_squareVertices[3].y = size.height;
Layer::setContentSize(size);
}
void LayerColor::changeWidthAndHeight(float w ,float h)
2019-11-23 20:27:39 +08:00
{
this->setContentSize(Size(w, h));
}
void LayerColor::changeWidth(float w)
{
this->setContentSize(Size(w, _contentSize.height));
}
void LayerColor::changeHeight(float h)
{
this->setContentSize(Size(_contentSize.width, h));
}
2021-08-30 10:47:07 +08:00
void LayerColor::updateColor()
{
for (int i = 0; i < 4; i++ )
{
_vertexData[i].colors.r = _displayedColor.r / 255.0f;
_vertexData[i].colors.g = _displayedColor.g / 255.0f;
_vertexData[i].colors.b = _displayedColor.b / 255.0f;
_vertexData[i].colors.a = _displayedOpacity / 255.0f;
}
}
void LayerColor::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
_customCommand.init(_globalZOrder, _blendFunc);
renderer->addCommand(&_customCommand);
cocos2d::Mat4 projectionMat = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
pipelineDescriptor.programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
for(int i = 0; i < 4; ++i)
{
Vec4 pos;
pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ;
pos.w = 1;
_modelViewTransform.transformVector(&pos);
_vertexData[i].vertices = Vec3(pos.x,pos.y,pos.z)/pos.w;
}
updateVertexBuffer();
}
void LayerColor::updateVertexBuffer()
{
_customCommand.updateVertexBuffer(_vertexData, sizeof(_vertexData));
}
2019-11-23 20:27:39 +08:00
//
// LayerGradient
//
2021-08-30 10:47:07 +08:00
LayerGradient::LayerGradient()
{
}
2019-11-23 20:27:39 +08:00
2021-08-30 10:47:07 +08:00
LayerGradient::~LayerGradient()
{
}
2019-11-23 20:27:39 +08:00
LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end)
{
2021-08-30 10:47:07 +08:00
LayerGradient * layer = new (std::nothrow) LayerGradient();
if( layer && layer->initWithColor(start, end))
2019-11-23 20:27:39 +08:00
{
layer->autorelease();
return layer;
}
CC_SAFE_DELETE(layer);
return nullptr;
}
LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end, const Vec2& v)
{
2021-08-30 10:47:07 +08:00
LayerGradient * layer = new (std::nothrow) LayerGradient();
if( layer && layer->initWithColor(start, end, v))
2019-11-23 20:27:39 +08:00
{
layer->autorelease();
return layer;
}
CC_SAFE_DELETE(layer);
return nullptr;
}
LayerGradient* LayerGradient::create()
{
LayerGradient* ret = new (std::nothrow) LayerGradient();
if (ret && ret->init())
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
bool LayerGradient::init()
{
return initWithColor(Color4B(0, 0, 0, 255), Color4B(0, 0, 0, 255));
}
bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end)
{
return initWithColor(start, end, Vec2(0, -1));
}
bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end, const Vec2& v)
{
2021-08-30 10:47:07 +08:00
_endColor.r = end.r;
_endColor.g = end.g;
_endColor.b = end.b;
2019-11-23 20:27:39 +08:00
2021-08-30 10:47:07 +08:00
_endOpacity = end.a;
_startOpacity = start.a;
_alongVector = v;
2019-11-23 20:27:39 +08:00
_compressedInterpolation = true;
return LayerColor::initWithColor(Color4B(start.r, start.g, start.b, 255));
}
void LayerGradient::updateColor()
{
2021-08-30 10:47:07 +08:00
LayerColor::updateColor();
2019-11-23 20:27:39 +08:00
float h = _alongVector.getLength();
if (h == 0)
return;
float c = sqrtf(2.0f);
Vec2 u(_alongVector.x / h, _alongVector.y / h);
// Compressed Interpolation mode
if (_compressedInterpolation)
{
2021-08-30 10:47:07 +08:00
float h2 = 1 / ( fabsf(u.x) + fabsf(u.y) );
u = u * (h2 * (float)c);
2019-11-23 20:27:39 +08:00
}
float opacityf = (float)_displayedOpacity / 255.0f;
2021-08-30 10:47:07 +08:00
Color4F S(
_displayedColor.r / 255.0f,
_displayedColor.g / 255.0f,
_displayedColor.b / 255.0f,
_startOpacity * opacityf / 255.0f
);
2019-11-23 20:27:39 +08:00
2021-08-30 10:47:07 +08:00
Color4F E(
_endColor.r / 255.0f,
_endColor.g / 255.0f,
_endColor.b / 255.0f,
_endOpacity * opacityf / 255.0f
);
2019-11-23 20:27:39 +08:00
// (-1, -1)
2021-08-30 10:47:07 +08:00
_vertexData[0].colors.r = E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0f * c));
_vertexData[0].colors.g = E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0f * c));
_vertexData[0].colors.b = E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0f * c));
_vertexData[0].colors.a = E.a + (S.a - E.a) * ((c + u.x + u.y) / (2.0f * c));
// (1, -1)
_vertexData[1].colors.r = E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0f * c));
_vertexData[1].colors.g = E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0f * c));
_vertexData[1].colors.b = E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0f * c));
_vertexData[1].colors.a = E.a + (S.a - E.a) * ((c - u.x + u.y) / (2.0f * c));
// (-1, 1)
_vertexData[2].colors.r = E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0f * c));
_vertexData[2].colors.g = E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0f * c));
_vertexData[2].colors.b = E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0f * c));
_vertexData[2].colors.a = E.a + (S.a - E.a) * ((c + u.x - u.y) / (2.0f * c));
// (1, 1)
_vertexData[3].colors.r = E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0f * c));
_vertexData[3].colors.g = E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0f * c));
_vertexData[3].colors.b = E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0f * c));
_vertexData[3].colors.a = E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0f * c));
2019-11-23 20:27:39 +08:00
}
const Color3B& LayerGradient::getStartColor() const
{
return _realColor;
}
void LayerGradient::setStartColor(const Color3B& color)
{
setColor(color);
}
void LayerGradient::setEndColor(const Color3B& color)
{
_endColor = color;
updateColor();
}
const Color3B& LayerGradient::getEndColor() const
{
return _endColor;
}
void LayerGradient::setStartOpacity(uint8_t o)
{
_startOpacity = o;
updateColor();
}
uint8_t LayerGradient::getStartOpacity() const
{
return _startOpacity;
}
void LayerGradient::setEndOpacity(uint8_t o)
{
_endOpacity = o;
updateColor();
}
uint8_t LayerGradient::getEndOpacity() const
{
return _endOpacity;
}
void LayerGradient::setVector(const Vec2& var)
{
_alongVector = var;
updateColor();
}
const Vec2& LayerGradient::getVector() const
{
return _alongVector;
}
bool LayerGradient::isCompressedInterpolation() const
{
return _compressedInterpolation;
}
void LayerGradient::setCompressedInterpolation(bool compress)
{
_compressedInterpolation = compress;
updateColor();
}
std::string LayerGradient::getDescription() const
{
return StringUtils::format("<LayerGradient | Tag = %d>", _tag);
}
/**
* LayerRadialGradient
*/
2021-08-30 10:47:07 +08:00
LayerRadialGradient* LayerRadialGradient::create(const Color4B& startColor, const Color4B& endColor, float radius, const Vec2& center, float expand)
2019-11-23 20:27:39 +08:00
{
auto layerGradient = new LayerRadialGradient();
if (layerGradient && layerGradient->initWithColor(startColor, endColor, radius, center, expand))
{
layerGradient->autorelease();
return layerGradient;
}
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
delete layerGradient;
return nullptr;
}
LayerRadialGradient* LayerRadialGradient::create()
{
auto layerGradient = new LayerRadialGradient();
2021-08-30 10:47:07 +08:00
if (layerGradient && layerGradient->initWithColor(Color4B::BLACK, Color4B::BLACK, 0, Vec2(0,0), 0))
2019-11-23 20:27:39 +08:00
{
layerGradient->autorelease();
return layerGradient;
}
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
delete layerGradient;
return nullptr;
}
LayerRadialGradient::LayerRadialGradient()
{
auto& pipelinePS = _customCommand.getPipelineDescriptor().programState;
2021-08-30 10:47:07 +08:00
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::LAYER_RADIA_GRADIENT);
//!!! LayerRadialGradient private programState don't want affect by Node::_programState, so store at _customCommand
2021-08-30 10:47:07 +08:00
pipelinePS = new backend::ProgramState(program);
_mvpMatrixLocation = pipelinePS->getUniformLocation("u_MVPMatrix");
_startColorLocation = pipelinePS->getUniformLocation("u_startColor");
2021-08-30 10:47:07 +08:00
_endColorLocation = pipelinePS->getUniformLocation("u_endColor");
_centerLocation = pipelinePS->getUniformLocation("u_center");
_radiusLocation = pipelinePS->getUniformLocation("u_radius");
_expandLocation = pipelinePS->getUniformLocation("u_expand");
2021-08-30 10:47:07 +08:00
auto vertexLayout = pipelinePS->getVertexLayout();
const auto& attributeInfo = pipelinePS->getProgram()->getActiveAttributes();
2021-08-30 10:47:07 +08:00
auto iter = attributeInfo.find("a_position");
if(iter != attributeInfo.end())
2019-11-23 20:27:39 +08:00
{
vertexLayout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT2, 0, false);
}
vertexLayout->setLayout(sizeof(_vertices[0]));
2021-08-30 10:47:07 +08:00
_customCommand.createVertexBuffer(sizeof(_vertices[0]), sizeof(_vertices) / sizeof(_vertices[0]), CustomCommand::BufferUsage::STATIC);
2019-11-23 20:27:39 +08:00
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
}
LayerRadialGradient::~LayerRadialGradient()
{
CC_SAFE_RELEASE_NULL(_customCommand.getPipelineDescriptor().programState);
2019-11-23 20:27:39 +08:00
}
2021-08-30 10:47:07 +08:00
bool LayerRadialGradient::initWithColor(const cocos2d::Color4B &startColor, const cocos2d::Color4B &endColor, float radius, const Vec2& center, float expand)
2019-11-23 20:27:39 +08:00
{
// should do it before Layer::init()
for (int i = 0; i < 4; ++i)
_vertices[i] = {0.0f, 0.0f};
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
if (Layer::init())
{
convertColor4B24F(_startColorRend, startColor);
_startColor = startColor;
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
convertColor4B24F(_endColorRend, endColor);
_endColor = endColor;
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
_expand = expand;
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
setRadius(radius);
setCenter(center);
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
return true;
}
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
2021-08-30 10:47:07 +08:00
void LayerRadialGradient::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
2019-11-23 20:27:39 +08:00
{
_customCommand.init(_globalZOrder, _blendFunc);
renderer->addCommand(&_customCommand);
const auto& projectionMat = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2021-08-30 10:47:07 +08:00
auto programState = _customCommand.getPipelineDescriptor().programState;
Mat4 finalMat = projectionMat * transform;
2019-11-23 20:27:39 +08:00
programState->setUniform(_mvpMatrixLocation, finalMat.m, sizeof(finalMat.m));
programState->setUniform(_startColorLocation, &_startColorRend, sizeof(_startColorRend));
programState->setUniform(_endColorLocation, &_endColorRend, sizeof(_endColorRend));
programState->setUniform(_centerLocation, &_center, sizeof(_center));
programState->setUniform(_radiusLocation, &_radius, sizeof(_radius));
programState->setUniform(_expandLocation, &_expand, sizeof(_expand));
}
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);
_customCommand.updateVertexBuffer(_vertices, sizeof(_vertices));
}
void LayerRadialGradient::setStartOpacity(uint8_t opacity)
{
_startColorRend.a = opacity / 255.0f;
2021-08-30 10:47:07 +08:00
_startColor.a = opacity;
2019-11-23 20:27:39 +08:00
}
uint8_t LayerRadialGradient::getStartOpacity() const
{
return _startColor.a;
}
void LayerRadialGradient::setEndOpacity(uint8_t opacity)
{
_endColorRend.a = opacity / 255.0f;
2021-08-30 10:47:07 +08:00
_endColor.a = opacity;
2019-11-23 20:27:39 +08:00
}
uint8_t 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));
}
2021-08-30 10:47:07 +08:00
void LayerRadialGradient::setStartColor(const cocos2d::Color4B &color)
2019-11-23 20:27:39 +08:00
{
_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));
}
2021-08-30 10:47:07 +08:00
void LayerRadialGradient::setEndColor(const cocos2d::Color4B &color)
2019-11-23 20:27:39 +08:00
{
_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;
}
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
/// MultiplexLayer
2021-08-30 10:47:07 +08:00
LayerMultiplex::LayerMultiplex()
: _enabledLayer(0)
{
}
2019-11-23 20:27:39 +08:00
LayerMultiplex::~LayerMultiplex()
{
2021-08-30 10:47:07 +08:00
for(const auto &layer : _layers) {
2019-11-23 20:27:39 +08:00
layer->cleanup();
}
}
2021-08-30 10:47:07 +08:00
LayerMultiplex * LayerMultiplex::create(Layer * layer, ...)
2019-11-23 20:27:39 +08:00
{
va_list args;
2021-08-30 10:47:07 +08:00
va_start(args,layer);
2019-11-23 20:27:39 +08:00
2021-08-30 10:47:07 +08:00
LayerMultiplex * multiplexLayer = new (std::nothrow) LayerMultiplex();
if(multiplexLayer && multiplexLayer->initWithLayers(layer, args))
2019-11-23 20:27:39 +08:00
{
multiplexLayer->autorelease();
va_end(args);
return multiplexLayer;
}
va_end(args);
CC_SAFE_DELETE(multiplexLayer);
return nullptr;
}
2021-08-30 10:47:07 +08:00
LayerMultiplex * LayerMultiplex::createWithLayer(Layer* layer)
2019-11-23 20:27:39 +08:00
{
return LayerMultiplex::create(layer, nullptr);
}
LayerMultiplex* LayerMultiplex::create()
{
LayerMultiplex* ret = new (std::nothrow) LayerMultiplex();
if (ret && ret->init())
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
LayerMultiplex* LayerMultiplex::createWithArray(const Vector<Layer*>& arrayOfLayers)
{
LayerMultiplex* ret = new (std::nothrow) LayerMultiplex();
if (ret && ret->initWithArray(arrayOfLayers))
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
void LayerMultiplex::addLayer(Layer* layer)
{
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
sEngine->retainScriptObject(this, layer);
}
2021-08-30 10:47:07 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
2019-11-23 20:27:39 +08:00
_layers.pushBack(layer);
}
bool LayerMultiplex::init()
{
if (Layer::init())
{
_enabledLayer = 0;
return true;
}
return false;
}
2021-08-30 10:47:07 +08:00
bool LayerMultiplex::initWithLayers(Layer *layer, va_list params)
2019-11-23 20:27:39 +08:00
{
if (Layer::init())
{
_layers.reserve(5);
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
sEngine->retainScriptObject(this, layer);
}
2021-08-30 10:47:07 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
2019-11-23 20:27:39 +08:00
_layers.pushBack(layer);
2021-08-30 10:47:07 +08:00
Layer *l = va_arg(params,Layer*);
while( l ) {
2019-11-23 20:27:39 +08:00
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
if (sEngine)
{
sEngine->retainScriptObject(this, l);
}
2021-08-30 10:47:07 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
2019-11-23 20:27:39 +08:00
_layers.pushBack(l);
2021-08-30 10:47:07 +08:00
l = va_arg(params,Layer*);
2019-11-23 20:27:39 +08:00
}
_enabledLayer = 0;
this->addChild(_layers.at(_enabledLayer));
return true;
}
return false;
}
bool LayerMultiplex::initWithArray(const Vector<Layer*>& arrayOfLayers)
{
if (Layer::init())
{
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
2021-08-30 10:47:07 +08:00
for (const auto &layer : arrayOfLayers)
2019-11-23 20:27:39 +08:00
{
if (layer)
{
sEngine->retainScriptObject(this, layer);
}
}
}
2021-08-30 10:47:07 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
2019-11-23 20:27:39 +08:00
_layers.reserve(arrayOfLayers.size());
_layers.pushBack(arrayOfLayers);
_enabledLayer = 0;
this->addChild(_layers.at(_enabledLayer));
return true;
}
return false;
}
void LayerMultiplex::switchTo(int n)
{
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
switchTo(n, true);
}
void LayerMultiplex::switchTo(int n, bool cleanup)
{
2021-08-30 10:47:07 +08:00
CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
2019-11-23 20:27:39 +08:00
this->removeChild(_layers.at(_enabledLayer), cleanup);
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
_enabledLayer = n;
2021-08-30 10:47:07 +08:00
2019-11-23 20:27:39 +08:00
this->addChild(_layers.at(n));
}
void LayerMultiplex::switchToAndReleaseMe(int n)
{
2021-08-30 10:47:07 +08:00
CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
2019-11-23 20:27:39 +08:00
this->removeChild(_layers.at(_enabledLayer), true);
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
sEngine->releaseScriptObject(this, _layers.at(_enabledLayer));
}
2021-08-30 10:47:07 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
2019-11-23 20:27:39 +08:00
_layers.replace(_enabledLayer, nullptr);
_enabledLayer = n;
this->addChild(_layers.at(n));
}
std::string LayerMultiplex::getDescription() const
{
return StringUtils::format("<LayerMultiplex | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
}
NS_CC_END