From 02d5cbec0170057da6275dc5e163eb1a2ff86ca6 Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Mon, 25 Apr 2016 16:21:42 +0800 Subject: [PATCH] Synchronization spriteFrame web and jsb code --- jsb/jsb-tex-sprite-frame.js | 137 ++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 69 deletions(-) diff --git a/jsb/jsb-tex-sprite-frame.js b/jsb/jsb-tex-sprite-frame.js index 7ad3821438..e89281b379 100644 --- a/jsb/jsb-tex-sprite-frame.js +++ b/jsb/jsb-tex-sprite-frame.js @@ -66,67 +66,85 @@ cc.SpriteFrame.prototype._ctor = function (filename, rect, rotated, offset, orig }; cc.SpriteFrame.prototype._initWithTexture = cc.SpriteFrame.prototype.initWithTexture; -cc.SpriteFrame.prototype.initWithTexture = function (texture, rect, rotated, offset, originalSize, _uuid) { - this.setTexture(texture, rect, rotated, offset, originalSize, _uuid); +cc.SpriteFrame.prototype.initWithTexture = function (texture, rect, rotated, offset, originalSize) { + this.setTexture(texture, rect, rotated, offset, originalSize); }; -cc.SpriteFrame.prototype.setTexture = function (textureOrTextureFile, rect, rotated, offset, originalSize, _uuid) { +cc.SpriteFrame.prototype.setTexture = function (textureOrTextureFile, rect, rotated, offset, originalSize) { - var localTexture; - if (!textureOrTextureFile && _uuid) { - // deserialize texture from uuid - var info = cc.AssetLibrary._getAssetInfoInRuntime(_uuid); - if (!info) { - cc.error('SpriteFrame: Failed to load sprite texture "%s"', _uuid); - return false; - } + if (rect) { + this.setRect(rect); + } - this._textureFilename = info.url; + if (offset) { + this.setOffset(offset); + } - localTexture = cc.textureCache.addImage(info.url); + if (originalSize) { + this.setOriginalSize(originalSize); + } + + this.setRotated(rotated || false); + + var texture = textureOrTextureFile; + if (cc.js.isString(textureOrTextureFile)) { + this._textureFilename = textureOrTextureFile; + texture = cc.textureCache.addImage(textureOrTextureFile); + } + + if (texture instanceof cc.Texture2D) { + this._refreshTexture(texture); } else { - if (cc.js.isString(textureOrTextureFile)) { - this._textureFilename = textureOrTextureFile; - localTexture = cc.textureCache.addImage(textureOrTextureFile); - } - else if (textureOrTextureFile instanceof cc.Texture2D) { - this._textureFilename = ""; - localTexture = textureOrTextureFile; - } + //todo log error } - if (localTexture instanceof cc.Texture2D && localTexture.isLoaded()) { - - if (!rect) { - rect = cc.rect(0, 0, localTexture.getPixelWidth(), localTexture.getPixelHeight()); - } - - offset = offset || cc.p(0, 0); - originalSize = originalSize || rect; - rotated = rotated || false; - - if (this.insetTop === undefined) { - this.insetTop = 0; - this.insetBottom = 0; - this.insetLeft = 0; - this.insetRight = 0; - } - - this._initWithTexture(localTexture, rect, rotated, offset, originalSize); - } - - this.emit('load'); - this._checkRect(this.getTexture()); return true; }; +cc.SpriteFrame.prototype._refreshTexture = function (texture) { + + if (this.getTexture() !== texture) { + + var w = texture.width, h = texture.height; + + var rect = this.getRect(); + if (rect.width === 0 || rect.height === 0) { + rect = cc.rect(0, 0, w, h); + } + else { + this._checkRect(texture); + } + + var originalSize = this.getOriginalSize(); + if (originalSize.width === 0 || originalSize.height === 0) { + originalSize = cc.size(w, h); + } + + var offset = this.getOffset(); + var rotated = this.isRotated(); + + this._initWithTexture(texture, rect, rotated, offset, originalSize); + + //dispatch 'load' event of cc.SpriteFrame + this.emit("load"); + } +}; + cc.SpriteFrame.prototype._deserialize = function (data, handle) { - var rect = data.rect ? new cc.Rect(data.rect[0], data.rect[1], data.rect[2], data.rect[3]) : undefined; - var offset = data.offset ? new cc.Vec2(data.offset[0], data.offset[1]) : undefined; - var size = data.originalSize ? new cc.Size(data.originalSize[0], data.originalSize[1]) : undefined; - var rotated = data.rotated === 1; - // init properties not included in this._initWithTexture() + var rect = data.rect; + 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.setRotated(data.rotated === 1); + this._name = data.name; var capInsets = data.capInsets; if (capInsets) { @@ -141,8 +159,6 @@ cc.SpriteFrame.prototype._deserialize = function (data, handle) { if (textureUuid) { handle.result.push(this, '_textureFilenameSetter', textureUuid); } - - this.initWithTexture(null, rect, rotated, offset, size, textureUuid); }; cc.SpriteFrame.prototype._checkRect = function (texture) { var rect = this.getRect(); @@ -171,25 +187,8 @@ cc.SpriteFrame.prototype.getTexture = function () { cc.js.set(cc.SpriteFrame.prototype, '_textureFilenameSetter', function (url) { this._textureFilename = url; if (url) { - cc.textureCache.addImage(url, function (texture) { - if (this.getTexture() !== texture) { - this.setTexture(texture); - var w = texture.getPixelWidth(), h = texture.getPixelHeight(); - if (this.getRect().width === 0) { - this.setRect(cc.rect(0, 0, w, h)); - } - if (this.getOriginalSize().width === 0) { - this.setOriginalSize(cc.size(w, h)); - } - if (this.getOffset().width === 0) { - this.setOffset(cc.v2(0, 0)); - } - - this._texture = texture; - } - this._checkRect(texture); - this.emit('load'); - }, this); + var texture = cc.textureCache.addImage(url); + this._refreshTexture(texture); } });