123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2011 Zynga Inc.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- /**
- * <p>
- * A cc.SpriteFrame has:<br/>
- * - texture: A cc.Texture2D that will be used by the cc.Sprite<br/>
- * - rectangle: A rectangle of the texture<br/>
- * <br/>
- * You can modify the frame of a cc.Sprite by doing:<br/>
- * </p>
- * @class
- * @extends cc.Class
- *
- * @example
- * var texture = cc.TextureCache.getInstance().addImage(s_dragon_animation);
- * var frame0 = cc.SpriteFrame.createWithTexture(texture, cc.rect(132 * 0, 132 * 0, 132, 132));
- */
- cc.SpriteFrame = cc.Class.extend(/** @lends cc.SpriteFrame# */{
- _offset:null,
- _originalSize:null,
- _rectInPixels:null,
- _rotated:false,
- _rect:null,
- _offsetInPixels:null,
- _originalSizeInPixels:null,
- _texture:null,
- _textureFilename:"",
- _textureLoaded:false,
- _eventListeners:null,
- ctor:function () {
- this._offset = cc.p(0, 0);
- this._offsetInPixels = cc.p(0, 0);
- this._originalSize = cc.size(0, 0);
- this._rotated = false;
- this._originalSizeInPixels = cc.size(0, 0);
- this._textureFilename = "";
- this._texture = null;
- this._textureLoaded = false;
- },
- // attributes
- textureLoaded:function(){
- return this._textureLoaded;
- },
- addLoadedEventListener:function(callback, target){
- if (this._eventListeners == null){
- this._eventListeners = [];
- }
- this._eventListeners.push({eventCallback:callback, eventTarget:target});
- },
- _callLoadedEventCallbacks:function(){
- var locListeners = this._eventListeners;
- if (!locListeners) return;
- for(var i = 0, len = locListeners.length; i < len; i++){
- var selCallback = locListeners[i];
- cc.doCallback(selCallback.eventCallback, selCallback.eventTarget, this);
- }
- locListeners.length = 0;
- },
- /**
- * @return {cc.Rect}
- */
- getRectInPixels:function () {
- var locRectInPixels = this._rectInPixels;
- return cc.rect(locRectInPixels.x, locRectInPixels.y, locRectInPixels.width, locRectInPixels.height);
- },
- /**
- * @param {cc.Rect} rectInPixels
- */
- setRectInPixels:function (rectInPixels) {
- if (!this._rectInPixels){
- this._rectInPixels = cc.rect(0,0,0,0);
- }
- this._rectInPixels.x = rectInPixels.x;
- this._rectInPixels.y = rectInPixels.y;
- this._rectInPixels.width = rectInPixels.width;
- this._rectInPixels.height = rectInPixels.height;
- this._rect = cc.RECT_PIXELS_TO_POINTS(rectInPixels);
- },
- /**
- * <p>
- * return is rotated of SpriteFrame. <br/>
- * </p>
- * @return {Boolean}
- */
- isRotated:function () {
- return this._rotated;
- },
- /**
- * set SpriteFrame is rotated
- * @param {Boolean} bRotated
- */
- setRotated:function (bRotated) {
- this._rotated = bRotated;
- },
- /**
- * get rect of the frame
- * @return {cc.Rect}
- */
- getRect:function () {
- var locRect = this._rect;
- return cc.rect(locRect.x, locRect.y, locRect.width, locRect.height);
- },
- /**
- * set rect of the frame
- * @param {cc.Rect} rect
- */
- setRect:function (rect) {
- if (!this._rect){
- this._rect = cc.rect(0,0,0,0);
- }
- this._rect.x = rect.x;
- this._rect.y = rect.y;
- this._rect.width = rect.width;
- this._rect.height = rect.height;
- this._rectInPixels = cc.RECT_POINTS_TO_PIXELS(this._rect);
- },
- /**
- * get offset of the frame
- * @return {cc.Point}
- */
- getOffsetInPixels:function () {
- return cc.p(this._offsetInPixels);
- },
- /**
- * set offset of the frame
- * @param {cc.Point} offsetInPixels
- */
- setOffsetInPixels:function (offsetInPixels) {
- this._offsetInPixels.x = offsetInPixels.x;
- this._offsetInPixels.y = offsetInPixels.y;
- this._offset = cc.POINT_PIXELS_TO_POINTS(this._offsetInPixels);
- },
- /**
- * get original size of the trimmed image
- * @const
- * @return {cc.Size}
- */
- getOriginalSizeInPixels:function () {
- return cc.size(this._originalSizeInPixels);
- },
- /**
- * set original size of the trimmed image
- * @param {cc.Size} sizeInPixels
- */
- setOriginalSizeInPixels:function (sizeInPixels) {
- this._originalSizeInPixels.width = sizeInPixels.width;
- this._originalSizeInPixels.height = sizeInPixels.height;
- },
- /**
- * get original size of the trimmed image
- * @const
- * @return {cc.Size}
- */
- getOriginalSize:function () {
- return cc.size(this._originalSize);
- },
- /**
- * set original size of the trimmed image
- * @param {cc.Size} sizeInPixels
- */
- setOriginalSize:function (sizeInPixels) {
- this._originalSize.width = sizeInPixels.width;
- this._originalSize.height = sizeInPixels.height;
- },
- /**
- * get texture of the frame
- * @return {cc.Texture2D}
- */
- getTexture:function () {
- if (this._texture)
- return this._texture;
- if (this._textureFilename !== "") {
- var locTexture = cc.TextureCache.getInstance().addImage(this._textureFilename);
- if (locTexture)
- this._textureLoaded = locTexture.isLoaded();
- return locTexture;
- }
- return null;
- },
- /**
- * set texture of the frame, the texture is retained
- * @param {cc.Texture2D} texture
- */
- setTexture:function (texture) {
- if (this._texture != texture) {
- var locLoaded = texture.isLoaded();
- this._textureLoaded = locLoaded;
- this._texture = texture;
- if(!locLoaded){
- texture.addLoadedEventListener(function(sender){
- this._textureLoaded = true;
- if(this._rotated && cc.renderContextType === cc.CANVAS){
- var tempElement = sender.getHtmlElementObj();
- tempElement = cc.cutRotateImageToCanvas(tempElement, this.getRect());
- var tempTexture = new cc.Texture2D();
- tempTexture.initWithElement(tempElement);
- tempTexture.handleLoadedTexture();
- this.setTexture(tempTexture);
- var rect = this.getRect();
- this.setRect(cc.rect(0, 0, rect.width, rect.height));
- }
- var locRect = this._rect;
- if(locRect.width === 0 && locRect.height === 0){
- var locContentSize = sender.getContentSize();
- this._rect.width = locContentSize.width;
- this._rect.height = locContentSize.height;
- this._rectInPixels = cc.RECT_POINTS_TO_PIXELS(this._rect);
- this._originalSizeInPixels.width = this._rectInPixels.width;
- this._originalSizeInPixels.height = this._rectInPixels.height;
- this._originalSize.width = locContentSize.width;
- this._originalSize.height = locContentSize.height;
- }
- this._callLoadedEventCallbacks();
- }, this);
- }
- }
- },
- /**
- * Offset getter
- * @const
- * @return {cc.Point}
- */
- getOffset:function () {
- return cc.p(this._offset);
- },
- /**
- * offset setter
- * @param {cc.Point} offsets
- */
- setOffset:function (offsets) {
- this._offset.x = offsets.x;
- this._offset.y = offsets.y;
- },
- clone: function(){
- var frame = new cc.SpriteFrame();
- frame.initWithTextureFilename(this._textureFilename, this._rectInPixels, this._rotated, this._offsetInPixels, this._originalSizeInPixels);
- frame.setTexture(this._texture);
- return frame;
- },
- /**
- * copy a new SpriteFrame
- * @return {cc.SpriteFrame}
- */
- copyWithZone:function () {
- var copy = new cc.SpriteFrame();
- copy.initWithTextureFilename(this._textureFilename, this._rectInPixels, this._rotated, this._offsetInPixels, this._originalSizeInPixels);
- copy.setTexture(this._texture);
- return copy;
- },
- copy:function () {
- return this.copyWithZone();
- },
- /**
- * Initializes SpriteFrame with Texture, rect, rotated, offset and originalSize in pixels.
- * @param {cc.Texture2D} texture
- * @param {cc.Rect} rect if parameters' length equal 2, rect in points, else rect in pixels
- * @param {Boolean} [rotated=false]
- * @param {cc.Point} [offset=cc.p(0,0)]
- * @param {cc.Size} [originalSize=rect.size]
- * @return {Boolean}
- */
- initWithTexture:function (texture, rect, rotated, offset, originalSize) {
- if(arguments.length === 2)
- rect = cc.RECT_POINTS_TO_PIXELS(rect);
- offset = offset || cc.p(0, 0);
- originalSize = originalSize || rect._size;
- this.setTexture(texture);
- this._rectInPixels = rect;
- this._rect = cc.RECT_PIXELS_TO_POINTS(rect);
- this._offsetInPixels.x = offset.x;
- this._offsetInPixels.y = offset.y;
- this._offset = cc.POINT_PIXELS_TO_POINTS(offset);
- this._originalSizeInPixels.width = originalSize.width;
- this._originalSizeInPixels.height = originalSize.height;
- this._originalSize = cc.SIZE_PIXELS_TO_POINTS(originalSize);
- this._rotated = rotated || false;
- return true;
- },
- /**
- * <p>
- * Initializes a cc.SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.<br/>
- * The originalSize is the size in pixels of the frame before being trimmed.
- * </p>
- * @param {string} filename
- * @param {cc.Rect} rect if parameters' length equal 2, rect in points, else rect in pixels
- * @param {Boolean} rotated
- * @param {cc.Point} [offset=cc.p(0,0)]
- * @param {cc.Size} [originalSize=rect.size]
- */
- initWithTextureFilename:function (filename, rect, rotated, offset, originalSize) {
- if(arguments.length === 2)
- rect = cc.RECT_POINTS_TO_PIXELS(rect);
- offset = offset || cc.p(0, 0);
- originalSize = originalSize || rect._size;
- this._texture = null;
- this._textureFilename = filename;
- this._rectInPixels = rect;
- this._rect = cc.RECT_PIXELS_TO_POINTS(rect);
- this._rotated = rotated || false;
- this._offsetInPixels.x = offset.x;
- this._offsetInPixels.y = offset.y;
- this._offset = cc.POINT_PIXELS_TO_POINTS(offset);
- this._originalSizeInPixels.width = originalSize.width;
- this._originalSizeInPixels.height = originalSize.height;
- this._originalSize = cc.SIZE_PIXELS_TO_POINTS(originalSize);
- return true;
- }
- });
- /**
- * <p>
- * Create a cc.SpriteFrame with a texture filename, rect, rotated, offset and originalSize in pixels.<br/>
- * The originalSize is the size in pixels of the frame before being trimmed.
- * </p>
- * @param {string} filename
- * @param {cc.Rect} rect if parameters' length equal 2, rect in points, else rect in pixels
- * @param {Boolean} rotated
- * @param {cc.Point} offset
- * @param {cc.Size} originalSize
- * @return {cc.SpriteFrame}
- */
- cc.SpriteFrame.create = function (filename, rect, rotated, offset, originalSize) {
- var spriteFrame = new cc.SpriteFrame();
- switch (arguments.length) {
- case 2:
- spriteFrame.initWithTextureFilename(filename, rect);
- break;
- case 5:
- spriteFrame.initWithTextureFilename(filename, rect, rotated, offset, originalSize);
- break;
- default:
- throw "Argument must be non-nil ";
- break;
- }
- return spriteFrame;
- };
- /**
- * Create a cc.SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.
- * @param {cc.Texture2D} texture
- * @param {cc.Rect} rect if parameters' length equal 2, rect in points, else rect in pixels
- * @param {Boolean} [rotated=]
- * @param {cc.Point} [offset=]
- * @param {cc.Size} [originalSize=]
- * @return {cc.SpriteFrame}
- * @example
- * //Create a cc.SpriteFrame with a texture, rect in texture.
- * var frame1 = cc.SpriteFrame.createWithTexture("grossini_dance.png",cc.rect(0,0,90,128));
- *
- * //Create a cc.SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels.
- * var frame2 = cc.SpriteFrame.createWithTexture(texture, frameRect, rotated, offset, sourceSize);
- */
- cc.SpriteFrame.createWithTexture = function (texture, rect, rotated, offset, originalSize) {
- var spriteFrame = new cc.SpriteFrame();
- switch (arguments.length) {
- case 2:
- spriteFrame.initWithTexture(texture, rect);
- break;
- case 5:
- spriteFrame.initWithTexture(texture, rect, rotated, offset, originalSize);
- break;
- default:
- throw "Argument must be non-nil ";
- break;
- }
- return spriteFrame;
- };
- cc.SpriteFrame._frameWithTextureForCanvas = function (texture, rect, rotated, offset, originalSize) {
- var spriteFrame = new cc.SpriteFrame();
- spriteFrame._texture = texture;
- spriteFrame._rectInPixels = rect;
- spriteFrame._rect = cc.RECT_PIXELS_TO_POINTS(rect);
- spriteFrame._offsetInPixels.x = offset.x;
- spriteFrame._offsetInPixels.y = offset.y;
- spriteFrame._offset = cc.POINT_PIXELS_TO_POINTS(spriteFrame._offsetInPixels);
- spriteFrame._originalSizeInPixels.width = originalSize.width;
- spriteFrame._originalSizeInPixels.height = originalSize.height;
- spriteFrame._originalSize = cc.SIZE_PIXELS_TO_POINTS(spriteFrame._originalSizeInPixels);
- spriteFrame._rotated = rotated;
- return spriteFrame;
- };
|