import UIView from "../ui/UIView" ; import { ResourceInfo , ResourceType , BUNDLE_RESOURCES } from "../base/Defines" ; import { Manager } from "../Framework" ; description export function addExtraLoadResource ( view , info ) { let uiManager = Manager . uiManager ; if ( view == < any > ( uiManager . retainMemory ) ) { uiManager . retainMemory . addLocal ( info ) ; } else if ( view && view instanceof UIView ) { uiManager . addLocal ( info , view . className ) ; } else { uiManager . garbage . addLocal ( info ) ; } } description export function addRemoteLoadResource ( view , info ) { let uiManager = Manager . uiManager ; if ( view == < any > ( uiManager . retainMemory ) ) { uiManager . retainMemory . addRemote ( info ) ; } else if ( view && view instanceof UIView ) { uiManager . addRemote ( info , view . className ) ; } else { uiManager . garbage . addRemote ( info ) ; } } description export function getBundle ( config ) { let bundle = BUNDLE_RESOURCES ; if ( config . bundle != BUNDLE_RESOURCES ) { if ( config . view ) { bundle = config . view . bundle ; } } return bundle ; } function isValidComponent ( component ) { if ( cc . isValid ( component ) && component . node && cc . isValid ( component . node ) ) { return true ; } return false ; } description param { * } view param { * } url param { * } sprite param { * } spriteFrame param { * } completeCallback param { * } resourceType param { * } retain param { * } isAtlas export function setSpriteSpriteFrame ( view , url , sprite , spriteFrame , completeCallback , bundle , resourceType = ResourceType . Local , retain = false , isAtlas = false ) { if ( ! isAtlas ) { let info = new ResourceInfo ; info . url = url ; info . type = cc . SpriteFrame ; info . data = spriteFrame ; info . retain = retain ; info . bundle = bundle ; if ( resourceType == ResourceType . Remote ) { addRemoteLoadResource ( view , info ) ; } else { addExtraLoadResource ( view , info ) ; } } if ( spriteFrame && isValidComponent ( sprite ) ) { let oldSpriteFrame = sprite . spriteFrame ; let replaceData = cc . isValid ( spriteFrame ) ? spriteFrame : null ; try { if ( replaceData ) sprite . spriteFrame = replaceData ; if ( completeCallback ) completeCallback ( replaceData ) ; } catch ( error ) { let temp = cc . isValid ( oldSpriteFrame ) ? oldSpriteFrame : null ; sprite . spriteFrame = temp ; if ( completeCallback ) completeCallback ( null ) ; cc . error ( `${ url } : ${ error ? error : "replace spriteframe error" }` ) ; } } else { if ( completeCallback && isValidComponent ( sprite ) ) completeCallback ( spriteFrame ) ; } } export enum ButtonSpriteMemberName { Norml = "normalSprite" , Pressed = "pressedSprite" , Hover = "hoverSprite" , Disable = "disabledSprite" } description param view param url param button param spriteFrame param memberName param completeCallback param isAtlas function _setSpriteFrame ( view , url , button , spriteFrame , memberName , completeCallback , isAtlas , bundle ) { if ( ! isAtlas ) { let info = new ResourceInfo ; info . url = url ; info . type = cc . SpriteFrame ; info . data = spriteFrame ; info . bundle = bundle ; addExtraLoadResource ( view , info ) ; } if ( spriteFrame && isValidComponent ( button ) ) { let oldSpriteFrame = button [ memberName ] ; try { let replaceData = cc . isValid ( spriteFrame ) ? spriteFrame : null ; if ( replaceData ) button [ memberName ] = replaceData ; if ( completeCallback ) completeCallback ( memberName , replaceData ) ; } catch ( error ) { let temp = cc . isValid ( oldSpriteFrame ) ? oldSpriteFrame : null ; button [ memberName ] = temp ; if ( completeCallback ) completeCallback ( memberName , null ) ; cc . error ( `${ url } : ${ error ? error : "replace spriteframe error" }` ) ; } } else { if ( completeCallback && isValidComponent ( button ) ) completeCallback ( memberName , spriteFrame ) ; } } ; description param button param memberName param view param url param spriteFrame param completeCallback param isAtlas function _setButtonSpriteFrame ( button , memberName , view , url , spriteFrame , completeCallback , bundle , isAtlas = false ) { if ( spriteFrame && isValidComponent ( button ) ) { _setSpriteFrame ( view , url , button , spriteFrame , memberName , completeCallback , isAtlas , bundle ) ; } else { if ( completeCallback && isValidComponent ( button ) ) completeCallback ( memberName , spriteFrame ) ; } } description param button param memberName param view param url param completeCallback function _setButtonWithType ( button , memberName , view , url , completeCallback ? , bundle ? ) { if ( url ) { if ( typeof url == "string" ) { Manager . cacheManager . getCacheByAsync ( url , cc . SpriteFrame , bundle ) . then ( ( spriteFrame ) => { _setButtonSpriteFrame ( button , memberName , view , url , spriteFrame , completeCallback , bundle ) ; } ) ; } else { Manager . cacheManager . getSpriteFrameByAsync ( url . urls , url . key , view , addExtraLoadResource , bundle ) . then ( ( data ) => { if ( data && data . isTryReload ) { } else { _setButtonSpriteFrame ( button , memberName , view , data . url , data . spriteFrame , completeCallback , bundle , true ) ; } } ) ; } } } description param button param config export function setButtonSpriteFrame ( button , config ) { let bundle = getBundle ( config ) ; _setButtonWithType ( button , ButtonSpriteMemberName . Norml , config . view , config . normalSprite , config . completeCallback , bundle ) ; _setButtonWithType ( button , ButtonSpriteMemberName . Pressed , config . view , config . pressedSprite , config . completeCallback , bundle ) ; _setButtonWithType ( button , ButtonSpriteMemberName . Hover , config . view , config . hoverSprite , config . completeCallback , bundle ) ; _setButtonWithType ( button , ButtonSpriteMemberName . Disable , config . view , config . disabledSprite , config . completeCallback , bundle ) ; } description param component param config param data export function setParticleSystemFile ( component , config , data ) { let info = new ResourceInfo ; info . url = config . url ; info . type = cc . ParticleAsset ; info . data = data ; info . bundle = getBundle ( config ) ; addExtraLoadResource ( config . view , info ) ; if ( data && isValidComponent ( component ) ) { let oldFile = component . file ; try { let replaceData = cc . isValid ( data ) ? data : null ; if ( replaceData ) component . file = replaceData ; if ( config . completeCallback ) config . completeCallback ( replaceData ) ; } catch ( error ) { let temp = cc . isValid ( oldFile ) ? oldFile : null ; component . file = temp ; if ( config . completeCallback ) config . completeCallback ( null ) ; cc . error ( `${ config . url } : ${ error ? error : "replace file error" }` ) ; } } else { if ( config . completeCallback && isValidComponent ( component ) ) config . completeCallback ( data ) ; } } description param component param config param data export function setLabelFont ( component , config , data ) { let info = new ResourceInfo ; info . url = config . font ; info . type = cc . Font ; info . data = data ; info . bundle = getBundle ( config ) ; addExtraLoadResource ( config . view , info ) ; if ( data && isValidComponent ( component ) ) { let oldFont = component . font ; try { let replaceData = cc . isValid ( data ) ? data : null ; if ( replaceData ) component . font = replaceData ; if ( config . completeCallback ) config . completeCallback ( replaceData ) ; } catch ( error ) { let temp = cc . isValid ( oldFont ) ? oldFont : null ; component . font = temp ; if ( config . completeCallback ) config . completeCallback ( null ) ; cc . error ( `${ config . font } : ${ error ? error : "replace font error" }` ) ; } } else { if ( config . completeCallback && isValidComponent ( component ) ) config . completeCallback ( data ) ; } } description param component param config param data export function setSkeletonSkeletonData ( component , config , data , resourceType = ResourceType . Local ) { let url = "" ; let retain = false ; if ( resourceType == ResourceType . Remote ) { let realConfig = < any > config ; url = `${ realConfig . path }/${ realConfig . name }` ; retain = realConfig . retain ? true : false ; } else { let realConfig = < any > config ; url = realConfig . url ; } let info = new ResourceInfo ; info . url = url ; info . type = sp . SkeletonData ; info . data = data ; info . retain = retain ; info . bundle = getBundle ( config ) ; if ( resourceType == ResourceType . Remote ) { addRemoteLoadResource ( config . view , info ) ; } else { addExtraLoadResource ( config . view , info ) ; } if ( data && isValidComponent ( component ) ) { let oldSkeletonData = component . skeletonData ; try { let replaceData = cc . isValid ( data ) ? data : null ; if ( replaceData ) component . skeletonData = replaceData ; if ( config . completeCallback ) config . completeCallback ( replaceData ) ; } catch ( error ) { let temp = cc . isValid ( oldSkeletonData ) ? oldSkeletonData : null ; component . skeletonData = temp ; if ( config . completeCallback ) config . completeCallback ( null ) ; cc . error ( `${ url } : ${ error ? error : "replace skeletonData error" }` ) ; } } else { if ( config . completeCallback && isValidComponent ( component ) ) config . completeCallback ( data ) ; } } description param config export function createNodeWithPrefab ( config , data ) { let info = new ResourceInfo ; info . url = config . url ; info . type = cc . Prefab ; info . data = data ; info . bundle = getBundle ( config ) ; addExtraLoadResource ( config . view , info ) ; if ( data && isValidComponent ( config . view ) && config . completeCallback ) { let node = cc . instantiate ( data ) ; config . completeCallback ( node ) ; } }