diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 385b9f84bb..bfccda855a 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -23,7 +23,23 @@ ****************************************************************************/ var SpriteType = cc.SpriteType; - +/** + * Sprite Size can track trimmed size, raw size or none + */ +var SizeMode = cc.Enum({ + /** + * @property {Number} CUSTOM + */ + CUSTOM: 0, + /** + * @property {Number} TRIMMED + */ + TRIMMED: 1, + /** + * @property {Number} RAW + */ + RAW: 2 +}); /** * Renders a sprite in the scene. * @class Sprite @@ -44,8 +60,10 @@ var Sprite = cc.Class({ type: cc.SpriteFrame }, _type: SpriteType.SIMPLE, + //FIXME:_useOriginalSize is deprecated, since v0.8, it need to be deleted _useOriginalSize: true, - + _sizeMode: -1, + _isTrimmedMode: true, /** * The Sprite Atlas. * @property _atlas @@ -99,7 +117,24 @@ var Sprite = cc.Class({ type: SpriteType, tooltip: 'i18n:COMPONENT.sprite.type', }, + /** + * specify the rendering mode + * @property isTrimmedMode + * @type {Boolean} + */ + isTrimmedMode: { + get: function () { + return this._isTrimmedMode; + }, + set: function (value) { + if (this._isTrimmedMode !== value) { + this._isTrimmedMode = value; + this._sgNode.enableTrimmedContentSize(value); + } + } + }, + //FIXME:_useOriginalSize is deprecated, since v0.8, it need to be deleted useOriginalSize: { get: function () { return this._useOriginalSize; @@ -112,7 +147,23 @@ var Sprite = cc.Class({ }, tooltip: 'i18n:COMPONENT.sprite.original_size', }, - + /** + * specify the size tracing mode + * @property sizeMode + * @type {SizeMode} + */ + sizeMode: { + get: function () { + return this._sizeMode; + }, + set: function (value) { + this._sizeMode = value; + if (value !== SizeMode.CUSTOM) { + this._applySpriteSize(); + } + }, + type: SizeMode + }, /** * Only for editor to calculate bounding box. */ @@ -145,7 +196,7 @@ var Sprite = cc.Class({ * @method setScale9Enabled * @param {Boolean} enabled - True to enable 9-slice, false otherwise. */ - setScale9Enabled: function(enabled) { + setScale9Enabled: function (enabled) { this.type = enabled ? cc.SpriteType.SLICED : cc.SpriteType.SIMPLE; }, @@ -154,7 +205,7 @@ var Sprite = cc.Class({ * @method isScale9Enabled * @return {Boolean} True if 9-slice is enabled, false otherwise. */ - isScale9Enabled: function(){ + isScale9Enabled: function () { return this.type === cc.SpriteType.SLICED; }, @@ -224,7 +275,7 @@ var Sprite = cc.Class({ * @method setInsetLeft * @param {Number} leftInset - The values to use for the cap inset. */ - setInsetLeft: function(insetLeft){ + setInsetLeft: function (insetLeft) { this._sgNode.setInsetLeft(insetLeft); }, @@ -233,7 +284,7 @@ var Sprite = cc.Class({ * @method getInsetLeft * @return {Number} The left sprite's cap inset. */ - getInsetLeft: function(){ + getInsetLeft: function () { return this._sgNode.getInsetLeft(); }, @@ -242,7 +293,7 @@ var Sprite = cc.Class({ * @method setInsetTop * @param {Number} topInset - The values to use for the cap inset. */ - setInsetTop: function(insetTop){ + setInsetTop: function (insetTop) { this._sgNode.setInsetTop(insetTop); }, @@ -251,7 +302,7 @@ var Sprite = cc.Class({ * @method getInsetTop * @return {Number} The top sprite's cap inset. */ - getInsetTop: function(){ + getInsetTop: function () { return this._sgNode.getInsetTop(); }, @@ -260,7 +311,7 @@ var Sprite = cc.Class({ * @method setInsetRight * @param {Number} rightInset - The values to use for the cap inset. */ - setInsetRight: function(insetRight){ + setInsetRight: function (insetRight) { this._sgNode.setInsetRight(insetRight); }, @@ -269,7 +320,7 @@ var Sprite = cc.Class({ * @method getInsetRight * @return {Number} The right sprite's cap inset. */ - getInsetRight: function(){ + getInsetRight: function () { return this._sgNode.getInsetRight(); }, @@ -278,7 +329,7 @@ var Sprite = cc.Class({ * @method setInsetBottom * @param {Number} bottomInset - The values to use for the cap inset. */ - setInsetBottom: function(insetBottom) { + setInsetBottom: function (insetBottom) { this._sgNode.setInsetBottom(insetBottom); }, @@ -287,12 +338,22 @@ var Sprite = cc.Class({ * @method getInsetBottom * @return {Number} The bottom sprite's cap inset. */ - getInsetBottom: function(){ + getInsetBottom: function () { return this._sgNode.getInsetBottom(); }, onLoad: function () { this._super(); + //do processing + if (-1 === this._sizeMode) { + //FIXME:_useOriginalSize is deprecated, since v0.8, it need to be deleted + if (this._useOriginalSize) { + this._sizeMode = SizeMode.TRIMMED; + } else { + this._sizeMode = SizeMode.CUSTOM; + } + this._isTrimmedMode = true; + } this.node.on('size-changed', this._resized, this); }, @@ -301,11 +362,11 @@ var Sprite = cc.Class({ this.node.off('size-changed', this._resized, this); }, - _applyAtlas: CC_EDITOR && function ( spriteFrame ) { + _applyAtlas: CC_EDITOR && function (spriteFrame) { // Set atlas if (spriteFrame && spriteFrame._atlasUuid) { var self = this; - cc.AssetLibrary.loadAsset(spriteFrame._atlasUuid, function(err, asset) { + cc.AssetLibrary.loadAsset(spriteFrame._atlasUuid, function (err, asset) { self._atlas = asset; }); } else { @@ -324,11 +385,15 @@ var Sprite = cc.Class({ }, _applySpriteSize: function () { - if (this._useOriginalSize && this._spriteFrame) { + if (SizeMode.CUSTOM === this._sizeMode || !this._spriteFrame) { + this.node.setContentSize(this.node.getContentSize(true)); + } else if (SizeMode.RAW === this._sizeMode) { + var size = this._spriteFrame.getOriginalSize(); + this.node.setContentSize(size); + } else if (SizeMode.TRIMMED === this._sizeMode) { var rect = this._spriteFrame.getRect(); this.node.setContentSize(cc.size(rect.width, rect.height)); - } - else { + } else { this.node.setContentSize(this.node.getContentSize(true)); } }, @@ -339,7 +404,7 @@ var Sprite = cc.Class({ sgNode.setSpriteFrame(self._spriteFrame); self._applyCapInset(); self._applySpriteSize(); - if ( this.enabledInHierarchy && !sgNode.isVisible() ) { + if (this.enabledInHierarchy && !sgNode.isVisible()) { sgNode.setVisible(true); } }, @@ -375,7 +440,7 @@ var Sprite = cc.Class({ _initSgNode: function () { var sgNode = this._sgNode; - if ( !this.enabledInHierarchy ) { + if (!this.enabledInHierarchy) { sgNode.setVisible(false); } @@ -385,18 +450,30 @@ var Sprite = cc.Class({ // otherwise setContentSize may not take effect sgNode.setContentSize(this.node.getContentSize(true)); this._applySpriteSize(); - + sgNode.setRenderingType(this._type); }, _resized: function () { - if (this._useOriginalSize && this._spriteFrame) { - var rect = this._spriteFrame.getRect(); - var expectedW = rect.width; - var expectedH = rect.height; + if (this._spriteFrame) { var actualSize = this.node.getContentSize(); + var expectedW = actualSize.width; + var expectedH = actualSize.height; + if (this._sizeMode === SizeMode.RAW) { + var size = this._spriteFrame.getOriginalSize(); + expectedW = size.width; + expectedH = size.height; + } else if (this._sizeMode === SizeMode.TRIMMED) { + var rect = this._spriteFrame.getRect(); + expectedW = rect.width; + expectedH = rect.height; + + } else { + + } + if (expectedW !== actualSize.width || expectedH !== actualSize.height) { - this.useOriginalSize = false; + this._sizeMode = SizeMode.CUSTOM; } } }, @@ -405,7 +482,7 @@ var Sprite = cc.Class({ var misc = require('../utils/misc'); var SameNameGetSets = ['atlas', 'capInsets', 'insetLeft', 'insetTop', 'insetRight', 'insetBottom']; var DiffNameGetSets = { - type: [ null, 'setRenderingType'] + type: [null, 'setRenderingType'] }; misc.propertyDefine(Sprite, SameNameGetSets, DiffNameGetSets); diff --git a/cocos2d/core/sprites/CCScale9Sprite.js b/cocos2d/core/sprites/CCScale9Sprite.js index 0f1c2483be..c343daa9a2 100644 --- a/cocos2d/core/sprites/CCScale9Sprite.js +++ b/cocos2d/core/sprites/CCScale9Sprite.js @@ -51,10 +51,10 @@ EventTarget = require("../cocos2d/core/event/event-target"); * @property {Number} insetBottom - The bottom inset of the 9-slice sprite */ cc.SimpleQuadGenerator = { - _rebuildQuads_base : function (spriteFrame, contentSize, colorOpacity) { + _rebuildQuads_base: function (spriteFrame, contentSize, colorOpacity, isTrimmedContentSize) { var quads = []; //build vertices - var vertices = this._calculateVertices(spriteFrame, contentSize); + var vertices = this._calculateVertices(spriteFrame, contentSize, isTrimmedContentSize); //build uvs var uvs = this._calculateUVs(spriteFrame); @@ -88,15 +88,31 @@ cc.SimpleQuadGenerator = { return quads; }, - _calculateVertices : function (spriteFrame, contentSize) { + _calculateVertices: function (spriteFrame, contentSize, isTrimmedContentSize) { + var x0, x3; + var y0, y3; + if (isTrimmedContentSize) { + x0 = 0; + x3 = contentSize.width; - var x0,x3; - var y0,y3; - x0 = 0; - x3 = contentSize.width; + y0 = 0; + y3 = contentSize.height; + } else { + var originalSize = spriteFrame.getOriginalSize(); + var rect = spriteFrame.getRect(); + var offset = spriteFrame.getOffset(); + var scaleX = contentSize.width / originalSize.width; + var scaleY = contentSize.height / originalSize.height; + var trimmLeft = offset.x + (originalSize.width - rect.width) / 2; + var trimmRight = offset.x - (originalSize.width - rect.width) / 2; + var trimmedBottom = offset.y + (originalSize.height - rect.height) / 2; + var trimmedTop = offset.y - (originalSize.height - rect.height) / 2; - y0 = 0; - y3 = contentSize.height; + x0 = trimmLeft * scaleX; + x3 = contentSize.width + trimmRight * scaleX; + y0 = trimmedBottom * scaleY; + y3 = contentSize.height + trimmedTop * scaleY; + } //apply contentscale factor x0 = x0 / cc.contentScaleFactor(); @@ -111,7 +127,7 @@ cc.SimpleQuadGenerator = { return vertices; }, - _calculateUVs : function (spriteFrame) { + _calculateUVs: function (spriteFrame) { var atlasWidth = spriteFrame._texture.getPixelWidth(); var atlasHeight = spriteFrame._texture.getPixelHeight(); @@ -145,7 +161,7 @@ cc.SimpleQuadGenerator = { }; cc.Scale9QuadGenerator = { - _rebuildQuads_base : function (spriteFrame, contentSize, colorOpacity,insetLeft, insetRight, insetTop, insetBottom) { + _rebuildQuads_base: function (spriteFrame, contentSize, colorOpacity, insetLeft, insetRight, insetTop, insetBottom) { var quads = []; //build vertices var vertices = this._calculateVertices(spriteFrame, contentSize, insetLeft, insetRight, insetTop, insetBottom); @@ -186,7 +202,7 @@ cc.Scale9QuadGenerator = { return quads; }, - _calculateVertices : function (spriteFrame, contentSize, insetLeft, insetRight, insetTop, insetBottom) { + _calculateVertices: function (spriteFrame, contentSize, insetLeft, insetRight, insetTop, insetBottom) { var leftWidth, centerWidth, rightWidth; var topHeight, centerHeight, bottomHeight; @@ -239,7 +255,7 @@ cc.Scale9QuadGenerator = { return vertices; }, - _calculateUVs : function (spriteFrame, insetLeft, insetRight, insetTop, insetBottom) { + _calculateUVs: function (spriteFrame, insetLeft, insetRight, insetTop, insetBottom) { var rect = spriteFrame._rect; var atlasWidth = spriteFrame._texture.getPixelWidth(); var atlasHeight = spriteFrame._texture.getPixelHeight(); @@ -295,7 +311,7 @@ cc.Scale9QuadGenerator = { }; cc.TiledQuadGenerator = { - _rebuildQuads_base : function (spriteFrame, contentSize, colorOpacity) { + _rebuildQuads_base: function (spriteFrame, contentSize, colorOpacity) { var quads = []; //build uvs @@ -309,8 +325,8 @@ cc.TiledQuadGenerator = { //build quads var hRepeat = contentSize.width / rectWidth; var vRepeat = contentSize.height / rectHeight; - for(var hindex = 0; hindex < Math.ceil(hRepeat); ++hindex) { - for(var vindex = 0; vindex < Math.ceil(vRepeat); ++vindex) { + for (var hindex = 0; hindex < Math.ceil(hRepeat); ++hindex) { + for (var vindex = 0; vindex < Math.ceil(vRepeat); ++vindex) { var quad; quad = new cc.V3F_C4B_T2F_Quad(); @@ -320,20 +336,20 @@ cc.TiledQuadGenerator = { quad._tr.colors = colorOpacity; quad._bl.vertices = new cc.Vertex3F(rectWidth * hindex, rectHeight * vindex, 0); - quad._br.vertices = new cc.Vertex3F(rectWidth * Math.min(hindex+1, hRepeat), rectHeight * vindex, 0); - quad._tl.vertices = new cc.Vertex3F(rectWidth * hindex, rectHeight * Math.min(vindex+1, vRepeat), 0); - quad._tr.vertices = new cc.Vertex3F(rectWidth * Math.min(hindex+1, hRepeat), rectHeight * Math.min(vindex+1, vRepeat), 0); + quad._br.vertices = new cc.Vertex3F(rectWidth * Math.min(hindex + 1, hRepeat), rectHeight * vindex, 0); + quad._tl.vertices = new cc.Vertex3F(rectWidth * hindex, rectHeight * Math.min(vindex + 1, vRepeat), 0); + quad._tr.vertices = new cc.Vertex3F(rectWidth * Math.min(hindex + 1, hRepeat), rectHeight * Math.min(vindex + 1, vRepeat), 0); if (!spriteFrame._rotated) { quad._bl.texCoords = new cc.Tex2F(u0, v0); - quad._br.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1,hRepeat - hindex), v0); - quad._tl.texCoords = new cc.Tex2F(u0, v0 + (v1 - v0) * Math.min(1,vRepeat - vindex)); - quad._tr.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1,hRepeat - hindex), v0 + (v1 - v0) * Math.min(1,vRepeat - vindex)); + quad._br.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1, hRepeat - hindex), v0); + quad._tl.texCoords = new cc.Tex2F(u0, v0 + (v1 - v0) * Math.min(1, vRepeat - vindex)); + quad._tr.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1, hRepeat - hindex), v0 + (v1 - v0) * Math.min(1, vRepeat - vindex)); } else { quad._bl.texCoords = new cc.Tex2F(u0, v1); - quad._br.texCoords = new cc.Tex2F(u0, v1 + (v0-v1) * Math.min(1,hRepeat - hindex)); - quad._tl.texCoords = new cc.Tex2F(u0 + (u1-u0) * Math.min(1,vRepeat - vindex), v1); - quad._tr.texCoords = new cc.Tex2F(u0 + (u1-u0) * Math.min(1,vRepeat - vindex), v1 + (v0-v1) * Math.min(1,hRepeat - hindex)); + quad._br.texCoords = new cc.Tex2F(u0, v1 + (v0 - v1) * Math.min(1, hRepeat - hindex)); + quad._tl.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1, vRepeat - vindex), v1); + quad._tr.texCoords = new cc.Tex2F(u0 + (u1 - u0) * Math.min(1, vRepeat - vindex), v1 + (v0 - v1) * Math.min(1, hRepeat - hindex)); } quads.push(quad); @@ -342,7 +358,7 @@ cc.TiledQuadGenerator = { return quads; }, - _calculateUVs : function (spriteFrame) { + _calculateUVs: function (spriteFrame) { var atlasWidth = spriteFrame._texture.getPixelWidth(); var atlasHeight = spriteFrame._texture.getPixelHeight(); @@ -396,6 +412,8 @@ cc.Scale9Sprite = _ccsg.Node.extend({ _quads: [], _quadsDirty: true, + _isTrimmedContentSize: true, + ctor: function (textureOrSpriteFrame) { _ccsg.Node.prototype.ctor.call(this); this._renderCmd.setState(this._brightState); @@ -522,6 +540,17 @@ cc.Scale9Sprite = _ccsg.Node.extend({ this._quadsDirty = true; }, + // + enableTrimmedContentSize: function (isTrimmed) { + if (this._isTrimmedContentSize !== isTrimmed) { + this._isTrimmedContentSize = isTrimmed; + this._quadsDirty = true; + } + }, + + isTrimmedContentSizeEnabled: function () { + return this._isTrimmedContentSize; + }, /** * Change the state of 9-slice sprite. * @see `State` @@ -634,12 +663,13 @@ cc.Scale9Sprite = _ccsg.Node.extend({ this._quads[index]._tr.colors = color; } }, - _rebuildQuads : function () { + + _rebuildQuads: function () { if (!this.loaded() || this._quadsDirty === false) return; var color = this.getDisplayedColor(); color.a = this.getDisplayedOpacity(); if (this._renderingType === cc.Scale9Sprite.RenderingType.SIMPLE) { - this._quads = cc.SimpleQuadGenerator._rebuildQuads_base(this._spriteFrame, this.getContentSize(), color); + this._quads = cc.SimpleQuadGenerator._rebuildQuads_base(this._spriteFrame, this.getContentSize(), color, this._isTrimmedContentSize); } else if (this._renderingType === cc.Scale9Sprite.RenderingType.SLICED) { this._quads = cc.Scale9QuadGenerator._rebuildQuads_base(this._spriteFrame, this.getContentSize(), color, this._insetLeft, this._insetRight, this._insetTop, this._insetBottom); } else if (this._renderingType === cc.Scale9Sprite.RenderingType.TILED) { @@ -725,7 +755,7 @@ cc.SpriteType = cc.Enum({ /* * @property {Number} TILED */ - TILED : 2, + TILED: 2, /* * @property {Number} FILLED */