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 /** 28 * <p> 29 * A cc.SpriteFrame has:<br/> 30 * - texture: A cc.Texture2D that will be used by the cc.Sprite<br/> 31 * - rectangle: A rectangle of the texture<br/> 32 * <br/> 33 * You can modify the frame of a cc.Sprite by doing:<br/> 34 * </p> 35 * @class 36 * @extends cc.Class 37 * 38 * @example 39 * var texture = cc.TextureCache.getInstance().addImage(s_dragon_animation); 40 * var frame0 = cc.SpriteFrame.createWithTexture(texture, cc.rect(132 * 0, 132 * 0, 132, 132)); 41 */ 42 cc.SpriteFrame = cc.Class.extend(/** @lends cc.SpriteFrame# */{ 43 _offset:null, 44 _originalSize:null, 45 _rectInPixels:null, 46 _rotated:null, 47 _rect:null, 48 _offsetInPixels:null, 49 _originalSizeInPixels:null, 50 _texture:null, 51 _textureFilename:"", 52 53 ctor:function () { 54 this._offset = cc.p(0, 0); 55 this._offsetInPixels = cc.p(0, 0); 56 this._originalSize = cc.size(0, 0); 57 this._rectInPixels = cc.rect(0, 0, 0, 0); 58 this._rect = cc.rect(0, 0, 0, 0); 59 this._originalSizeInPixels = cc.size(0, 0); 60 this._textureFilename = ""; 61 }, 62 63 // attributes 64 /** 65 * @return {cc.Rect} 66 */ 67 getRectInPixels:function () { 68 return this._rectInPixels; 69 }, 70 71 /** 72 * @param {cc.Rect} rectInPixels 73 */ 74 setRectInPixels:function (rectInPixels) { 75 this._rectInPixels = rectInPixels; 76 this._rect = cc.RECT_PIXELS_TO_POINTS(rectInPixels); 77 }, 78 79 80 /** 81 * <p> 82 * return is rotated of SpriteFrame. <br/> 83 * </p> 84 * @return {Boolean} 85 */ 86 isRotated:function () { 87 return this._rotated; 88 }, 89 90 /** 91 * set SpriteFrame is rotated 92 * @param {Boolean} bRotated 93 */ 94 setRotated:function (bRotated) { 95 this._rotated = bRotated; 96 }, 97 98 /** 99 * get rect of the frame 100 * @return {cc.Rect} 101 */ 102 getRect:function () { 103 return this._rect; 104 }, 105 106 /** 107 * set rect of the frame 108 * @param {cc.Rect} rect 109 */ 110 setRect:function (rect) { 111 this._rect = rect; 112 this._rectInPixels = cc.RECT_POINTS_TO_PIXELS(this._rect); 113 }, 114 115 /** 116 * get offset of the frame 117 * @return {cc.Point} 118 */ 119 getOffsetInPixels:function () { 120 return cc.p(this._offsetInPixels.x, this._offsetInPixels.y); 121 }, 122 123 /** 124 * set offset of the frame 125 * @param {cc.Point} offsetInPixels 126 */ 127 setOffsetInPixels:function (offsetInPixels) { 128 this._offsetInPixels = offsetInPixels; 129 this._offset = cc.POINT_PIXELS_TO_POINTS(this._offsetInPixels); 130 }, 131 132 /** 133 * get original size of the trimmed image 134 * @return {cc.Size} 135 */ 136 getOriginalSizeInPixels:function () { 137 return this._originalSizeInPixels; 138 }, 139 140 /** 141 * set original size of the trimmed image 142 * @param {cc.Size} sizeInPixels 143 */ 144 setOriginalSizeInPixels:function (sizeInPixels) { 145 this._originalSizeInPixels = sizeInPixels; 146 }, 147 148 /** 149 * get original size of the trimmed image 150 * @return {cc.Size} 151 */ 152 getOriginalSize:function () { 153 return cc.size(this._originalSize.width, this._originalSize.height); 154 }, 155 156 /** 157 * set original size of the trimmed image 158 * @param {cc.Size} sizeInPixels 159 */ 160 setOriginalSize:function (sizeInPixels) { 161 this._originalSize = sizeInPixels; 162 }, 163 164 /** 165 * get texture of the frame 166 * @return {cc.Texture2D|HTMLImageElement} 167 */ 168 getTexture:function () { 169 if (this._texture) 170 return this._texture; 171 if (this._textureFilename !== "") 172 return cc.TextureCache.getInstance().addImage(this._textureFilename); 173 return null; 174 }, 175 176 /** 177 * set texture of the frame, the texture is retained 178 * @param {cc.Texture2D|HTMLImageElement} texture 179 */ 180 setTexture:function (texture) { 181 if (this._texture != texture) { 182 this._texture = texture; 183 } 184 }, 185 186 /** 187 * Offset getter 188 * @return {cc.Point} 189 */ 190 getOffset:function () { 191 return cc.p(this._offset.x, this._offset.y); 192 }, 193 194 /** 195 * offset setter 196 * @param {cc.Point} offsets 197 */ 198 setOffset:function (offsets) { 199 this._offset = offsets; 200 }, 201 202 /** 203 * copy a new SpriteFrame 204 * @return {cc.SpriteFrame} 205 */ 206 copyWithZone:function () { 207 var copy = new cc.SpriteFrame(); 208 copy.initWithTextureFilename(this._textureFilename, this._rectInPixels, this._rotated, this._offsetInPixels, this._originalSizeInPixels); 209 copy.setTexture(this._texture); 210 return copy; 211 }, 212 213 /** 214 * Initializes SpriteFrame with Texture, rect, rotated, offset and originalSize in pixels. 215 * @param {cc.Texture2D|HTMLImageElement} texture 216 * @param {cc.Rect} rect 217 * @param {Boolean} rotated 218 * @param {cc.Point} offset 219 * @param {cc.Size} originalSize 220 * @return {Boolean} 221 */ 222 initWithTexture:function (texture, rect, rotated, offset, originalSize) { 223 var argnum = arguments.length; 224 switch (argnum) { 225 /** Initializes a cc.SpriteFrame with a texture, rect in points. 226 It is assumed that the frame was not trimmed. 227 */ 228 case 2: 229 var rectInPixels = cc.RECT_POINTS_TO_PIXELS(rect); 230 return this.initWithTexture(texture, rectInPixels, false, cc.PointZero(), rectInPixels.size); 231 break; 232 233 /** Initializes a cc.SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. 234 The originalSize is the size in points of the frame before being trimmed. 235 */ 236 case 5: 237 this._texture = texture; 238 this._rectInPixels = rect; 239 this._rect = cc.RECT_PIXELS_TO_POINTS(rect); 240 this._offsetInPixels = offset; 241 this._offset = cc.POINT_PIXELS_TO_POINTS(this._offsetInPixels); 242 this._originalSizeInPixels = originalSize; 243 this._originalSize = cc.SIZE_PIXELS_TO_POINTS(this._originalSizeInPixels); 244 this._rotated = rotated; 245 return true; 246 break; 247 default: 248 throw "Argument must be non-nil "; 249 break; 250 } 251 }, 252 253 /** 254 * <p> 255 * Initializes a cc.SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.<br/> 256 * The originalSize is the size in pixels of the frame before being trimmed. 257 * </p> 258 * @param {string} filename 259 * @param {cc.Rect} rect 260 * @param {Boolean} rotated 261 * @param {cc.Point} offset 262 * @param {cc.Size} originalSize 263 */ 264 initWithTextureFilename:function (filename, rect, rotated, offset, originalSize) { 265 var rectInPixels = cc.RECT_POINTS_TO_PIXELS(rect); 266 offset = offset || cc.size(0, 0); 267 originalSize = originalSize || rectInPixels.size; 268 269 this._texture = null; 270 this._textureFilename = filename; 271 this._rectInPixels = rectInPixels; 272 this._rect = cc.RECT_PIXELS_TO_POINTS(rectInPixels); 273 this._rotated = rotated || false; 274 this._offsetInPixels = offset; 275 this._offset = cc.POINT_PIXELS_TO_POINTS(offset); 276 this._originalSizeInPixels = originalSize; 277 this._originalSize = cc.SIZE_PIXELS_TO_POINTS(originalSize); 278 279 return true; 280 } 281 }); 282 283 /** 284 * <p> 285 * Create a cc.SpriteFrame with a texture filename, rect, rotated, offset and originalSize in pixels.<br/> 286 * The originalSize is the size in pixels of the frame before being trimmed. 287 * </p> 288 * @param {string} filename 289 * @param {cc.Rect} rect 290 * @param {Boolean} rotated 291 * @param {cc.Point} offset 292 * @param {cc.Size} originalSize 293 * @return {cc.SpriteFrame} 294 */ 295 cc.SpriteFrame.create = function (filename, rect, rotated, offset, originalSize) { 296 var spriteFrame = new cc.SpriteFrame(); 297 switch (arguments.length) { 298 case 2: 299 spriteFrame.initWithTextureFilename(filename, rect); 300 break; 301 case 5: 302 spriteFrame.initWithTextureFilename(filename, rect, rotated, offset, originalSize); 303 break; 304 default: 305 throw "Argument must be non-nil "; 306 break; 307 } 308 return spriteFrame; 309 }; 310 311 /** 312 * Create a cc.SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. 313 * @param {cc.Texture2D|HTMLImageElement} texture 314 * @param {cc.Rect} rect 315 * @param {Boolean} rotated 316 * @param {cc.Point} offset 317 * @param {cc.Size} originalSize 318 * @return {cc.SpriteFrame} 319 * @example 320 * //Create a cc.SpriteFrame with a texture, rect in texture. 321 * var frame1 = cc.SpriteFrame.createWithTexture("grossini_dance.png",cc.rect(0,0,90,128)); 322 * 323 * //Create a cc.SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. 324 * var frame2 = cc.SpriteFrame.createWithTexture(texture, frameRect, rotated, offset, sourceSize); 325 */ 326 cc.SpriteFrame.createWithTexture = function (texture, rect, rotated, offset, originalSize) { 327 var argnum = arguments.length; 328 var spriteFrame = new cc.SpriteFrame(); 329 switch (argnum) { 330 /** Create a cc.SpriteFrame with a texture, rect in points. 331 It is assumed that the frame was not trimmed. 332 */ 333 case 2: 334 spriteFrame.initWithTexture(texture, rect); 335 break; 336 /** Create a cc.SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. 337 The originalSize is the size in points of the frame before being trimmed. 338 */ 339 case 5: 340 spriteFrame.initWithTexture(texture, rect, rotated, offset, originalSize); 341 break; 342 default: 343 throw "Argument must be non-nil "; 344 break; 345 } 346 return spriteFrame; 347 }; 348 349 cc.SpriteFrame._frameWithTextureForCanvas = function (texture, rect, rotated, offset, originalSize) { 350 var spriteFrame = new cc.SpriteFrame(); 351 spriteFrame._texture = texture; 352 spriteFrame._rectInPixels = rect; 353 spriteFrame._rect = cc.RECT_PIXELS_TO_POINTS(rect); 354 spriteFrame._offsetInPixels = offset; 355 spriteFrame._offset = cc.POINT_PIXELS_TO_POINTS(spriteFrame._offsetInPixels); 356 spriteFrame._originalSizeInPixels = originalSize; 357 spriteFrame._originalSize = cc.SIZE_PIXELS_TO_POINTS(spriteFrame._originalSizeInPixels); 358 spriteFrame._rotated = rotated; 359 return spriteFrame; 360 }; 361