Merge pull request #242 from jareguo/master

add _initSgNode for ComponentInSG
This commit is contained in:
Jare Guo
2016-01-13 14:09:08 +08:00
5 changed files with 35 additions and 22 deletions
+8 -1
View File
@@ -25,6 +25,8 @@ var ComponentInSG = cc.Class({
onLoad: function () {
var sgNode = this._createSgNode();
this._sgNode = sgNode;
this._initSgNode();
this._appendSgNode(sgNode);
if ( !this.node._sizeProvider ) {
this.node._sizeProvider = sgNode;
@@ -57,6 +59,12 @@ var ComponentInSG = cc.Class({
*/
_createSgNode: null,
/**
* @method _initSgNode
* @private
*/
_initSgNode: null,
_removeSgNode: SceneGraphHelper.removeSgNode,
_appendSgNode: function (sgNode) {
@@ -82,7 +90,6 @@ var ComponentInSG = cc.Class({
// retain immediately
// will be released in SceneGraphHelper.removeSgNode
sgNode.retain();
this._sgNode = sgNode;
}
});
+5 -3
View File
@@ -261,7 +261,11 @@ var Label = cc.Class({
},
_createSgNode: function () {
var sgNode = new _ccsg.Label(this.string, this.file);
return new _ccsg.Label(this.string, this.file);
},
_initSgNode: function () {
var sgNode = this._sgNode;
// TODO
// sgNode.enableRichText = this.enableRichText;
@@ -276,8 +280,6 @@ var Label = cc.Class({
sgNode.setContentSize(this.node.getContentSize());
}
sgNode.setColor(this.node.color);
return sgNode;
},
_resized: function () {
+17 -15
View File
@@ -72,7 +72,7 @@ var Sprite = cc.Class({
var lastSprite = this._spriteFrame;
this._spriteFrame = value;
if (this._sgNode) {
this._applySpriteFrame(this._sgNode, lastSprite);
this._applySpriteFrame(lastSprite);
// color cleared after reset texture, should reapply color
this._sgNode.setColor(this.node._color);
this._sgNode.setOpacity(this.node._opacity);
@@ -313,9 +313,9 @@ var Sprite = cc.Class({
}
},
_applyCapInset: function (sgNode) {
_applyCapInset: function () {
if (this._type === SpriteType.SLICED && this._spriteFrame) {
sgNode = sgNode || this._sgNode;
var sgNode = this._sgNode;
sgNode.setInsetTop(this._spriteFrame.insetTop);
sgNode.setInsetBottom(this._spriteFrame.insetBottom);
sgNode.setInsetRight(this._spriteFrame.insetRight);
@@ -323,8 +323,7 @@ var Sprite = cc.Class({
}
},
_applySpriteSize: function (sgNode) {
sgNode = sgNode || this._sgNode;
_applySpriteSize: function () {
if (this._useOriginalSize && this._spriteFrame) {
var rect = this._spriteFrame.getRect();
this.node.setContentSize(cc.size(rect.width, rect.height));
@@ -334,25 +333,26 @@ var Sprite = cc.Class({
}
},
_onSpriteFrameLoaded: function (event, sgNode) {
_onSpriteFrameLoaded: function (event) {
var self = this;
sgNode = sgNode || this._sgNode;
var sgNode = this._sgNode;
sgNode.setSpriteFrame(self._spriteFrame);
self._applyCapInset(sgNode);
self._applyCapInset();
self._applySpriteSize();
if ( this.enabledInHierarchy && !sgNode.isVisible() ) {
sgNode.setVisible(true);
}
},
_applySpriteFrame: function (sgNode, oldFrame) {
_applySpriteFrame: function (oldFrame) {
var sgNode = this._sgNode;
if (oldFrame && oldFrame.off) {
oldFrame.off('load', this._onSpriteFrameLoaded, this);
}
if (this._spriteFrame) {
if (this._spriteFrame.textureLoaded()) {
this._onSpriteFrameLoaded(null, sgNode);
this._onSpriteFrameLoaded(null);
}
else {
this._spriteFrame.once('load', this._onSpriteFrameLoaded, this);
@@ -369,18 +369,20 @@ var Sprite = cc.Class({
},
_createSgNode: function () {
var sgNode = new cc.Scale9Sprite();
return new cc.Scale9Sprite();
},
this._applySpriteFrame(sgNode, null);
_initSgNode: function () {
var sgNode = this._sgNode;
this._applySpriteFrame(null);
// should keep the size of the sg node the same as entity,
// otherwise setContentSize may not take effect
sgNode.setContentSize(this.node.getContentSize(true));
this._applySpriteSize(sgNode);
this._applySpriteSize();
sgNode.setRenderingType(this._type);
return sgNode;
},
_resized: function () {
+5 -2
View File
@@ -646,7 +646,11 @@ var ParticleSystem = cc.Class({
// OVERRIDE METHODS
_createSgNode: function () {
var sgNode = this._sgNode = new _ccsg.ParticleSystem();
return new _ccsg.ParticleSystem();
},
_initSgNode: function () {
var sgNode = this._sgNode;
var loadCustomAfterFile = false;
if (this._file) {
@@ -660,7 +664,6 @@ var ParticleSystem = cc.Class({
// stop by default
sgNode.stopSystem();
return sgNode;
},
// APIS
@@ -40,7 +40,6 @@ var createEntity = function (file, x, y) {
var spriteFrame = new cc.SpriteFrame(tex, cc.rect(0, 0, tex.getPixelWidth(), tex.getPixelHeight()));
var spriteRenderer = node.addComponent(cc.Sprite);
spriteRenderer.spriteFrame = spriteFrame;
spriteRenderer._createSgNode();
}
node.x = x;
node.y = y;