@@ -81,7 +81,6 @@ ccui.Scale9Sprite = cc.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprit
8181 _insetRight : 0 ,
8282 _insetBottom : 0 ,
8383
84- _spritesGenerated : false ,
8584 _spriteFrameRotated : false ,
8685 _textureLoaded :false ,
8786 _className :"Scale9Sprite" ,
@@ -258,7 +257,19 @@ ccui.Scale9Sprite = cc.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprit
258257 _getPreferredHeight : function ( ) {
259258 return this . _preferredSize . height ;
260259 } ,
260+
261+ _asyncSetPreferredSize : function ( ) {
262+ this . removeEventListener ( 'load' , this . _asyncSetPreferredSize , this ) ;
263+ this . _cachePreferredSize = null ;
264+ this . setPreferredSize ( this . _cachePreferredSize ) ;
265+ } ,
261266 setPreferredSize : function ( preferredSize ) {
267+ if ( ! this . _textureLoaded ) {
268+ this . _cachePreferredSize = preferredSize ;
269+ this . removeEventListener ( 'load' , this . _asyncSetPreferredSize , this ) ;
270+ this . addEventListener ( 'load' , this . _asyncSetPreferredSize , this ) ;
271+ return false ;
272+ }
262273 this . setContentSize ( preferredSize ) ;
263274 this . _preferredSize = preferredSize ;
264275
@@ -310,15 +321,22 @@ ccui.Scale9Sprite = cc.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprit
310321 return cc . rect ( this . _capInsets ) ;
311322 } ,
312323
324+ asyncSetCapInsets : function ( ) {
325+ this . removeEventListener ( 'load' , this . asyncSetCapInsets , this ) ;
326+ this . setCapInsets ( this . _cacheCapInsets ) ;
327+ this . _cacheCapInsets = null ;
328+ } ,
313329 setCapInsets : function ( capInsets ) {
314330 var contentSize = this . _contentSize ;
315331 var tempWidth = contentSize . width , tempHeight = contentSize . height ;
316332 // Asynchronous loading texture requires this data
317333 // This data does not take effect immediately, so it does not affect the existing texture.
318- this . _capInsets . x = capInsets . x ;
319- this . _capInsets . y = capInsets . y ;
320- this . _capInsets . width = capInsets . width ;
321- this . _capInsets . height = capInsets . height ;
334+ if ( ! this . _textureLoaded ) {
335+ this . _cacheCapInsets = capInsets ;
336+ this . removeEventListener ( 'load' , this . asyncSetCapInsets , this ) ;
337+ this . addEventListener ( 'load' , this . asyncSetCapInsets , this ) ;
338+ return false ;
339+ }
322340
323341 this . updateWithSprite ( this . _scale9Image ,
324342 this . _spriteRect ,
@@ -497,24 +515,13 @@ ccui.Scale9Sprite = cc.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprit
497515 var locLoaded = texture . isLoaded ( ) ;
498516 this . _textureLoaded = locLoaded ;
499517 this . _loader . clear ( ) ;
500- if ( ! locLoaded ) {
501- this . _loader . add ( texture , function ( sender ) {
502- var preferredSize = this . _preferredSize ,
503- restorePreferredSize = preferredSize . width !== 0 && preferredSize . height !== 0 ;
504- if ( restorePreferredSize ) {
505- preferredSize = cc . size ( preferredSize . width , preferredSize . height ) ;
506- }
507- var size = sender . getContentSize ( ) ;
508- this . updateWithBatchNode ( this . _scale9Image , cc . rect ( 0 , 0 , size . width , size . height ) , false , this . _capInsets ) ;
509- if ( restorePreferredSize ) {
510- this . setPreferredSize ( preferredSize ) ;
511- }
512- this . _positionsAreDirty = true ;
513- this . setNodeDirty ( ) ;
518+ if ( ! locLoaded ) {
519+ this . _loader . add ( texture , function ( ) {
520+ this . initWithFile ( file , rect , capInsets ) ;
514521 this . dispatchEvent ( "load" ) ;
515522 } , this ) ;
523+ return false ;
516524 }
517-
518525 return this . initWithBatchNode ( new cc . SpriteBatchNode ( file , 9 ) , rect , false , capInsets ) ;
519526 } ,
520527
@@ -535,36 +542,18 @@ ccui.Scale9Sprite = cc.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprit
535542 capInsets = capInsets || cc . rect ( 0 , 0 , 0 , 0 ) ;
536543
537544 var texture = spriteFrame . getTexture ( ) ;
538- var loaded = texture . isLoaded ( ) ;
539-
545+ var loaded = this . _textureLoaded = texture . isLoaded ( ) ;
540546 this . _loader . clear ( ) ;
541- if ( loaded ) {
542- var batchNode = new cc . SpriteBatchNode ( spriteFrame . getTexture ( ) , 9 ) ;
543- // the texture is rotated on Canvas render mode, so isRotated always is false.
544- this . initWithBatchNode ( batchNode , spriteFrame . getRect ( ) , cc . _renderType === cc . game . RENDER_TYPE_WEBGL && spriteFrame . isRotated ( ) , capInsets ) ;
545- } else {
546- this . _capInsets . x = capInsets . x ;
547- this . _capInsets . y = capInsets . y ;
548- this . _capInsets . width = capInsets . width ;
549- this . _capInsets . height = capInsets . height ;
550- var textureCallback = function ( ) {
551- var preferredSize = this . _preferredSize ,
552- restorePreferredSize = preferredSize . width !== 0 && preferredSize . height !== 0 ;
553- if ( restorePreferredSize ) {
554- preferredSize = cc . size ( preferredSize . width , preferredSize . height ) ;
555- }
556- var batchNode = new cc . SpriteBatchNode ( spriteFrame . getTexture ( ) , 9 ) ;
557- // the texture is rotated on Canvas render mode, so isRotated always is false.
558- this . initWithBatchNode ( batchNode , spriteFrame . getRect ( ) , cc . _renderType === cc . game . RENDER_TYPE_WEBGL && spriteFrame . isRotated ( ) , this . _capInsets ) ;
559- if ( restorePreferredSize ) {
560- this . setPreferredSize ( preferredSize ) ;
561- }
562- this . _positionsAreDirty = true ;
547+ if ( ! loaded ) {
548+ this . _loader . add ( texture , function ( ) {
549+ this . initWithSpriteFrame ( spriteFrame , capInsets ) ;
563550 this . dispatchEvent ( "load" ) ;
564- } ;
565- this . _loader . add ( texture , textureCallback , this ) ;
551+ } , this ) ;
552+ return false ;
566553 }
567-
554+ var batchNode = new cc . SpriteBatchNode ( spriteFrame . getTexture ( ) , 9 ) ;
555+ // the texture is rotated on Canvas render mode, so isRotated always is false.
556+ this . initWithBatchNode ( batchNode , spriteFrame . getRect ( ) , cc . _renderType === cc . game . RENDER_TYPE_WEBGL && spriteFrame . isRotated ( ) , capInsets ) ;
568557 return true ;
569558 } ,
570559
@@ -870,10 +859,24 @@ ccui.Scale9Sprite = cc.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprit
870859 * @return True if update success, false otherwise.
871860 */
872861 updateWithSprite : function ( sprite , spriteRect , spriteFrameRotated , offset , originalSize , capInsets ) {
873- if ( ! sprite || ! sprite . getSpriteFrame ( ) ) {
862+ if ( ! sprite ) return false ;
863+
864+ this . _loader . clear ( ) ;
865+ this . _textureLoaded = sprite . _textureLoaded ;
866+ if ( ! sprite . _textureLoaded ) {
867+ this . _loader . add ( sprite , function ( ) {
868+ this . updateWithSprite ( sprite , spriteRect , spriteFrameRotated , offset , originalSize , capInsets ) ;
869+ this . dispatchEvent ( "load" ) ;
870+ } , this ) ;
874871 return false ;
875872 }
876873
874+ var spriteFrame = sprite . getSpriteFrame ( ) ;
875+ if ( spriteFrame && cc . _renderType === cc . game . RENDER_TYPE_CANVAS ) {
876+ spriteFrameRotated = false ;
877+ spriteRect = { x : 0 , y : 0 , height : spriteRect . height , width : spriteRect . width }
878+ }
879+
877880 this . _scale9Image = sprite ;
878881 if ( ! this . _scale9Image ) return false ;
879882
@@ -906,11 +909,8 @@ ccui.Scale9Sprite = cc.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprit
906909
907910 this . setState ( this . _brightState ) ;
908911 this . setContentSize ( size ) ;
909- if ( this . _spritesGenerated === true ) {
910- this . setOpacity ( opacity ) ;
911- this . setColor ( color ) ;
912- }
913- this . _spritesGenerated = true ;
912+ this . setOpacity ( opacity ) ;
913+ this . setColor ( color ) ;
914914 return true ;
915915 } ,
916916 /**
@@ -926,38 +926,22 @@ ccui.Scale9Sprite = cc.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprit
926926 return false ;
927927 }
928928
929- this . _capInsets . x = capInsets . x ;
930- this . _capInsets . y = capInsets . y ;
931- this . _capInsets . width = capInsets . width ;
932- this . _capInsets . height = capInsets . height ;
933-
934- var sprite = new cc . Sprite ( batchNode . getTexture ( ) ) ;
935- var pos = cc . p ( 0 , 0 ) ;
936- var originalSize = cc . size ( originalRect . width , originalRect . height ) ;
937-
929+ var texture = batchNode . getTexture ( ) ;
938930 this . _loader . clear ( ) ;
939- var tmpTexture = batchNode . getTexture ( ) ;
940- var loaded = this . _textureLoaded = tmpTexture . isLoaded ( ) ;
941- if ( loaded ) {
942- return this . updateWithSprite ( sprite , originalRect , rotated , pos , originalSize , capInsets ) ;
943- } else {
944- this . _loader . add ( tmpTexture , function ( sender ) {
945- var preferredSize = this . _preferredSize ,
946- restorePreferredSize = preferredSize . width !== 0 && preferredSize . height !== 0 ;
947- if ( restorePreferredSize ) {
948- preferredSize = cc . size ( preferredSize . width , preferredSize . height ) ;
949- }
931+ var loaded = this . _textureLoaded = texture . isLoaded ( ) ;
932+ if ( ! loaded ) {
933+ this . _loader . add ( texture , function ( ) {
950934 this . updateWithBatchNode ( batchNode , originalRect , rotated , capInsets ) ;
951- if ( restorePreferredSize ) {
952- this . setPreferredSize ( preferredSize ) ;
953- }
954- this . _positionsAreDirty = true ;
955- this . setVisible ( true ) ;
956- this . setNodeDirty ( ) ;
957935 this . dispatchEvent ( "load" ) ;
958936 } , this ) ;
959- this . setVisible ( false ) ;
937+ return false ;
960938 }
939+
940+ var sprite = new cc . Sprite ( texture ) ;
941+ var pos = cc . p ( 0 , 0 ) ;
942+ var originalSize = cc . size ( originalRect . width , originalRect . height ) ;
943+
944+ return this . updateWithSprite ( sprite , originalRect , rotated , pos , originalSize , capInsets ) ;
961945 } ,
962946
963947 /**
@@ -967,30 +951,20 @@ ccui.Scale9Sprite = cc.Scale9Sprite = cc.Node.extend(/** @lends ccui.Scale9Sprit
967951 */
968952 setSpriteFrame : function ( spriteFrame , capInsets ) {
969953 // Reset insets
970- if ( ! capInsets )
971- capInsets = cc . rect ( ) ;
972- var sprite = new cc . Sprite ( spriteFrame . getTexture ( ) ) ;
973- var locLoaded = spriteFrame . textureLoaded ( ) ;
974- this . _textureLoaded = locLoaded ;
954+ capInsets = capInsets || cc . rect ( ) ;
955+ var texture = spriteFrame . getTexture ( ) ;
956+ this . _textureLoaded = texture . _textureLoaded ;
975957 this . _loader . clear ( ) ;
976- if ( ! locLoaded ) {
977- this . _loader . add ( spriteFrame , function ( sender ) {
978- var preferredSize = this . _preferredSize ,
979- restorePreferredSize = preferredSize . width !== 0 && preferredSize . height !== 0 ;
980- if ( restorePreferredSize ) {
981- preferredSize = cc . size ( preferredSize . width , preferredSize . height ) ;
982- }
983- this . updateWithBatchNode ( this . _scale9Image , sender . getRect ( ) , cc . _renderType === cc . game . RENDER_TYPE_WEBGL && sender . isRotated ( ) , this . _capInsets ) ;
984- if ( restorePreferredSize ) {
985- this . setPreferredSize ( preferredSize ) ;
986- }
987- this . _positionsAreDirty = true ;
988- this . setNodeDirty ( ) ;
958+ if ( ! texture . _textureLoaded ) {
959+ this . _loader . add ( spriteFrame , function ( ) {
960+ this . setSpriteFrame ( spriteFrame , capInsets ) ;
989961 this . dispatchEvent ( "load" ) ;
990- } , this ) ;
962+ } , this ) ;
963+ return false ;
991964 }
992- this . updateWithSprite ( sprite , spriteFrame . getRect ( ) , spriteFrame . isRotated ( ) , spriteFrame . getOffset ( ) , spriteFrame . getOriginalSize ( ) , capInsets ) ;
993965
966+ var sprite = new cc . Sprite ( spriteFrame . getTexture ( ) ) ;
967+ this . updateWithSprite ( sprite , spriteFrame . getRect ( ) , spriteFrame . isRotated ( ) , spriteFrame . getOffset ( ) , spriteFrame . getOriginalSize ( ) , capInsets ) ;
994968 this . _insetLeft = capInsets . x ;
995969 this . _insetTop = capInsets . y ;
996970 this . _insetRight = this . _originalSize . width - this . _insetLeft - capInsets . width ;
0 commit comments