12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298 |
- /****************************************************************************
- 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.
-
- 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 "2d/CCLayer.h"
- #include "base/CCScriptSupport.h"
- #include "platform/CCDevice.h"
- #include "renderer/CCRenderer.h"
- #include "renderer/ccGLStateCache.h"
- #include "renderer/CCGLProgramState.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"
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
- #include "platform/desktop/CCGLViewImpl-desktop.h"
- #endif
- NS_CC_BEGIN
- // Layer
- Layer::Layer()
- : _touchEnabled(false)
- , _accelerometerEnabled(false)
- , _keyboardEnabled(false)
- , _touchListener(nullptr)
- , _keyboardListener(nullptr)
- , _accelerationListener(nullptr)
- , _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
- , _swallowsTouches(true)
- {
- _ignoreAnchorPointForPosition = true;
- setAnchorPoint(Vec2(0.5f, 0.5f));
- }
- Layer::~Layer()
- {
- }
- bool Layer::init()
- {
- Director * director = Director::getInstance();
- setContentSize(director->getWinSize());
- return true;
- }
- Layer *Layer::create()
- {
- Layer *ret = new (std::nothrow) Layer();
- 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
- if (kScriptTypeLua == _scriptType)
- {
- TouchScriptData data(eventType, this, touch, event);
- ScriptEvent scriptEvent(kTouchEvent, &data);
- return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
- }
- #else
- CC_UNUSED_PARAM(eventType);
- CC_UNUSED_PARAM(touch);
- CC_UNUSED_PARAM(event);
- #endif
- return 0;
- }
- int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType, const std::vector<Touch*>& touches, Event* event)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if (kScriptTypeLua == _scriptType)
- {
- TouchesScriptData data(eventType, this, touches, event);
- ScriptEvent scriptEvent(kTouchesEvent, &data);
- return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
- }
- #else
- CC_UNUSED_PARAM(eventType);
- CC_UNUSED_PARAM(touches);
- CC_UNUSED_PARAM(event);
- #endif
- return 0;
- }
- 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*/) {}
- #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
- /// isTouchEnabled getter
- bool Layer::isTouchEnabled() const
- {
- return _touchEnabled;
- }
- /// isTouchEnabled setter
- void Layer::setTouchEnabled(bool enabled)
- {
- if (_touchEnabled != enabled)
- {
- _touchEnabled = enabled;
- if (enabled)
- {
- 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;
- }
- }
- else
- {
- _eventDispatcher->removeEventListener(_touchListener);
- _touchListener = nullptr;
- }
- }
- }
- void Layer::setTouchMode(Touch::DispatchMode mode)
- {
- if(_touchMode != mode)
- {
- _touchMode = mode;
- if( _touchEnabled)
- {
- setTouchEnabled(false);
- setTouchEnabled(true);
- }
- }
- }
- void Layer::setSwallowsTouches(bool swallowsTouches)
- {
- if (_swallowsTouches != swallowsTouches)
- {
- _swallowsTouches = swallowsTouches;
- if( _touchEnabled)
- {
- setTouchEnabled(false);
- setTouchEnabled(true);
- }
- }
- }
- 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);
- _eventDispatcher->removeEventListener(_accelerationListener);
- _accelerationListener = nullptr;
- if (enabled)
- {
- _accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(Layer::onAcceleration, this));
- _eventDispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);
- }
- }
- }
- void Layer::setAccelerometerInterval(double interval) {
- if (_accelerometerEnabled)
- {
- if (_running)
- {
- Device::setAccelerometerInterval(interval);
- }
- }
- }
- void Layer::onAcceleration(Acceleration* acc, Event* /*unused_event*/)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if(kScriptTypeNone != _scriptType)
- {
- BasicScriptData data(this,(void*)acc);
- ScriptEvent event(kAccelerometerEvent,&data);
- ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
- }
- #else
- CC_UNUSED_PARAM(acc);
- #endif
- }
- void Layer::onKeyPressed(EventKeyboard::KeyCode /*keyCode*/, Event* /*unused_event*/)
- {
- }
- void Layer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* /*unused_event*/)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if(kScriptTypeNone != _scriptType)
- {
- KeypadScriptData data(keyCode, this);
- ScriptEvent event(kKeypadEvent,&data);
- ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
- }
- #else
- CC_UNUSED_PARAM(keyCode);
- #endif
- }
- /// 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();
- listener->onKeyPressed = CC_CALLBACK_2(Layer::onKeyPressed, this);
- listener->onKeyReleased = CC_CALLBACK_2(Layer::onKeyReleased, this);
- _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
- _keyboardListener = listener;
- }
- }
- }
- void Layer::setKeypadEnabled(bool enabled)
- {
- setKeyboardEnabled(enabled);
- }
- /// Callbacks
- bool Layer::onTouchBegan(Touch *touch, Event *event)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if (kScriptTypeLua == _scriptType)
- {
- return executeScriptTouchHandler(EventTouch::EventCode::BEGAN, touch, event) == 0 ? false : true;
- }
- #else
- CC_UNUSED_PARAM(touch);
- CC_UNUSED_PARAM(event);
- #endif
- CCASSERT(false, "Layer#ccTouchBegan override me");
- return true;
- }
- void Layer::onTouchMoved(Touch *touch, Event *event)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if (kScriptTypeLua == _scriptType)
- {
- executeScriptTouchHandler(EventTouch::EventCode::MOVED, touch, event);
- return;
- }
- #else
- CC_UNUSED_PARAM(touch);
- CC_UNUSED_PARAM(event);
- #endif
- }
- void Layer::onTouchEnded(Touch *touch, Event *event)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if (kScriptTypeLua == _scriptType)
- {
- executeScriptTouchHandler(EventTouch::EventCode::ENDED, touch, event);
- return;
- }
- #else
- CC_UNUSED_PARAM(touch);
- CC_UNUSED_PARAM(event);
- #endif
- }
- void Layer::onTouchCancelled(Touch *touch, Event *event)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if (kScriptTypeLua == _scriptType)
- {
- executeScriptTouchHandler(EventTouch::EventCode::CANCELLED, touch, event);
- return;
- }
- #else
- CC_UNUSED_PARAM(touch);
- CC_UNUSED_PARAM(event);
- #endif
- }
- void Layer::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if (kScriptTypeLua == _scriptType)
- {
- executeScriptTouchesHandler(EventTouch::EventCode::BEGAN, touches, event);
- return;
- }
- #else
- CC_UNUSED_PARAM(touches);
- CC_UNUSED_PARAM(event);
- #endif
- }
- void Layer::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if (kScriptTypeLua == _scriptType)
- {
- executeScriptTouchesHandler(EventTouch::EventCode::MOVED, touches, event);
- return;
- }
- #else
- CC_UNUSED_PARAM(touches);
- CC_UNUSED_PARAM(event);
- #endif
- }
- void Layer::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if (kScriptTypeLua == _scriptType)
- {
- executeScriptTouchesHandler(EventTouch::EventCode::ENDED, touches, event);
- return;
- }
- #else
- CC_UNUSED_PARAM(touches);
- CC_UNUSED_PARAM(event);
- #endif
- }
- void Layer::onTouchesCancelled(const std::vector<Touch*>& touches, Event *event)
- {
- #if CC_ENABLE_SCRIPT_BINDING
- if (kScriptTypeLua == _scriptType)
- {
- executeScriptTouchesHandler(EventTouch::EventCode::CANCELLED, touches, event);
- return;
- }
- #else
- CC_UNUSED_PARAM(touches);
- CC_UNUSED_PARAM(event);
- #endif
- }
- std::string Layer::getDescription() const
- {
- return StringUtils::format("<Layer | Tag = %d>", _tag);
- }
- __LayerRGBA::__LayerRGBA()
- {
- CCLOG("LayerRGBA deprecated.");
- }
- #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
- /// LayerColor
- LayerColor::LayerColor()
- {
- // default blend function
- _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
- }
-
- LayerColor::~LayerColor()
- {
- }
- /// blendFunc getter
- const BlendFunc &LayerColor::getBlendFunc() const
- {
- return _blendFunc;
- }
- /// blendFunc setter
- void LayerColor::setBlendFunc(const BlendFunc &var)
- {
- _blendFunc = var;
- }
- LayerColor* LayerColor::create()
- {
- LayerColor* ret = new (std::nothrow) LayerColor();
- if (ret && ret->init())
- {
- ret->autorelease();
- }
- else
- {
- CC_SAFE_DELETE(ret);
- }
- return ret;
- }
- LayerColor * LayerColor::create(const Color4B& color, GLfloat width, GLfloat height)
- {
- LayerColor * layer = new (std::nothrow) LayerColor();
- if( layer && layer->initWithColor(color,width,height))
- {
- layer->autorelease();
- return layer;
- }
- CC_SAFE_DELETE(layer);
- return nullptr;
- }
- LayerColor * LayerColor::create(const Color4B& color)
- {
- LayerColor * layer = new (std::nothrow) LayerColor();
- if(layer && layer->initWithColor(color))
- {
- layer->autorelease();
- return layer;
- }
- CC_SAFE_DELETE(layer);
- return nullptr;
- }
- bool LayerColor::init()
- {
- Size s = Director::getInstance()->getWinSize();
- return initWithColor(Color4B(0,0,0,0), s.width, s.height);
- }
- bool LayerColor::initWithColor(const Color4B& color, GLfloat w, GLfloat h)
- {
- if (Layer::init())
- {
- // 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();
- setContentSize(Size(w, h));
- setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
- return true;
- }
- return false;
- }
- bool LayerColor::initWithColor(const Color4B& color)
- {
- Size s = Director::getInstance()->getWinSize();
- return initWithColor(color, s.width, s.height);
- }
- /// 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(GLfloat w ,GLfloat h)
- {
- this->setContentSize(Size(w, h));
- }
- void LayerColor::changeWidth(GLfloat w)
- {
- this->setContentSize(Size(w, _contentSize.height));
- }
- void LayerColor::changeHeight(GLfloat h)
- {
- this->setContentSize(Size(_contentSize.width, h));
- }
- void LayerColor::updateColor()
- {
- for( unsigned int i=0; i < 4; i++ )
- {
- _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;
- }
- }
- void LayerColor::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
- {
- _customCommand.init(_globalZOrder, transform, flags);
- _customCommand.func = CC_CALLBACK_0(LayerColor::onDraw, this, transform, flags);
- renderer->addCommand(&_customCommand);
-
- 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);
- _noMVPVertices[i] = Vec3(pos.x,pos.y,pos.z)/pos.w;
- }
- }
- void LayerColor::onDraw(const Mat4& transform, uint32_t /*flags*/)
- {
- getGLProgram()->use();
- getGLProgram()->setUniformsForBuiltins(transform);
-
- GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR );
-
- //
- // Attributes
- //
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
- glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
- GL::blendFunc( _blendFunc.src, _blendFunc.dst );
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4);
- }
- std::string LayerColor::getDescription() const
- {
- return StringUtils::format("<LayerColor | Tag = %d>", _tag);
- }
- //
- // LayerGradient
- //
- LayerGradient::LayerGradient()
- : _startColor(Color4B::BLACK)
- , _endColor(Color4B::BLACK)
- , _startOpacity(255)
- , _endOpacity(255)
- , _alongVector(Vec2(0, -1))
- , _compressedInterpolation(true)
- {
-
- }
- LayerGradient::~LayerGradient()
- {
- }
- LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end)
- {
- LayerGradient * layer = new (std::nothrow) LayerGradient();
- if( layer && layer->initWithColor(start, end))
- {
- layer->autorelease();
- return layer;
- }
- CC_SAFE_DELETE(layer);
- return nullptr;
- }
- LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end, const Vec2& v)
- {
- LayerGradient * layer = new (std::nothrow) LayerGradient();
- if( layer && layer->initWithColor(start, end, v))
- {
- 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)
- {
- _endColor.r = end.r;
- _endColor.g = end.g;
- _endColor.b = end.b;
- _endOpacity = end.a;
- _startOpacity = start.a;
- _alongVector = v;
- _compressedInterpolation = true;
- return LayerColor::initWithColor(Color4B(start.r, start.g, start.b, 255));
- }
- void LayerGradient::updateColor()
- {
- LayerColor::updateColor();
- 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)
- {
- float h2 = 1 / ( fabsf(u.x) + fabsf(u.y) );
- u = u * (h2 * (float)c);
- }
- float opacityf = (float)_displayedOpacity / 255.0f;
- Color4F S(
- _displayedColor.r / 255.0f,
- _displayedColor.g / 255.0f,
- _displayedColor.b / 255.0f,
- _startOpacity * opacityf / 255.0f
- );
- Color4F E(
- _endColor.r / 255.0f,
- _endColor.g / 255.0f,
- _endColor.b / 255.0f,
- _endOpacity * opacityf / 255.0f
- );
- // (-1, -1)
- _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));
- // (1, -1)
- _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));
- // (-1, 1)
- _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));
- // (1, 1)
- _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));
- }
- 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(GLubyte o)
- {
- _startOpacity = o;
- updateColor();
- }
- GLubyte LayerGradient::getStartOpacity() const
- {
- return _startOpacity;
- }
- void LayerGradient::setEndOpacity(GLubyte o)
- {
- _endOpacity = o;
- updateColor();
- }
- GLubyte 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
- */
- 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)
- , _radius(0.f)
- , _expand(0.f)
- , _center(Vec2(0,0))
- , _uniformLocationCenter(0)
- , _uniformLocationRadius(0)
- , _uniformLocationExpand(0)
- , _uniformLocationEndColor(0)
- , _uniformLocationStartColor(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;
- }
- /// MultiplexLayer
- LayerMultiplex::LayerMultiplex()
- : _enabledLayer(0)
- {
- }
- LayerMultiplex::~LayerMultiplex()
- {
- for(const auto &layer : _layers) {
- layer->cleanup();
- }
- }
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
- LayerMultiplex * LayerMultiplex::createVariadic(Layer * layer, ...)
- {
- va_list args;
- va_start(args,layer);
- LayerMultiplex * multiplexLayer = new (std::nothrow) LayerMultiplex();
- if(multiplexLayer && multiplexLayer->initWithLayers(layer, args))
- {
- multiplexLayer->autorelease();
- va_end(args);
- return multiplexLayer;
- }
- va_end(args);
- CC_SAFE_DELETE(multiplexLayer);
- return nullptr;
- }
- #else
- LayerMultiplex * LayerMultiplex::create(Layer * layer, ...)
- {
- va_list args;
- va_start(args,layer);
- LayerMultiplex * multiplexLayer = new (std::nothrow) LayerMultiplex();
- if(multiplexLayer && multiplexLayer->initWithLayers(layer, args))
- {
- multiplexLayer->autorelease();
- va_end(args);
- return multiplexLayer;
- }
- va_end(args);
- CC_SAFE_DELETE(multiplexLayer);
- return nullptr;
- }
- #endif
- LayerMultiplex * LayerMultiplex::createWithLayer(Layer* layer)
- {
- 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);
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _layers.pushBack(layer);
- }
- bool LayerMultiplex::init()
- {
- if (Layer::init())
- {
- _enabledLayer = 0;
- return true;
- }
- return false;
- }
- bool LayerMultiplex::initWithLayers(Layer *layer, va_list params)
- {
- if (Layer::init())
- {
- _layers.reserve(5);
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
- if (sEngine)
- {
- sEngine->retainScriptObject(this, layer);
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _layers.pushBack(layer);
- Layer *l = va_arg(params,Layer*);
- while( l ) {
- #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- if (sEngine)
- {
- sEngine->retainScriptObject(this, l);
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _layers.pushBack(l);
- l = va_arg(params,Layer*);
- }
- _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)
- {
- for (const auto &layer : arrayOfLayers)
- {
- if (layer)
- {
- sEngine->retainScriptObject(this, layer);
- }
- }
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
- _layers.reserve(arrayOfLayers.size());
- _layers.pushBack(arrayOfLayers);
- _enabledLayer = 0;
- this->addChild(_layers.at(_enabledLayer));
- return true;
- }
- return false;
- }
- void LayerMultiplex::switchTo(int n)
- {
-
- switchTo(n, true);
- }
- void LayerMultiplex::switchTo(int n, bool cleanup)
- {
- CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
-
- this->removeChild(_layers.at(_enabledLayer), cleanup);
-
- _enabledLayer = n;
-
- this->addChild(_layers.at(n));
- }
- void LayerMultiplex::switchToAndReleaseMe(int n)
- {
- CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
- 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));
- }
- #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
-
- _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
|