CCLayer.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include <stdarg.h>
  25. #include "2d/CCLayer.h"
  26. #include "base/CCScriptSupport.h"
  27. #include "platform/CCDevice.h"
  28. #include "renderer/CCRenderer.h"
  29. #include "renderer/ccGLStateCache.h"
  30. #include "renderer/CCGLProgramState.h"
  31. #include "base/CCDirector.h"
  32. #include "base/CCEventDispatcher.h"
  33. #include "base/CCEventListenerTouch.h"
  34. #include "base/CCEventTouch.h"
  35. #include "base/CCEventKeyboard.h"
  36. #include "base/CCEventListenerKeyboard.h"
  37. #include "base/CCEventAcceleration.h"
  38. #include "base/CCEventListenerAcceleration.h"
  39. #include "base/ccUTF8.h"
  40. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  41. #include "platform/desktop/CCGLViewImpl-desktop.h"
  42. #endif
  43. NS_CC_BEGIN
  44. // Layer
  45. Layer::Layer()
  46. : _touchEnabled(false)
  47. , _accelerometerEnabled(false)
  48. , _keyboardEnabled(false)
  49. , _touchListener(nullptr)
  50. , _keyboardListener(nullptr)
  51. , _accelerationListener(nullptr)
  52. , _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
  53. , _swallowsTouches(true)
  54. {
  55. _ignoreAnchorPointForPosition = true;
  56. setAnchorPoint(Vec2(0.5f, 0.5f));
  57. }
  58. Layer::~Layer()
  59. {
  60. }
  61. bool Layer::init()
  62. {
  63. Director * director = Director::getInstance();
  64. setContentSize(director->getWinSize());
  65. return true;
  66. }
  67. Layer *Layer::create()
  68. {
  69. Layer *ret = new (std::nothrow) Layer();
  70. if (ret && ret->init())
  71. {
  72. ret->autorelease();
  73. return ret;
  74. }
  75. else
  76. {
  77. CC_SAFE_DELETE(ret);
  78. return nullptr;
  79. }
  80. }
  81. int Layer::executeScriptTouchHandler(EventTouch::EventCode eventType, Touch* touch, Event* event)
  82. {
  83. #if CC_ENABLE_SCRIPT_BINDING
  84. if (kScriptTypeLua == _scriptType)
  85. {
  86. TouchScriptData data(eventType, this, touch, event);
  87. ScriptEvent scriptEvent(kTouchEvent, &data);
  88. return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
  89. }
  90. #else
  91. CC_UNUSED_PARAM(eventType);
  92. CC_UNUSED_PARAM(touch);
  93. CC_UNUSED_PARAM(event);
  94. #endif
  95. return 0;
  96. }
  97. int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType, const std::vector<Touch*>& touches, Event* event)
  98. {
  99. #if CC_ENABLE_SCRIPT_BINDING
  100. if (kScriptTypeLua == _scriptType)
  101. {
  102. TouchesScriptData data(eventType, this, touches, event);
  103. ScriptEvent scriptEvent(kTouchesEvent, &data);
  104. return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
  105. }
  106. #else
  107. CC_UNUSED_PARAM(eventType);
  108. CC_UNUSED_PARAM(touches);
  109. CC_UNUSED_PARAM(event);
  110. #endif
  111. return 0;
  112. }
  113. bool Layer::ccTouchBegan(Touch* /*pTouch*/, Event* /*pEvent*/) {return false;};
  114. void Layer::ccTouchMoved(Touch* /*pTouch*/, Event* /*pEvent*/) {}
  115. void Layer::ccTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/) {}
  116. void Layer::ccTouchCancelled(Touch* /*pTouch*/, Event* /*pEvent*/) {}
  117. void Layer::ccTouchesBegan(__Set* /*pTouches*/, Event* /*pEvent*/) {}
  118. void Layer::ccTouchesMoved(__Set* /*pTouches*/, Event* /*pEvent*/) {}
  119. void Layer::ccTouchesEnded(__Set* /*pTouches*/, Event* /*pEvent*/) {}
  120. void Layer::ccTouchesCancelled(__Set* /*pTouches*/, Event* /*pEvent*/) {}
  121. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  122. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  123. #elif _MSC_VER >= 1400 //vs 2005 or higher
  124. #pragma warning (push)
  125. #pragma warning (disable: 4996)
  126. #endif
  127. /// isTouchEnabled getter
  128. bool Layer::isTouchEnabled() const
  129. {
  130. return _touchEnabled;
  131. }
  132. /// isTouchEnabled setter
  133. void Layer::setTouchEnabled(bool enabled)
  134. {
  135. if (_touchEnabled != enabled)
  136. {
  137. _touchEnabled = enabled;
  138. if (enabled)
  139. {
  140. if (_touchListener != nullptr)
  141. return;
  142. if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE )
  143. {
  144. // Register Touch Event
  145. auto listener = EventListenerTouchAllAtOnce::create();
  146. listener->onTouchesBegan = CC_CALLBACK_2(Layer::onTouchesBegan, this);
  147. listener->onTouchesMoved = CC_CALLBACK_2(Layer::onTouchesMoved, this);
  148. listener->onTouchesEnded = CC_CALLBACK_2(Layer::onTouchesEnded, this);
  149. listener->onTouchesCancelled = CC_CALLBACK_2(Layer::onTouchesCancelled, this);
  150. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  151. _touchListener = listener;
  152. }
  153. else
  154. {
  155. // Register Touch Event
  156. auto listener = EventListenerTouchOneByOne::create();
  157. listener->setSwallowTouches(_swallowsTouches);
  158. listener->onTouchBegan = CC_CALLBACK_2(Layer::onTouchBegan, this);
  159. listener->onTouchMoved = CC_CALLBACK_2(Layer::onTouchMoved, this);
  160. listener->onTouchEnded = CC_CALLBACK_2(Layer::onTouchEnded, this);
  161. listener->onTouchCancelled = CC_CALLBACK_2(Layer::onTouchCancelled, this);
  162. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  163. _touchListener = listener;
  164. }
  165. }
  166. else
  167. {
  168. _eventDispatcher->removeEventListener(_touchListener);
  169. _touchListener = nullptr;
  170. }
  171. }
  172. }
  173. void Layer::setTouchMode(Touch::DispatchMode mode)
  174. {
  175. if(_touchMode != mode)
  176. {
  177. _touchMode = mode;
  178. if( _touchEnabled)
  179. {
  180. setTouchEnabled(false);
  181. setTouchEnabled(true);
  182. }
  183. }
  184. }
  185. void Layer::setSwallowsTouches(bool swallowsTouches)
  186. {
  187. if (_swallowsTouches != swallowsTouches)
  188. {
  189. _swallowsTouches = swallowsTouches;
  190. if( _touchEnabled)
  191. {
  192. setTouchEnabled(false);
  193. setTouchEnabled(true);
  194. }
  195. }
  196. }
  197. Touch::DispatchMode Layer::getTouchMode() const
  198. {
  199. return _touchMode;
  200. }
  201. bool Layer::isSwallowsTouches() const
  202. {
  203. return _swallowsTouches;
  204. }
  205. /// isAccelerometerEnabled getter
  206. bool Layer::isAccelerometerEnabled() const
  207. {
  208. return _accelerometerEnabled;
  209. }
  210. /// isAccelerometerEnabled setter
  211. void Layer::setAccelerometerEnabled(bool enabled)
  212. {
  213. if (enabled != _accelerometerEnabled)
  214. {
  215. _accelerometerEnabled = enabled;
  216. Device::setAccelerometerEnabled(enabled);
  217. _eventDispatcher->removeEventListener(_accelerationListener);
  218. _accelerationListener = nullptr;
  219. if (enabled)
  220. {
  221. _accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(Layer::onAcceleration, this));
  222. _eventDispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);
  223. }
  224. }
  225. }
  226. void Layer::setAccelerometerInterval(double interval) {
  227. if (_accelerometerEnabled)
  228. {
  229. if (_running)
  230. {
  231. Device::setAccelerometerInterval(interval);
  232. }
  233. }
  234. }
  235. void Layer::onAcceleration(Acceleration* acc, Event* /*unused_event*/)
  236. {
  237. #if CC_ENABLE_SCRIPT_BINDING
  238. if(kScriptTypeNone != _scriptType)
  239. {
  240. BasicScriptData data(this,(void*)acc);
  241. ScriptEvent event(kAccelerometerEvent,&data);
  242. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  243. }
  244. #else
  245. CC_UNUSED_PARAM(acc);
  246. #endif
  247. }
  248. void Layer::onKeyPressed(EventKeyboard::KeyCode /*keyCode*/, Event* /*unused_event*/)
  249. {
  250. }
  251. void Layer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* /*unused_event*/)
  252. {
  253. #if CC_ENABLE_SCRIPT_BINDING
  254. if(kScriptTypeNone != _scriptType)
  255. {
  256. KeypadScriptData data(keyCode, this);
  257. ScriptEvent event(kKeypadEvent,&data);
  258. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  259. }
  260. #else
  261. CC_UNUSED_PARAM(keyCode);
  262. #endif
  263. }
  264. /// isKeyboardEnabled getter
  265. bool Layer::isKeyboardEnabled() const
  266. {
  267. return _keyboardEnabled;
  268. }
  269. /// isKeyboardEnabled setter
  270. void Layer::setKeyboardEnabled(bool enabled)
  271. {
  272. if (enabled != _keyboardEnabled)
  273. {
  274. _keyboardEnabled = enabled;
  275. _eventDispatcher->removeEventListener(_keyboardListener);
  276. _keyboardListener = nullptr;
  277. if (enabled)
  278. {
  279. auto listener = EventListenerKeyboard::create();
  280. listener->onKeyPressed = CC_CALLBACK_2(Layer::onKeyPressed, this);
  281. listener->onKeyReleased = CC_CALLBACK_2(Layer::onKeyReleased, this);
  282. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  283. _keyboardListener = listener;
  284. }
  285. }
  286. }
  287. void Layer::setKeypadEnabled(bool enabled)
  288. {
  289. setKeyboardEnabled(enabled);
  290. }
  291. /// Callbacks
  292. bool Layer::onTouchBegan(Touch *touch, Event *event)
  293. {
  294. #if CC_ENABLE_SCRIPT_BINDING
  295. if (kScriptTypeLua == _scriptType)
  296. {
  297. return executeScriptTouchHandler(EventTouch::EventCode::BEGAN, touch, event) == 0 ? false : true;
  298. }
  299. #else
  300. CC_UNUSED_PARAM(touch);
  301. CC_UNUSED_PARAM(event);
  302. #endif
  303. CCASSERT(false, "Layer#ccTouchBegan override me");
  304. return true;
  305. }
  306. void Layer::onTouchMoved(Touch *touch, Event *event)
  307. {
  308. #if CC_ENABLE_SCRIPT_BINDING
  309. if (kScriptTypeLua == _scriptType)
  310. {
  311. executeScriptTouchHandler(EventTouch::EventCode::MOVED, touch, event);
  312. return;
  313. }
  314. #else
  315. CC_UNUSED_PARAM(touch);
  316. CC_UNUSED_PARAM(event);
  317. #endif
  318. }
  319. void Layer::onTouchEnded(Touch *touch, Event *event)
  320. {
  321. #if CC_ENABLE_SCRIPT_BINDING
  322. if (kScriptTypeLua == _scriptType)
  323. {
  324. executeScriptTouchHandler(EventTouch::EventCode::ENDED, touch, event);
  325. return;
  326. }
  327. #else
  328. CC_UNUSED_PARAM(touch);
  329. CC_UNUSED_PARAM(event);
  330. #endif
  331. }
  332. void Layer::onTouchCancelled(Touch *touch, Event *event)
  333. {
  334. #if CC_ENABLE_SCRIPT_BINDING
  335. if (kScriptTypeLua == _scriptType)
  336. {
  337. executeScriptTouchHandler(EventTouch::EventCode::CANCELLED, touch, event);
  338. return;
  339. }
  340. #else
  341. CC_UNUSED_PARAM(touch);
  342. CC_UNUSED_PARAM(event);
  343. #endif
  344. }
  345. void Layer::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
  346. {
  347. #if CC_ENABLE_SCRIPT_BINDING
  348. if (kScriptTypeLua == _scriptType)
  349. {
  350. executeScriptTouchesHandler(EventTouch::EventCode::BEGAN, touches, event);
  351. return;
  352. }
  353. #else
  354. CC_UNUSED_PARAM(touches);
  355. CC_UNUSED_PARAM(event);
  356. #endif
  357. }
  358. void Layer::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
  359. {
  360. #if CC_ENABLE_SCRIPT_BINDING
  361. if (kScriptTypeLua == _scriptType)
  362. {
  363. executeScriptTouchesHandler(EventTouch::EventCode::MOVED, touches, event);
  364. return;
  365. }
  366. #else
  367. CC_UNUSED_PARAM(touches);
  368. CC_UNUSED_PARAM(event);
  369. #endif
  370. }
  371. void Layer::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
  372. {
  373. #if CC_ENABLE_SCRIPT_BINDING
  374. if (kScriptTypeLua == _scriptType)
  375. {
  376. executeScriptTouchesHandler(EventTouch::EventCode::ENDED, touches, event);
  377. return;
  378. }
  379. #else
  380. CC_UNUSED_PARAM(touches);
  381. CC_UNUSED_PARAM(event);
  382. #endif
  383. }
  384. void Layer::onTouchesCancelled(const std::vector<Touch*>& touches, Event *event)
  385. {
  386. #if CC_ENABLE_SCRIPT_BINDING
  387. if (kScriptTypeLua == _scriptType)
  388. {
  389. executeScriptTouchesHandler(EventTouch::EventCode::CANCELLED, touches, event);
  390. return;
  391. }
  392. #else
  393. CC_UNUSED_PARAM(touches);
  394. CC_UNUSED_PARAM(event);
  395. #endif
  396. }
  397. std::string Layer::getDescription() const
  398. {
  399. return StringUtils::format("<Layer | Tag = %d>", _tag);
  400. }
  401. __LayerRGBA::__LayerRGBA()
  402. {
  403. CCLOG("LayerRGBA deprecated.");
  404. }
  405. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  406. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  407. #elif _MSC_VER >= 1400 //vs 2005 or higher
  408. #pragma warning (pop)
  409. #endif
  410. /// LayerColor
  411. LayerColor::LayerColor()
  412. {
  413. // default blend function
  414. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  415. }
  416. LayerColor::~LayerColor()
  417. {
  418. }
  419. /// blendFunc getter
  420. const BlendFunc &LayerColor::getBlendFunc() const
  421. {
  422. return _blendFunc;
  423. }
  424. /// blendFunc setter
  425. void LayerColor::setBlendFunc(const BlendFunc &var)
  426. {
  427. _blendFunc = var;
  428. }
  429. LayerColor* LayerColor::create()
  430. {
  431. LayerColor* ret = new (std::nothrow) LayerColor();
  432. if (ret && ret->init())
  433. {
  434. ret->autorelease();
  435. }
  436. else
  437. {
  438. CC_SAFE_DELETE(ret);
  439. }
  440. return ret;
  441. }
  442. LayerColor * LayerColor::create(const Color4B& color, GLfloat width, GLfloat height)
  443. {
  444. LayerColor * layer = new (std::nothrow) LayerColor();
  445. if( layer && layer->initWithColor(color,width,height))
  446. {
  447. layer->autorelease();
  448. return layer;
  449. }
  450. CC_SAFE_DELETE(layer);
  451. return nullptr;
  452. }
  453. LayerColor * LayerColor::create(const Color4B& color)
  454. {
  455. LayerColor * layer = new (std::nothrow) LayerColor();
  456. if(layer && layer->initWithColor(color))
  457. {
  458. layer->autorelease();
  459. return layer;
  460. }
  461. CC_SAFE_DELETE(layer);
  462. return nullptr;
  463. }
  464. bool LayerColor::init()
  465. {
  466. Size s = Director::getInstance()->getWinSize();
  467. return initWithColor(Color4B(0,0,0,0), s.width, s.height);
  468. }
  469. bool LayerColor::initWithColor(const Color4B& color, GLfloat w, GLfloat h)
  470. {
  471. if (Layer::init())
  472. {
  473. // default blend function
  474. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  475. _displayedColor.r = _realColor.r = color.r;
  476. _displayedColor.g = _realColor.g = color.g;
  477. _displayedColor.b = _realColor.b = color.b;
  478. _displayedOpacity = _realOpacity = color.a;
  479. for (size_t i = 0; i<sizeof(_squareVertices) / sizeof( _squareVertices[0]); i++ )
  480. {
  481. _squareVertices[i].x = 0.0f;
  482. _squareVertices[i].y = 0.0f;
  483. }
  484. updateColor();
  485. setContentSize(Size(w, h));
  486. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
  487. return true;
  488. }
  489. return false;
  490. }
  491. bool LayerColor::initWithColor(const Color4B& color)
  492. {
  493. Size s = Director::getInstance()->getWinSize();
  494. return initWithColor(color, s.width, s.height);
  495. }
  496. /// override contentSize
  497. void LayerColor::setContentSize(const Size & size)
  498. {
  499. _squareVertices[1].x = size.width;
  500. _squareVertices[2].y = size.height;
  501. _squareVertices[3].x = size.width;
  502. _squareVertices[3].y = size.height;
  503. Layer::setContentSize(size);
  504. }
  505. void LayerColor::changeWidthAndHeight(GLfloat w ,GLfloat h)
  506. {
  507. this->setContentSize(Size(w, h));
  508. }
  509. void LayerColor::changeWidth(GLfloat w)
  510. {
  511. this->setContentSize(Size(w, _contentSize.height));
  512. }
  513. void LayerColor::changeHeight(GLfloat h)
  514. {
  515. this->setContentSize(Size(_contentSize.width, h));
  516. }
  517. void LayerColor::updateColor()
  518. {
  519. for( unsigned int i=0; i < 4; i++ )
  520. {
  521. _squareColors[i].r = _displayedColor.r / 255.0f;
  522. _squareColors[i].g = _displayedColor.g / 255.0f;
  523. _squareColors[i].b = _displayedColor.b / 255.0f;
  524. _squareColors[i].a = _displayedOpacity / 255.0f;
  525. }
  526. }
  527. void LayerColor::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  528. {
  529. _customCommand.init(_globalZOrder, transform, flags);
  530. _customCommand.func = CC_CALLBACK_0(LayerColor::onDraw, this, transform, flags);
  531. renderer->addCommand(&_customCommand);
  532. for(int i = 0; i < 4; ++i)
  533. {
  534. Vec4 pos;
  535. pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ;
  536. pos.w = 1;
  537. _modelViewTransform.transformVector(&pos);
  538. _noMVPVertices[i] = Vec3(pos.x,pos.y,pos.z)/pos.w;
  539. }
  540. }
  541. void LayerColor::onDraw(const Mat4& transform, uint32_t /*flags*/)
  542. {
  543. getGLProgram()->use();
  544. getGLProgram()->setUniformsForBuiltins(transform);
  545. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR );
  546. //
  547. // Attributes
  548. //
  549. glBindBuffer(GL_ARRAY_BUFFER, 0);
  550. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
  551. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
  552. GL::blendFunc( _blendFunc.src, _blendFunc.dst );
  553. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  554. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4);
  555. }
  556. std::string LayerColor::getDescription() const
  557. {
  558. return StringUtils::format("<LayerColor | Tag = %d>", _tag);
  559. }
  560. //
  561. // LayerGradient
  562. //
  563. LayerGradient::LayerGradient()
  564. : _startColor(Color4B::BLACK)
  565. , _endColor(Color4B::BLACK)
  566. , _startOpacity(255)
  567. , _endOpacity(255)
  568. , _alongVector(Vec2(0, -1))
  569. , _compressedInterpolation(true)
  570. {
  571. }
  572. LayerGradient::~LayerGradient()
  573. {
  574. }
  575. LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end)
  576. {
  577. LayerGradient * layer = new (std::nothrow) LayerGradient();
  578. if( layer && layer->initWithColor(start, end))
  579. {
  580. layer->autorelease();
  581. return layer;
  582. }
  583. CC_SAFE_DELETE(layer);
  584. return nullptr;
  585. }
  586. LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end, const Vec2& v)
  587. {
  588. LayerGradient * layer = new (std::nothrow) LayerGradient();
  589. if( layer && layer->initWithColor(start, end, v))
  590. {
  591. layer->autorelease();
  592. return layer;
  593. }
  594. CC_SAFE_DELETE(layer);
  595. return nullptr;
  596. }
  597. LayerGradient* LayerGradient::create()
  598. {
  599. LayerGradient* ret = new (std::nothrow) LayerGradient();
  600. if (ret && ret->init())
  601. {
  602. ret->autorelease();
  603. }
  604. else
  605. {
  606. CC_SAFE_DELETE(ret);
  607. }
  608. return ret;
  609. }
  610. bool LayerGradient::init()
  611. {
  612. return initWithColor(Color4B(0, 0, 0, 255), Color4B(0, 0, 0, 255));
  613. }
  614. bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end)
  615. {
  616. return initWithColor(start, end, Vec2(0, -1));
  617. }
  618. bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end, const Vec2& v)
  619. {
  620. _endColor.r = end.r;
  621. _endColor.g = end.g;
  622. _endColor.b = end.b;
  623. _endOpacity = end.a;
  624. _startOpacity = start.a;
  625. _alongVector = v;
  626. _compressedInterpolation = true;
  627. return LayerColor::initWithColor(Color4B(start.r, start.g, start.b, 255));
  628. }
  629. void LayerGradient::updateColor()
  630. {
  631. LayerColor::updateColor();
  632. float h = _alongVector.getLength();
  633. if (h == 0)
  634. return;
  635. float c = sqrtf(2.0f);
  636. Vec2 u(_alongVector.x / h, _alongVector.y / h);
  637. // Compressed Interpolation mode
  638. if (_compressedInterpolation)
  639. {
  640. float h2 = 1 / ( fabsf(u.x) + fabsf(u.y) );
  641. u = u * (h2 * (float)c);
  642. }
  643. float opacityf = (float)_displayedOpacity / 255.0f;
  644. Color4F S(
  645. _displayedColor.r / 255.0f,
  646. _displayedColor.g / 255.0f,
  647. _displayedColor.b / 255.0f,
  648. _startOpacity * opacityf / 255.0f
  649. );
  650. Color4F E(
  651. _endColor.r / 255.0f,
  652. _endColor.g / 255.0f,
  653. _endColor.b / 255.0f,
  654. _endOpacity * opacityf / 255.0f
  655. );
  656. // (-1, -1)
  657. _squareColors[0].r = E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0f * c));
  658. _squareColors[0].g = E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0f * c));
  659. _squareColors[0].b = E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0f * c));
  660. _squareColors[0].a = E.a + (S.a - E.a) * ((c + u.x + u.y) / (2.0f * c));
  661. // (1, -1)
  662. _squareColors[1].r = E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0f * c));
  663. _squareColors[1].g = E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0f * c));
  664. _squareColors[1].b = E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0f * c));
  665. _squareColors[1].a = E.a + (S.a - E.a) * ((c - u.x + u.y) / (2.0f * c));
  666. // (-1, 1)
  667. _squareColors[2].r = E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0f * c));
  668. _squareColors[2].g = E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0f * c));
  669. _squareColors[2].b = E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0f * c));
  670. _squareColors[2].a = E.a + (S.a - E.a) * ((c + u.x - u.y) / (2.0f * c));
  671. // (1, 1)
  672. _squareColors[3].r = E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0f * c));
  673. _squareColors[3].g = E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0f * c));
  674. _squareColors[3].b = E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0f * c));
  675. _squareColors[3].a = E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0f * c));
  676. }
  677. const Color3B& LayerGradient::getStartColor() const
  678. {
  679. return _realColor;
  680. }
  681. void LayerGradient::setStartColor(const Color3B& color)
  682. {
  683. setColor(color);
  684. }
  685. void LayerGradient::setEndColor(const Color3B& color)
  686. {
  687. _endColor = color;
  688. updateColor();
  689. }
  690. const Color3B& LayerGradient::getEndColor() const
  691. {
  692. return _endColor;
  693. }
  694. void LayerGradient::setStartOpacity(GLubyte o)
  695. {
  696. _startOpacity = o;
  697. updateColor();
  698. }
  699. GLubyte LayerGradient::getStartOpacity() const
  700. {
  701. return _startOpacity;
  702. }
  703. void LayerGradient::setEndOpacity(GLubyte o)
  704. {
  705. _endOpacity = o;
  706. updateColor();
  707. }
  708. GLubyte LayerGradient::getEndOpacity() const
  709. {
  710. return _endOpacity;
  711. }
  712. void LayerGradient::setVector(const Vec2& var)
  713. {
  714. _alongVector = var;
  715. updateColor();
  716. }
  717. const Vec2& LayerGradient::getVector() const
  718. {
  719. return _alongVector;
  720. }
  721. bool LayerGradient::isCompressedInterpolation() const
  722. {
  723. return _compressedInterpolation;
  724. }
  725. void LayerGradient::setCompressedInterpolation(bool compress)
  726. {
  727. _compressedInterpolation = compress;
  728. updateColor();
  729. }
  730. std::string LayerGradient::getDescription() const
  731. {
  732. return StringUtils::format("<LayerGradient | Tag = %d>", _tag);
  733. }
  734. /**
  735. * LayerRadialGradient
  736. */
  737. LayerRadialGradient* LayerRadialGradient::create(const Color4B& startColor, const Color4B& endColor, float radius, const Vec2& center, float expand)
  738. {
  739. auto layerGradient = new LayerRadialGradient();
  740. if (layerGradient && layerGradient->initWithColor(startColor, endColor, radius, center, expand))
  741. {
  742. layerGradient->autorelease();
  743. return layerGradient;
  744. }
  745. delete layerGradient;
  746. return nullptr;
  747. }
  748. LayerRadialGradient* LayerRadialGradient::create()
  749. {
  750. auto layerGradient = new LayerRadialGradient();
  751. if (layerGradient && layerGradient->initWithColor(Color4B::BLACK, Color4B::BLACK, 0, Vec2(0,0), 0))
  752. {
  753. layerGradient->autorelease();
  754. return layerGradient;
  755. }
  756. delete layerGradient;
  757. return nullptr;
  758. }
  759. LayerRadialGradient::LayerRadialGradient()
  760. : _startColor(Color4B::BLACK)
  761. , _startColorRend(Color4F::BLACK)
  762. , _endColor(Color4B::BLACK)
  763. , _endColorRend(Color4F::BLACK)
  764. , _radius(0.f)
  765. , _expand(0.f)
  766. , _center(Vec2(0,0))
  767. , _uniformLocationCenter(0)
  768. , _uniformLocationRadius(0)
  769. , _uniformLocationExpand(0)
  770. , _uniformLocationEndColor(0)
  771. , _uniformLocationStartColor(0)
  772. , _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
  773. { }
  774. LayerRadialGradient::~LayerRadialGradient()
  775. {}
  776. bool LayerRadialGradient::initWithColor(const cocos2d::Color4B &startColor, const cocos2d::Color4B &endColor, float radius, const Vec2& center, float expand)
  777. {
  778. // should do it before Layer::init()
  779. for (int i = 0; i < 4; ++i)
  780. _vertices[i] = {0.0f, 0.0f};
  781. if (Layer::init())
  782. {
  783. convertColor4B24F(_startColorRend, startColor);
  784. _startColor = startColor;
  785. convertColor4B24F(_endColorRend, endColor);
  786. _endColor = endColor;
  787. _expand = expand;
  788. setRadius(radius);
  789. setCenter(center);
  790. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_LAYER_RADIAL_GRADIENT));
  791. auto program = getGLProgram();
  792. _uniformLocationStartColor = program->getUniformLocation("u_startColor");
  793. _uniformLocationEndColor = program->getUniformLocation("u_endColor");
  794. _uniformLocationExpand = program->getUniformLocation("u_expand");
  795. _uniformLocationRadius = program->getUniformLocation("u_radius");
  796. _uniformLocationCenter = program->getUniformLocation("u_center");
  797. return true;
  798. }
  799. return false;
  800. }
  801. void LayerRadialGradient::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  802. {
  803. _customCommand.init(_globalZOrder, transform, flags);
  804. _customCommand.func = CC_CALLBACK_0(LayerRadialGradient::onDraw, this, transform, flags);
  805. renderer->addCommand(&_customCommand);
  806. }
  807. void LayerRadialGradient::onDraw(const Mat4& transform, uint32_t /*flags*/)
  808. {
  809. auto program = getGLProgram();
  810. program->use();
  811. program->setUniformsForBuiltins(transform);
  812. program->setUniformLocationWith4f(_uniformLocationStartColor, _startColorRend.r,
  813. _startColorRend.g, _startColorRend.b, _startColorRend.a);
  814. program->setUniformLocationWith4f(_uniformLocationEndColor, _endColorRend.r,
  815. _endColorRend.g, _endColorRend.b, _endColorRend.a);
  816. program->setUniformLocationWith2f(_uniformLocationCenter, _center.x, _center.y);
  817. program->setUniformLocationWith1f(_uniformLocationRadius, _radius);
  818. program->setUniformLocationWith1f(_uniformLocationExpand, _expand);
  819. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION);
  820. //
  821. // Attributes
  822. //
  823. glBindBuffer(GL_ARRAY_BUFFER, 0);
  824. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
  825. GL::blendFunc(_blendFunc.src, _blendFunc.dst);
  826. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  827. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4);
  828. }
  829. void LayerRadialGradient::setContentSize(const Size& size)
  830. {
  831. _vertices[1].x = size.width;
  832. _vertices[2].y = size.height;
  833. _vertices[3].x = size.width;
  834. _vertices[3].y = size.height;
  835. Layer::setContentSize(size);
  836. }
  837. void LayerRadialGradient::setStartOpacity(GLubyte opacity)
  838. {
  839. _startColorRend.a = opacity / 255.0f;
  840. _startColor.a = opacity;
  841. }
  842. GLubyte LayerRadialGradient::getStartOpacity() const
  843. {
  844. return _startColor.a;
  845. }
  846. void LayerRadialGradient::setEndOpacity(GLubyte opacity)
  847. {
  848. _endColorRend.a = opacity / 255.0f;
  849. _endColor.a = opacity;
  850. }
  851. GLubyte LayerRadialGradient::getEndOpacity() const
  852. {
  853. return _endColor.a;
  854. }
  855. void LayerRadialGradient::setRadius(float radius)
  856. {
  857. _radius = radius;
  858. }
  859. float LayerRadialGradient::getRadius() const
  860. {
  861. return _radius;
  862. }
  863. void LayerRadialGradient::setCenter(const Vec2& center)
  864. {
  865. _center = center;
  866. }
  867. Vec2 LayerRadialGradient::getCenter() const
  868. {
  869. return _center;
  870. }
  871. void LayerRadialGradient::setExpand(float expand)
  872. {
  873. _expand = expand;
  874. }
  875. float LayerRadialGradient::getExpand() const
  876. {
  877. return _expand;
  878. }
  879. void LayerRadialGradient::setStartColor(const Color3B& color)
  880. {
  881. setStartColor(Color4B(color));
  882. }
  883. void LayerRadialGradient::setStartColor(const cocos2d::Color4B &color)
  884. {
  885. _startColor = color;
  886. convertColor4B24F(_startColorRend, _startColor);
  887. }
  888. Color4B LayerRadialGradient::getStartColor() const
  889. {
  890. return _startColor;
  891. }
  892. Color3B LayerRadialGradient::getStartColor3B() const
  893. {
  894. return Color3B(_startColor);
  895. }
  896. void LayerRadialGradient::setEndColor(const Color3B& color)
  897. {
  898. setEndColor(Color4B(color));
  899. }
  900. void LayerRadialGradient::setEndColor(const cocos2d::Color4B &color)
  901. {
  902. _endColor = color;
  903. convertColor4B24F(_endColorRend, _endColor);
  904. }
  905. Color4B LayerRadialGradient::getEndColor() const
  906. {
  907. return _endColor;
  908. }
  909. Color3B LayerRadialGradient::getEndColor3B() const
  910. {
  911. return Color3B(_endColor);
  912. }
  913. void LayerRadialGradient::setBlendFunc(const BlendFunc& blendFunc)
  914. {
  915. _blendFunc = blendFunc;
  916. }
  917. BlendFunc LayerRadialGradient::getBlendFunc() const
  918. {
  919. return _blendFunc;
  920. }
  921. void LayerRadialGradient::convertColor4B24F(Color4F& outColor, const Color4B& inColor)
  922. {
  923. outColor.r = inColor.r / 255.0f;
  924. outColor.g = inColor.g / 255.0f;
  925. outColor.b = inColor.b / 255.0f;
  926. outColor.a = inColor.a / 255.0f;
  927. }
  928. /// MultiplexLayer
  929. LayerMultiplex::LayerMultiplex()
  930. : _enabledLayer(0)
  931. {
  932. }
  933. LayerMultiplex::~LayerMultiplex()
  934. {
  935. for(const auto &layer : _layers) {
  936. layer->cleanup();
  937. }
  938. }
  939. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  940. LayerMultiplex * LayerMultiplex::createVariadic(Layer * layer, ...)
  941. {
  942. va_list args;
  943. va_start(args,layer);
  944. LayerMultiplex * multiplexLayer = new (std::nothrow) LayerMultiplex();
  945. if(multiplexLayer && multiplexLayer->initWithLayers(layer, args))
  946. {
  947. multiplexLayer->autorelease();
  948. va_end(args);
  949. return multiplexLayer;
  950. }
  951. va_end(args);
  952. CC_SAFE_DELETE(multiplexLayer);
  953. return nullptr;
  954. }
  955. #else
  956. LayerMultiplex * LayerMultiplex::create(Layer * layer, ...)
  957. {
  958. va_list args;
  959. va_start(args,layer);
  960. LayerMultiplex * multiplexLayer = new (std::nothrow) LayerMultiplex();
  961. if(multiplexLayer && multiplexLayer->initWithLayers(layer, args))
  962. {
  963. multiplexLayer->autorelease();
  964. va_end(args);
  965. return multiplexLayer;
  966. }
  967. va_end(args);
  968. CC_SAFE_DELETE(multiplexLayer);
  969. return nullptr;
  970. }
  971. #endif
  972. LayerMultiplex * LayerMultiplex::createWithLayer(Layer* layer)
  973. {
  974. return LayerMultiplex::create(layer, nullptr);
  975. }
  976. LayerMultiplex* LayerMultiplex::create()
  977. {
  978. LayerMultiplex* ret = new (std::nothrow) LayerMultiplex();
  979. if (ret && ret->init())
  980. {
  981. ret->autorelease();
  982. }
  983. else
  984. {
  985. CC_SAFE_DELETE(ret);
  986. }
  987. return ret;
  988. }
  989. LayerMultiplex* LayerMultiplex::createWithArray(const Vector<Layer*>& arrayOfLayers)
  990. {
  991. LayerMultiplex* ret = new (std::nothrow) LayerMultiplex();
  992. if (ret && ret->initWithArray(arrayOfLayers))
  993. {
  994. ret->autorelease();
  995. }
  996. else
  997. {
  998. CC_SAFE_DELETE(ret);
  999. }
  1000. return ret;
  1001. }
  1002. void LayerMultiplex::addLayer(Layer* layer)
  1003. {
  1004. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1005. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  1006. if (sEngine)
  1007. {
  1008. sEngine->retainScriptObject(this, layer);
  1009. }
  1010. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1011. _layers.pushBack(layer);
  1012. }
  1013. bool LayerMultiplex::init()
  1014. {
  1015. if (Layer::init())
  1016. {
  1017. _enabledLayer = 0;
  1018. return true;
  1019. }
  1020. return false;
  1021. }
  1022. bool LayerMultiplex::initWithLayers(Layer *layer, va_list params)
  1023. {
  1024. if (Layer::init())
  1025. {
  1026. _layers.reserve(5);
  1027. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1028. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  1029. if (sEngine)
  1030. {
  1031. sEngine->retainScriptObject(this, layer);
  1032. }
  1033. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1034. _layers.pushBack(layer);
  1035. Layer *l = va_arg(params,Layer*);
  1036. while( l ) {
  1037. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1038. if (sEngine)
  1039. {
  1040. sEngine->retainScriptObject(this, l);
  1041. }
  1042. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1043. _layers.pushBack(l);
  1044. l = va_arg(params,Layer*);
  1045. }
  1046. _enabledLayer = 0;
  1047. this->addChild(_layers.at(_enabledLayer));
  1048. return true;
  1049. }
  1050. return false;
  1051. }
  1052. bool LayerMultiplex::initWithArray(const Vector<Layer*>& arrayOfLayers)
  1053. {
  1054. if (Layer::init())
  1055. {
  1056. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1057. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  1058. if (sEngine)
  1059. {
  1060. for (const auto &layer : arrayOfLayers)
  1061. {
  1062. if (layer)
  1063. {
  1064. sEngine->retainScriptObject(this, layer);
  1065. }
  1066. }
  1067. }
  1068. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1069. _layers.reserve(arrayOfLayers.size());
  1070. _layers.pushBack(arrayOfLayers);
  1071. _enabledLayer = 0;
  1072. this->addChild(_layers.at(_enabledLayer));
  1073. return true;
  1074. }
  1075. return false;
  1076. }
  1077. void LayerMultiplex::switchTo(int n)
  1078. {
  1079. switchTo(n, true);
  1080. }
  1081. void LayerMultiplex::switchTo(int n, bool cleanup)
  1082. {
  1083. CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
  1084. this->removeChild(_layers.at(_enabledLayer), cleanup);
  1085. _enabledLayer = n;
  1086. this->addChild(_layers.at(n));
  1087. }
  1088. void LayerMultiplex::switchToAndReleaseMe(int n)
  1089. {
  1090. CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
  1091. this->removeChild(_layers.at(_enabledLayer), true);
  1092. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1093. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  1094. if (sEngine)
  1095. {
  1096. sEngine->releaseScriptObject(this, _layers.at(_enabledLayer));
  1097. }
  1098. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  1099. _layers.replace(_enabledLayer, nullptr);
  1100. _enabledLayer = n;
  1101. this->addChild(_layers.at(n));
  1102. }
  1103. std::string LayerMultiplex::getDescription() const
  1104. {
  1105. return StringUtils::format("<LayerMultiplex | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
  1106. }
  1107. NS_CC_END