-
Notifications
You must be signed in to change notification settings - Fork 905
Asynchronous loading texture #3350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
920fb0f
Merge branch 'develop' of git://github.com/cocos2d/cocos2d-html5 into…
VisualSJ 81cceea
Asynchronous loading texture
VisualSJ 73e9281
Merge branch 'develop' of git://github.com/cocos2d/cocos2d-html5 into…
VisualSJ a22afda
Asynchronous loading texture
VisualSJ bade37a
pandamicro review code
VisualSJ fbb3d04
Asynchronous loading texture
VisualSJ 42494a4
Target must be the same
VisualSJ df5fb23
Merge branch 'develop' of git://github.com/cocos2d/cocos2d-html5 into…
VisualSJ b5c9645
texture.isLoaded()....
VisualSJ 15d00e3
Simplify sprite asynchronous callback logic
VisualSJ 9ee41bd
Simplify scale9sprite asynchronous callback logic
VisualSJ 58ac462
panda review code
VisualSJ a52c830
Fixed a bug about s9sprite update texture
VisualSJ 7a84cdc
Asynchronous loading texture(CCMenuItem)
VisualSJ 06dcfeb
Fixed a bug about scale9sprite anchorpoint
VisualSJ 3c9515f
Fixed a bug about throw error
VisualSJ 4dcd488
Fixed a bug about throw error
VisualSJ ef34d55
update _normalTextureLoaded (UIButton)
VisualSJ 7f9f471
change load audio event (ios)
VisualSJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{ | |
ctor: function (fileName, rect, rotated) { | ||
var self = this; | ||
cc.Node.prototype.ctor.call(self); | ||
self._loader = new cc.Sprite.LoadManager(); | ||
self._shouldBeHidden = false; | ||
self._offsetPosition = cc.p(0, 0); | ||
self._unflippedOffsetPositionFromCenter = cc.p(0, 0); | ||
|
@@ -246,19 +247,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{ | |
*/ | ||
initWithSpriteFrame:function (spriteFrame) { | ||
cc.assert(spriteFrame, cc._LogInfos.Sprite_initWithSpriteFrame); | ||
|
||
if(!spriteFrame.textureLoaded()){ | ||
//add event listener | ||
this._textureLoaded = false; | ||
spriteFrame.addEventListener("load", this._renderCmd._spriteFrameLoadedCallback, this); | ||
} | ||
|
||
//TODO | ||
var rotated = cc._renderType === cc.game.RENDER_TYPE_CANVAS ? false : spriteFrame._rotated; | ||
var ret = this.initWithTexture(spriteFrame.getTexture(), spriteFrame.getRect(), rotated); | ||
this.setSpriteFrame(spriteFrame); | ||
|
||
return ret; | ||
return this.setSpriteFrame(spriteFrame); | ||
}, | ||
|
||
/** | ||
|
@@ -645,14 +634,21 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{ | |
var tex = cc.textureCache.getTextureForKey(filename); | ||
if (!tex) { | ||
tex = cc.textureCache.addImage(filename); | ||
return this.initWithTexture(tex, rect || cc.rect(0, 0, tex._contentSize.width, tex._contentSize.height)); | ||
} else { | ||
if (!rect) { | ||
var size = tex.getContentSize(); | ||
rect = cc.rect(0, 0, size.width, size.height); | ||
} | ||
return this.initWithTexture(tex, rect); | ||
} | ||
this._loader.clear(); | ||
if (!tex.isLoaded()) { | ||
this._loader.add(tex, function () { | ||
this.initWithFile(filename, rect); | ||
this.dispatchEvent("load"); | ||
}, this); | ||
return false; | ||
} | ||
|
||
if (!rect) { | ||
var size = tex.getContentSize(); | ||
rect = cc.rect(0, 0, size.width, size.height); | ||
} | ||
return this.initWithTexture(tex, rect); | ||
}, | ||
|
||
/** | ||
|
@@ -669,6 +665,16 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{ | |
initWithTexture: function (texture, rect, rotated, counterclockwise) { | ||
var _t = this; | ||
cc.assert(arguments.length !== 0, cc._LogInfos.CCSpriteBatchNode_initWithTexture); | ||
this._loader.clear(); | ||
|
||
_t._textureLoaded = texture.isLoaded(); | ||
if (!_t._textureLoaded) { | ||
this._loader.add(texture, function () { | ||
this.initWithTexture(texture, rect, rotated, counterclockwise); | ||
this.dispatchEvent("load"); | ||
}, this); | ||
return false; | ||
} | ||
|
||
rotated = rotated || false; | ||
texture = this._renderCmd._handleTextureForRotatedTexture(texture, rect, rotated, counterclockwise); | ||
|
@@ -694,22 +700,12 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{ | |
_t._offsetPosition.y = 0; | ||
_t._hasChildren = false; | ||
|
||
var locTextureLoaded = texture.isLoaded(); | ||
_t._textureLoaded = locTextureLoaded; | ||
|
||
if (!locTextureLoaded) { | ||
_t._rectRotated = rotated; | ||
if (rect) { | ||
_t._rect.x = rect.x; | ||
_t._rect.y = rect.y; | ||
_t._rect.width = rect.width; | ||
_t._rect.height = rect.height; | ||
} | ||
if(_t.texture) | ||
_t.texture.removeEventListener("load", _t); | ||
texture.addEventListener("load", _t._renderCmd._textureLoadedCallback, _t); | ||
_t.setTexture(texture); | ||
return true; | ||
_t._rectRotated = rotated; | ||
if (rect) { | ||
_t._rect.x = rect.x; | ||
_t._rect.y = rect.y; | ||
_t._rect.width = rect.width; | ||
_t._rect.height = rect.height; | ||
} | ||
|
||
if (!rect) | ||
|
@@ -789,6 +785,7 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{ | |
newFrame = cc.spriteFrameCache.getSpriteFrame(newFrame); | ||
cc.assert(newFrame, cc._LogInfos.Sprite_setSpriteFrame) | ||
} | ||
this._loader.clear(); | ||
|
||
this.setNodeDirty(true); | ||
|
||
|
@@ -798,29 +795,20 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{ | |
|
||
// update rect | ||
var pNewTexture = newFrame.getTexture(); | ||
var locTextureLoaded = newFrame.textureLoaded(); | ||
if (!locTextureLoaded) { | ||
_t._textureLoaded = false; | ||
newFrame.addEventListener("load", function (sender) { | ||
_t.setNodeDirty(true); | ||
_t._textureLoaded = true; | ||
var locNewTexture = sender.getTexture(); | ||
if (locNewTexture !== _t._texture) | ||
_t._setTexture(locNewTexture); | ||
_t.setTextureRect(sender.getRect(), sender.isRotated(), sender.getOriginalSize()); | ||
_t.dispatchEvent("load"); | ||
_t.setColor(_t._realColor); | ||
}, _t); | ||
} else { | ||
_t._textureLoaded = true; | ||
// update texture before updating texture rect | ||
if (pNewTexture !== _t._texture) { | ||
_t._setTexture(pNewTexture); | ||
_t.setColor(_t._realColor); | ||
} | ||
_t.setTextureRect(newFrame.getRect(), newFrame.isRotated(), newFrame.getOriginalSize()); | ||
_t._textureLoaded = newFrame.textureLoaded(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. frameOffset above should be placed after |
||
this._loader.clear(); | ||
if (!_t._textureLoaded) { | ||
this._loader.add(pNewTexture, function () { | ||
this.setSpriteFrame(newFrame); | ||
this.dispatchEvent("load"); | ||
}, this); | ||
return false; | ||
} | ||
if (pNewTexture !== _t._texture) { | ||
this._renderCmd._setTexture(pNewTexture); | ||
_t.setColor(_t._realColor); | ||
} | ||
this._renderCmd._updateForSetSpriteFrame(pNewTexture); | ||
_t.setTextureRect(newFrame.getRect(), newFrame.isRotated(), newFrame.getOriginalSize()); | ||
}, | ||
|
||
/** | ||
|
@@ -907,33 +895,28 @@ cc.Sprite = cc.Node.extend(/** @lends cc.Sprite# */{ | |
if(isFileName) | ||
texture = cc.textureCache.addImage(texture); | ||
|
||
if(texture._textureLoaded){ | ||
this._setTexture(texture, isFileName); | ||
this.setColor(this._realColor); | ||
this._textureLoaded = true; | ||
}else{ | ||
this._renderCmd._setTexture(null); | ||
texture.addEventListener("load", function(){ | ||
this.setNodeDirty(true); | ||
this._setTexture(texture, isFileName); | ||
this.setColor(this._realColor); | ||
this._textureLoaded = true; | ||
this._loader.clear(); | ||
if (!texture._textureLoaded) { | ||
// wait for the load to be set again | ||
this._loader.add(texture, function () { | ||
this.setTexture(texture); | ||
this.dispatchEvent("load"); | ||
}, this); | ||
return false; | ||
} | ||
}, | ||
|
||
_setTexture: function(texture, change){ | ||
this._renderCmd._setTexture(texture); | ||
if(change) | ||
this._changeRectWithTexture(texture); | ||
this._changeRectWithTexture(texture); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure we should always call |
||
this.setColor(this._realColor); | ||
this._textureLoaded = true; | ||
}, | ||
|
||
_changeRectWithTexture: function(texture){ | ||
var contentSize = texture._contentSize; | ||
var rect = cc.rect( | ||
0, 0, | ||
contentSize.width, contentSize.height | ||
); | ||
0, 0, | ||
contentSize.width, contentSize.height | ||
); | ||
this.setTextureRect(rect); | ||
}, | ||
|
||
|
@@ -990,3 +973,24 @@ cc.EventHelper.prototype.apply(cc.Sprite.prototype); | |
cc.assert(cc.isFunction(cc._tmp.PrototypeSprite), cc._LogInfos.MissingFile, "SpritesPropertyDefine.js"); | ||
cc._tmp.PrototypeSprite(); | ||
delete cc._tmp.PrototypeSprite; | ||
|
||
(function () { | ||
var manager = cc.Sprite.LoadManager = function () { | ||
this.list = []; | ||
}; | ||
|
||
manager.prototype.add = function (source, callback, target) { | ||
source.addEventListener('load', callback, target); | ||
this.list.push({ | ||
source: source, | ||
listener: callback, | ||
target: target | ||
}); | ||
}; | ||
manager.prototype.clear = function () { | ||
while (this.list.length > 0) { | ||
var item = this.list.pop(); | ||
item.source.removeEventListener('load', item.listener, item.target); | ||
} | ||
}; | ||
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can put it into the if section
if (!tex.isLoaded())
, becauseinitWithTexture
will clear the loader by itself