1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga 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}           pixelFormat     - <@readonly> Pixel format of the texture
114      * @property {Number}           pixelsWidth     - <@readonly> Width in pixels
115      * @property {Number}           pixelsHeight    - <@readonly> Height in pixels
116      * @property {Number}           width           - Content width in points
117      * @property {Number}           height          - Content height in points
118      * @property {cc.GLProgram}     shaderProgram   - The shader program used by drawAtPoint and drawInRect
119      * @property {Number}           maxS            - Texture max S
120      * @property {Number}           maxT            - Texture max T
121      */
122 
123     cc.Texture2D = cc.Class.extend({
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         getPixelsWide: function () {
138             return this._contentSize.width;
139         },
140 
141         getPixelsHigh: function () {
142             return this._contentSize.height;
143         },
144 
145         getContentSize: function () {
146             var locScaleFactor = cc.contentScaleFactor();
147             return cc.size(this._contentSize.width / locScaleFactor, this._contentSize.height / locScaleFactor);
148         },
149 
150         _getWidth: function () {
151             return this._contentSize.width / cc.contentScaleFactor();
152         },
153         _getHeight: function () {
154             return this._contentSize.height / cc.contentScaleFactor();
155         },
156 
157         getContentSizeInPixels: function () {
158             return this._contentSize;
159         },
160 
161         initWithElement: function (element) {
162             if (!element)
163                 return;
164             this._htmlElementObj = element;
165         },
166 
167         /**
168          * HTMLElement Object getter
169          * @return {HTMLElement}
170          */
171         getHtmlElementObj: function () {
172             return this._htmlElementObj;
173         },
174 
175         isLoaded: function () {
176             return this._isLoaded;
177         },
178 
179         handleLoadedTexture: function () {
180             var self = this
181             if (self._isLoaded) return;
182             if (!self._htmlElementObj) {
183                 var img = cc.loader.getRes(self.url);
184                 if (!img) return;
185                 self.initWithElement(img);
186             }
187 
188             self._isLoaded = true;
189             var locElement = self._htmlElementObj;
190             self._contentSize.width = locElement.width;
191             self._contentSize.height = locElement.height;
192 
193             self._callLoadedEventCallbacks();
194         },
195 
196         description: function () {
197             return "<cc.Texture2D | width = " + this._contentSize.width + " height " + this._contentSize.height + ">";
198         },
199 
200         initWithData: function (data, pixelFormat, pixelsWide, pixelsHigh, contentSize) {
201             //support only in WebGl rendering mode
202             return false;
203         },
204 
205         initWithImage: function (uiImage) {
206             //support only in WebGl rendering mode
207             return false;
208         },
209 
210         initWithString: function (text, fontName, fontSize, dimensions, hAlignment, vAlignment) {
211             //support only in WebGl rendering mode
212             return false;
213         },
214 
215         releaseTexture: function () {
216             //support only in WebGl rendering mode
217         },
218 
219         getName: function () {
220             //support only in WebGl rendering mode
221             return null;
222         },
223 
224         getMaxS: function () {
225             //support only in WebGl rendering mode
226             return 1;
227         },
228 
229         setMaxS: function (maxS) {
230             //support only in WebGl rendering mode
231         },
232 
233         getMaxT: function () {
234             return 1;
235         },
236 
237         setMaxT: function (maxT) {
238             //support only in WebGl rendering mode
239         },
240 
241         getPixelFormat: function () {
242             //support only in WebGl rendering mode
243             return null;
244         },
245 
246         getShaderProgram: function () {
247             //support only in WebGl rendering mode
248             return null;
249         },
250 
251         setShaderProgram: function (shaderProgram) {
252             //support only in WebGl rendering mode
253         },
254 
255         hasPremultipliedAlpha: function () {
256             //support only in WebGl rendering mode
257             return false;
258         },
259 
260         hasMipmaps: function () {
261             //support only in WebGl rendering mode
262             return false;
263         },
264 
265         releaseData: function (data) {
266             //support only in WebGl rendering mode
267             data = null;
268         },
269 
270         keepData: function (data, length) {
271             //support only in WebGl rendering mode
272             return data;
273         },
274 
275         drawAtPoint: function (point) {
276             //support only in WebGl rendering mode
277         },
278 
279         drawInRect: function (rect) {
280             //support only in WebGl rendering mode
281         },
282 
283         initWithETCFile: function (file) {
284             cc.log(cc._LogInfos.Texture2D_initWithETCFile);
285             return false;
286         },
287 
288         initWithPVRFile: function (file) {
289             cc.log(cc._LogInfos.Texture2D_initWithPVRFile);
290             return false;
291         },
292 
293         initWithPVRTCData: function (data, level, bpp, hasAlpha, length, pixelFormat) {
294             cc.log(cc._LogInfos.Texture2D_initWithPVRTCData);
295             return false;
296         },
297 
298         setTexParameters: function (texParams) {
299             //support only in WebGl rendering mode
300         },
301 
302         setAntiAliasTexParameters: function () {
303             //support only in WebGl rendering mode
304         },
305 
306         setAliasTexParameters: function () {
307             //support only in WebGl rendering mode
308         },
309 
310         generateMipmap: function () {
311             //support only in WebGl rendering mode
312         },
313 
314         stringForFormat: function () {
315             //support only in WebGl rendering mode
316             return "";
317         },
318 
319         bitsPerPixelForFormat: function (format) {
320             //support only in WebGl rendering mode
321             return -1;
322         },
323 
324         addLoadedEventListener: function (callback, target) {
325             if (!this._loadedEventListeners)
326                 this._loadedEventListeners = [];
327             this._loadedEventListeners.push({eventCallback: callback, eventTarget: target});
328         },
329 
330         removeLoadedEventListener: function (target) {
331             if (!this._loadedEventListeners)
332                 return;
333             var locListeners = this._loadedEventListeners;
334             for (var i = 0; i < locListeners.length; i++) {
335                 var selCallback = locListeners[i];
336                 if (selCallback.eventTarget == target) {
337                     locListeners.splice(i, 1);
338                 }
339             }
340         },
341 
342         _callLoadedEventCallbacks: function () {
343             if (!this._loadedEventListeners)
344                 return;
345             var locListeners = this._loadedEventListeners;
346             for (var i = 0, len = locListeners.length; i < len; i++) {
347                 var selCallback = locListeners[i];
348                 selCallback.eventCallback.call(selCallback.eventTarget, this);
349             }
350             locListeners.length = 0;
351         }
352     });
353 
354 } else {
355     _tmp.WebGLTexture2D();
356     delete _tmp.WebGLTexture2D;
357 }
358 
359 _tmp.PrototypeTexture2D();
360 delete _tmp.PrototypeTexture2D;