CCLayer.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. #ifndef __CCLAYER_H__
  25. #define __CCLAYER_H__
  26. #include "2d/CCNode.h"
  27. #include "base/CCProtocols.h"
  28. #include "renderer/CCCustomCommand.h"
  29. NS_CC_BEGIN
  30. /**
  31. * @addtogroup _2d
  32. * @{
  33. */
  34. class __Set;
  35. class TouchScriptHandlerEntry;
  36. class EventListenerTouch;
  37. class EventListenerKeyboard;
  38. class EventListenerAcceleration;
  39. class Touch;
  40. //
  41. // Layer
  42. //
  43. /** @class Layer
  44. * @brief Layer is a subclass of Node that implements the TouchEventsDelegate protocol.
  45. All features from Node are valid, plus the following new features:
  46. - It can receive iPhone Touches
  47. - It can receive Accelerometer input
  48. */
  49. class CC_DLL Layer : public Node
  50. {
  51. public:
  52. /** Creates a fullscreen black layer.
  53. *
  54. * @return An autoreleased Layer object.
  55. */
  56. static Layer *create();
  57. // Deprecated touch callbacks.
  58. CC_DEPRECATED_ATTRIBUTE virtual bool ccTouchBegan(Touch *pTouch, Event *pEvent) final;
  59. CC_DEPRECATED_ATTRIBUTE virtual void ccTouchMoved(Touch *pTouch, Event *pEvent) final;
  60. CC_DEPRECATED_ATTRIBUTE virtual void ccTouchEnded(Touch *pTouch, Event *pEvent) final;
  61. CC_DEPRECATED_ATTRIBUTE virtual void ccTouchCancelled(Touch *pTouch, Event *pEvent) final;
  62. CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesBegan(__Set *pTouches, Event *pEvent) final;
  63. CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesMoved(__Set *pTouches, Event *pEvent) final;
  64. CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesEnded(__Set *pTouches, Event *pEvent) final;
  65. CC_DEPRECATED_ATTRIBUTE virtual void ccTouchesCancelled(__Set *pTouches, Event *pEvent) final;
  66. /* Callback function should not be deprecated, it will generate lots of warnings.
  67. Since 'setTouchEnabled' was deprecated, it will make warnings if developer overrides onTouchXXX and invokes setTouchEnabled(true) instead of using EventDispatcher::addEventListenerWithXXX.
  68. */
  69. /** Callback function for touch began.
  70. *
  71. * @param touch Touch information.
  72. * @param unused_event Event information.
  73. * @return if return false, onTouchMoved, onTouchEnded, onTouchCancelled will never called.
  74. * @js NA
  75. */
  76. virtual bool onTouchBegan(Touch *touch, Event *unused_event);
  77. /** Callback function for touch moved.
  78. *
  79. * @param touch Touch information.
  80. * @param unused_event Event information.
  81. * @js NA
  82. */
  83. virtual void onTouchMoved(Touch *touch, Event *unused_event);
  84. /** Callback function for touch ended.
  85. *
  86. * @param touch Touch information.
  87. * @param unused_event Event information.
  88. * @js NA
  89. */
  90. virtual void onTouchEnded(Touch *touch, Event *unused_event);
  91. /** Callback function for touch cancelled.
  92. *
  93. * @param touch Touch information.
  94. * @param unused_event Event information.
  95. * @js NA
  96. */
  97. virtual void onTouchCancelled(Touch *touch, Event *unused_event);
  98. /** Callback function for multiple touches began.
  99. *
  100. * @param touches Touches information.
  101. * @param unused_event Event information.
  102. * @js NA
  103. */
  104. virtual void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
  105. /** Callback function for multiple touches moved.
  106. *
  107. * @param touches Touches information.
  108. * @param unused_event Event information.
  109. * @js NA
  110. */
  111. virtual void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);
  112. /** Callback function for multiple touches ended.
  113. *
  114. * @param touches Touches information.
  115. * @param unused_event Event information.
  116. * @js NA
  117. */
  118. virtual void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);
  119. /** Callback function for multiple touches cancelled.
  120. *
  121. * @param touches Touches information.
  122. * @param unused_event Event information.
  123. * @js NA
  124. */
  125. virtual void onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event);
  126. /**
  127. @deprecated Please override onAcceleration
  128. @js NA
  129. */
  130. CC_DEPRECATED_ATTRIBUTE virtual void didAccelerate(Acceleration* /*accelerationValue*/) final {};
  131. /* Callback function should not be deprecated, it will generate lots of warnings.
  132. Since 'setAccelerometerEnabled' was deprecated, it will make warnings if developer overrides onAcceleration and invokes setAccelerometerEnabled(true) instead of using EventDispatcher::addEventListenerWithXXX.
  133. */
  134. /** Callback function for acceleration.
  135. * @param acc Acceleration information.
  136. * @param unused_event Event information.
  137. * @js NA
  138. */
  139. virtual void onAcceleration(Acceleration* acc, Event* unused_event);
  140. /** If isTouchEnabled, this method is called onEnter. Override it to change the
  141. way Layer receives touch events.
  142. ( Default: TouchDispatcher::sharedDispatcher()->addStandardDelegate(this,0); )
  143. Example:
  144. void Layer::registerWithTouchDispatcher()
  145. {
  146. TouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,INT_MIN+1,true);
  147. }
  148. @since v0.8.0
  149. @js NA
  150. */
  151. CC_DEPRECATED_ATTRIBUTE virtual void registerWithTouchDispatcher() final {};
  152. /** whether or not it will receive Touch events.
  153. You can enable / disable touch events with this property.
  154. Only the touches of this node will be affected. This "method" is not propagated to it's children.
  155. @since v0.8.1
  156. @js NA
  157. */
  158. CC_DEPRECATED_ATTRIBUTE bool isTouchEnabled() const;
  159. CC_DEPRECATED_ATTRIBUTE void setTouchEnabled(bool value);
  160. CC_DEPRECATED_ATTRIBUTE virtual void setTouchMode(Touch::DispatchMode mode);
  161. CC_DEPRECATED_ATTRIBUTE virtual Touch::DispatchMode getTouchMode() const;
  162. /**
  163. swallowsTouches of the touch events. Default is true
  164. @js NA
  165. */
  166. CC_DEPRECATED_ATTRIBUTE virtual void setSwallowsTouches(bool swallowsTouches);
  167. CC_DEPRECATED_ATTRIBUTE virtual bool isSwallowsTouches() const;
  168. /** whether or not it will receive Accelerometer events
  169. You can enable / disable accelerometer events with this property.
  170. @since v0.8.1
  171. @js NA
  172. */
  173. CC_DEPRECATED_ATTRIBUTE virtual bool isAccelerometerEnabled() const;
  174. CC_DEPRECATED_ATTRIBUTE virtual void setAccelerometerEnabled(bool value);
  175. CC_DEPRECATED_ATTRIBUTE virtual void setAccelerometerInterval(double interval);
  176. /** whether or not it will receive keyboard or keypad events
  177. You can enable / disable accelerometer events with this property.
  178. it's new in cocos2d-x
  179. @js NA
  180. */
  181. CC_DEPRECATED_ATTRIBUTE virtual bool isKeyboardEnabled() const;
  182. CC_DEPRECATED_ATTRIBUTE virtual void setKeyboardEnabled(bool value);
  183. /**
  184. Please use onKeyPressed instead.
  185. @js NA
  186. */
  187. CC_DEPRECATED_ATTRIBUTE virtual void keyPressed(int /*keyCode*/) final {};
  188. /**
  189. Please use onKeyReleased instead.
  190. @js NA
  191. */
  192. CC_DEPRECATED_ATTRIBUTE virtual void keyReleased(int /*keyCode*/) final {};
  193. /* Callback function should not be deprecated, it will generate lots of warnings.
  194. Since 'setKeyboardEnabled' was deprecated, it will make warnings if developer overrides onKeyXXX and invokes setKeyboardEnabled(true) instead of using EventDispatcher::addEventListenerWithXXX.
  195. */
  196. /** Callback function for key pressed.
  197. * @param keyCode KeyCode information.
  198. * @param event Event information.
  199. * @js NA
  200. */
  201. virtual void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event);
  202. /** Callback function for key released.
  203. * @param keyCode KeyCode information.
  204. * @param event Event information.
  205. * @js NA
  206. */
  207. virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);
  208. CC_DEPRECATED_ATTRIBUTE virtual bool isKeypadEnabled() const final { return _keyboardEnabled; }
  209. CC_DEPRECATED_ATTRIBUTE virtual void setKeypadEnabled(bool value);
  210. /**
  211. @deprecated Please override onKeyReleased and check the keycode of KeyboardEvent::KeyCode::Menu(KEY_BACKSPACE) instead.
  212. @js NA
  213. */
  214. CC_DEPRECATED_ATTRIBUTE virtual void keyBackClicked() final {};
  215. CC_DEPRECATED_ATTRIBUTE virtual void keyMenuClicked() final {};
  216. // Overrides
  217. virtual std::string getDescription() const override;
  218. CC_CONSTRUCTOR_ACCESS:
  219. Layer();
  220. virtual ~Layer();
  221. virtual bool init() override;
  222. protected:
  223. //add the api for avoid use deprecated api
  224. CC_DEPRECATED_ATTRIBUTE void _addTouchListener() {}
  225. CC_DEPRECATED_ATTRIBUTE void addTouchListener() {}
  226. CC_DEPRECATED_ATTRIBUTE int executeScriptTouchHandler(EventTouch::EventCode eventType, Touch* touch, Event* event);
  227. CC_DEPRECATED_ATTRIBUTE int executeScriptTouchesHandler(EventTouch::EventCode eventType, const std::vector<Touch*>& touches, Event* event);
  228. bool _touchEnabled;
  229. bool _accelerometerEnabled;
  230. bool _keyboardEnabled;
  231. EventListener* _touchListener;
  232. EventListenerKeyboard* _keyboardListener;
  233. EventListenerAcceleration* _accelerationListener;
  234. Touch::DispatchMode _touchMode;
  235. bool _swallowsTouches;
  236. private:
  237. CC_DISALLOW_COPY_AND_ASSIGN(Layer);
  238. };
  239. /** @class __LayerRGBA
  240. * @brief LayerRGBA is a subclass of Layer that implements the RGBAProtocol protocol using a solid color as the background.
  241. All features from Layer are valid, plus the following new features that propagate into children that conform to the RGBAProtocol:
  242. - opacity
  243. - RGB colors
  244. @since 2.1
  245. @js NA
  246. */
  247. class CC_DLL __LayerRGBA : public Layer, public __RGBAProtocol
  248. {
  249. public:
  250. CREATE_FUNC(__LayerRGBA);
  251. //
  252. // Overrides
  253. //
  254. virtual GLubyte getOpacity() const override { return Layer::getOpacity(); }
  255. virtual GLubyte getDisplayedOpacity() const override { return Layer::getDisplayedOpacity(); }
  256. virtual void setOpacity(GLubyte opacity) override { Layer::setOpacity(opacity); }
  257. virtual void updateDisplayedOpacity(GLubyte parentOpacity) override { Layer::updateDisplayedOpacity(parentOpacity); }
  258. virtual bool isCascadeOpacityEnabled() const override { return Layer::isCascadeOpacityEnabled(); }
  259. virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled) override { Layer::setCascadeOpacityEnabled(cascadeOpacityEnabled); }
  260. virtual const Color3B& getColor() const override { return Layer::getColor(); }
  261. virtual const Color3B& getDisplayedColor() const override { return Layer::getDisplayedColor(); }
  262. virtual void setColor(const Color3B& color) override { Layer::setColor(color); }
  263. virtual void updateDisplayedColor(const Color3B& parentColor) override { Layer::updateDisplayedColor(parentColor); }
  264. virtual bool isCascadeColorEnabled() const override { return Layer::isCascadeOpacityEnabled(); }
  265. virtual void setCascadeColorEnabled(bool cascadeColorEnabled) override { Layer::setCascadeColorEnabled(cascadeColorEnabled); }
  266. virtual void setOpacityModifyRGB(bool bValue) override { Layer::setOpacityModifyRGB(bValue); }
  267. virtual bool isOpacityModifyRGB() const override { return Layer::isOpacityModifyRGB(); }
  268. CC_CONSTRUCTOR_ACCESS:
  269. __LayerRGBA();
  270. virtual ~__LayerRGBA() {}
  271. private:
  272. CC_DISALLOW_COPY_AND_ASSIGN(__LayerRGBA);
  273. };
  274. //
  275. // LayerColor
  276. //
  277. /** @class LayerColor
  278. * @brief LayerColor is a subclass of Layer that implements the RGBAProtocol protocol.
  279. All features from Layer are valid, plus the following new features:
  280. - opacity
  281. - RGB colors
  282. */
  283. class CC_DLL LayerColor : public Layer, public BlendProtocol
  284. {
  285. public:
  286. /** Creates a fullscreen black layer.
  287. *
  288. * @return An autoreleased LayerColor object.
  289. */
  290. static LayerColor* create();
  291. /** Creates a Layer with color, width and height in Points.
  292. *
  293. * @param color The color of layer.
  294. * @param width The width of layer.
  295. * @param height The height of layer.
  296. * @return An autoreleased LayerColor object.
  297. */
  298. static LayerColor * create(const Color4B& color, GLfloat width, GLfloat height);
  299. /** Creates a Layer with color. Width and height are the window size.
  300. *
  301. * @param color The color of layer.
  302. * @return An autoreleased LayerColor object.
  303. */
  304. static LayerColor * create(const Color4B& color);
  305. /** Change width in Points.
  306. *
  307. * @param w The width of layer.
  308. */
  309. void changeWidth(GLfloat w);
  310. /** Change height in Points.
  311. *
  312. * @param h The height of layer.
  313. */
  314. void changeHeight(GLfloat h);
  315. /** Change width and height in Points.
  316. *
  317. * @param w The width of layer.
  318. * @param h The Height of layer.
  319. @since v0.8
  320. */
  321. void changeWidthAndHeight(GLfloat w ,GLfloat h);
  322. //
  323. // Overrides
  324. //
  325. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  326. virtual void setContentSize(const Size & var) override;
  327. /** BlendFunction. Conforms to BlendProtocol protocol */
  328. /**
  329. * @lua NA
  330. */
  331. virtual const BlendFunc& getBlendFunc() const override;
  332. /**
  333. *@code
  334. *When this function bound into js or lua,the parameter will be changed
  335. *In js: var setBlendFunc(var src, var dst)
  336. *In lua: local setBlendFunc(local src, local dst)
  337. *@endcode
  338. */
  339. virtual void setBlendFunc(const BlendFunc& blendFunc) override;
  340. virtual std::string getDescription() const override;
  341. CC_CONSTRUCTOR_ACCESS:
  342. LayerColor();
  343. virtual ~LayerColor();
  344. bool init() override;
  345. bool initWithColor(const Color4B& color, GLfloat width, GLfloat height);
  346. bool initWithColor(const Color4B& color);
  347. protected:
  348. void onDraw(const Mat4& transform, uint32_t flags);
  349. virtual void updateColor() override;
  350. BlendFunc _blendFunc;
  351. Vec2 _squareVertices[4];
  352. Color4F _squareColors[4];
  353. CustomCommand _customCommand;
  354. Vec3 _noMVPVertices[4];
  355. private:
  356. CC_DISALLOW_COPY_AND_ASSIGN(LayerColor);
  357. };
  358. //
  359. // LayerGradient
  360. //
  361. /** @class LayerGradient
  362. * @brief LayerGradient is a subclass of LayerColor that draws gradients across the background.
  363. All features from LayerColor are valid, plus the following new features:
  364. - direction
  365. - final color
  366. - interpolation mode
  367. Color is interpolated between the startColor and endColor along the given
  368. vector (starting at the origin, ending at the terminus). If no vector is
  369. supplied, it defaults to (0, -1) -- a fade from top to bottom.
  370. If 'compressedInterpolation' is disabled, you will not see either the start or end color for
  371. non-cardinal vectors; a smooth gradient implying both end points will be still
  372. be drawn, however.
  373. If ' compressedInterpolation' is enabled (default mode) you will see both the start and end colors of the gradient.
  374. @since v0.99.5
  375. */
  376. class CC_DLL LayerGradient : public LayerColor
  377. {
  378. public:
  379. /** Creates a fullscreen black layer.
  380. *
  381. * @return An autoreleased LayerGradient object.
  382. */
  383. static LayerGradient* create();
  384. /** Creates a full-screen Layer with a gradient between start and end.
  385. *
  386. * @param start The start color.
  387. * @param end The end color.
  388. * @return An autoreleased LayerGradient object.
  389. */
  390. static LayerGradient* create(const Color4B& start, const Color4B& end);
  391. /** Creates a full-screen Layer with a gradient between start and end in the direction of v.
  392. *
  393. * @param start The start color.
  394. * @param end The end color.
  395. * @param v The direction of gradient color.
  396. * @return An autoreleased LayerGradient object.
  397. */
  398. static LayerGradient* create(const Color4B& start, const Color4B& end, const Vec2& v);
  399. /** Whether or not the interpolation will be compressed in order to display all the colors of the gradient both in canonical and non canonical vectors.
  400. Default: true.
  401. *
  402. * @param compressedInterpolation The interpolation will be compressed if true.
  403. */
  404. void setCompressedInterpolation(bool compressedInterpolation);
  405. /** Get the compressedInterpolation
  406. *
  407. * @return The interpolation will be compressed if true.
  408. */
  409. bool isCompressedInterpolation() const;
  410. /** Sets the start color of the gradient.
  411. *
  412. * @param startColor The start color.
  413. */
  414. void setStartColor( const Color3B& startColor );
  415. /** Returns the start color of the gradient.
  416. *
  417. * @return The start color.
  418. */
  419. const Color3B& getStartColor() const;
  420. /** Sets the end color of the gradient.
  421. *
  422. * @param endColor The end color.
  423. */
  424. void setEndColor( const Color3B& endColor );
  425. /** Returns the end color of the gradient.
  426. *
  427. * @return The end color.
  428. */
  429. const Color3B& getEndColor() const;
  430. /** Returns the start opacity of the gradient.
  431. *
  432. * @param startOpacity The start opacity, from 0 to 255.
  433. */
  434. void setStartOpacity( GLubyte startOpacity );
  435. /** Returns the start opacity of the gradient.
  436. *
  437. * @return The start opacity.
  438. */
  439. GLubyte getStartOpacity() const;
  440. /** Returns the end opacity of the gradient.
  441. *
  442. * @param endOpacity The end opacity, from 0 to 255.
  443. */
  444. void setEndOpacity( GLubyte endOpacity );
  445. /** Returns the end opacity of the gradient.
  446. *
  447. * @return The end opacity.
  448. */
  449. GLubyte getEndOpacity() const;
  450. /** Sets the directional vector that will be used for the gradient.
  451. The default value is vertical direction (0,-1).
  452. *
  453. * @param alongVector The direction of gradient.
  454. */
  455. void setVector(const Vec2& alongVector);
  456. /** Returns the directional vector used for the gradient.
  457. *
  458. * @return The direction of gradient.
  459. */
  460. const Vec2& getVector() const;
  461. virtual std::string getDescription() const override;
  462. CC_CONSTRUCTOR_ACCESS:
  463. LayerGradient();
  464. virtual ~LayerGradient();
  465. virtual bool init() override;
  466. /** Initializes the Layer with a gradient between start and end.
  467. * @js init
  468. * @lua init
  469. */
  470. bool initWithColor(const Color4B& start, const Color4B& end);
  471. /** Initializes the Layer with a gradient between start and end in the direction of v.
  472. * @js init
  473. * @lua init
  474. */
  475. bool initWithColor(const Color4B& start, const Color4B& end, const Vec2& v);
  476. protected:
  477. virtual void updateColor() override;
  478. Color3B _startColor;
  479. Color3B _endColor;
  480. GLubyte _startOpacity;
  481. GLubyte _endOpacity;
  482. Vec2 _alongVector;
  483. bool _compressedInterpolation;
  484. };
  485. /** @class LayerRadialGradient
  486. * @brief LayerRadialGradient is a subclass of Layer that draws radial gradients across the background.
  487. @since v3.16
  488. */
  489. class CC_DLL LayerRadialGradient : public Layer
  490. {
  491. public:
  492. /** Create a LayerRadialGradient
  493. * @param startColor the inner color of the gradient
  494. * @param endColor the out color of the gradient
  495. * @param radius the radius of the gradient(length from center of gradient to outer color)
  496. * @param center the position of the center of the gradient
  497. * @param expand an alpha value(0.f-1.f) that specifies how much of that radius in only inner color(the gradient
  498. starts outside of that amount)
  499. */
  500. static LayerRadialGradient* create(const Color4B& startColor, const Color4B& endColor, float radius, const Vec2& center, float expand);
  501. static LayerRadialGradient* create();
  502. //
  503. // overrides
  504. //
  505. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  506. virtual void setContentSize(const Size& size) override;
  507. void setStartOpacity(GLubyte opacity);
  508. GLubyte getStartOpacity() const;
  509. void setEndOpacity(GLubyte opacity);
  510. GLubyte getEndOpacity() const;
  511. void setRadius(float radius);
  512. float getRadius() const;
  513. void setCenter(const Vec2& center);
  514. Vec2 getCenter() const;
  515. void setExpand(float expand);
  516. float getExpand() const;
  517. void setStartColor(const Color3B& color);
  518. void setStartColor(const Color4B& color);
  519. Color4B getStartColor() const;
  520. Color3B getStartColor3B() const;
  521. void setEndColor(const Color3B& color);
  522. void setEndColor(const Color4B& color);
  523. Color4B getEndColor() const;
  524. Color3B getEndColor3B() const;
  525. void setBlendFunc(const BlendFunc& blendFunc);
  526. BlendFunc getBlendFunc() const;
  527. CC_CONSTRUCTOR_ACCESS:
  528. LayerRadialGradient();
  529. virtual ~LayerRadialGradient();
  530. bool initWithColor(const Color4B& startColor, const Color4B& endColor, float radius, const Vec2& center, float expand);
  531. protected:
  532. void onDraw(const Mat4& transform, uint32_t flags);
  533. private:
  534. void convertColor4B24F(Color4F& outColor, const Color4B& inColor);
  535. Color4B _startColor;
  536. Color4F _startColorRend; // start color used in shader
  537. Color4B _endColor;
  538. Color4F _endColorRend; // end color used in shader
  539. Vec2 _center;
  540. float _radius;
  541. float _expand;
  542. Vec2 _vertices[4];
  543. CustomCommand _customCommand;
  544. GLint _uniformLocationStartColor;
  545. GLint _uniformLocationEndColor;
  546. GLint _uniformLocationCenter;
  547. GLint _uniformLocationRadius;
  548. GLint _uniformLocationExpand;
  549. BlendFunc _blendFunc;
  550. };
  551. /** @class LayerMultiplex
  552. * @brief MultipleLayer is a Layer with the ability to multiplex it's children.
  553. Features:
  554. - It supports one or more children
  555. - Only one children will be active a time
  556. */
  557. class CC_DLL LayerMultiplex : public Layer
  558. {
  559. public:
  560. /** Creates and initializes a LayerMultiplex object.
  561. * @lua NA
  562. *
  563. * @return An autoreleased LayerMultiplex object.
  564. */
  565. static LayerMultiplex* create();
  566. /** Creates a LayerMultiplex with an array of layers.
  567. @since v2.1
  568. * @js NA
  569. *
  570. * @param arrayOfLayers An array of layers.
  571. * @return An autoreleased LayerMultiplex object.
  572. */
  573. static LayerMultiplex* createWithArray(const Vector<Layer*>& arrayOfLayers);
  574. /** Creates a LayerMultiplex with one or more layers using a variable argument list.
  575. * @code
  576. * When this function bound to lua or js,the input params are changed.
  577. * In js:var create(...)
  578. * In lua:local create(...)
  579. * @endcode
  580. */
  581. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  582. // VS2013 does not support nullptr in variable args lists and variadic templates are also not supported
  583. typedef Layer* M;
  584. static LayerMultiplex* create(M m1, std::nullptr_t listEnd) { return createVariadic(m1, NULL); }
  585. static LayerMultiplex* create(M m1, M m2, std::nullptr_t listEnd) { return createVariadic(m1, m2, NULL); }
  586. static LayerMultiplex* create(M m1, M m2, M m3, std::nullptr_t listEnd) { return createVariadic(m1, m2, m3, NULL); }
  587. static LayerMultiplex* create(M m1, M m2, M m3, M m4, std::nullptr_t listEnd) { return createVariadic(m1, m2, m3, m4, NULL); }
  588. static LayerMultiplex* create(M m1, M m2, M m3, M m4, M m5, std::nullptr_t listEnd) { return createVariadic(m1, m2, m3, m4, m5, NULL); }
  589. static LayerMultiplex* create(M m1, M m2, M m3, M m4, M m5, M m6, std::nullptr_t listEnd) { return createVariadic(m1, m2, m3, m4, m5, m6, NULL); }
  590. static LayerMultiplex* create(M m1, M m2, M m3, M m4, M m5, M m6, M m7, std::nullptr_t listEnd) { return createVariadic(m1, m2, m3, m4, m5, m6, m7, NULL); }
  591. static LayerMultiplex* create(M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, std::nullptr_t listEnd) { return createVariadic(m1, m2, m3, m4, m5, m6, m7, m8, NULL); }
  592. static LayerMultiplex* create(M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, M m9, std::nullptr_t listEnd) { return createVariadic(m1, m2, m3, m4, m5, m6, m7, m8, m9, NULL); }
  593. static LayerMultiplex* create(M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, M m9, M m10, std::nullptr_t listEnd) { return createVariadic(m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, NULL); }
  594. // On WP8 for variable argument lists longer than 10 items, use createWithArray or createVariadic with NULL as the last argument
  595. static LayerMultiplex* createVariadic(Layer* item, ...) CC_REQUIRES_NULL_TERMINATION;
  596. #else
  597. static LayerMultiplex * create(Layer* layer, ... );
  598. #endif
  599. /** Creates a LayerMultiplex with one layer.
  600. * Lua script can not init with undetermined number of variables
  601. * so add these functions to be used with lua.
  602. * @js NA
  603. * @lua NA
  604. *
  605. * @param layer A certain layer.
  606. * @return An autoreleased LayerMultiplex object.
  607. */
  608. static LayerMultiplex * createWithLayer(Layer* layer);
  609. /** Add a certain layer to LayerMultiplex.
  610. *
  611. * @param layer A layer need to be added to the LayerMultiplex.
  612. */
  613. void addLayer(Layer* layer);
  614. /** Switches to a certain layer indexed by n.
  615. The current (old) layer will be removed from it's parent with 'cleanup=true'.
  616. *
  617. * @param n The layer indexed by n will display.
  618. */
  619. void switchTo(int n);
  620. /** The same as switchTo(int), but has a parameter to set if need to clean up child.
  621. */
  622. void switchTo(int n, bool cleanup);
  623. /** release the current layer and switches to another layer indexed by n.
  624. The current (old) layer will be removed from it's parent with 'cleanup=true'.
  625. *
  626. * @param n The layer indexed by n will display.
  627. */
  628. void switchToAndReleaseMe(int n);
  629. virtual std::string getDescription() const override;
  630. CC_CONSTRUCTOR_ACCESS:
  631. /**
  632. * @js ctor
  633. */
  634. LayerMultiplex();
  635. /**
  636. * @js NA
  637. * @lua NA
  638. */
  639. virtual ~LayerMultiplex();
  640. virtual bool init() override;
  641. /** initializes a MultiplexLayer with one or more layers using a variable argument list.
  642. * @js NA
  643. * @lua NA
  644. */
  645. bool initWithLayers(Layer* layer, va_list params);
  646. /** initializes a MultiplexLayer with an array of layers
  647. @since v2.1
  648. */
  649. bool initWithArray(const Vector<Layer*>& arrayOfLayers);
  650. protected:
  651. unsigned int _enabledLayer;
  652. Vector<Layer*> _layers;
  653. private:
  654. CC_DISALLOW_COPY_AND_ASSIGN(LayerMultiplex);
  655. };
  656. // end of _2d group
  657. /// @}
  658. NS_CC_END
  659. #endif // __CCLAYER_H__