CCTexture2D.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. //CONSTANTS:
  23. /**
  24. * Horizontal center and vertical center.
  25. * @constant
  26. * @type Number
  27. */
  28. cc.ALIGN_CENTER = 0x33;
  29. /**
  30. * Horizontal center and vertical top.
  31. * @constant
  32. * @type Number
  33. */
  34. cc.ALIGN_TOP = 0x13;
  35. /**
  36. * Horizontal right and vertical top.
  37. * @constant
  38. * @type Number
  39. */
  40. cc.ALIGN_TOP_RIGHT = 0x12;
  41. /**
  42. * Horizontal right and vertical center.
  43. * @constant
  44. * @type Number
  45. */
  46. cc.ALIGN_RIGHT = 0x32;
  47. /**
  48. * Horizontal right and vertical bottom.
  49. * @constant
  50. * @type Number
  51. */
  52. cc.ALIGN_BOTTOM_RIGHT = 0x22;
  53. /**
  54. * Horizontal center and vertical bottom.
  55. * @constant
  56. * @type Number
  57. */
  58. cc.ALIGN_BOTTOM = 0x23;
  59. /**
  60. * Horizontal left and vertical bottom.
  61. * @constant
  62. * @type Number
  63. */
  64. cc.ALIGN_BOTTOM_LEFT = 0x21;
  65. /**
  66. * Horizontal left and vertical center.
  67. * @constant
  68. * @type Number
  69. */
  70. cc.ALIGN_LEFT = 0x31;
  71. /**
  72. * Horizontal left and vertical top.
  73. * @constant
  74. * @type Number
  75. */
  76. cc.ALIGN_TOP_LEFT = 0x11;
  77. //----------------------Possible texture pixel formats----------------------------
  78. // By default PVR images are treated as if they don't have the alpha channel premultiplied
  79. cc.PVRHaveAlphaPremultiplied_ = false;
  80. //cc.Texture2DWebGL move to TextureWebGL.js
  81. if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
  82. /**
  83. * <p>
  84. * This class allows to easily create OpenGL or Canvas 2D textures from images, text or raw data. <br/>
  85. * The created cc.Texture2D object will always have power-of-two dimensions. <br/>
  86. * Depending on how you create the cc.Texture2D object, the actual image area of the texture might be smaller than the texture dimensions <br/>
  87. * i.e. "contentSize" != (pixelsWide, pixelsHigh) and (maxS, maxT) != (1.0, 1.0). <br/>
  88. * Be aware that the content of the generated textures will be upside-down! </p>
  89. * @name cc.Texture2D
  90. * @class
  91. * @extends cc.Class
  92. *
  93. * @property {WebGLTexture} name - <@readonly> WebGLTexture Object
  94. * @property {Number} defaultPixelFormat - The default pixel format
  95. * @property {Number} pixelFormat - <@readonly> Pixel format of the texture
  96. * @property {Number} pixelsWidth - <@readonly> Width in pixels
  97. * @property {Number} pixelsHeight - <@readonly> Height in pixels
  98. * @property {Number} width - Content width in points
  99. * @property {Number} height - Content height in points
  100. * @property {cc.GLProgram} shaderProgram - The shader program used by drawAtPoint and drawInRect
  101. * @property {Number} maxS - Texture max S
  102. * @property {Number} maxT - Texture max T
  103. */
  104. cc.Texture2D = cc.Class.extend(/** @lends cc.Texture2D# */{
  105. _contentSize: null,
  106. _isLoaded: false,
  107. _htmlElementObj: null,
  108. _loadedEventListeners: null,
  109. url: null,
  110. ctor: function () {
  111. this._contentSize = cc.size(0, 0);
  112. this._isLoaded = false;
  113. this._htmlElementObj = null;
  114. },
  115. /**
  116. * get width in pixels
  117. * @return {Number}
  118. */
  119. getPixelsWide: function () {
  120. return this._contentSize.width;
  121. },
  122. /**
  123. * get height of in pixels
  124. * @return {Number}
  125. */
  126. getPixelsHigh: function () {
  127. return this._contentSize.height;
  128. },
  129. /**
  130. * get content size
  131. * @returns {cc.Size}
  132. */
  133. getContentSize: function () {
  134. var locScaleFactor = cc.contentScaleFactor();
  135. return cc.size(this._contentSize.width / locScaleFactor, this._contentSize.height / locScaleFactor);
  136. },
  137. _getWidth: function () {
  138. return this._contentSize.width / cc.contentScaleFactor();
  139. },
  140. _getHeight: function () {
  141. return this._contentSize.height / cc.contentScaleFactor();
  142. },
  143. /**
  144. * get content size in pixels
  145. * @returns {cc.Size}
  146. */
  147. getContentSizeInPixels: function () {
  148. return this._contentSize;
  149. },
  150. /**
  151. * init with HTML element
  152. * @param {HTMLImageElement|HTMLCanvasElement} element
  153. */
  154. initWithElement: function (element) {
  155. if (!element)
  156. return;
  157. this._htmlElementObj = element;
  158. },
  159. /**
  160. * HTMLElement Object getter
  161. * @return {HTMLImageElement|HTMLCanvasElement}
  162. */
  163. getHtmlElementObj: function () {
  164. return this._htmlElementObj;
  165. },
  166. /**
  167. * check whether texture is loaded
  168. * @returns {boolean}
  169. */
  170. isLoaded: function () {
  171. return this._isLoaded;
  172. },
  173. /**
  174. * handle loaded texture
  175. */
  176. handleLoadedTexture: function () {
  177. var self = this
  178. if (self._isLoaded) return;
  179. if (!self._htmlElementObj) {
  180. var img = cc.loader.getRes(self.url);
  181. if (!img) return;
  182. self.initWithElement(img);
  183. }
  184. self._isLoaded = true;
  185. var locElement = self._htmlElementObj;
  186. self._contentSize.width = locElement.width;
  187. self._contentSize.height = locElement.height;
  188. self._callLoadedEventCallbacks();
  189. },
  190. /**
  191. * description of cc.Texture2D
  192. * @returns {string}
  193. */
  194. description: function () {
  195. return "<cc.Texture2D | width = " + this._contentSize.width + " height " + this._contentSize.height + ">";
  196. },
  197. initWithData: function (data, pixelFormat, pixelsWide, pixelsHigh, contentSize) {
  198. //support only in WebGl rendering mode
  199. return false;
  200. },
  201. initWithImage: function (uiImage) {
  202. //support only in WebGl rendering mode
  203. return false;
  204. },
  205. initWithString: function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) {
  206. //support only in WebGl rendering mode
  207. return false;
  208. },
  209. releaseTexture: function () {
  210. //support only in WebGl rendering mode
  211. },
  212. getName: function () {
  213. //support only in WebGl rendering mode
  214. return null;
  215. },
  216. getMaxS: function () {
  217. //support only in WebGl rendering mode
  218. return 1;
  219. },
  220. setMaxS: function (maxS) {
  221. //support only in WebGl rendering mode
  222. },
  223. getMaxT: function () {
  224. return 1;
  225. },
  226. setMaxT: function (maxT) {
  227. //support only in WebGl rendering mode
  228. },
  229. getPixelFormat: function () {
  230. //support only in WebGl rendering mode
  231. return null;
  232. },
  233. getShaderProgram: function () {
  234. //support only in WebGl rendering mode
  235. return null;
  236. },
  237. setShaderProgram: function (shaderProgram) {
  238. //support only in WebGl rendering mode
  239. },
  240. hasPremultipliedAlpha: function () {
  241. //support only in WebGl rendering mode
  242. return false;
  243. },
  244. hasMipmaps: function () {
  245. //support only in WebGl rendering mode
  246. return false;
  247. },
  248. releaseData: function (data) {
  249. //support only in WebGl rendering mode
  250. data = null;
  251. },
  252. keepData: function (data, length) {
  253. //support only in WebGl rendering mode
  254. return data;
  255. },
  256. drawAtPoint: function (point) {
  257. //support only in WebGl rendering mode
  258. },
  259. drawInRect: function (rect) {
  260. //support only in WebGl rendering mode
  261. },
  262. /**
  263. * init with ETC file
  264. * @warning does not support on HTML5
  265. */
  266. initWithETCFile: function (file) {
  267. cc.log(cc._LogInfos.Texture2D_initWithETCFile);
  268. return false;
  269. },
  270. /**
  271. * init with PVR file
  272. * @warning does not support on HTML5
  273. */
  274. initWithPVRFile: function (file) {
  275. cc.log(cc._LogInfos.Texture2D_initWithPVRFile);
  276. return false;
  277. },
  278. /**
  279. * init with PVRTC data
  280. * @warning does not support on HTML5
  281. */
  282. initWithPVRTCData: function (data, level, bpp, hasAlpha, length, pixelFormat) {
  283. cc.log(cc._LogInfos.Texture2D_initWithPVRTCData);
  284. return false;
  285. },
  286. setTexParameters: function (texParams) {
  287. //support only in WebGl rendering mode
  288. },
  289. setAntiAliasTexParameters: function () {
  290. //support only in WebGl rendering mode
  291. },
  292. setAliasTexParameters: function () {
  293. //support only in WebGl rendering mode
  294. },
  295. generateMipmap: function () {
  296. //support only in WebGl rendering mode
  297. },
  298. stringForFormat: function () {
  299. //support only in WebGl rendering mode
  300. return "";
  301. },
  302. bitsPerPixelForFormat: function (format) {
  303. //support only in WebGl rendering mode
  304. return -1;
  305. },
  306. /**
  307. * add listener for loaded event
  308. * @param {Function} callback
  309. * @param {cc.Node} target
  310. */
  311. addLoadedEventListener: function (callback, target) {
  312. if (!this._loadedEventListeners)
  313. this._loadedEventListeners = [];
  314. this._loadedEventListeners.push({eventCallback: callback, eventTarget: target});
  315. },
  316. /**
  317. * remove listner for loaded event
  318. * @param {cc.Node} target
  319. */
  320. removeLoadedEventListener: function (target) {
  321. if (!this._loadedEventListeners)
  322. return;
  323. var locListeners = this._loadedEventListeners;
  324. for (var i = 0; i < locListeners.length; i++) {
  325. var selCallback = locListeners[i];
  326. if (selCallback.eventTarget == target) {
  327. locListeners.splice(i, 1);
  328. }
  329. }
  330. },
  331. _callLoadedEventCallbacks: function () {
  332. if (!this._loadedEventListeners)
  333. return;
  334. var locListeners = this._loadedEventListeners;
  335. for (var i = 0, len = locListeners.length; i < len; i++) {
  336. var selCallback = locListeners[i];
  337. selCallback.eventCallback.call(selCallback.eventTarget, this);
  338. }
  339. locListeners.length = 0;
  340. }
  341. });
  342. } else {
  343. cc.assert(typeof cc._tmp.WebGLTexture2D === "function", cc._LogInfos.MissingFile, "TexturesWebGL.js");
  344. cc._tmp.WebGLTexture2D();
  345. delete cc._tmp.WebGLTexture2D;
  346. }
  347. cc.assert(typeof cc._tmp.PrototypeTexture2D === "function", cc._LogInfos.MissingFile, "TexturesPropertyDefine.js");
  348. cc._tmp.PrototypeTexture2D();
  349. delete cc._tmp.PrototypeTexture2D;