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