CCAtlasNode.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * <p>cc.AtlasNode is a subclass of cc.Node, it knows how to render a TextureAtlas object. </p>
  24. *
  25. * <p>If you are going to render a TextureAtlas consider subclassing cc.AtlasNode (or a subclass of cc.AtlasNode)</p>
  26. *
  27. * <p>All features from cc.Node are valid</p>
  28. *
  29. * <p>You can create a cc.AtlasNode with an Atlas file, the width, the height of each item and the quantity of items to render</p>
  30. *
  31. * @class
  32. * @extends cc.Node
  33. *
  34. * @param {String} tile
  35. * @param {Number} tileWidth
  36. * @param {Number} tileHeight
  37. * @param {Number} itemsToRender
  38. * @example
  39. * var node = new cc.AtlasNode("pathOfTile", 16, 16, 1);
  40. *
  41. * @property {cc.Texture2D} texture - Current used texture
  42. * @property {cc.TextureAtlas} textureAtlas - Texture atlas for cc.AtlasNode
  43. * @property {Number} quadsToDraw - Number of quads to draw
  44. */
  45. cc.AtlasNode = cc.Node.extend(/** @lends cc.AtlasNode# */{
  46. textureAtlas: null,
  47. quadsToDraw: 0,
  48. //! chars per row
  49. _itemsPerRow: 0,
  50. //! chars per column
  51. _itemsPerColumn: 0,
  52. //! width of each char
  53. _itemWidth: 0,
  54. //! height of each char
  55. _itemHeight: 0,
  56. _colorUnmodified: null,
  57. // protocol variables
  58. _opacityModifyRGB: false,
  59. _blendFunc: null,
  60. // This variable is only used for CCLabelAtlas FPS display. So plz don't modify its value.
  61. _ignoreContentScaleFactor: false,
  62. _className: "AtlasNode",
  63. /**
  64. * <p>Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.</p>
  65. * @param {String} tile
  66. * @param {Number} tileWidth
  67. * @param {Number} tileHeight
  68. * @param {Number} itemsToRender
  69. */
  70. ctor: function (tile, tileWidth, tileHeight, itemsToRender) {
  71. cc.Node.prototype.ctor.call(this);
  72. this._colorUnmodified = cc.color.WHITE;
  73. this._blendFunc = {src: cc.BLEND_SRC, dst: cc.BLEND_DST};
  74. this._ignoreContentScaleFactor = false;
  75. itemsToRender !== undefined && this.initWithTileFile(tile, tileWidth, tileHeight, itemsToRender);
  76. },
  77. /**
  78. * Updates the Atlas (indexed vertex array).
  79. * Empty implementation, shall be overridden in subclasses
  80. * @function
  81. */
  82. updateAtlasValues: function () {
  83. cc.log(cc._LogInfos.AtlasNode_updateAtlasValues);
  84. },
  85. /**
  86. * Get color value of the atlas node
  87. * @function
  88. * @return {cc.Color}
  89. */
  90. getColor: function () {
  91. if (this._opacityModifyRGB)
  92. return this._colorUnmodified;
  93. return cc.Node.prototype.getColor.call(this);
  94. },
  95. /**
  96. * Set whether color should be changed with the opacity value,
  97. * if true, node color will change while opacity changes.
  98. * @function
  99. * @param {Boolean} value
  100. */
  101. setOpacityModifyRGB: function (value) {
  102. var oldColor = this.color;
  103. this._opacityModifyRGB = value;
  104. this.color = oldColor;
  105. },
  106. /**
  107. * Get whether color should be changed with the opacity value
  108. * @function
  109. * @return {Boolean}
  110. */
  111. isOpacityModifyRGB: function () {
  112. return this._opacityModifyRGB;
  113. },
  114. /**
  115. * Get node's blend function
  116. * @function
  117. * @return {cc.BlendFunc}
  118. */
  119. getBlendFunc: function () {
  120. return this._blendFunc;
  121. },
  122. /**
  123. * Set node's blend function
  124. * This function accept either cc.BlendFunc object or source value and destination value
  125. * @function
  126. * @param {Number | cc.BlendFunc} src
  127. * @param {Number} dst
  128. */
  129. setBlendFunc: function (src, dst) {
  130. if (dst === undefined)
  131. this._blendFunc = src;
  132. else
  133. this._blendFunc = {src: src, dst: dst};
  134. },
  135. /**
  136. * Set the atlas texture
  137. * @function
  138. * @param {cc.TextureAtlas} value The texture
  139. */
  140. setTextureAtlas: function (value) {
  141. this.textureAtlas = value;
  142. },
  143. /**
  144. * Get the atlas texture
  145. * @function
  146. * @return {cc.TextureAtlas}
  147. */
  148. getTextureAtlas: function () {
  149. return this.textureAtlas;
  150. },
  151. /**
  152. * Get the number of quads to be rendered
  153. * @function
  154. * @return {Number}
  155. */
  156. getQuadsToDraw: function () {
  157. return this.quadsToDraw;
  158. },
  159. /**
  160. * Set the number of quads to be rendered
  161. * @function
  162. * @param {Number} quadsToDraw
  163. */
  164. setQuadsToDraw: function (quadsToDraw) {
  165. this.quadsToDraw = quadsToDraw;
  166. },
  167. _textureForCanvas: null,
  168. _originalTexture: null,
  169. _uniformColor: null,
  170. _colorF32Array: null,
  171. /**
  172. * Initializes an cc.AtlasNode object with an atlas texture file name, the width, the height of each tile and the quantity of tiles to render
  173. * @function
  174. * @param {String} tile The atlas texture file name
  175. * @param {Number} tileWidth The width of each tile
  176. * @param {Number} tileHeight The height of each tile
  177. * @param {Number} itemsToRender The quantity of tiles to be rendered
  178. * @return {Boolean}
  179. */
  180. initWithTileFile: function (tile, tileWidth, tileHeight, itemsToRender) {
  181. if (!tile)
  182. throw "cc.AtlasNode.initWithTileFile(): title should not be null";
  183. var texture = cc.textureCache.addImage(tile);
  184. return this.initWithTexture(texture, tileWidth, tileHeight, itemsToRender);
  185. },
  186. /**
  187. * Initializes an CCAtlasNode with an atlas texture, the width, the height of each tile and the quantity of tiles to render
  188. * @function
  189. * @param {cc.Texture2D} texture The atlas texture
  190. * @param {Number} tileWidth The width of each tile
  191. * @param {Number} tileHeight The height of each tile
  192. * @param {Number} itemsToRender The quantity of tiles to be rendered
  193. * @return {Boolean}
  194. */
  195. initWithTexture: null,
  196. _initWithTextureForCanvas: function (texture, tileWidth, tileHeight, itemsToRender) {
  197. this._itemWidth = tileWidth;
  198. this._itemHeight = tileHeight;
  199. this._opacityModifyRGB = true;
  200. this._originalTexture = texture;
  201. if (!this._originalTexture) {
  202. cc.log(cc._LogInfos.AtlasNode__initWithTexture);
  203. return false;
  204. }
  205. this._textureForCanvas = this._originalTexture;
  206. this._calculateMaxItems();
  207. this.quadsToDraw = itemsToRender;
  208. return true;
  209. },
  210. _initWithTextureForWebGL: function (texture, tileWidth, tileHeight, itemsToRender) {
  211. this._itemWidth = tileWidth;
  212. this._itemHeight = tileHeight;
  213. this._colorUnmodified = cc.color.WHITE;
  214. this._opacityModifyRGB = true;
  215. this._blendFunc.src = cc.BLEND_SRC;
  216. this._blendFunc.dst = cc.BLEND_DST;
  217. var locRealColor = this._realColor;
  218. this._colorF32Array = new Float32Array([locRealColor.r / 255.0, locRealColor.g / 255.0, locRealColor.b / 255.0, this._realOpacity / 255.0]);
  219. this.textureAtlas = new cc.TextureAtlas();
  220. this.textureAtlas.initWithTexture(texture, itemsToRender);
  221. if (!this.textureAtlas) {
  222. cc.log(cc._LogInfos.AtlasNode__initWithTexture);
  223. return false;
  224. }
  225. this._updateBlendFunc();
  226. this._updateOpacityModifyRGB();
  227. this._calculateMaxItems();
  228. this.quadsToDraw = itemsToRender;
  229. //shader stuff
  230. this.shaderProgram = cc.shaderCache.programForKey(cc.SHADER_POSITION_TEXTURE_UCOLOR);
  231. this._uniformColor = cc._renderContext.getUniformLocation(this.shaderProgram.getProgram(), "u_color");
  232. return true;
  233. },
  234. /**
  235. * Render function using the canvas 2d context or WebGL context, internal usage only, please do not call this function
  236. * @function
  237. * @param {CanvasRenderingContext2D | WebGLRenderingContext} ctx The render context
  238. */
  239. draw: null,
  240. _drawForWebGL: function (ctx) {
  241. var context = ctx || cc._renderContext;
  242. cc.nodeDrawSetup(this);
  243. cc.glBlendFunc(this._blendFunc.src, this._blendFunc.dst);
  244. if(this._uniformColor && this._colorF32Array){
  245. context.uniform4fv(this._uniformColor, this._colorF32Array);
  246. this.textureAtlas.drawNumberOfQuads(this.quadsToDraw, 0);
  247. }
  248. },
  249. /**
  250. * Set node's color
  251. * @function
  252. * @param {cc.Color} color Color object created with cc.color(r, g, b).
  253. */
  254. setColor: null,
  255. _setColorForCanvas: function (color3) {
  256. var locRealColor = this._realColor;
  257. if ((locRealColor.r == color3.r) && (locRealColor.g == color3.g) && (locRealColor.b == color3.b))
  258. return;
  259. var temp = cc.color(color3.r, color3.g, color3.b);
  260. this._colorUnmodified = color3;
  261. if (this._opacityModifyRGB) {
  262. var locDisplayedOpacity = this._displayedOpacity;
  263. temp.r = temp.r * locDisplayedOpacity / 255;
  264. temp.g = temp.g * locDisplayedOpacity / 255;
  265. temp.b = temp.b * locDisplayedOpacity / 255;
  266. }
  267. cc.Node.prototype.setColor.call(this, color3);
  268. this._changeTextureColor();
  269. },
  270. _changeTextureColor: function(){
  271. var locTexture = this.getTexture();
  272. if (locTexture && this._originalTexture) {
  273. var element = this._originalTexture.getHtmlElementObj();
  274. if(!element)
  275. return;
  276. var locElement = locTexture.getHtmlElementObj();
  277. var textureRect = cc.rect(0, 0, element.width, element.height);
  278. if (locElement instanceof HTMLCanvasElement)
  279. cc.generateTintImageWithMultiply(element, this._displayedColor, textureRect, locElement);
  280. else {
  281. locElement = cc.generateTintImageWithMultiply(element, this._displayedColor, textureRect);
  282. locTexture = new cc.Texture2D();
  283. locTexture.initWithElement(locElement);
  284. locTexture.handleLoadedTexture();
  285. this.setTexture(locTexture);
  286. }
  287. }
  288. },
  289. _setColorForWebGL: function (color3) {
  290. var temp = cc.color(color3.r, color3.g, color3.b);
  291. this._colorUnmodified = color3;
  292. var locDisplayedOpacity = this._displayedOpacity;
  293. if (this._opacityModifyRGB) {
  294. temp.r = temp.r * locDisplayedOpacity / 255;
  295. temp.g = temp.g * locDisplayedOpacity / 255;
  296. temp.b = temp.b * locDisplayedOpacity / 255;
  297. }
  298. cc.Node.prototype.setColor.call(this, color3);
  299. var locDisplayedColor = this._displayedColor;
  300. this._colorF32Array = new Float32Array([locDisplayedColor.r / 255.0, locDisplayedColor.g / 255.0,
  301. locDisplayedColor.b / 255.0, locDisplayedOpacity / 255.0]);
  302. },
  303. /**
  304. * Set node's opacity
  305. * @function
  306. * @param {Number} opacity The opacity value
  307. */
  308. setOpacity: function (opacity) {
  309. },
  310. _setOpacityForCanvas: function (opacity) {
  311. cc.Node.prototype.setOpacity.call(this, opacity);
  312. // special opacity for premultiplied textures
  313. if (this._opacityModifyRGB) {
  314. this.color = this._colorUnmodified;
  315. }
  316. },
  317. _setOpacityForWebGL: function (opacity) {
  318. cc.Node.prototype.setOpacity.call(this, opacity);
  319. // special opacity for premultiplied textures
  320. if (this._opacityModifyRGB) {
  321. this.color = this._colorUnmodified;
  322. } else {
  323. var locDisplayedColor = this._displayedColor;
  324. this._colorF32Array = new Float32Array([locDisplayedColor.r / 255.0, locDisplayedColor.g / 255.0,
  325. locDisplayedColor.b / 255.0, this._displayedOpacity / 255.0]);
  326. }
  327. },
  328. /**
  329. * Get the current texture
  330. * @function
  331. * @return {cc.Texture2D}
  332. */
  333. getTexture: null,
  334. _getTextureForCanvas: function () {
  335. return this._textureForCanvas;
  336. },
  337. _getTextureForWebGL: function () {
  338. return this.textureAtlas.texture;
  339. },
  340. /**
  341. * Replace the current texture with a new one
  342. * @function
  343. * @param {cc.Texture2D} texture The new texture
  344. */
  345. setTexture: null,
  346. _setTextureForCanvas: function (texture) {
  347. this._textureForCanvas = texture;
  348. },
  349. _setTextureForWebGL: function (texture) {
  350. this.textureAtlas.texture = texture;
  351. this._updateBlendFunc();
  352. this._updateOpacityModifyRGB();
  353. },
  354. _calculateMaxItems: null,
  355. _calculateMaxItemsForCanvas: function () {
  356. var selTexture = this.texture;
  357. var size = selTexture.getContentSize();
  358. this._itemsPerColumn = 0 | (size.height / this._itemHeight);
  359. this._itemsPerRow = 0 | (size.width / this._itemWidth);
  360. },
  361. _calculateMaxItemsForWebGL: function () {
  362. var selTexture = this.texture;
  363. var size = selTexture.getContentSize();
  364. if (this._ignoreContentScaleFactor)
  365. size = selTexture.getContentSizeInPixels();
  366. this._itemsPerColumn = 0 | (size.height / this._itemHeight);
  367. this._itemsPerRow = 0 | (size.width / this._itemWidth);
  368. },
  369. _updateBlendFunc: function () {
  370. if (!this.textureAtlas.texture.hasPremultipliedAlpha()) {
  371. this._blendFunc.src = cc.SRC_ALPHA;
  372. this._blendFunc.dst = cc.ONE_MINUS_SRC_ALPHA;
  373. }
  374. },
  375. _updateOpacityModifyRGB: function () {
  376. this._opacityModifyRGB = this.textureAtlas.texture.hasPremultipliedAlpha();
  377. },
  378. _setIgnoreContentScaleFactor: function (ignoreContentScaleFactor) {
  379. this._ignoreContentScaleFactor = ignoreContentScaleFactor;
  380. }
  381. });
  382. var _p = cc.AtlasNode.prototype;
  383. if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
  384. _p.initWithTexture = _p._initWithTextureForWebGL;
  385. _p.draw = _p._drawForWebGL;
  386. _p.setColor = _p._setColorForWebGL;
  387. _p.setOpacity = _p._setOpacityForWebGL;
  388. _p.getTexture = _p._getTextureForWebGL;
  389. _p.setTexture = _p._setTextureForWebGL;
  390. _p._calculateMaxItems = _p._calculateMaxItemsForWebGL;
  391. } else {
  392. _p.initWithTexture = _p._initWithTextureForCanvas;
  393. _p.draw = cc.Node.prototype.draw;
  394. _p.setColor = _p._setColorForCanvas;
  395. _p.setOpacity = _p._setOpacityForCanvas;
  396. _p.getTexture = _p._getTextureForCanvas;
  397. _p.setTexture = _p._setTextureForCanvas;
  398. _p._calculateMaxItems = _p._calculateMaxItemsForCanvas;
  399. if(!cc.sys._supportCanvasNewBlendModes)
  400. _p._changeTextureColor = function(){
  401. var locElement, locTexture = this.getTexture();
  402. if (locTexture && this._originalTexture) {
  403. locElement = locTexture.getHtmlElementObj();
  404. if (!locElement)
  405. return;
  406. var element = this._originalTexture.getHtmlElementObj();
  407. var cacheTextureForColor = cc.textureCache.getTextureColors(element);
  408. if (cacheTextureForColor) {
  409. var textureRect = cc.rect(0, 0, element.width, element.height);
  410. if (locElement instanceof HTMLCanvasElement)
  411. cc.generateTintImage(locElement, cacheTextureForColor, this._displayedColor, textureRect, locElement);
  412. else {
  413. locElement = cc.generateTintImage(locElement, cacheTextureForColor, this._displayedColor, textureRect);
  414. locTexture = new cc.Texture2D();
  415. locTexture.initWithElement(locElement);
  416. locTexture.handleLoadedTexture();
  417. this.setTexture(locTexture);
  418. }
  419. }
  420. }
  421. };
  422. }
  423. // Override properties
  424. cc.defineGetterSetter(_p, "opacity", _p.getOpacity, _p.setOpacity);
  425. cc.defineGetterSetter(_p, "color", _p.getColor, _p.setColor);
  426. // Extended properties
  427. /** @expose */
  428. _p.texture;
  429. cc.defineGetterSetter(_p, "texture", _p.getTexture, _p.setTexture);
  430. /** @expose */
  431. _p.textureAtlas;
  432. /** @expose */
  433. _p.quadsToDraw;
  434. /**
  435. * Creates a cc.AtlasNode with an Atlas file the width and height of each item and the quantity of items to render
  436. * @deprecated since v3.0, please use new construction instead
  437. * @function
  438. * @static
  439. * @param {String} tile
  440. * @param {Number} tileWidth
  441. * @param {Number} tileHeight
  442. * @param {Number} itemsToRender
  443. * @return {cc.AtlasNode}
  444. */
  445. cc.AtlasNode.create = function (tile, tileWidth, tileHeight, itemsToRender) {
  446. return new cc.AtlasNode(tile, tileWidth, tileHeight, itemsToRender);
  447. };