1
0

CCLayer.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. NS_CC_BEGIN
  40. // Layer
  41. Layer::Layer()
  42. : _touchEnabled(false)
  43. , _accelerometerEnabled(false)
  44. , _keyboardEnabled(false)
  45. , _touchListener(nullptr)
  46. , _keyboardListener(nullptr)
  47. , _accelerationListener(nullptr)
  48. , _touchMode(Touch::DispatchMode::ALL_AT_ONCE)
  49. , _swallowsTouches(true)
  50. {
  51. _ignoreAnchorPointForPosition = true;
  52. setAnchorPoint(Vec2(0.5f, 0.5f));
  53. }
  54. Layer::~Layer()
  55. {
  56. }
  57. bool Layer::init()
  58. {
  59. Director * director = Director::getInstance();
  60. setContentSize(director->getWinSize());
  61. return true;
  62. }
  63. Layer *Layer::create()
  64. {
  65. Layer *ret = new (std::nothrow) Layer();
  66. if (ret && ret->init())
  67. {
  68. ret->autorelease();
  69. return ret;
  70. }
  71. else
  72. {
  73. CC_SAFE_DELETE(ret);
  74. return nullptr;
  75. }
  76. }
  77. int Layer::executeScriptTouchHandler(EventTouch::EventCode eventType, Touch* touch, Event* event)
  78. {
  79. #if CC_ENABLE_SCRIPT_BINDING
  80. if (kScriptTypeLua == _scriptType)
  81. {
  82. TouchScriptData data(eventType, this, touch, event);
  83. ScriptEvent scriptEvent(kTouchEvent, &data);
  84. return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
  85. }
  86. #else
  87. CC_UNUSED_PARAM(eventType);
  88. CC_UNUSED_PARAM(touch);
  89. CC_UNUSED_PARAM(event);
  90. #endif
  91. return 0;
  92. }
  93. int Layer::executeScriptTouchesHandler(EventTouch::EventCode eventType, const std::vector<Touch*>& touches, Event* event)
  94. {
  95. #if CC_ENABLE_SCRIPT_BINDING
  96. if (kScriptTypeLua == _scriptType)
  97. {
  98. TouchesScriptData data(eventType, this, touches, event);
  99. ScriptEvent scriptEvent(kTouchesEvent, &data);
  100. return ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
  101. }
  102. #else
  103. CC_UNUSED_PARAM(eventType);
  104. CC_UNUSED_PARAM(touches);
  105. CC_UNUSED_PARAM(event);
  106. #endif
  107. return 0;
  108. }
  109. bool Layer::ccTouchBegan(Touch* /*pTouch*/, Event* /*pEvent*/) {return false;};
  110. void Layer::ccTouchMoved(Touch* /*pTouch*/, Event* /*pEvent*/) {}
  111. void Layer::ccTouchEnded(Touch* /*pTouch*/, Event* /*pEvent*/) {}
  112. void Layer::ccTouchCancelled(Touch* /*pTouch*/, Event* /*pEvent*/) {}
  113. void Layer::ccTouchesBegan(__Set* /*pTouches*/, Event* /*pEvent*/) {}
  114. void Layer::ccTouchesMoved(__Set* /*pTouches*/, Event* /*pEvent*/) {}
  115. void Layer::ccTouchesEnded(__Set* /*pTouches*/, Event* /*pEvent*/) {}
  116. void Layer::ccTouchesCancelled(__Set* /*pTouches*/, Event* /*pEvent*/) {}
  117. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  118. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  119. #elif _MSC_VER >= 1400 //vs 2005 or higher
  120. #pragma warning (push)
  121. #pragma warning (disable: 4996)
  122. #endif
  123. /// isTouchEnabled getter
  124. bool Layer::isTouchEnabled() const
  125. {
  126. return _touchEnabled;
  127. }
  128. /// isTouchEnabled setter
  129. void Layer::setTouchEnabled(bool enabled)
  130. {
  131. if (_touchEnabled != enabled)
  132. {
  133. _touchEnabled = enabled;
  134. if (enabled)
  135. {
  136. if (_touchListener != nullptr)
  137. return;
  138. if( _touchMode == Touch::DispatchMode::ALL_AT_ONCE )
  139. {
  140. // Register Touch Event
  141. auto listener = EventListenerTouchAllAtOnce::create();
  142. listener->onTouchesBegan = CC_CALLBACK_2(Layer::onTouchesBegan, this);
  143. listener->onTouchesMoved = CC_CALLBACK_2(Layer::onTouchesMoved, this);
  144. listener->onTouchesEnded = CC_CALLBACK_2(Layer::onTouchesEnded, this);
  145. listener->onTouchesCancelled = CC_CALLBACK_2(Layer::onTouchesCancelled, this);
  146. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  147. _touchListener = listener;
  148. }
  149. else
  150. {
  151. // Register Touch Event
  152. auto listener = EventListenerTouchOneByOne::create();
  153. listener->setSwallowTouches(_swallowsTouches);
  154. listener->onTouchBegan = CC_CALLBACK_2(Layer::onTouchBegan, this);
  155. listener->onTouchMoved = CC_CALLBACK_2(Layer::onTouchMoved, this);
  156. listener->onTouchEnded = CC_CALLBACK_2(Layer::onTouchEnded, this);
  157. listener->onTouchCancelled = CC_CALLBACK_2(Layer::onTouchCancelled, this);
  158. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  159. _touchListener = listener;
  160. }
  161. }
  162. else
  163. {
  164. _eventDispatcher->removeEventListener(_touchListener);
  165. _touchListener = nullptr;
  166. }
  167. }
  168. }
  169. void Layer::setTouchMode(Touch::DispatchMode mode)
  170. {
  171. if(_touchMode != mode)
  172. {
  173. _touchMode = mode;
  174. if( _touchEnabled)
  175. {
  176. setTouchEnabled(false);
  177. setTouchEnabled(true);
  178. }
  179. }
  180. }
  181. void Layer::setSwallowsTouches(bool swallowsTouches)
  182. {
  183. if (_swallowsTouches != swallowsTouches)
  184. {
  185. _swallowsTouches = swallowsTouches;
  186. if( _touchEnabled)
  187. {
  188. setTouchEnabled(false);
  189. setTouchEnabled(true);
  190. }
  191. }
  192. }
  193. Touch::DispatchMode Layer::getTouchMode() const
  194. {
  195. return _touchMode;
  196. }
  197. bool Layer::isSwallowsTouches() const
  198. {
  199. return _swallowsTouches;
  200. }
  201. /// isAccelerometerEnabled getter
  202. bool Layer::isAccelerometerEnabled() const
  203. {
  204. return _accelerometerEnabled;
  205. }
  206. /// isAccelerometerEnabled setter
  207. void Layer::setAccelerometerEnabled(bool enabled)
  208. {
  209. if (enabled != _accelerometerEnabled)
  210. {
  211. _accelerometerEnabled = enabled;
  212. Device::setAccelerometerEnabled(enabled);
  213. _eventDispatcher->removeEventListener(_accelerationListener);
  214. _accelerationListener = nullptr;
  215. if (enabled)
  216. {
  217. _accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(Layer::onAcceleration, this));
  218. _eventDispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);
  219. }
  220. }
  221. }
  222. void Layer::setAccelerometerInterval(double interval) {
  223. if (_accelerometerEnabled)
  224. {
  225. if (_running)
  226. {
  227. Device::setAccelerometerInterval(interval);
  228. }
  229. }
  230. }
  231. void Layer::onAcceleration(Acceleration* acc, Event* /*unused_event*/)
  232. {
  233. #if CC_ENABLE_SCRIPT_BINDING
  234. if(kScriptTypeNone != _scriptType)
  235. {
  236. BasicScriptData data(this,(void*)acc);
  237. ScriptEvent event(kAccelerometerEvent,&data);
  238. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  239. }
  240. #else
  241. CC_UNUSED_PARAM(acc);
  242. #endif
  243. }
  244. void Layer::onKeyPressed(EventKeyboard::KeyCode /*keyCode*/, Event* /*unused_event*/)
  245. {
  246. }
  247. void Layer::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* /*unused_event*/)
  248. {
  249. #if CC_ENABLE_SCRIPT_BINDING
  250. if(kScriptTypeNone != _scriptType)
  251. {
  252. KeypadScriptData data(keyCode, this);
  253. ScriptEvent event(kKeypadEvent,&data);
  254. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  255. }
  256. #else
  257. CC_UNUSED_PARAM(keyCode);
  258. #endif
  259. }
  260. /// isKeyboardEnabled getter
  261. bool Layer::isKeyboardEnabled() const
  262. {
  263. return _keyboardEnabled;
  264. }
  265. /// isKeyboardEnabled setter
  266. void Layer::setKeyboardEnabled(bool enabled)
  267. {
  268. if (enabled != _keyboardEnabled)
  269. {
  270. _keyboardEnabled = enabled;
  271. _eventDispatcher->removeEventListener(_keyboardListener);
  272. _keyboardListener = nullptr;
  273. if (enabled)
  274. {
  275. auto listener = EventListenerKeyboard::create();
  276. listener->onKeyPressed = CC_CALLBACK_2(Layer::onKeyPressed, this);
  277. listener->onKeyReleased = CC_CALLBACK_2(Layer::onKeyReleased, this);
  278. _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
  279. _keyboardListener = listener;
  280. }
  281. }
  282. }
  283. void Layer::setKeypadEnabled(bool enabled)
  284. {
  285. setKeyboardEnabled(enabled);
  286. }
  287. /// Callbacks
  288. bool Layer::onTouchBegan(Touch *touch, Event *event)
  289. {
  290. #if CC_ENABLE_SCRIPT_BINDING
  291. if (kScriptTypeLua == _scriptType)
  292. {
  293. return executeScriptTouchHandler(EventTouch::EventCode::BEGAN, touch, event) == 0 ? false : true;
  294. }
  295. #else
  296. CC_UNUSED_PARAM(touch);
  297. CC_UNUSED_PARAM(event);
  298. #endif
  299. CCASSERT(false, "Layer#ccTouchBegan override me");
  300. return true;
  301. }
  302. void Layer::onTouchMoved(Touch *touch, Event *event)
  303. {
  304. #if CC_ENABLE_SCRIPT_BINDING
  305. if (kScriptTypeLua == _scriptType)
  306. {
  307. executeScriptTouchHandler(EventTouch::EventCode::MOVED, touch, event);
  308. return;
  309. }
  310. #else
  311. CC_UNUSED_PARAM(touch);
  312. CC_UNUSED_PARAM(event);
  313. #endif
  314. }
  315. void Layer::onTouchEnded(Touch *touch, Event *event)
  316. {
  317. #if CC_ENABLE_SCRIPT_BINDING
  318. if (kScriptTypeLua == _scriptType)
  319. {
  320. executeScriptTouchHandler(EventTouch::EventCode::ENDED, touch, event);
  321. return;
  322. }
  323. #else
  324. CC_UNUSED_PARAM(touch);
  325. CC_UNUSED_PARAM(event);
  326. #endif
  327. }
  328. void Layer::onTouchCancelled(Touch *touch, Event *event)
  329. {
  330. #if CC_ENABLE_SCRIPT_BINDING
  331. if (kScriptTypeLua == _scriptType)
  332. {
  333. executeScriptTouchHandler(EventTouch::EventCode::CANCELLED, touch, event);
  334. return;
  335. }
  336. #else
  337. CC_UNUSED_PARAM(touch);
  338. CC_UNUSED_PARAM(event);
  339. #endif
  340. }
  341. void Layer::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
  342. {
  343. #if CC_ENABLE_SCRIPT_BINDING
  344. if (kScriptTypeLua == _scriptType)
  345. {
  346. executeScriptTouchesHandler(EventTouch::EventCode::BEGAN, touches, event);
  347. return;
  348. }
  349. #else
  350. CC_UNUSED_PARAM(touches);
  351. CC_UNUSED_PARAM(event);
  352. #endif
  353. }
  354. void Layer::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
  355. {
  356. #if CC_ENABLE_SCRIPT_BINDING
  357. if (kScriptTypeLua == _scriptType)
  358. {
  359. executeScriptTouchesHandler(EventTouch::EventCode::MOVED, touches, event);
  360. return;
  361. }
  362. #else
  363. CC_UNUSED_PARAM(touches);
  364. CC_UNUSED_PARAM(event);
  365. #endif
  366. }
  367. void Layer::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
  368. {
  369. #if CC_ENABLE_SCRIPT_BINDING
  370. if (kScriptTypeLua == _scriptType)
  371. {
  372. executeScriptTouchesHandler(EventTouch::EventCode::ENDED, touches, event);
  373. return;
  374. }
  375. #else
  376. CC_UNUSED_PARAM(touches);
  377. CC_UNUSED_PARAM(event);
  378. #endif
  379. }
  380. void Layer::onTouchesCancelled(const std::vector<Touch*>& touches, Event *event)
  381. {
  382. #if CC_ENABLE_SCRIPT_BINDING
  383. if (kScriptTypeLua == _scriptType)
  384. {
  385. executeScriptTouchesHandler(EventTouch::EventCode::CANCELLED, touches, event);
  386. return;
  387. }
  388. #else
  389. CC_UNUSED_PARAM(touches);
  390. CC_UNUSED_PARAM(event);
  391. #endif
  392. }
  393. std::string Layer::getDescription() const
  394. {
  395. return StringUtils::format("<Layer | Tag = %d>", _tag);
  396. }
  397. __LayerRGBA::__LayerRGBA()
  398. {
  399. CCLOG("LayerRGBA deprecated.");
  400. }
  401. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  402. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  403. #elif _MSC_VER >= 1400 //vs 2005 or higher
  404. #pragma warning (pop)
  405. #endif
  406. /// LayerColor
  407. LayerColor::LayerColor()
  408. {
  409. // default blend function
  410. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  411. }
  412. LayerColor::~LayerColor()
  413. {
  414. }
  415. /// blendFunc getter
  416. const BlendFunc &LayerColor::getBlendFunc() const
  417. {
  418. return _blendFunc;
  419. }
  420. /// blendFunc setter
  421. void LayerColor::setBlendFunc(const BlendFunc &var)
  422. {
  423. _blendFunc = var;
  424. }
  425. LayerColor* LayerColor::create()
  426. {
  427. LayerColor* ret = new (std::nothrow) LayerColor();
  428. if (ret && ret->init())
  429. {
  430. ret->autorelease();
  431. }
  432. else
  433. {
  434. CC_SAFE_DELETE(ret);
  435. }
  436. return ret;
  437. }
  438. LayerColor * LayerColor::create(const Color4B& color, GLfloat width, GLfloat height)
  439. {
  440. LayerColor * layer = new (std::nothrow) LayerColor();
  441. if( layer && layer->initWithColor(color,width,height))
  442. {
  443. layer->autorelease();
  444. return layer;
  445. }
  446. CC_SAFE_DELETE(layer);
  447. return nullptr;
  448. }
  449. LayerColor * LayerColor::create(const Color4B& color)
  450. {
  451. LayerColor * layer = new (std::nothrow) LayerColor();
  452. if(layer && layer->initWithColor(color))
  453. {
  454. layer->autorelease();
  455. return layer;
  456. }
  457. CC_SAFE_DELETE(layer);
  458. return nullptr;
  459. }
  460. bool LayerColor::init()
  461. {
  462. Size s = Director::getInstance()->getWinSize();
  463. return initWithColor(Color4B(0,0,0,0), s.width, s.height);
  464. }
  465. bool LayerColor::initWithColor(const Color4B& color, GLfloat w, GLfloat h)
  466. {
  467. if (Layer::init())
  468. {
  469. // default blend function
  470. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  471. _displayedColor.r = _realColor.r = color.r;
  472. _displayedColor.g = _realColor.g = color.g;
  473. _displayedColor.b = _realColor.b = color.b;
  474. _displayedOpacity = _realOpacity = color.a;
  475. for (size_t i = 0; i<sizeof(_squareVertices) / sizeof( _squareVertices[0]); i++ )
  476. {
  477. _squareVertices[i].x = 0.0f;
  478. _squareVertices[i].y = 0.0f;
  479. }
  480. updateColor();
  481. setContentSize(Size(w, h));
  482. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_COLOR_NO_MVP));
  483. return true;
  484. }
  485. return false;
  486. }
  487. bool LayerColor::initWithColor(const Color4B& color)
  488. {
  489. Size s = Director::getInstance()->getWinSize();
  490. return initWithColor(color, s.width, s.height);
  491. }
  492. /// override contentSize
  493. void LayerColor::setContentSize(const Size & size)
  494. {
  495. _squareVertices[1].x = size.width;
  496. _squareVertices[2].y = size.height;
  497. _squareVertices[3].x = size.width;
  498. _squareVertices[3].y = size.height;
  499. Layer::setContentSize(size);
  500. }
  501. void LayerColor::changeWidthAndHeight(GLfloat w ,GLfloat h)
  502. {
  503. this->setContentSize(Size(w, h));
  504. }
  505. void LayerColor::changeWidth(GLfloat w)
  506. {
  507. this->setContentSize(Size(w, _contentSize.height));
  508. }
  509. void LayerColor::changeHeight(GLfloat h)
  510. {
  511. this->setContentSize(Size(_contentSize.width, h));
  512. }
  513. void LayerColor::updateColor()
  514. {
  515. for( unsigned int i=0; i < 4; i++ )
  516. {
  517. _squareColors[i].r = _displayedColor.r / 255.0f;
  518. _squareColors[i].g = _displayedColor.g / 255.0f;
  519. _squareColors[i].b = _displayedColor.b / 255.0f;
  520. _squareColors[i].a = _displayedOpacity / 255.0f;
  521. }
  522. }
  523. void LayerColor::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  524. {
  525. _customCommand.init(_globalZOrder, transform, flags);
  526. _customCommand.func = CC_CALLBACK_0(LayerColor::onDraw, this, transform, flags);
  527. renderer->addCommand(&_customCommand);
  528. for(int i = 0; i < 4; ++i)
  529. {
  530. Vec4 pos;
  531. pos.x = _squareVertices[i].x; pos.y = _squareVertices[i].y; pos.z = _positionZ;
  532. pos.w = 1;
  533. _modelViewTransform.transformVector(&pos);
  534. _noMVPVertices[i] = Vec3(pos.x,pos.y,pos.z)/pos.w;
  535. }
  536. }
  537. void LayerColor::onDraw(const Mat4& transform, uint32_t /*flags*/)
  538. {
  539. getGLProgram()->use();
  540. getGLProgram()->setUniformsForBuiltins(transform);
  541. GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR );
  542. //
  543. // Attributes
  544. //
  545. glBindBuffer(GL_ARRAY_BUFFER, 0);
  546. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _noMVPVertices);
  547. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, _squareColors);
  548. GL::blendFunc( _blendFunc.src, _blendFunc.dst );
  549. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  550. CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,4);
  551. }
  552. std::string LayerColor::getDescription() const
  553. {
  554. return StringUtils::format("<LayerColor | Tag = %d>", _tag);
  555. }
  556. //
  557. // LayerGradient
  558. //
  559. LayerGradient::LayerGradient()
  560. : _startColor(Color4B::BLACK)
  561. , _endColor(Color4B::BLACK)
  562. , _startOpacity(255)
  563. , _endOpacity(255)
  564. , _alongVector(Vec2(0, -1))
  565. , _compressedInterpolation(true)
  566. {
  567. }
  568. LayerGradient::~LayerGradient()
  569. {
  570. }
  571. LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end)
  572. {
  573. LayerGradient * layer = new (std::nothrow) LayerGradient();
  574. if( layer && layer->initWithColor(start, end))
  575. {
  576. layer->autorelease();
  577. return layer;
  578. }
  579. CC_SAFE_DELETE(layer);
  580. return nullptr;
  581. }
  582. LayerGradient* LayerGradient::create(const Color4B& start, const Color4B& end, const Vec2& v)
  583. {
  584. LayerGradient * layer = new (std::nothrow) LayerGradient();
  585. if( layer && layer->initWithColor(start, end, v))
  586. {
  587. layer->autorelease();
  588. return layer;
  589. }
  590. CC_SAFE_DELETE(layer);
  591. return nullptr;
  592. }
  593. LayerGradient* LayerGradient::create()
  594. {
  595. LayerGradient* ret = new (std::nothrow) LayerGradient();
  596. if (ret && ret->init())
  597. {
  598. ret->autorelease();
  599. }
  600. else
  601. {
  602. CC_SAFE_DELETE(ret);
  603. }
  604. return ret;
  605. }
  606. bool LayerGradient::init()
  607. {
  608. return initWithColor(Color4B(0, 0, 0, 255), Color4B(0, 0, 0, 255));
  609. }
  610. bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end)
  611. {
  612. return initWithColor(start, end, Vec2(0, -1));
  613. }
  614. bool LayerGradient::initWithColor(const Color4B& start, const Color4B& end, const Vec2& v)
  615. {
  616. _endColor.r = end.r;
  617. _endColor.g = end.g;
  618. _endColor.b = end.b;
  619. _endOpacity = end.a;
  620. _startOpacity = start.a;
  621. _alongVector = v;
  622. _compressedInterpolation = true;
  623. return LayerColor::initWithColor(Color4B(start.r, start.g, start.b, 255));
  624. }
  625. void LayerGradient::updateColor()
  626. {
  627. LayerColor::updateColor();
  628. float h = _alongVector.getLength();
  629. if (h == 0)
  630. return;
  631. float c = sqrtf(2.0f);
  632. Vec2 u(_alongVector.x / h, _alongVector.y / h);
  633. // Compressed Interpolation mode
  634. if (_compressedInterpolation)
  635. {
  636. float h2 = 1 / ( fabsf(u.x) + fabsf(u.y) );
  637. u = u * (h2 * (float)c);
  638. }
  639. float opacityf = (float)_displayedOpacity / 255.0f;
  640. Color4F S(
  641. _displayedColor.r / 255.0f,
  642. _displayedColor.g / 255.0f,
  643. _displayedColor.b / 255.0f,
  644. _startOpacity * opacityf / 255.0f
  645. );
  646. Color4F E(
  647. _endColor.r / 255.0f,
  648. _endColor.g / 255.0f,
  649. _endColor.b / 255.0f,
  650. _endOpacity * opacityf / 255.0f
  651. );
  652. // (-1, -1)
  653. _squareColors[0].r = E.r + (S.r - E.r) * ((c + u.x + u.y) / (2.0f * c));
  654. _squareColors[0].g = E.g + (S.g - E.g) * ((c + u.x + u.y) / (2.0f * c));
  655. _squareColors[0].b = E.b + (S.b - E.b) * ((c + u.x + u.y) / (2.0f * c));
  656. _squareColors[0].a = E.a + (S.a - E.a) * ((c + u.x + u.y) / (2.0f * c));
  657. // (1, -1)
  658. _squareColors[1].r = E.r + (S.r - E.r) * ((c - u.x + u.y) / (2.0f * c));
  659. _squareColors[1].g = E.g + (S.g - E.g) * ((c - u.x + u.y) / (2.0f * c));
  660. _squareColors[1].b = E.b + (S.b - E.b) * ((c - u.x + u.y) / (2.0f * c));
  661. _squareColors[1].a = E.a + (S.a - E.a) * ((c - u.x + u.y) / (2.0f * c));
  662. // (-1, 1)
  663. _squareColors[2].r = E.r + (S.r - E.r) * ((c + u.x - u.y) / (2.0f * c));
  664. _squareColors[2].g = E.g + (S.g - E.g) * ((c + u.x - u.y) / (2.0f * c));
  665. _squareColors[2].b = E.b + (S.b - E.b) * ((c + u.x - u.y) / (2.0f * c));
  666. _squareColors[2].a = E.a + (S.a - E.a) * ((c + u.x - u.y) / (2.0f * c));
  667. // (1, 1)
  668. _squareColors[3].r = E.r + (S.r - E.r) * ((c - u.x - u.y) / (2.0f * c));
  669. _squareColors[3].g = E.g + (S.g - E.g) * ((c - u.x - u.y) / (2.0f * c));
  670. _squareColors[3].b = E.b + (S.b - E.b) * ((c - u.x - u.y) / (2.0f * c));
  671. _squareColors[3].a = E.a + (S.a - E.a) * ((c - u.x - u.y) / (2.0f * c));
  672. }
  673. const Color3B& LayerGradient::getStartColor() const
  674. {
  675. return _realColor;
  676. }
  677. void LayerGradient::setStartColor(const Color3B& color)
  678. {
  679. setColor(color);
  680. }
  681. void LayerGradient::setEndColor(const Color3B& color)
  682. {
  683. _endColor = color;
  684. updateColor();
  685. }
  686. const Color3B& LayerGradient::getEndColor() const
  687. {
  688. return _endColor;
  689. }
  690. void LayerGradient::setStartOpacity(GLubyte o)
  691. {
  692. _startOpacity = o;
  693. updateColor();
  694. }
  695. GLubyte LayerGradient::getStartOpacity() const
  696. {
  697. return _startOpacity;
  698. }
  699. void LayerGradient::setEndOpacity(GLubyte o)
  700. {
  701. _endOpacity = o;
  702. updateColor();
  703. }
  704. GLubyte LayerGradient::getEndOpacity() const
  705. {
  706. return _endOpacity;
  707. }
  708. void LayerGradient::setVector(const Vec2& var)
  709. {
  710. _alongVector = var;
  711. updateColor();
  712. }
  713. const Vec2& LayerGradient::getVector() const
  714. {
  715. return _alongVector;
  716. }
  717. bool LayerGradient::isCompressedInterpolation() const
  718. {
  719. return _compressedInterpolation;
  720. }
  721. void LayerGradient::setCompressedInterpolation(bool compress)
  722. {
  723. _compressedInterpolation = compress;
  724. updateColor();
  725. }
  726. std::string LayerGradient::getDescription() const
  727. {
  728. return StringUtils::format("<LayerGradient | Tag = %d>", _tag);
  729. }
  730. /// MultiplexLayer
  731. LayerMultiplex::LayerMultiplex()
  732. : _enabledLayer(0)
  733. {
  734. }
  735. LayerMultiplex::~LayerMultiplex()
  736. {
  737. for(const auto &layer : _layers) {
  738. layer->cleanup();
  739. }
  740. }
  741. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  742. LayerMultiplex * LayerMultiplex::createVariadic(Layer * layer, ...)
  743. {
  744. va_list args;
  745. va_start(args,layer);
  746. LayerMultiplex * multiplexLayer = new (std::nothrow) LayerMultiplex();
  747. if(multiplexLayer && multiplexLayer->initWithLayers(layer, args))
  748. {
  749. multiplexLayer->autorelease();
  750. va_end(args);
  751. return multiplexLayer;
  752. }
  753. va_end(args);
  754. CC_SAFE_DELETE(multiplexLayer);
  755. return nullptr;
  756. }
  757. #else
  758. LayerMultiplex * LayerMultiplex::create(Layer * layer, ...)
  759. {
  760. va_list args;
  761. va_start(args,layer);
  762. LayerMultiplex * multiplexLayer = new (std::nothrow) LayerMultiplex();
  763. if(multiplexLayer && multiplexLayer->initWithLayers(layer, args))
  764. {
  765. multiplexLayer->autorelease();
  766. va_end(args);
  767. return multiplexLayer;
  768. }
  769. va_end(args);
  770. CC_SAFE_DELETE(multiplexLayer);
  771. return nullptr;
  772. }
  773. #endif
  774. LayerMultiplex * LayerMultiplex::createWithLayer(Layer* layer)
  775. {
  776. return LayerMultiplex::create(layer, nullptr);
  777. }
  778. LayerMultiplex* LayerMultiplex::create()
  779. {
  780. LayerMultiplex* ret = new (std::nothrow) LayerMultiplex();
  781. if (ret && ret->init())
  782. {
  783. ret->autorelease();
  784. }
  785. else
  786. {
  787. CC_SAFE_DELETE(ret);
  788. }
  789. return ret;
  790. }
  791. LayerMultiplex* LayerMultiplex::createWithArray(const Vector<Layer*>& arrayOfLayers)
  792. {
  793. LayerMultiplex* ret = new (std::nothrow) LayerMultiplex();
  794. if (ret && ret->initWithArray(arrayOfLayers))
  795. {
  796. ret->autorelease();
  797. }
  798. else
  799. {
  800. CC_SAFE_DELETE(ret);
  801. }
  802. return ret;
  803. }
  804. void LayerMultiplex::addLayer(Layer* layer)
  805. {
  806. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  807. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  808. if (sEngine)
  809. {
  810. sEngine->retainScriptObject(this, layer);
  811. }
  812. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  813. _layers.pushBack(layer);
  814. }
  815. bool LayerMultiplex::init()
  816. {
  817. if (Layer::init())
  818. {
  819. _enabledLayer = 0;
  820. return true;
  821. }
  822. return false;
  823. }
  824. bool LayerMultiplex::initWithLayers(Layer *layer, va_list params)
  825. {
  826. if (Layer::init())
  827. {
  828. _layers.reserve(5);
  829. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  830. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  831. if (sEngine)
  832. {
  833. sEngine->retainScriptObject(this, layer);
  834. }
  835. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  836. _layers.pushBack(layer);
  837. Layer *l = va_arg(params,Layer*);
  838. while( l ) {
  839. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  840. if (sEngine)
  841. {
  842. sEngine->retainScriptObject(this, l);
  843. }
  844. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  845. _layers.pushBack(l);
  846. l = va_arg(params,Layer*);
  847. }
  848. _enabledLayer = 0;
  849. this->addChild(_layers.at(_enabledLayer));
  850. return true;
  851. }
  852. return false;
  853. }
  854. bool LayerMultiplex::initWithArray(const Vector<Layer*>& arrayOfLayers)
  855. {
  856. if (Layer::init())
  857. {
  858. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  859. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  860. if (sEngine)
  861. {
  862. for (const auto &layer : arrayOfLayers)
  863. {
  864. if (layer)
  865. {
  866. sEngine->retainScriptObject(this, layer);
  867. }
  868. }
  869. }
  870. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  871. _layers.reserve(arrayOfLayers.size());
  872. _layers.pushBack(arrayOfLayers);
  873. _enabledLayer = 0;
  874. this->addChild(_layers.at(_enabledLayer));
  875. return true;
  876. }
  877. return false;
  878. }
  879. void LayerMultiplex::switchTo(int n)
  880. {
  881. CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
  882. this->removeChild(_layers.at(_enabledLayer), true);
  883. _enabledLayer = n;
  884. this->addChild(_layers.at(n));
  885. }
  886. void LayerMultiplex::switchToAndReleaseMe(int n)
  887. {
  888. CCASSERT( n < _layers.size(), "Invalid index in MultiplexLayer switchTo message" );
  889. this->removeChild(_layers.at(_enabledLayer), true);
  890. #if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  891. auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  892. if (sEngine)
  893. {
  894. sEngine->releaseScriptObject(this, _layers.at(_enabledLayer));
  895. }
  896. #endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
  897. _layers.replace(_enabledLayer, nullptr);
  898. _enabledLayer = n;
  899. this->addChild(_layers.at(n));
  900. }
  901. std::string LayerMultiplex::getDescription() const
  902. {
  903. return StringUtils::format("<LayerMultiplex | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
  904. }
  905. NS_CC_END