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