CCLayer.cpp 33 KB

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