diff --git a/cocos2d/core/base-ui/CCWidgetManager.js b/cocos2d/core/base-ui/CCWidgetManager.js index 3ec2633d2f..fc8b659aca 100644 --- a/cocos2d/core/base-ui/CCWidgetManager.js +++ b/cocos2d/core/base-ui/CCWidgetManager.js @@ -127,19 +127,95 @@ function visitNode (node) { function visit () { var scene = cc.director.getScene(); if (scene) { + widgetManager.isAligning = true; visitNode(scene); + widgetManager.isAligning = false; } } -cc._widgetManager = { +var adjustWidgetToAllowMovingInEditor = CC_EDITOR && function (event) { + if (widgetManager.isAligning) { + return; + } + var oldPos = event.detail; + var newPos = this.node.position; + var delta = newPos.sub(oldPos); + if (this.isAlignTop) { + this.top -= delta.y; + } + if (this.isAlignBottom) { + this.bottom += delta.y; + } + if (this.isAlignLeft) { + this.left += delta.x; + } + if (this.isAlignRight) { + this.right -= delta.x; + } + if (this.isAlignHorizontalCenter) { + if (oldPos.x !== newPos.x) { + this.isAlignHorizontalCenter = false; + } + } + if (this.isAlignVerticalCenter) { + if (oldPos.y !== newPos.y) { + this.isAlignVerticalCenter = false; + } + } +}; + +var adjustWidgetToAllowResizingInEditor = CC_EDITOR && function (event) { + if (widgetManager.isAligning) { + return; + } + var oldSize = event.detail; + var newSize = this.node.getContentSize(); + var sizeDelta = cc.p(newSize.width - oldSize.width, newSize.height - oldSize.height); + var anchor = this.node.getAnchorPoint(); + + if (this.isAlignTop) { + this.top -= sizeDelta.y * (1 - anchor.y); + } + if (this.isAlignBottom) { + this.bottom -= sizeDelta.y * anchor.y; + } + if (this.isAlignLeft) { + this.left -= sizeDelta.x * anchor.x; + } + if (this.isAlignRight) { + this.right -= sizeDelta.x * (1 - anchor.x); + } + if (this.isAlignHorizontalCenter) { + if (sizeDelta.x !== 0 && anchor.x !== 0.5) { + this.isAlignHorizontalCenter = false; + } + } + if (this.isAlignVerticalCenter) { + if (sizeDelta.y !== 0 && anchor.y !== 0.5) { + this.isAlignVerticalCenter = false; + } + } +}; + + +var widgetManager = cc._widgetManager = { + isAligning: false, init: function (director) { director.on(cc.Director.EVENT_BEFORE_VISIT, visit); }, add: function (widget) { widget.node._widget = widget; + if (CC_EDITOR && !cc.engine.isPlaying) { + widget.node.on('position-changed', adjustWidgetToAllowMovingInEditor, widget); + widget.node.on('size-changed', adjustWidgetToAllowResizingInEditor, widget); + } }, remove: function (widget) { widget.node._widget = null; + if (CC_EDITOR && !cc.engine.isPlaying) { + widget.node.off('position-changed', adjustWidgetToAllowMovingInEditor, widget); + widget.node.off('size-changed', adjustWidgetToAllowResizingInEditor, widget); + } }, _getParentSize: getParentSize }; diff --git a/cocos2d/core/components/CCButton.js b/cocos2d/core/components/CCButton.js index 243aea430d..9319875045 100644 --- a/cocos2d/core/components/CCButton.js +++ b/cocos2d/core/components/CCButton.js @@ -108,7 +108,7 @@ var EVENT_HOVER_OUT = 'hover-out'; * When Button state changed: * If Transition type is Button.Transition.NONE, Button will do nothing * If Transition type is Button.Transition.COLOR, Button will change target's color - * If Transition type is Button.Transition.SPRITE, Button will change target SpriteRenderer's sprite + * If Transition type is Button.Transition.SPRITE, Button will change target Sprite's sprite * * Button will trigger 5 events: * Button.EVENT_TOUCH_DOWN @@ -287,7 +287,7 @@ var Button = cc.Class({ * When Button state changed: * If Transition type is Button.Transition.NONE, Button will do nothing * If Transition type is Button.Transition.COLOR, Button will change target's color - * If Transition type is Button.Transition.SPRITE, Button will change target SpriteRenderer's sprite + * If Transition type is Button.Transition.SPRITE, Button will change target Sprite's sprite * @property {cc.Node} target */ target: { @@ -414,7 +414,7 @@ var Button = cc.Class({ _applyTarget: function () { var target = this.target; if (target) { - this._sprite = target.getComponent(cc.SpriteRenderer); + this._sprite = target.getComponent(cc.Sprite); } else { this._sprite = null; @@ -493,7 +493,7 @@ var Button = cc.Class({ } } else if (transition === Transition.SPRITE && this._sprite && sprite) { - this._sprite.sprite = sprite; + this._sprite.spriteFrame = sprite; } }, diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index 0eea6c9dea..b45fefe518 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -239,7 +239,8 @@ var Component = cc.Class({ } }, displayName: 'Script', - type: cc._Script + type: cc._Script, + tooltip: 'i18n:INSPECTOR.component.script' }, /** diff --git a/cocos2d/core/components/CCProgressBar.js b/cocos2d/core/components/CCProgressBar.js index 4631baaeec..37a83f9654 100644 --- a/cocos2d/core/components/CCProgressBar.js +++ b/cocos2d/core/components/CCProgressBar.js @@ -127,12 +127,12 @@ var ProgressBar = cc.Class({ properties: { /** - * The targeted SpriteRenderer which will be changed progressively. - *@property {cc.SpriteRenderer} barSprite + * The targeted Sprite which will be changed progressively. + *@property {cc.Sprite} barSprite */ barSprite: { default: null, - type: cc.SpriteRenderer, + type: cc.Sprite, tooltip: 'i18n:COMPONENT.progress.bar_sprite', notify: function() { this._initBarSprite(); diff --git a/cocos2d/core/components/CCScrollBar.js b/cocos2d/core/components/CCScrollBar.js index 0a547867ee..6cd6947c53 100644 --- a/cocos2d/core/components/CCScrollBar.js +++ b/cocos2d/core/components/CCScrollBar.js @@ -62,11 +62,11 @@ var Scrollbar = cc.Class({ /** * The "handle" part of the scrollbar. - * @property {cc.SpriteRenderer} handle + * @property {cc.Sprite} handle */ handle: { default: null, - type: cc.SpriteRenderer, + type: cc.Sprite, tooltip: 'i18n:COMPONENT.scrollbar.handle', notify: function() { this._onScroll(cc.p(0, 0)); @@ -111,7 +111,7 @@ var Scrollbar = cc.Class({ }, _convertToScrollViewSpace: function(content) { - var worldSpacePos = content.convertToWorldSpaceAR(cc.p(0, 0)); + var worldSpacePos = content.convertToWorldSpace(cc.p(0, 0)); var scrollViewSpacePos = this._scrollView.node.convertToNodeSpace(worldSpacePos); return scrollViewSpacePos; }, diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index c85c233810..0ace6d005b 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -123,28 +123,31 @@ var ScrollView = cc.Class({ }, /** - * It determines how quickly the content stop moving. A value of 0 will stop the movement immediately. - * When the deceleration rate is very large, it will takes up too much time to stop the movement. - *@property {Float} decelerationRate + * It determines how quickly the content stop moving. A value of 1 will stop the movement immediately. + * A value of 0 will never stop the movement until it reaches to the boundary of scrollview. + *@property {Float} brake */ - decelerationRate: { - default: 0.5 + brake: { + default: 0.5, + type: 'Float', + range: [0, 1, 0.1] }, /** - * When spring is set, the content will be bounce back when move out of boundary. - *@property {Boolean} spring - */ - spring: { - default: true, - }, - - /** - * The elapse time of bouncing back. A value of 0 will bouncing back immediately. - *@property {Float} elastic + * When elastic is set, the content will be bounce back when move out of boundary. + *@property {Boolean} elastic */ elastic: { - default: 0.1 + default: true + }, + + /** + * The elapse time of bouncing back. A value of 0 will bounce back immediately. + *@property {Float} bounceDuration + */ + bounceDuration: { + default: 1, + range: [0, 10] }, /** @@ -181,12 +184,7 @@ var ScrollView = cc.Class({ return; } - var worldSpacePos = this.node.convertToWorldSpace(position); - var contentParent = this.content.parent; - - var localPositionInParent = contentParent.convertToNodeSpaceAR(worldSpacePos); - - this.content.setPosition(localPositionInParent); + this.content.setPosition(position); this._outOfBoundaryAmountDirty = true; @@ -194,9 +192,7 @@ var ScrollView = cc.Class({ }, getContentPosition: function() { - var contentSize = this.content.getContentSize(); - var contentAnchor = this.content.getAnchorPoint(); - return this._convertToScrollViewSpace(cc.p(-contentSize.width * contentAnchor.x, -contentSize.height * contentAnchor.y)); + return this.content.getPosition(); }, //private methods @@ -218,19 +214,32 @@ var ScrollView = cc.Class({ } }, - start: function() { + _calculateBoundary: function() { var scrollViewSize = this.node.getContentSize(); - this._rightBoundary = scrollViewSize.width; - this._topBoundary = scrollViewSize.height; + + var leftBottomPosition = this._convertToContentParentSpace(cc.p(0, 0)); + this._leftBoundary = leftBottomPosition.x; + this._bottomBoundary = leftBottomPosition.y; + + var topRightPosition = this._convertToContentParentSpace(cc.p(scrollViewSize.width, scrollViewSize.height)); + this._rightBoundary = topRightPosition.x; + this._topBoundary = topRightPosition.y; + }, + + _convertToContentParentSpace: function(position) { + var scrollViewPositionInWorldSpace = this.node.convertToWorldSpace(position); + var contentParent = this.content.parent; + + return contentParent.convertToNodeSpaceAR(scrollViewPositionInWorldSpace); + }, + + start: function() { + this._calculateBoundary(); this._updateScrollBar(0); }, update: function(dt) { - var scrollViewSize = this.node.getContentSize(); - this._rightBoundary = scrollViewSize.width; - this._topBoundary = scrollViewSize.height; - if (this._autoScrolling) { this._processAutoScrolling(dt); } @@ -285,15 +294,17 @@ var ScrollView = cc.Class({ }, _scrollChildren: function(deltaMove) { + deltaMove = this._clampDelta(deltaMove); + var realMove = deltaMove; var outOfBoundary; - if (this.spring) { + if (this.elastic) { outOfBoundary = this._getHowMuchOutOfBoundary(); realMove.x *= (outOfBoundary.x === 0 ? 1 : 0.5); realMove.y *= (outOfBoundary.y === 0 ? 1 : 0.5); } - if (!this.spring) { + if (!this.elastic) { outOfBoundary = this._getHowMuchOutOfBoundary(realMove); realMove = cc.pAdd(realMove, outOfBoundary); } @@ -311,7 +322,22 @@ var ScrollView = cc.Class({ this._onScrollBarTouchBegan(); }, + _clampDelta: function(delta){ + var contentSize = this.content.getContentSize(); + var scrollViewSize = this.node.getContentSize(); + if (contentSize.width <= scrollViewSize.width) { + delta.x = 0; + } + if (contentSize.height <= scrollViewSize.height) { + delta.y = 0; + } + + return delta; + }, + _gatherTouchMove: function(delta) { + delta = this._clampDelta(delta); + while (this._touchMoveDisplacements.length >= NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED) { this._touchMoveDisplacements.shift(); this._touchMoveTimeDeltas.shift(); @@ -325,16 +351,18 @@ var ScrollView = cc.Class({ }, _startBounceBackIfNeeded: function() { - if (!this.spring) { + if (!this.elastic) { return false; } var bounceBackAmount = this._getHowMuchOutOfBoundary(); + bounceBackAmount = this._clampDelta(bounceBackAmount); + if (cc.pFuzzyEqual(bounceBackAmount, cc.p(0, 0), EPSILON)) { return false; } - var bounceBackTime = Math.max(this.elastic, 0); + var bounceBackTime = Math.max(this.bounceDuration, 0); this._startAutoScroll(bounceBackAmount, bounceBackTime, true); return true; @@ -353,7 +381,7 @@ var ScrollView = cc.Class({ var bounceBackStarted = this._startBounceBackIfNeeded(); if (!bounceBackStarted && this.inertia) { var touchMoveVelocity = this._calculateTouchMoveVelocity(); - if (!cc.pFuzzyEqual(touchMoveVelocity, cc.p(0, 0), EPSILON) && this.decelerationRate > 0) { + if (!cc.pFuzzyEqual(touchMoveVelocity, cc.p(0, 0), EPSILON) && this.brake < 1) { this._startInertiaScroll(touchMoveVelocity); } } @@ -389,7 +417,7 @@ var ScrollView = cc.Class({ _processAutoScrolling: function(dt) { var isAutoScrollBrake = this._isNecessaryAutoScrollBrake(); - var brakingFactor = isAutoScrollBrake ? OUT_OF_BOUNDARY_BREAKING_FACTOR : this.decelerationRate * 5; + var brakingFactor = isAutoScrollBrake ? OUT_OF_BOUNDARY_BREAKING_FACTOR : 1; this._autoScrollAccumulatedTime += dt * (1 / brakingFactor); var percentage = Math.min(1, this._autoScrollAccumulatedTime / this._autoScrollTotalTime); @@ -400,7 +428,7 @@ var ScrollView = cc.Class({ var newPosition = cc.pAdd(this._autoScrollStartPosition, cc.pMult(this._autoScrollTargetDelta, percentage)); var reachedEnd = (percentage === 1); - if (this.spring) { + if (this.elastic) { var brakeOffsetPosition = cc.pSub(newPosition, this._autoScrollBrakingStartPosition); if (isAutoScrollBrake) { brakeOffsetPosition = cc.pMult(brakeOffsetPosition, brakingFactor); @@ -431,7 +459,21 @@ var ScrollView = cc.Class({ _startAttenuatingAutoScroll: function(deltaMove, initialVelocity) { var time = this._calculateAutoScrollTimeByInitalSpeed(cc.pLength(initialVelocity)); - this._startAutoScroll(deltaMove, time, true); + var originalMoveLength = cc.pLength(deltaMove); + + var targetDelta = cc.pNormalize(deltaMove); + var contentSize = this.content.getContentSize(); + var scrollviewSize = this.node.getContentSize(); + + targetDelta = cc.p(targetDelta.x * (contentSize.width - scrollviewSize.width) * (1 - this.brake), + targetDelta.y * (contentSize.height - scrollviewSize.height) * (1 - this.brake)); + + targetDelta = cc.pAdd(deltaMove, targetDelta); + var factor = cc.pLength(targetDelta) / originalMoveLength; + + time = time * factor; + + this._startAutoScroll(targetDelta, time, true); }, _calculateAutoScrollTimeByInitalSpeed: function(initalSpeed) { @@ -487,12 +529,6 @@ var ScrollView = cc.Class({ return result; }, - _convertToScrollViewSpace: function(position) { - var contentWorldPosition = this.content.convertToWorldSpaceAR(position); - - return this.node.convertToNodeSpace(contentWorldPosition); - }, - _moveContent: function(deltaMove, canStartBounceBack) { var adjustedMove = this._flattenVectorByDirection(deltaMove); @@ -503,29 +539,33 @@ var ScrollView = cc.Class({ var outOfBoundary = this._getHowMuchOutOfBoundary(); this._updateScrollBar(outOfBoundary); - if (this.spring && canStartBounceBack) { + if (this.elastic && canStartBounceBack) { this._startBounceBackIfNeeded(); } }, + _getContentLeftBoundary: function() { var contentPos = this.getContentPosition(); - return contentPos.x; + var leftBoundary = contentPos.x - this.content.getAnchorPoint().x * this.content.getContentSize().width; + return leftBoundary; }, _getContentRightBoundary: function() { var contentSize = this.content.getContentSize(); - return this._convertToScrollViewSpace(cc.p(contentSize.width, contentSize.height)).x; + return this._getContentLeftBoundary() + contentSize.width; }, _getContentTopBoundary: function() { var contentSize = this.content.getContentSize(); - return this._convertToScrollViewSpace(cc.p(contentSize.width, contentSize.height)).y; + return this._getContentBottomBoundary() + contentSize.height; }, _getContentBottomBoundary: function() { - return this.getContentPosition().y; + var contentPos = this.getContentPosition(); + var bottomBoundary = contentPos.y - this.content.getAnchorPoint().y * this.content.getContentSize().height; + return bottomBoundary; }, _getHowMuchOutOfBoundary: function(addition) { @@ -552,6 +592,8 @@ var ScrollView = cc.Class({ this._outOfBoundaryAmountDirty = false; } + outOfBoundaryAmount = this._clampDelta(outOfBoundaryAmount); + return outOfBoundaryAmount; }, diff --git a/cocos2d/core/components/CCSpriteRenderer.js b/cocos2d/core/components/CCSprite.js similarity index 61% rename from cocos2d/core/components/CCSpriteRenderer.js rename to cocos2d/core/components/CCSprite.js index 161cb2c1fc..db297ebc3c 100644 --- a/cocos2d/core/components/CCSpriteRenderer.js +++ b/cocos2d/core/components/CCSprite.js @@ -29,7 +29,7 @@ var SpriteType = cc.SpriteType; * @class Sprite * @extends _ComponentInSG */ -var SpriteRenderer = cc.Class({ +var Sprite = cc.Class({ name: 'cc.Sprite', extends: require('./CCComponentInSG'), @@ -39,13 +39,11 @@ var SpriteRenderer = cc.Class({ }, properties: { - _sprite: { + _spriteFrame: { default: null, type: cc.SpriteFrame }, _type: SpriteType.SIMPLE, - _isFlippedX: false, - _isFlippedY: false, _useOriginalSize: true, /** @@ -66,19 +64,14 @@ var SpriteRenderer = cc.Class({ * @property sprite * @type {SpriteFrame} */ - sprite: { + spriteFrame: { get: function () { - return this._sprite; + return this._spriteFrame; }, set: function (value, force) { - var lastSprite = this._sprite; - this._sprite = value; + var lastSprite = this._spriteFrame; + this._spriteFrame = value; if (this._sgNode) { - if (CC_EDITOR) { - if (force) { - this._sgNode._scale9Image = null; - } - } this._applySprite(this._sgNode, lastSprite); // color cleared after reset texture, should reapply color this._sgNode.setColor(this.node._color); @@ -165,15 +158,6 @@ var SpriteRenderer = cc.Class({ return this.type === cc.SpriteType.SLICED }, - /** - * Get the original no 9-sliced sprite. - * @method getSprite - * @return {SpriteFrame} A sprite instance. - */ - getSprite : function(){ - return this._sprite; - }, - /** * Initializes a 9-slice sprite with a texture file, a delimitation zone and * with the specified cap insets. @@ -187,8 +171,8 @@ var SpriteRenderer = cc.Class({ * is the whole image. If the shape is the whole texture, set this to the texture's full rect. * @param {Rect} capInsets - The values to use for the cap insets. */ - initWithFile: function (file, rect, capInsets) { - this._sgNode.initWithFile(file, rect, capInsets); + initWithFile: function (file) { + this._sgNode.initWithFile(file); }, /** @@ -202,9 +186,9 @@ var SpriteRenderer = cc.Class({ * @param {SpriteFrame} spriteFrame - The sprite frame object. * @param {Rect} capInsets - The values to use for the cap insets. */ - initWithSpriteFrame: function (spriteFrame, capInsets) { - this._sprite = spriteFrame; - this._sgNode.initWithSpriteFrame(spriteFrame, capInsets); + initWithSpriteFrame: function (spriteFrame) { + this._spriteFrame = spriteFrame; + this._sgNode.initWithSpriteFrame(spriteFrame); }, /** @@ -218,54 +202,12 @@ var SpriteRenderer = cc.Class({ * @param {String} spriteFrameName - The sprite frame name. * @param {Rect} capInsets - The values to use for the cap insets. */ - initWithSpriteFrameName: function (spriteFrameName, capInsets) { - var initialized = this._sgNode.initWithSpriteFrame(spriteFrameName, capInsets); + initWithSpriteFrameName: function (spriteFrameName) { + var initialized = this._sgNode.initWithSpriteFrame(spriteFrameName); if (initialized === false) { return; } - this._sprite = this._sgNode.getSprite(); - }, - - /** - * Creates and returns a new sprite object with the specified cap insets. - * You use this method to add cap insets to a sprite or to change the existing - * cap insets of a sprite. In both cases, you get back a new image and the - * original sprite remains untouched. - * - * @method resizableSpriteWithCapInsets - * @param {Rect} capInsets - The values to use for the cap insets. - * @return {Scale9Sprite} A Scale9Sprite instance. - */ - resizableSpriteWithCapInsets: function (capInsets) { - return this._sgNode.resizableSpriteWithCapInsets(capInsets); - }, - - /** - * Update Scale9Sprite with a specified sprite. - * - * @method updateWithSprite - * @param {SpriteFrame} sprite - A sprite pointer. - * @param {Rect} rect - A delimitation zone. - * @param {Number} rotated - Whether the sprite is rotated or not. - * @param {Size} offset - The offset when slice the sprite. - * @param {Size} originalSize - The origial size of the sprite. - * @param {Rect} capInsets - The Values to use for the cap insets. - * @return {Boolean} True if update success, false otherwise. - */ - updateWithSprite: function (sprite, textureRect, rotated, offset, originalSize, capInsets) { - this._sprite = sprite; - return this._sgNode.updateWithSprite(sprite, textureRect, rotated, offset, originalSize, capInsets); - }, - - /** - * Sets a new sprite frame to the sprite. - * @method setSpriteFrame - * @param {SpriteFrame} spriteFrame - * @param {Rect} capInsets - */ - setSpriteFrame: function (spriteFrame, capInsets) { - this.sprite = spriteFrame; - this.setCapInsets(capInsets); + this._spriteFrame = this._sgNode.getSpriteFrame(); }, /** @@ -277,41 +219,6 @@ var SpriteRenderer = cc.Class({ return this._sgNode.getOriginalSize(); }, - /** - * Change the preferred size of Scale9Sprite. - * @method setPreferredSize - * @param {Size} size - A delimitation zone. - */ - setPreferredSize: function (size) { - this.node.setContentSize(size); - }, - - /** - * Query the Scale9Sprite's preferred size. - * @method getPreferredSize - * @return {Size} Scale9Sprite's preferred size. - */ - getPreferredSize: function () { - return this._sgNode.getPreferredSize(); - }, - - /** - * Change the cap inset size. - * @method setCapInsets - * @param {Rect} capInsets - A delimitation zone. - */ - setCapInsets: function (capInsets) { - this._sgNode.setCapInsets(capInsets); - }, - /** - * Query the Scale9Sprite's preferred size. - * @method getCapInsets - * @return {Rect} Scale9Sprite's cap inset. - */ - getCapInsets: function () { - return this._sgNode.getCapInsets(); - }, - /** * Change the left sprite's cap inset. * @method setInsetLeft @@ -384,56 +291,6 @@ var SpriteRenderer = cc.Class({ return this._sgNode.getInsetBottom(); }, - /** - * Sets whether the widget should be flipped horizontally or not. - * @method setFlippedX - * @param {Boolean} flippedX - true if the sprite should be flipped horizontally, false otherwise. - */ - setFlippedX: function (flippedX) { - this._isFlippedX = flippedX; - this._sgNode.setFlippedX(flippedX); - }, - - /** - * Returns the flag which indicates whether the widget is flipped horizontally or not. - * - * It only flips the texture of the widget, and not the texture of the widget's children. - * Also, flipping the texture doesn't alter the anchorPoint. - * If you want to flip the anchorPoint too, and/or to flip the children too use: - * widget->setScaleX(sprite->getScaleX() * -1); - * - * @method isFlippedX - * @return {Boolean} true if the sprite is flipped horizontally, false otherwise. - */ - isFlippedX: function () { - return this._isFlippedX; - }, - - /** - * Sets whether the sprite should be flipped vertically or not. - * @method setFlippedY - * @param {Boolean} flippedY - true if the sprite should be flipped vertically, false otherwise. - */ - setFlippedY: function (flippedY) { - this._isFlippedY = flippedY; - this._sgNode.setFlippedY(flippedY); - }, - - /** - * Return the flag which indicates whether the widget is flipped vertically or not. - * - * It only flips the texture of the widget, and not the texture of the widget's children. - * Also, flipping the texture doesn't alter the anchorPoint. - * If you want to flip the anchorPoint too, and/or to flip the children too use: - * widget->setScaleY(widget->getScaleY() * -1); - * - * @method isFlippedY - * @return {Boolean} true if the sprite is flipped vertically, false otherwise. - */ - isFlippedY: function () { - return this._isFlippedY; - }, - onLoad: function () { this._super(); this.node.on('size-changed', this._resized, this); @@ -444,11 +301,11 @@ var SpriteRenderer = cc.Class({ this.node.off('size-changed', this._resized, this); }, - _applyAtlas: CC_EDITOR && function ( sprite ) { + _applyAtlas: CC_EDITOR && function ( spriteFrame ) { // Set atlas - if (sprite._atlasUuid) { + if (spriteFrame._atlasUuid) { var self = this; - cc.AssetLibrary.loadAsset(sprite._atlasUuid, function(err, asset) { + cc.AssetLibrary.loadAsset(spriteFrame._atlasUuid, function(err, asset) { self._atlas = asset; }); } else { @@ -459,21 +316,21 @@ var SpriteRenderer = cc.Class({ _applyCapInset: function (node) { if (this._type === SpriteType.SLICED) { var node = node || this._sgNode; - node.setInsetTop(this._sprite.insetTop); - node.setInsetBottom(this._sprite.insetBottom); - node.setInsetRight(this._sprite.insetRight); - node.setInsetLeft(this._sprite.insetLeft); + node.setInsetTop(this._spriteFrame.insetTop); + node.setInsetBottom(this._spriteFrame.insetBottom); + node.setInsetRight(this._spriteFrame.insetRight); + node.setInsetLeft(this._spriteFrame.insetLeft); } }, _applySpriteSize: function (node) { var node = node || this._sgNode; if (this._useOriginalSize) { - var rect = this._sprite.getRect(); - node.setPreferredSize(cc.size(rect.width, rect.height)); + var rect = this._spriteFrame.getOriginalSize(); + node.setContentSize(cc.size(rect.width, rect.height)); } else { - node.setPreferredSize(this.node.getContentSize(true)); + node.setContentSize(this.node.getContentSize(true)); } }, @@ -481,15 +338,15 @@ var SpriteRenderer = cc.Class({ if (oldSprite && oldSprite.off) { oldSprite.off('load', this._applyCapInset, this); } - if (!this._sprite) return; + if (!this._spriteFrame) return; - sgNode.setSpriteFrame(this._sprite); - var locLoaded = this._sprite.textureLoaded(); + sgNode.setSpriteFrame(this._spriteFrame); + var locLoaded = this._spriteFrame.textureLoaded(); if (!locLoaded) { if ( !this._useOriginalSize ) { - sgNode.setPreferredSize(this.node.getContentSize(true)); + sgNode.setContentSize(this.node.getContentSize(true)); } - this._sprite.once('load', function () { + this._spriteFrame.once('load', function () { this._applyCapInset(); this._applySpriteSize(); }, this); @@ -501,7 +358,7 @@ var SpriteRenderer = cc.Class({ if (CC_EDITOR) { // Set atlas - this._applyAtlas(this._sprite); + this._applyAtlas(this._spriteFrame); } }, @@ -520,11 +377,8 @@ var SpriteRenderer = cc.Class({ var misc = require('../utils/misc'); var SameNameGetSets = ['atlas', 'capInsets', 'insetLeft', 'insetTop', 'insetRight', 'insetBottom']; var DiffNameGetSets = { - type: [ null, 'setRenderingType'], - sprite: ['getSprite', null], - flippedX: ['isFlippedX', 'setFlippedX'], - flippedY: ['isFlippedY', 'setFlippedY'], + type: [ null, 'setRenderingType'] }; -misc.propertyDefine(SpriteRenderer, SameNameGetSets, DiffNameGetSets); +misc.propertyDefine(Sprite, SameNameGetSets, DiffNameGetSets); -cc.SpriteRenderer = module.exports = SpriteRenderer; +cc.Sprite = module.exports = Sprite; diff --git a/cocos2d/core/components/index.js b/cocos2d/core/components/index.js index 4dfb3d8ffe..23dc40bbd7 100644 --- a/cocos2d/core/components/index.js +++ b/cocos2d/core/components/index.js @@ -2,7 +2,7 @@ require('./CCComponent'); require('./CCComponentInSG'); module.exports = [ - require('./CCSpriteRenderer'), + require('./CCSprite'), require('./CCWidget'), require('./CCCanvas'), require('./CCAudioSource'), diff --git a/cocos2d/core/event/event-target.js b/cocos2d/core/event/event-target.js index 749c3ada75..01c4134ac5 100644 --- a/cocos2d/core/event/event-target.js +++ b/cocos2d/core/event/event-target.js @@ -6,7 +6,7 @@ var JS = cc.js; var cachedArray = new Array(16); cachedArray.length = 0; -var _doDispatchEvent = function (owner, event, args) { +var _doDispatchEvent = function (owner, event) { var target, i; event.target = owner; diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 596bccdd1d..7240fc1b6a 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -380,8 +380,8 @@ var AssetLibrary = { var loaded = img.width > 0 || img.height > 0; if ( !loaded ) { function loadCallback () { - img.removeEventListener('load', loadCallback, false); - img.removeEventListener('error', errorCallback, false); + img.removeEventListener('load', loadCallback); + img.removeEventListener('error', errorCallback); obj[prop] = dependsUrl; --pendingCount; @@ -391,8 +391,15 @@ var AssetLibrary = { } } function errorCallback () { - img.removeEventListener('load', loadCallback, false); - img.removeEventListener('error', errorCallback, false); + img.removeEventListener('load', loadCallback); + img.removeEventListener('error', errorCallback); + + obj[prop] = ''; + --pendingCount; + if (callback && pendingCount === 0) { + callback(); + callback = null; + } } img.addEventListener('load', loadCallback); img.addEventListener('error', errorCallback); diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 28d23399f7..949b470882 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -235,8 +235,7 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ this._sgNode.rotation = value; this.emit(ROTATION_CHANGED, old); } - }, - tooltip: "The clockwise degrees of rotation relative to the parent" + } }, /** diff --git a/cocos2d/deprecated.js b/cocos2d/deprecated.js index 06b3bb4d4c..2e5009957c 100644 --- a/cocos2d/deprecated.js +++ b/cocos2d/deprecated.js @@ -409,23 +409,37 @@ if (CC_DEV) { // cc.SpriteRenderer - markAsRemoved(cc.SpriteRenderer, [ + markAsRemoved(cc.Sprite, [ 'textureLoaded', 'setBlendFunc', 'getBlendFunc', 'setState', 'getState', + 'resizableSpriteWithCapInsets', + 'flippedX', + 'flippedY', + 'setFlippedX', + 'setFlippedY', + 'isFlippedX', + 'isFlippedY', + 'getCapInsets', + 'setCapInsets', ]); - provideClearError(cc.SpriteRenderer, { + provideClearError(cc.Sprite, { create: 'node.addComponent', createWithTexture: 'node.addComponent', createWithSpriteFrameName: 'node.addComponent', createWithSpriteFrame: 'node.addComponent', }); - provideClearError(cc.SpriteRenderer.prototype, { + provideClearError(cc.Sprite.prototype, { ignoreAnchorPointForPosition: 'instance.ignoreAnchor', + getPreferredSize: 'node.getContentSize', + setPreferredSize: 'node.setContentSize', + updateWithSprite: 'spriteFrame', + getSpriteFrame: 'spriteFrame', + setSpriteFrame: 'spriteFrame', }); - shouldNotUseNodeProp(cc.SpriteRenderer); + shouldNotUseNodeProp(cc.Sprite); // Particle markAsRemoved(cc.ParticleSystem, [ diff --git a/test/qunit/unit/test-animation.js b/test/qunit/unit/test-animation.js index e27c4c6074..f43e257ec2 100644 --- a/test/qunit/unit/test-animation.js +++ b/test/qunit/unit/test-animation.js @@ -92,7 +92,7 @@ test('EntityAnimator.animate', function () { var EntityAnimator = cc._Test.EntityAnimator; var entity = new cc.Node(); - var renderer = entity.addComponent(cc.SpriteRenderer); + var renderer = entity.addComponent(cc.Sprite); renderer.colorProp = Color.BLACK; var animator = new EntityAnimator(entity); @@ -179,7 +179,7 @@ test('AnimationNode', function () { var entity = new cc.Node(); entity.x = 321; - var renderer = entity.addComponent(cc.SpriteRenderer); + var renderer = entity.addComponent(cc.Sprite); renderer.colorProp = Color.BLACK; var animator = new EntityAnimator(entity); @@ -364,12 +364,12 @@ test('initClipData', function () { var entity = new cc.Node(); entity.name = 'foo'; - var renderer = entity.addComponent(cc.SpriteRenderer); + var renderer = entity.addComponent(cc.Sprite); renderer.testColor = Color.BLACK; var childEntity = new cc.Node(); childEntity.name = 'bar'; - var childRenderer = childEntity.addComponent(cc.SpriteRenderer); + var childRenderer = childEntity.addComponent(cc.Sprite); childRenderer.testColor = Color.BLACK; entity.addChild(childEntity); diff --git a/test/qunit/unit/test-spriteRenderer.js b/test/qunit/unit/test-spriteRenderer.js index 96da914761..5611dee542 100644 --- a/test/qunit/unit/test-spriteRenderer.js +++ b/test/qunit/unit/test-spriteRenderer.js @@ -1,4 +1,4 @@ -largeModule('SpriteRenderer'); +largeModule('Sprite'); asyncTest('basic test', function () { var url = assetDir + '/button.png'; @@ -7,16 +7,21 @@ asyncTest('basic test', function () { cc.director.getScene().addChild(node); node.color = Color.RED; - var render = node.addComponent(cc.SpriteRenderer); + var render = node.addComponent(cc.Sprite); deepEqual(render._sgNode.color, Color.RED, 'color set success'); var newSprite = new cc.SpriteFrame(); - newSprite.initWithTexture(url, cc.rect(0, 0, 10, 10)); - render.sprite = newSprite; - var tex = newSprite.getTexture(); - tex.once('load', function () { - strictEqual(render._sgNode._scale9Image.texture.url, url, 'texture set success'); - start(); - }); + var texture = cc.textureCache.addImage(url); + newSprite.setTexture(texture); + render.spriteFrame = newSprite; + if (!texture.isLoaded()) { + render.spriteFrame.once('load', function () { + strictEqual(render._sgNode._resourceData._texture.url, url, 'texture set success'); + start(); + }); + } + else { + strictEqual(render._sgNode._resourceData._texture.url, url, 'texture set success'); + } }); diff --git a/test/visual-tests/run-helper.sh b/test/visual-tests/run-helper.sh index 815d6a0abe..f2f05c57d3 100644 --- a/test/visual-tests/run-helper.sh +++ b/test/visual-tests/run-helper.sh @@ -1,3 +1,3 @@ #!/bin/bash sleep 1 -/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome http://127.0.0.1:8512/test/visual-tests/index.html?notrycatch=true +/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome http://127.0.0.1:8512/test/visual-tests/index.html