From e3ae41cb0521d0e8bccff905bf457c6c059cfa48 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Mon, 29 Feb 2016 18:07:45 +0800 Subject: [PATCH] Support Scale9SpriteV2 in JSB --- jsb/jsb-scale9sprite.js | 296 ++++++++++++++++++++++------------------ 1 file changed, 160 insertions(+), 136 deletions(-) diff --git a/jsb/jsb-scale9sprite.js b/jsb/jsb-scale9sprite.js index 231c9cae4d..9b33a85c92 100644 --- a/jsb/jsb-scale9sprite.js +++ b/jsb/jsb-scale9sprite.js @@ -26,6 +26,12 @@ // cc.Scale9Sprite +var v2Found = false; +if (cc.Scale9SpriteV2) { + v2Found = true; + cc.Scale9Sprite = cc.Scale9SpriteV2; +} + cc.Scale9Sprite.state = {NORMAL: 0, GRAY: 1}; /** @@ -60,154 +66,172 @@ cc.Scale9Sprite.FillType = cc.Enum({ var s9sPrototype = cc.Scale9Sprite.prototype; -s9sPrototype.setFillType = function () {}; -s9sPrototype.setFillCenter = function () {}; -s9sPrototype.setFillStart = function () {}; -s9sPrototype.setFillRange = function () {}; -s9sPrototype.enableTrimmedContentSize = function () {}; +if (v2Found) { + var _jsbSetContentSize = s9sPrototype.setContentSize; + s9sPrototype.setContentSize = function (size, height) { + if (void 0 !== height) { + size = new cc.Size(size, height); + } + _jsbSetContentSize.call(this, size); + }; + var _jsbSetAnchorPoint = s9sPrototype.setAnchorPoint; + s9sPrototype.setAnchorPoint = function (anchorPoint, y) { + if (void 0 !== y) { + anchorPoint = new cc.Vec2(anchorPoint, y); + } + _jsbSetAnchorPoint.call(this, anchorPoint); + }; +} +else { + s9sPrototype.setFillType = function () {}; + s9sPrototype.setFillCenter = function () {}; + s9sPrototype.setFillStart = function () {}; + s9sPrototype.setFillRange = function () {}; + s9sPrototype.enableTrimmedContentSize = function () {}; -s9sPrototype._lazyInit = function () { - if (this._onceInit) return; - this._onceInit = true; - this._insets = {left: 0, right: 0, top: 0, bottom: 0}; - this._trim = {left: 0, right: 0, top: 0, bottom: 0}; - this._contentSizeTrimmed = new cc.Size(0, 0); - this._anchorPointTrimmed = new cc.Vec2(0, 0); - this._sizeAfterTrimmed = new cc.Size(0, 0); -}; + s9sPrototype._lazyInit = function () { + if (this._onceInit) return; + this._onceInit = true; + this._insets = {left: 0, right: 0, top: 0, bottom: 0}; + this._trim = {left: 0, right: 0, top: 0, bottom: 0}; + this._contentSizeTrimmed = new cc.Size(0, 0); + this._anchorPointTrimmed = new cc.Vec2(0, 0); + this._sizeAfterTrimmed = new cc.Size(0, 0); + }; -s9sPrototype._applyInsetsContentAnchor = function () { - var renderingType = this._renderingType || (this.getRenderingType && this.getRenderingType()); - var trimScaleX = 1; - var trimScaleY = 1; - if (renderingType === cc.Scale9Sprite.RenderingType.SIMPLE) { - trimScaleX = this._contentSizeTrimmed.width / this._sizeAfterTrimmed.width; - trimScaleY = this._contentSizeTrimmed.height / this._sizeAfterTrimmed.height; - } - //apply contentSize - var contentSize = new cc.Size(0, 0); - contentSize.width = this._contentSizeTrimmed.width + (this._trim.left + this._trim.right) * trimScaleX; - contentSize.height = this._contentSizeTrimmed.height + (this._trim.top + this._trim.bottom) * trimScaleY; - this._setContentSize(contentSize); + s9sPrototype._applyInsetsContentAnchor = function () { + var renderingType = this._renderingType || (this.getRenderingType && this.getRenderingType()); + var trimScaleX = 1; + var trimScaleY = 1; + if (renderingType === cc.Scale9Sprite.RenderingType.SIMPLE) { + trimScaleX = this._contentSizeTrimmed.width / this._sizeAfterTrimmed.width; + trimScaleY = this._contentSizeTrimmed.height / this._sizeAfterTrimmed.height; + } + //apply contentSize + var contentSize = new cc.Size(0, 0); + contentSize.width = this._contentSizeTrimmed.width + (this._trim.left + this._trim.right) * trimScaleX; + contentSize.height = this._contentSizeTrimmed.height + (this._trim.top + this._trim.bottom) * trimScaleY; + this._setContentSize(contentSize); - //apply anchorpoint - var anchorPoint = new cc.Vec2(0, 0); - anchorPoint.x = (this._contentSizeTrimmed.width * this._anchorPointTrimmed.x) + this._trim.left * trimScaleX; - anchorPoint.y = (this._contentSizeTrimmed.height * this._anchorPointTrimmed.y) + this._trim.bottom * trimScaleY; - anchorPoint.x = anchorPoint.x / contentSize.width; - anchorPoint.y = anchorPoint.y / contentSize.height; + //apply anchorpoint + var anchorPoint = new cc.Vec2(0, 0); + anchorPoint.x = (this._contentSizeTrimmed.width * this._anchorPointTrimmed.x) + this._trim.left * trimScaleX; + anchorPoint.y = (this._contentSizeTrimmed.height * this._anchorPointTrimmed.y) + this._trim.bottom * trimScaleY; + anchorPoint.x = anchorPoint.x / contentSize.width; + anchorPoint.y = anchorPoint.y / contentSize.height; - this._setAnchorPoint(anchorPoint); + this._setAnchorPoint(anchorPoint); - //apply capinsets - var capinsets = new cc.Rect(0, 0, 0, 0); - capinsets.x = this._trim.left + this._insets.left; - capinsets.y = this._trim.top + this._insets.top; - capinsets.width = this._sizeAfterTrimmed.width - this._insets.left - this._insets.right; - capinsets.height = this._sizeAfterTrimmed.height - this._insets.top - this._insets.bottom; - this.setCapInsets(capinsets); -}; + //apply capinsets + var capinsets = new cc.Rect(0, 0, 0, 0); + capinsets.x = this._trim.left + this._insets.left; + capinsets.y = this._trim.top + this._insets.top; + capinsets.width = this._sizeAfterTrimmed.width - this._insets.left - this._insets.right; + capinsets.height = this._sizeAfterTrimmed.height - this._insets.top - this._insets.bottom; + this.setCapInsets(capinsets); + }; -s9sPrototype._setBlendFunc = s9sPrototype.setBlendFunc; -s9sPrototype.setBlendFunc = function (blendFunc, dst) { - if (void 0 !== dst) { - blendFunc = { - src: blendFunc, - dst: dst - }; - } - this._setBlendFunc(blendFunc); -}; -s9sPrototype._getContentSize = s9sPrototype.getContentSize; -s9sPrototype.getContentSize = function () { - return new cc.Size(this._contentSizeTrimmed); -}; + s9sPrototype._setBlendFunc = s9sPrototype.setBlendFunc; + s9sPrototype.setBlendFunc = function (blendFunc, dst) { + if (void 0 !== dst) { + blendFunc = { + src: blendFunc, + dst: dst + }; + } + this._setBlendFunc(blendFunc); + }; + s9sPrototype._getContentSize = s9sPrototype.getContentSize; + s9sPrototype.getContentSize = function () { + return new cc.Size(this._contentSizeTrimmed); + }; -s9sPrototype._setContentSize = s9sPrototype.setContentSize; -s9sPrototype.setContentSize = function (size, height) { - this._lazyInit(); - if (void 0 !== height) { - size = new cc.Size(size, height); - } - this._contentSizeTrimmed = new cc.Size(size); - this._applyInsetsContentAnchor(); -}; + s9sPrototype._setContentSize = s9sPrototype.setContentSize; + s9sPrototype.setContentSize = function (size, height) { + this._lazyInit(); + if (void 0 !== height) { + size = new cc.Size(size, height); + } + this._contentSizeTrimmed = new cc.Size(size); + this._applyInsetsContentAnchor(); + }; -s9sPrototype._getAnchorPoint = s9sPrototype.getAnchorPoint; -s9sPrototype.getAnchorPoint = function () { - return new cc.Vec2(this._anchorPointTrimmed); -}; + s9sPrototype._getAnchorPoint = s9sPrototype.getAnchorPoint; + s9sPrototype.getAnchorPoint = function () { + return new cc.Vec2(this._anchorPointTrimmed); + }; -s9sPrototype._setAnchorPoint = s9sPrototype.setAnchorPoint; -s9sPrototype.setAnchorPoint = function (anchorPoint, y) { - this._lazyInit(); - if (void 0 !== y) { - anchorPoint = new cc.Vec2(anchorPoint, y); - } - this._anchorPointTrimmed = new cc.Vec2(anchorPoint); - this._applyInsetsContentAnchor(); -}; + s9sPrototype._setAnchorPoint = s9sPrototype.setAnchorPoint; + s9sPrototype.setAnchorPoint = function (anchorPoint, y) { + this._lazyInit(); + if (void 0 !== y) { + anchorPoint = new cc.Vec2(anchorPoint, y); + } + this._anchorPointTrimmed = new cc.Vec2(anchorPoint); + this._applyInsetsContentAnchor(); + }; -s9sPrototype._getInsetLeft = s9sPrototype.getInsetLeft; -s9sPrototype._getInsetRight = s9sPrototype.getInsetRight; -s9sPrototype._getInsetBottom = s9sPrototype.getInsetBottom; -s9sPrototype._getInsetTop = s9sPrototype.getInsetTop; -s9sPrototype.getInsetLeft = function () { - return this._insets.left; -}; -s9sPrototype.getInsetRight = function () { - return this._insets.right; -}; -s9sPrototype.getInsetBottom = function () { - return this._insets.bottom; -}; -s9sPrototype.getInsetTop = function () { - return this._insets.top; -}; + s9sPrototype._getInsetLeft = s9sPrototype.getInsetLeft; + s9sPrototype._getInsetRight = s9sPrototype.getInsetRight; + s9sPrototype._getInsetBottom = s9sPrototype.getInsetBottom; + s9sPrototype._getInsetTop = s9sPrototype.getInsetTop; + s9sPrototype.getInsetLeft = function () { + return this._insets.left; + }; + s9sPrototype.getInsetRight = function () { + return this._insets.right; + }; + s9sPrototype.getInsetBottom = function () { + return this._insets.bottom; + }; + s9sPrototype.getInsetTop = function () { + return this._insets.top; + }; -s9sPrototype._setInsetLeft = s9sPrototype.setInsetLeft; -s9sPrototype.setInsetLeft = function (insetLeft) { - this._lazyInit(); - this._insets.left = insetLeft; - this._applyInsetsContentAnchor(); -}; + s9sPrototype._setInsetLeft = s9sPrototype.setInsetLeft; + s9sPrototype.setInsetLeft = function (insetLeft) { + this._lazyInit(); + this._insets.left = insetLeft; + this._applyInsetsContentAnchor(); + }; -s9sPrototype._setInsetRight = s9sPrototype.setInsetRight; -s9sPrototype.setInsetRight = function (insetRight) { - this._lazyInit(); - this._insets.right = insetRight; - this._applyInsetsContentAnchor(); -}; + s9sPrototype._setInsetRight = s9sPrototype.setInsetRight; + s9sPrototype.setInsetRight = function (insetRight) { + this._lazyInit(); + this._insets.right = insetRight; + this._applyInsetsContentAnchor(); + }; -s9sPrototype._setInsetTop = s9sPrototype.setInsetTop; -s9sPrototype.setInsetTop = function (insetTop) { - this._lazyInit(); - this._insets.top = insetTop; - this._applyInsetsContentAnchor(); -}; + s9sPrototype._setInsetTop = s9sPrototype.setInsetTop; + s9sPrototype.setInsetTop = function (insetTop) { + this._lazyInit(); + this._insets.top = insetTop; + this._applyInsetsContentAnchor(); + }; -s9sPrototype._setInsetBottom = s9sPrototype.setInsetBottom; -s9sPrototype.setInsetBottom = function (insetBottom) { - this._lazyInit(); - this._insets.bottom = insetBottom; - this._applyInsetsContentAnchor(); -}; + s9sPrototype._setInsetBottom = s9sPrototype.setInsetBottom; + s9sPrototype.setInsetBottom = function (insetBottom) { + this._lazyInit(); + this._insets.bottom = insetBottom; + this._applyInsetsContentAnchor(); + }; -s9sPrototype._setSpriteFrame = s9sPrototype.setSpriteFrame; -s9sPrototype.setSpriteFrame = function (spriteFrame) { - this._lazyInit(); - var originalSize = spriteFrame.getOriginalSize(); - var spriteRect = spriteFrame.getRect(); - var offset = spriteFrame.getOffset(); - var leftTrim = (originalSize.width + (2 * offset.x) - spriteRect.width) / 2; - var rightTrim = originalSize.width - leftTrim - spriteRect.width; - var bottomTrim = (originalSize.height + (2 * offset.y) - spriteRect.height) / 2; - var topTrim = originalSize.height - bottomTrim - spriteRect.height; - this._trim.left = leftTrim; - this._trim.right = rightTrim; - this._trim.top = topTrim; - this._trim.bottom = bottomTrim; - this._sizeAfterTrimmed = new cc.Size(spriteRect.width, spriteRect.height); - this._setSpriteFrame(spriteFrame); - this._applyInsetsContentAnchor(); -}; \ No newline at end of file + s9sPrototype._setSpriteFrame = s9sPrototype.setSpriteFrame; + s9sPrototype.setSpriteFrame = function (spriteFrame) { + this._lazyInit(); + var originalSize = spriteFrame.getOriginalSize(); + var spriteRect = spriteFrame.getRect(); + var offset = spriteFrame.getOffset(); + var leftTrim = (originalSize.width + (2 * offset.x) - spriteRect.width) / 2; + var rightTrim = originalSize.width - leftTrim - spriteRect.width; + var bottomTrim = (originalSize.height + (2 * offset.y) - spriteRect.height) / 2; + var topTrim = originalSize.height - bottomTrim - spriteRect.height; + this._trim.left = leftTrim; + this._trim.right = rightTrim; + this._trim.top = topTrim; + this._trim.bottom = bottomTrim; + this._sizeAfterTrimmed = new cc.Size(spriteRect.width, spriteRect.height); + this._setSpriteFrame(spriteFrame); + this._applyInsetsContentAnchor(); + }; +} \ No newline at end of file