Merge pull request #106 from dabingnn/refine-spriteFrame

Refine sprite frame
This commit is contained in:
pandamicro
2015-12-22 18:55:52 +08:00
4 changed files with 276 additions and 364 deletions
+202 -160
View File
@@ -27,7 +27,6 @@
var EventTarget = require("../event/event-target");
/**
* <p>
* A cc.SpriteFrame has:<br/>
@@ -47,8 +46,8 @@ var EventTarget = require("../event/event-target");
* @example {@link utils/api/engine/docs/cocos2d/core/sprites/SpriteFrame.js}
*/
cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
name:'cc.SpriteFrame',
extends:require('../assets/CCAsset'),
name: 'cc.SpriteFrame',
extends: require('../assets/CCAsset'),
mixins: [EventTarget],
properties: {
@@ -66,23 +65,18 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
// just packing
return;
}
// texture will be init in getTexture()
var texture = this.getTexture();
var texture = cc.textureCache.addImage(url);
this._refreshTexture(texture);
if (this._textureLoaded) {
this._checkRect(texture);
this.emit('load');
}
else {
// register event in setTexture()
this._texture = null;
this.setTexture(texture);
}
}
}
},
}
},
ctor:function () {
ctor: function () {
var filename = arguments[0];
var rect = arguments[1];
var rotated = arguments[2];
@@ -90,16 +84,16 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
var originalSize = arguments[4];
// the location of the sprite on rendering texture
this._rect = new cc.Rect();
this._rectInPixels = new cc.Rect();
this._rect = null;
this._rectInPixels = null;
// for trimming
this._offset = new cc.Vec2();
this._offsetInPixels = new cc.Vec2();
this._offset = null;
this._offsetInPixels = null;
// for trimming
this._originalSize = new cc.Size();
this._originalSizeInPixels = new cc.Size();
this._originalSize = null;
this._originalSizeInPixels = null;
this._rotated = false;
@@ -144,11 +138,10 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
this._atlasUuid = '';
}
if(filename !== undefined && rect !== undefined ){
if(rotated === undefined || offset === undefined || originalSize === undefined)
this.initWithTexture(filename, rect);
else
this.initWithTexture(filename, rect, rotated, offset, originalSize)
if (filename !== undefined) {
this.initWithTexture(filename, rect, rotated, offset, originalSize);
} else {
//todo log Error
}
},
@@ -157,7 +150,7 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method textureLoaded
* @returns {boolean}
*/
textureLoaded:function(){
textureLoaded: function () {
return this._textureLoaded;
},
@@ -168,7 +161,7 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @param {Object} target
* @deprecated since 3.1, please use EventTarget API instead
*/
addLoadedEventListener:function(callback, target){
addLoadedEventListener: function (callback, target) {
this.once("load", callback, target);
},
@@ -177,9 +170,13 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method getRectInPixels
* @return {Rect}
*/
getRectInPixels:function () {
getRectInPixels: function () {
var locRectInPixels = this._rectInPixels;
return cc.rect(locRectInPixels.x, locRectInPixels.y, locRectInPixels.width, locRectInPixels.height);
if (locRectInPixels) {
return cc.rect(locRectInPixels);
} else {
return cc.rect(0, 0, 0, 0);
}
},
/**
@@ -187,15 +184,17 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method setRectInPixels
* @param {Rect} rectInPixels
*/
setRectInPixels:function (rectInPixels) {
var rect = this._rectInPixels;
if (!rect){
this._rectInPixels = rect = cc.rect();
setRectInPixels: function (rectInPixels) {
if (!this._rectInPixels) {
this._rectInPixels = cc.rect(rectInPixels);
} else {
var rect = this._rectInPixels;
rect.x = rectInPixels.x;
rect.y = rectInPixels.y;
rect.width = rectInPixels.width;
rect.height = rectInPixels.height;
}
rect.x = rectInPixels.x;
rect.y = rectInPixels.y;
rect.width = rectInPixels.width;
rect.height = rectInPixels.height;
this._rect = cc.rectPixelsToPoints(rectInPixels);
},
@@ -204,7 +203,7 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method isRotated
* @return {Boolean}
*/
isRotated:function () {
isRotated: function () {
return this._rotated;
},
@@ -213,7 +212,7 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method setRotated
* @param {Boolean} bRotated
*/
setRotated:function (bRotated) {
setRotated: function (bRotated) {
this._rotated = bRotated;
},
@@ -222,8 +221,13 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method getRect
* @return {Rect}
*/
getRect:function () {
return this._rect.clone();
getRect: function () {
var locRect = this._rect;
if (locRect) {
return cc.rect(locRect);
} else {
return cc.rect(0, 0, 0, 0);
}
},
/**
@@ -231,15 +235,17 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method setRect
* @param {Rect} rect
*/
setRect:function (rect) {
if (!this._rect){
this._rect = cc.rect(0,0,0,0);
setRect: function (rect) {
if (!this._rect) {
this._rect = cc.rect(rect);
} else {
var locRect = this._rect;
locRect.x = rect.x;
locRect.y = rect.y;
locRect.width = rect.width;
locRect.height = rect.height;
}
this._rect.x = rect.x;
this._rect.y = rect.y;
this._rect.width = rect.width;
this._rect.height = rect.height;
this._rectInPixels = cc.rectPointsToPixels(this._rect);
this._rectInPixels = cc.rectPointsToPixels(rect);
},
/**
@@ -247,8 +253,13 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method getOffsetInPixels
* @return {Vec2}
*/
getOffsetInPixels:function () {
return cc.p(this._offsetInPixels);
getOffsetInPixels: function () {
var offsetPixels = this._offsetInPixels;
if (offsetPixels) {
return cc.p(offsetPixels);
} else {
return cc.p(0, 0);
}
},
/**
@@ -256,10 +267,15 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method setOffsetInPixels
* @param {Vec2} offsetInPixels
*/
setOffsetInPixels:function (offsetInPixels) {
this._offsetInPixels.x = offsetInPixels.x;
this._offsetInPixels.y = offsetInPixels.y;
cc._pointPixelsToPointsOut(this._offsetInPixels, this._offset);
setOffsetInPixels: function (offsetInPixels) {
if (!this._offsetInPixels) {
this._offsetInPixels = cc.p(offsetInPixels);
} else {
this._offsetInPixels.x = offsetInPixels.x;
this._offsetInPixels.y = offsetInPixels.y;
}
this._offset = cc.pointPixelsToPoints(offsetInPixels);
},
/**
@@ -267,8 +283,13 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method getOriginalSizeInPixels
* @return {Size}
*/
getOriginalSizeInPixels:function () {
return cc.size(this._originalSizeInPixels);
getOriginalSizeInPixels: function () {
var locSizeInPixels = this._originalSizeInPixels;
if (locSizeInPixels) {
return cc.size(locSizeInPixels);
} else {
return cc.size(0, 0);
}
},
/**
@@ -276,9 +297,15 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method setOriginalSizeInPixels
* @param {Size} sizeInPixels
*/
setOriginalSizeInPixels:function (sizeInPixels) {
this._originalSizeInPixels.width = sizeInPixels.width;
this._originalSizeInPixels.height = sizeInPixels.height;
setOriginalSizeInPixels: function (sizeInPixels) {
if (!this._originalSizeInPixels) {
this._originalSizeInPixels = cc.size(sizeInPixels);
} else {
this._originalSizeInPixels.width = sizeInPixels.width;
this._originalSizeInPixels.height = sizeInPixels.height;
}
this._originalSize = cc.sizePixelsToPoints(sizeInPixels);
},
/**
@@ -286,8 +313,13 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method getOriginalSize
* @return {Size}
*/
getOriginalSize:function () {
return cc.size(this._originalSize);
getOriginalSize: function () {
var locSize = this._originalSize;
if (locSize) {
return cc.size(locSize);
} else {
return cc.size(0, 0);
}
},
/**
@@ -295,9 +327,15 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method setOriginalSize
* @param {Size} size
*/
setOriginalSize:function (size) {
this._originalSize.width = size.width;
this._originalSize.height = size.height;
setOriginalSize: function (size) {
if (!this._originalSize) {
this._originalSize = cc.size(size);
} else {
this._originalSize.width = size.width;
this._originalSize.height = size.height;
}
this._originalSizeInPixels = cc.sizePointsToPixels(size);
},
/**
@@ -305,57 +343,52 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method getTexture
* @return {Texture2D}
*/
getTexture:function () {
if (this._texture)
return this._texture;
if (this._textureFilename) {
var locTexture = this._texture = cc.textureCache.addImage(this._textureFilename);
if (locTexture)
this._textureLoaded = locTexture.isLoaded();
return locTexture;
}
return null;
getTexture: function () {
return this._texture;
},
/**
* Sets the texture of the frame, the texture is retained automatically.
* @method setTexture
* @method _refreshTexture
* @param {Texture2D} texture
*/
setTexture:function (texture) {
_refreshTexture: function (texture) {
if (this._texture !== texture) {
var locLoaded = texture.isLoaded();
this._textureLoaded = locLoaded;
this._texture = texture;
if(!locLoaded){
texture.once("load", function (event) {
var sender = event.currentTarget;
this._textureLoaded = true;
if(this._rotated && cc._renderType === cc.game.RENDER_TYPE_CANVAS){
var tempElement = sender.getHtmlElementObj();
tempElement = _ccsg.Sprite.CanvasRenderCmd._cutRotateImageToCanvas(tempElement, this.getRect());
var tempTexture = new cc.Texture2D();
tempTexture.initWithElement(tempElement);
tempTexture.handleLoadedTexture();
this.setTexture(tempTexture);
var self = this;
var textureLoadedCallback = function () {
self._textureLoaded = true;
if (self._rotated && cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
var tempElement = sender.getHtmlElementObj();
tempElement = _ccsg.Sprite.CanvasRenderCmd._cutRotateImageToCanvas(tempElement, self.getRect());
var tempTexture = new cc.Texture2D();
tempTexture.initWithElement(tempElement);
tempTexture.handleLoadedTexture();
self.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 w = sender.width, h = sender.height;
this._rect.width = w;
this._rect.height = h;
this._rectInPixels = cc.rectPointsToPixels(this._rect);
this._originalSizeInPixels.width = this._rectInPixels.width;
this._originalSizeInPixels.height = this._rectInPixels.height;
this._originalSize.width = w;
this._originalSize.height = h;
}
//dispatch 'load' event of cc.SpriteFrame
this.emit("load");
}, this);
var rect = self.getRect();
self.setRect(cc.rect(0, 0, rect.width, rect.height));
}
var w = texture.width, h = texture.height;
if (!self._rect) {
self.setRect(cc.rect(0, 0, w, h));
}
if (!self._originalSize) {
self.setOriginalSize(cc.size(w, h));
}
if (!self._offset) {
self.setOffset(cc.p(0, 0));
}
//dispatch 'load' event of cc.SpriteFrame
self.emit("load");
};
if (!locLoaded) {
texture.once("load", textureLoadedCallback, this);
} else {
textureLoadedCallback();
}
}
},
@@ -365,8 +398,13 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method getOffset
* @return {Vec2}
*/
getOffset:function () {
return cc.p(this._offset);
getOffset: function () {
var offset = this._offset;
if (offset) {
return cc.p(offset);
} else {
return cc.p(0, 0);
}
},
/**
@@ -374,9 +412,14 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method setOffset
* @param {Vec2} offsets
*/
setOffset:function (offsets) {
this._offset.x = offsets.x;
this._offset.y = offsets.y;
setOffset: function (offsets) {
if (!this._offset) {
this._offset = cc.p(offsets);
} else {
this._offset.x = offsets.x;
this._offset.y = offsets.y;
}
this._offsetInPixels = cc.pointPointsToPixels(offsets);
},
/**
@@ -384,11 +427,8 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @method clone
* @return {SpriteFrame}
*/
clone:function(){
var frame = new cc.SpriteFrame();
frame.initWithTexture(this._textureFilename, this._rectInPixels, this._rotated, this._offsetInPixels, this._originalSizeInPixels);
frame.setTexture(this._texture);
return frame;
clone: function () {
return new cc.SpriteFrame(this._textureFilename, this._rectInPixels, this._rotated, this._offsetInPixels, this._originalSizeInPixels);
},
/**
@@ -402,35 +442,45 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
* @param {Size} [originalSize=rect.size]
* @return {Boolean}
*/
initWithTexture:function (texture, rect, rotated, offset, originalSize) {
if(arguments.length === 2)
rect = cc.rectPointsToPixels(rect);
initWithTexture: function (textureOrTextureFile, rect, rotated, offset, originalSize) {
this.setTexture(textureOrTextureFile, rect, rotated, offset, originalSize);
},
offset = offset || cc.p(0, 0);
originalSize = originalSize || rect;
rotated = rotated || false;
setTexture: function (textureOrTextureFile, rect, rotated, offset, originalSize) {
if (cc.js.isString(texture)){
this._texture = null;
this._textureFilename = texture;
} else if (texture instanceof cc.Texture2D) {
this.setTexture(texture);
if (rect) {
this.setRectInPixels(rect);
} else {
this._rect = this._rectInPixels = null;
}
// texture will be init in getTexture()
texture = this.getTexture();
if (offset) {
this.setOffsetInPixels(offset);
} else {
this._offset = this._offsetInPixels = null;
}
this._rectInPixels = rect;
this._rect = cc.rectPixelsToPoints(rect);
this._offsetInPixels.x = offset.x;
this._offsetInPixels.y = offset.y;
cc._pointPixelsToPointsOut(offset, this._offset);
this._originalSizeInPixels.width = originalSize.width;
this._originalSizeInPixels.height = originalSize.height;
cc._sizePixelsToPointsOut(originalSize, this._originalSize);
this._rotated = rotated;
if (originalSize) {
this.setOriginalSizeInPixels(originalSize);
} else {
this._originalSize = this._originalSizeInPixels = null;
}
if(texture && texture.url && texture.isLoaded()) {
this._rotated = rotated || false;
//loading texture
var texture = textureOrTextureFile;
if (cc.js.isString(texture)) {
this._textureFilename = texture;
texture = cc.textureCache.addImage(texture);
}
if (texture instanceof cc.Texture2D) {
this._refreshTexture(texture);
} else {
//todo log error
}
if (texture && texture.url && texture.isLoaded()) {
this._checkRect(texture);
}
@@ -483,9 +533,9 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
name: this._name,
texture: uuid || undefined,
atlas: exporting ? undefined : this._atlasUuid, // strip from json if exporting
rect: [rect.x, rect.y, rect.width, rect.height],
offset: [offset.x, offset.y],
originalSize: [size.width, size.height],
rect: rect ? [rect.x, rect.y, rect.width, rect.height] : undefined,
offset: offset ? [offset.x, offset.y] : undefined,
originalSize: size ? [size.width, size.height] : undefined,
rotated: this._rotated ? 1 : 0,
capInsets: capInsets
};
@@ -493,12 +543,15 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
_deserialize: function (data, handle) {
var rect = data.rect;
this._rect = new cc.Rect(rect[0], rect[1], rect[2], rect[3]);
this._rectInPixels = cc.rectPointsToPixels(this._rect);
this._offset = new cc.Vec2(data.offset[0], data.offset[1]);
this._offsetInPixels = cc.pointPointsToPixels(this._offset);
this._originalSize = new cc.Size(data.originalSize[0], data.originalSize[1]);
this._originalSizeInPixels = cc.sizePointsToPixels(this._originalSize);
if (rect) {
this.setRect(new cc.Rect(rect[0], rect[1], rect[2], rect[3]));
}
if (data.offset) {
this.setOffset(new cc.Vec2(data.offset[0], data.offset[1]));
}
if (data.originalSize) {
this.setOriginalSize(new cc.Size(data.originalSize[0], data.originalSize[1]));
}
this._rotated = data.rotated === 1;
this._name = data.name;
var capInsets = data.capInsets;
@@ -517,7 +570,7 @@ cc.SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
if (textureUuid) {
handle.result.push(this, '_textureFilenameSetter', textureUuid);
}
},
}
});
var proto = cc.SpriteFrame.prototype;
@@ -551,7 +604,7 @@ proto.copy = proto.clone;
* @return {cc.SpriteFrame}
*/
cc.SpriteFrame.create = function (filename, rect, rotated, offset, originalSize) {
return new cc.SpriteFrame(filename,rect,rotated,offset,originalSize);
return new cc.SpriteFrame(filename, rect, rotated, offset, originalSize);
};
/**
@@ -562,16 +615,5 @@ cc.SpriteFrame.create = function (filename, rect, rotated, offset, originalSize)
cc.SpriteFrame.createWithTexture = cc.SpriteFrame.create;
cc.SpriteFrame._frameWithTextureForCanvas = function (texture, rect, rotated, offset, originalSize) {
var spriteFrame = new cc.SpriteFrame();
spriteFrame._texture = texture;
spriteFrame._rectInPixels = rect;
spriteFrame._rect = cc.rectPixelsToPoints(rect);
spriteFrame._offsetInPixels.x = offset.x;
spriteFrame._offsetInPixels.y = offset.y;
cc._pointPixelsToPointsOut(spriteFrame._offsetInPixels, spriteFrame._offset);
spriteFrame._originalSizeInPixels.width = originalSize.width;
spriteFrame._originalSizeInPixels.height = originalSize.height;
cc._sizePixelsToPointsOut(spriteFrame._originalSizeInPixels, spriteFrame._originalSize);
spriteFrame._rotated = rotated;
return spriteFrame;
return new cc.SpriteFrame(texture, rect, rotated, offset, originalSize);
};
+72 -202
View File
@@ -52,7 +52,7 @@ EventTarget = require("../cocos2d/core/event/event-target");
*/
ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
//resource data, could be async loaded.
_resourceData: null,
_spriteFrame: null,
//scale 9 data
_insetLeft: 0,
@@ -69,32 +69,32 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
_quads: [],
_quadsDirty: true,
ctor: function (fileOrData) {
ctor: function (textureOrSpriteFrame) {
_ccsg.Node.prototype.ctor.call(this);
this._renderCmd.setState(this._brightState);
this._blendFunc = cc.BlendFunc._alphaNonPremultiplied();
this.setAnchorPoint(cc.p(0.5,0.5));
//todo
if(fileOrData instanceof cc.Scale9Sprite.Scale9ResourceData) {
this.initWithResourceData(fileOrData);
}
else {
if(typeof fileOrData === 'string') {
var frame = cc.spriteFrameCache.getSpriteFrame(fileOrData);
if(frame) {
this.initWithSpriteFrame(frame);
} else {
this.initWithTexture(fileOrData);
}
this.setAnchorPoint(cc.p(0.5, 0.5));
//
if (typeof textureOrSpriteFrame === 'string') {
var frame = cc.spriteFrameCache.getSpriteFrame(textureOrSpriteFrame);
if (frame) {
this.initWithSpriteFrame(frame);
} else {
this.initWithTexture(textureOrSpriteFrame);
}
} else if (textureOrSpriteFrame instanceof cc.SpriteFrame) {
this.initWithSpriteFrame(textureOrSpriteFrame);
}
else if (textureOrSpriteFrame instanceof cc.Texture2D) {
this.initWithTexture(textureOrSpriteFrame);
}
},
loaded: function () {
if (this._resourceData === null) {
if (this._spriteFrame === null) {
return false;
} else {
return this._resourceData.loaded();
return this._spriteFrame.textureLoaded();
}
},
@@ -115,19 +115,14 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
this.setSpriteFrame(spriteFrameOrSFName);
},
initWithResourceData: function (s9ResData) {
this.setResourceData(s9ResData);
},
/**
* Change the texture file of 9 slice sprite
*
* @param textureOrTextureFile The name of the texture file.
*/
setTexture: function (textureOrTextureFile) {
var resourceData = new cc.Scale9Sprite.Scale9ResourceData();
resourceData.initWithTexture(textureOrTextureFile);
this.setResourceData(resourceData);
var spriteFrame = cc.SpriteFrame.createWithTexture(textureOrTextureFile);
this.setSpriteFrame(spriteFrame);
},
/**
@@ -135,29 +130,30 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
*
* @param spriteFrameOrSFFileName The name of the texture file.
*/
setSpriteFrame: function (spriteFrameOrSFFileName) {
var resourceData = new cc.Scale9Sprite.Scale9ResourceData();
resourceData.initWithSpriteFrame(spriteFrameOrSFFileName);
this.setResourceData(resourceData);
},
setResourceData: function (s9ResData) {
if (s9ResData instanceof cc.Scale9Sprite.Scale9ResourceData) {
this._resourceData = s9ResData;
this._quadsDirty = true;
var self = this;
var onResourceDataLoaded = function() {
if(cc.sizeEqualToSize(self._contentSize, cc.size(0,0))) {
self.setContentSize(self._resourceData._originalSize);
}
};
if(s9ResData.loaded()) {
onResourceDataLoaded();
} else {
s9ResData.once('load', onResourceDataLoaded, this);
}
setSpriteFrame: function (spriteFrameOrSFName) {
var spriteFrame;
if (spriteFrameOrSFName instanceof cc.SpriteFrame) {
spriteFrame = spriteFrameOrSFName;
}
else {
spriteFrame = cc.spriteFrameCache.getSpriteFrame(spriteFrameOrSFName);
}
if (spriteFrame) {
this._spriteFrame = spriteFrame;
this._quadsDirty = true;
var self = this;
var onResourceDataLoaded = function () {
if (cc.sizeEqualToSize(self._contentSize, cc.size(0, 0))) {
self.setContentSize(self._spriteFrame.getRect());
}
};
if (spriteFrame.textureLoaded()) {
onResourceDataLoaded();
} else {
spriteFrame.once('load', onResourceDataLoaded, this);
}
}
},
/**
@@ -186,7 +182,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
},
// overrides
setContentSize : function(width, height){
setContentSize: function (width, height) {
if (height === undefined) {
height = width.height;
width = width.width;
@@ -204,7 +200,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* @see `State`
* @param state A enum value in State.
*/
setState : function(state){
setState: function (state) {
this._brightState = state;
this._renderCmd.setState(state);
},
@@ -213,7 +209,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* Query the current bright state.
* @return @see `State`
*/
getState : function(){
getState: function () {
return this._brightState;
},
@@ -221,8 +217,8 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* change the rendering type, could be simple or slice
* @return @see `RenderingType`
*/
setRenderingType: function(type) {
if(this._renderingType == type) return;
setRenderingType: function (type) {
if (this._renderingType == type) return;
this._renderingType = type;
this._quadsDirty = true;
},
@@ -230,14 +226,14 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* get the rendering type, could be simple or slice
* @return @see `RenderingType`
*/
getRenderingType: function() {
getRenderingType: function () {
return this._renderingType;
},
/**
* change the left border of 9 slice sprite, it should be specified before trimmed.
* @param insetLeft left border.
*/
setInsetLeft : function(insetLeft){
setInsetLeft: function (insetLeft) {
this._insetLeft = insetLeft;
this._quadsDirty = true;
},
@@ -245,14 +241,14 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* get the left border of 9 slice sprite, the result is specified before trimmed.
* @return left border.
*/
getInsetLeft : function(){
getInsetLeft: function () {
return this._insetLeft;
},
/**
* change the top border of 9 slice sprite, it should be specified before trimmed.
* @param insetTop top border.
*/
setInsetTop : function(insetTop){
setInsetTop: function (insetTop) {
this._insetTop = insetTop;
this._quadsDirty = true;
},
@@ -261,7 +257,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* get the top border of 9 slice sprite, the result is specified before trimmed.
* @return top border.
*/
getInsetTop : function(){
getInsetTop: function () {
return this._insetTop;
},
@@ -269,7 +265,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* change the right border of 9 slice sprite, it should be specified before trimmed.
* @param insetRight right border.
*/
setInsetRight : function(insetRight){
setInsetRight: function (insetRight) {
this._insetRight = insetRight;
this._quadsDirty = true;
},
@@ -278,7 +274,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* get the right border of 9 slice sprite, the result is specified before trimmed.
* @return right border.
*/
getInsetRight : function(){
getInsetRight: function () {
return this._insetRight;
},
@@ -286,8 +282,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* change the bottom border of 9 slice sprite, it should be specified before trimmed.
* @param insetBottom bottom border.
*/
setInsetBottom : function(insetBottom)
{
setInsetBottom: function (insetBottom) {
this._insetBottom = insetBottom;
this._quadsDirty = true;
},
@@ -295,13 +290,13 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
* get the bottom border of 9 slice sprite, the result is specified before trimmed.
* @return bottom border.
*/
getInsetBottom : function(){
getInsetBottom: function () {
return this._insetBottom;
},
_rebuildQuads: function () {
if (!this.loaded() || this._quadsDirty === false) return;
var resourceData = this._resourceData;
var spriteFrame = this._spriteFrame;
var color = this.getDisplayedColor();
color.a = this.getDisplayedOpacity();
@@ -327,7 +322,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
quad._tl.vertices = new cc.Vertex3F(vertices[0].x, vertices[3].y, 0);
quad._tr.vertices = new cc.Vertex3F(vertices[3].x, vertices[3].y, 0);
if (!resourceData._rotated) {
if (!spriteFrame._rotated) {
quad._bl.texCoords = new cc.Tex2F(uvs[0].x, uvs[0].y);
quad._br.texCoords = new cc.Tex2F(uvs[3].x, uvs[0].y);
quad._tl.texCoords = new cc.Tex2F(uvs[0].x, uvs[3].y);
@@ -354,7 +349,7 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
quad._tl.vertices = new cc.Vertex3F(vertices[i].x, vertices[j + 1].y, 0);
quad._tr.vertices = new cc.Vertex3F(vertices[i + 1].x, vertices[j + 1].y, 0);
if (!resourceData._rotated) {
if (!spriteFrame._rotated) {
quad._bl.texCoords = new cc.Tex2F(uvs[i].x, uvs[j].y);
quad._br.texCoords = new cc.Tex2F(uvs[i + 1].x, uvs[j].y);
quad._tl.texCoords = new cc.Tex2F(uvs[i].x, uvs[j + 1].y);
@@ -377,14 +372,15 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
var leftWidth, centerWidth, rightWidth;
var topHeight, centerHeight, bottomHeight;
var resourceData = this._resourceData;
var spriteFrame = this._spriteFrame;
var rect = spriteFrame._rect;
leftWidth = this._insetLeft;
rightWidth = this._insetRight;
centerWidth = resourceData._spriteRect.width - leftWidth - rightWidth;
centerWidth = rect.width - leftWidth - rightWidth;
topHeight = this._insetTop;
bottomHeight = this._insetBottom;
centerHeight = resourceData._spriteRect.height - topHeight - bottomHeight;
centerHeight = rect.height - topHeight - bottomHeight;
var preferSize = this.getContentSize();
var sizableWidth = preferSize.width - leftWidth - rightWidth;
@@ -427,28 +423,29 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
},
_calculateUVs: function () {
var resourceData = this._resourceData;
var atlasWidth = resourceData._texture.getPixelWidth();
var atlasHeight = resourceData._texture.getPixelHeight();
var spriteFrame = this._spriteFrame;
var rect = spriteFrame._rect;
var atlasWidth = spriteFrame._texture.getPixelWidth();
var atlasHeight = spriteFrame._texture.getPixelHeight();
//caculate texture coordinate
var leftWidth, centerWidth, rightWidth;
var topHeight, centerHeight, bottomHeight;
leftWidth = this._insetLeft;
rightWidth = this._insetRight;
centerWidth = resourceData._spriteRect.width - leftWidth - rightWidth;
centerWidth = rect.width - leftWidth - rightWidth;
topHeight = this._insetTop;
bottomHeight = this._insetBottom;
centerHeight = resourceData._spriteRect.height - topHeight - bottomHeight;
centerHeight = rect.height - topHeight - bottomHeight;
var textureRect = cc.rectPointsToPixels(resourceData._spriteRect);
var textureRect = cc.rectPointsToPixels(spriteFrame.getRect());
//uv computation should take spritesheet into account.
var u0, u1, u2, u3;
var v0, v1, v2, v3;
if (resourceData._rotated) {
if (spriteFrame._rotated) {
u0 = textureRect.x / atlasWidth;
u1 = (bottomHeight + textureRect.x) / atlasWidth;
u2 = (bottomHeight + centerHeight + textureRect.x) / atlasWidth;
@@ -480,12 +477,12 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
return uvCoordinates;
},
_onColorOpacityDirty : function() {
_onColorOpacityDirty: function () {
var color = this.getDisplayedColor();
color.a = this.getDisplayedOpacity();
var index;
var quadLength = this._quads.length;
for(index = 0; index < quadLength; ++index) {
for (index = 0; index < quadLength; ++index) {
//svar quad = this._quads[index];
this._quads[index]._bl.colors = color;
this._quads[index]._br.colors = color;
@@ -503,133 +500,6 @@ ccui.Scale9Sprite = cc.Scale9Sprite = _ccsg.Node.extend({
});
ccui.Scale9Sprite.Scale9ResourceData = cc.Scale9Sprite.Scale9ResourceData = function ()
{
this._texture = null;
this._spriteRect = null;
this._rotated = false;
this._trimmedOffset = null;
this._originalSize = null;
this._loaded = false;
var trimmedResult = [0,0,0,0]; //left, right, bottom, top
var trimmedDirty = true;
this.loaded = function() {
return this._loaded;
};
this.computeTrimmed = function () {
if(this._loaded && trimmedDirty) {
trimmedResult = [];
var result = trimmedResult;
var leftTrim = (this._originalSize.width + (2 * this._trimmedOffset.x) - this._spriteRect.width)/2;
var rightTrim = this._originalSize.width - leftTrim - this._spriteRect.width;
var bottomTrim = (this._originalSize.height + (2 * this._trimmedOffset.y) - this._spriteRect.height) /2;
var topTrim = this._originalSize.height - bottomTrim - this._spriteRect.height;
result.push(leftTrim);
result.push(rightTrim);
result.push(bottomTrim);
result.push(topTrim);
trimmedDirty = false;
}
};
this.getTrimmedLeft = function () {
this.computeTrimmed();
return trimmedResult[0];
};
this.getTrimmedRight = function () {
this.computeTrimmed();
return trimmedResult[1];
};
this.getTrimmedBottom = function () {
this.computeTrimmed();
return trimmedResult[2];
};
this.getTrimmedTop = function () {
this.computeTrimmed();
return trimmedResult[3];
};
/**
textureOrTextureFile: texture handle or texture file image;
Other params is optional
*/
this.initWithTexture = function(textureOrTextureFile, spriteRect, rotated, trimmedOffset, originalSize){
var texture;
if(textureOrTextureFile instanceof cc.Texture2D) {
texture = textureOrTextureFile;
} else {
texture = cc.textureCache.addImage(textureOrTextureFile);
}
if(!texture) {
throw new Error("Scale9 Sprite can not be inited with a null texture");
}
this._texture = texture;
this._spriteRect = spriteRect || null;
this._rotated = rotated || false;
this._trimmedOffset = trimmedOffset || cc.p(0,0);
this._originalSize = originalSize || null;
if(texture) {
var self = this;
var textureLoadedCallback = function() {
self._loaded = true;
var texturesize = texture.getContentSize();
if(self._spriteRect === null) {
self._spriteRect = cc.rect(0,0, texturesize.width, texturesize.height);
}
if(self._originalSize === null) {
self._originalSize = cc.size(texturesize);
}
self.emit('load');
};
if(texture.isLoaded()) {
textureLoadedCallback();
} else {
texture.once("load",textureLoadedCallback);
}
trimmedDirty = true;
}
return true;
};
this.initWithSpriteFrame = function(SpriteFrameOrSFName){
var spriteFrame;
if(SpriteFrameOrSFName instanceof cc.SpriteFrame) {
spriteFrame = SpriteFrameOrSFName;
}
else {
spriteFrame = cc.spriteFrameCache.getSpriteFrame(SpriteFrameOrSFName);
}
if(spriteFrame) {
var self = this;
var spriteFrameLoadedCallback = function() {
self._loaded = true;
self._texture = spriteFrame.getTexture();
self._spriteRect = spriteFrame.getRect();
self._rotated = spriteFrame.isRotated();
self._trimmedOffset = spriteFrame.getOffset();
self._originalSize = spriteFrame.getOriginalSize();
self.emit('load');
};
if(spriteFrame.textureLoaded()){
spriteFrameLoadedCallback();
} else {
spriteFrame.once("load",spriteFrameLoadedCallback);
}
trimmedDirty = true;
}
return true;
};
};
cc.js.mixin(ccui.Scale9Sprite.Scale9ResourceData.prototype, cc.EventTarget.prototype);
var _p = ccui.Scale9Sprite.prototype;
cc.js.addon(_p, EventTarget.prototype);
@@ -56,7 +56,7 @@
var locDisplayOpacity = this._displayedOpacity;
var alpha = locDisplayOpacity/ 255;
var locTexture = null;
if(node._resourceData) locTexture = node._resourceData._texture;
if(node._spriteFrame) locTexture = node._spriteFrame._texture;
if (!node.loaded() || locDisplayOpacity === 0)
return;
if(this._textureToRender === null){
@@ -40,7 +40,7 @@
var node = this._node;
var locTexture = null;
if(node._resourceData) locTexture = node._resourceData._texture;
if(node._spriteFrame) locTexture = node._spriteFrame._texture;
if (!node.loaded() || this._displayedOpacity === 0)
return;
var needRebuildWebBuffer = false;