import EventComponent from "../base/EventComponent" ; import AudioComponent from "../base/AudioComponent" ; import { Manager } from "../Framework" ; description const { ccclass , property } = cc . _decorator ; export interface UIClass < T extends UIView > { new ( ) : T ; description getPrefabUrl ( ) : string ; } @ ccclass export default abstract class UIView extends EventComponent { description private _isEnableKey = false ; public static getPrefabUrl ( ) { if ( CC_DEBUG ) { cc . error ( `请求实现public static getPrefabUrl` ) ; } return "unknown" ; } description private _args : any [ ] = null ; description public get args ( ) { return this . _args ; } description param path param referenceNode protected find ( path , referenceNode ? ) { if ( referenceNode ) { return cc . find ( path , referenceNode ) ; } else { if ( this . content ) { return cc . find ( path , this . content ) ; } else { return cc . find ( path , referenceNode ) ; } } } description private _content : cc . Node = null ; protected set content ( value ) { this . _content = value ; } protected get content ( ) { return this . _content ; } /**本组件的类名 */ private _className : string = "unknow" ; public set className ( value ) { this . _className = value ; } public get className ( ) : string { return this . _className ; } private _bundle : BUNDLE_TYPE = null ; /**指向当前View打开时的bundle */ public set bundle ( value ) { this . _bundle = value ; } public get bundle ( ) { return this . _bundle ; } public close ( ) { Manager . uiManager . close ( this . className ) ; } description public show ( args ) { this . _args = args ; if ( this . node ) this . node . active = true ; } public hide ( ) { if ( this . node ) this . node . removeFromParent ( false ) ; } description param isOverrideShow param completeCallback example public showWithAction ( isOverrideShow = false , completeCallback ? ) { if ( this . content ) { if ( ! isOverrideShow ) this . show ( this . args ) ; this . content . stopAllActions ( ) ; cc . tween ( this . content ) . set ( { scale : 0.2 } ) . to ( 0.2 , { scale : 1.15 } ) . delay ( 0.05 ) . to ( 0.1 , { scale : 1.0 } ) . call ( ( ) => { if ( completeCallback ) completeCallback ( ) ; } ) . start ( ) ; } } description param isOverrideHide param completeCallback public hideWithAction ( completeCallback ? ) { if ( this . content ) { this . content . stopAllActions ( ) ; cc . tween ( this . content ) . to ( 0.2 , { scale : 1.15 } ) . to ( 0.1 , { scale : 0.3 } ) . call ( ( ) => { this . hide ( ) ; this . content . scale = 1.0 ; if ( completeCallback ) completeCallback ( ) ; } ) . start ( ) ; } } description param completeCallback public closeWithAction ( completeCallback ? ) { if ( this . content ) { this . content . stopAllActions ( ) ; cc . tween ( this . content ) . to ( 0.2 , { scale : 1.15 } ) . to ( 0.1 , { scale : 0.3 } ) . call ( ( ) => { if ( completeCallback ) completeCallback ( ) ; this . close ( ) ; } ) . start ( ) ; } } description param isEnabled example protected setEnabledKeyBack ( isEnabled ) { if ( isEnabled ) { cc . systemEvent . off ( cc . SystemEvent . EventType . KEY_UP , this . onKeyUp , this ) ; cc . systemEvent . on ( cc . SystemEvent . EventType . KEY_UP , this . onKeyUp , this ) ; } else { cc . systemEvent . off ( cc . SystemEvent . EventType . KEY_UP , this . onKeyUp , this ) ; } this . _isEnableKey = isEnabled ; } protected isEnabledKeyBack ( ) { return this . _isEnableKey ; } protected onKeyUp ( ev ) { if ( CC_DEBUG ) { cc . log ( `[${ cc . js . getClassName ( this ) }] onKeyUp keyCode : ${ ev . keyCode }` ) ; } if ( ev . keyCode == cc . macro . KEY . escape ) { this . onKeyBack ( ev ) ; } else { ev . stopPropagation ( ) ; } } protected onKeyBack ( ev ) { ev . stopPropagation ( ) ; } protected audioHelper : AudioComponent = null ; onLoad ( ) { this . audioHelper = this . addComponent ( AudioComponent ) ; this . audioHelper . owner = this ; super . onLoad ( ) ; } onDestroy ( ) { this . setEnabledKeyBack ( false ) ; this . enableFrontAndBackgroundSwitch = false ; super . onDestroy ( ) ; } private _enterBackgroundTime = 0 ; private _enableFrontAndBackgroundSwitch = false ; public set enableFrontAndBackgroundSwitch ( value ) { this . _enableFrontAndBackgroundSwitch = value ; if ( value ) { cc . game . on ( cc . game . EVENT_SHOW , this . _onEnterForgeGround , this ) ; cc . game . on ( cc . game . EVENT_HIDE , this . _onEnterBackground , this ) ; } else { cc . game . off ( cc . game . EVENT_SHOW , this . _onEnterForgeGround , this ) ; cc . game . off ( cc . game . EVENT_HIDE , this . _onEnterBackground , this ) ; } } public get enableFrontAndBackgroundSwitch ( ) { return this . _enableFrontAndBackgroundSwitch ; } protected _onEnterBackground ( ) { this . _enterBackgroundTime = Date . timeNow ( ) ; this . onEnterBackground ( ) ; } private _onEnterForgeGround ( ) { let now = Date . timeNow ( ) ; let inBackgroundTime = now - this . _enterBackgroundTime ; this . onEnterForgeground ( inBackgroundTime ) ; } protected onEnterForgeground ( inBackgroundTime ) { } protected onEnterBackground ( ) { } }