/******************************************************************************* * * 窗体管理器 * * 对scenes/wins二层进行自动置顶显示,其他界面透明度归零,减少DC次数 ******************************************************************************/ const { SCENE_TAG } = require( "define" ); const { PANELS, TPanel } = require( "bridge.panels" ); const DELAY_SECS = 0.01; cc.Class( { extends: cc.Component, properties: { root: { default: null, type: cc.Node }, }, // 开启 onEnable: function() { nx.bridge.ui = this; this.root = this.root || this.node; // 重建层级 this.node_list = []; this.addSceneNode( SCENE_TAG.scene, nx.gui.find( this.root, "scenes" ) ); this.addSceneNode( SCENE_TAG.win, nx.gui.find( this.root, "wins" ) ); this.addSceneNode( SCENE_TAG.panel, nx.gui.find( this.root, "panels" ) ); this.addSceneNode( SCENE_TAG.chat, nx.gui.find( this.root, "chat_tag" ) ); this.addSceneNode( SCENE_TAG.popup, nx.gui.find( this.root, "popups" ) ); this.addSceneNode( SCENE_TAG.plot, nx.gui.find( this.root, "plots" ) ); this.addSceneNode( SCENE_TAG.msg, nx.gui.find( this.root, "msg_tag" ) ); this.addSceneNode( SCENE_TAG.summon, nx.gui.find( this.root, "summon" ) ); // 界面层优化 this.scenes = this.getSceneNode( SCENE_TAG.scene ); this.wins = this.getSceneNode( SCENE_TAG.win ); // UI互斥层优化 this.open( true ); }, // 关闭 onDisable: function() { // UI互斥层优化 this.open( false ); nx.bridge.ui = null; }, // 储存节点 addSceneNode: function( tag, node ) { this.node_list[ tag ] = node; }, // 将节点add到指定的层 addToSceneNode: function( node, tag ) { if( !node ) { return; } var scene_node = this.node_list[ tag ] if( !scene_node ) { return; } scene_node.active = true; scene_node.addChild( node ); // 置顶 scene_node.nxOrder = scene_node.nxOrder || 0; node.zIndex = ++scene_node.nxOrder; }, // 专门hide UI层 hideOnUITag: function( tag ) { var scene_node = this.node_list[ SCENE_TAG.win ] if( tag != null ) { scene_node = this.node_list[ tag ] } if( scene_node ) { scene_node.active = false; } }, showOnUITag: function( tag ) { var scene_node = this.node_list[ SCENE_TAG.win ] if( tag != null ) { scene_node = this.node_list[ tag ] } if( scene_node ) { scene_node.active = true; } }, // 获取指定节点 getSceneNode: function( tag ) { if( tag == null ) { tag = SCENE_TAG.panel; } var node = this.node_list[ tag ]; return node; }, // 开关 open: function( _open ) { if( _open ) { // 关注全屏界面层 this.wins.on( "child-added", this.delayFresh.bind( this ) ); this.wins.on( "child-removed", this.doFresh.bind( this ) ); this.wins.on( "child-reorder", this.doFresh.bind( this ) ); } else { // 关注全屏界面层 this.wins.off( "child-added", this.delayFresh.bind( this ) ); this.wins.off( "child-removed", this.doFresh.bind( this ) ); this.wins.off( "child-reorder", this.doFresh.bind( this ) ); } }, // 刷新 doFresh: function() { // 全屏界面层检测 let chds = this.wins.children; if( chds.length > 0 ) { let find = false; for( let i = chds.length - 1; i >= 0; --i ) { let node = chds[ i ]; if( !find && node.active ) { find = true; node.opacity = 255; } else { node.opacity = 0; } } if( find ) { this.scenes.opacity = 0; if( nx.bridge.mainui ) { nx.bridge.mainui.show( false ); } return; } } // 主场景 this.scenes.opacity = 255; if( nx.bridge.mainui ) { nx.bridge.mainui.show( true ); } }, // 延迟刷新 delayFresh: function() { this.unscheduleAllCallbacks(); this.scheduleOnce( () => { this.doFresh(); }, DELAY_SECS ); }, // ================================================== // 窗口操作 // ================================================== // 打开窗口 openWindow: function( _wname, _params, _cb, _directly = false ) { // 配置查找 let info = PANELS[ _wname ]; if( nx.dt.objEmpty( info ) ) { nx.error( "$UI:无效窗口,打开失败!", _wname ); nx.dt.fnInvoke( _cb, false, "ErrOpenWindow" ); return; } // 非直接创建 if( !_directly && nx.bridge.popups && info.tag == SCENE_TAG.popup ) { nx.bridge.popups.push( _wname, _params, info ); if( nx.dt.fnGood( _cb ) ) { nx.error( `WindowManager:队列弹窗检查,暂不支持回调!` ); } return; } // 所属层级 let root = this.getSceneNode( info.tag ); if( !root || !root.children ) { nx.error( "$UI:无效标记,打开失败!", _wname, info.tag ); nx.dt.fnInvoke( _cb, false, "ErrOpenWindow" ); return; } nx.bridge.setTag2PanelInfos(_wname, _params, info.tag); // 查找&&新建 let wnd = root.getChildByName( _wname ); let panel = wnd ? wnd.view : null; if( !panel ) { panel = new TPanel(); panel.init( _wname, _params ? _params.__keys : null ); } // 打开 panel.open( _params || {}, _cb ); }, // 关闭窗口 closeWindow: function( _wname ) { // 配置查找 let info = PANELS[ _wname ]; if( nx.dt.objEmpty( info ) ) { nx.error( "$UI:无效窗口,关闭失败!", _wname ); return; } // 所属层级 let root = this.getSceneNode( info.tag ); if( !root || !root.children ) { nx.error( "$UI:无效标记,关闭失败!", _wname, info.tag ); return; } // 查找&&关闭 let wnd = root.getChildByName( _wname ); let panel = wnd ? wnd.view : null; if( panel ) { panel.close(); } }, // 彻底关闭非场景层所有窗口 cleanWindows: function() { let cleanLayer = function( _layer ) { if( !_layer || !_layer.parent ) { return; } _layer.children.forEach( _cd => { if( _cd.view ) { // if( _cd.view && _cd.name != "WndBattleScene" ) { _cd.view.close(); } } ); }; cleanLayer( this.getSceneNode( SCENE_TAG.chat ) ); cleanLayer( this.getSceneNode( SCENE_TAG.popup ) ); cleanLayer( this.getSceneNode( SCENE_TAG.panel ) ); cleanLayer( this.getSceneNode( SCENE_TAG.battleUI ) ); cleanLayer( this.getSceneNode( SCENE_TAG.win ) ); }, // 彻底关闭非场景层所有窗口 cleanWindowsByTag: function( _tag ) { let cleanLayer = function( _layer ) { if( !_layer || !_layer.parent ) { return; } _layer.children.forEach( _cd => { if( _cd.view ) { // if( _cd.view && _cd.name != "WndBattleScene" ) { _cd.view.close(); } } ); }; cleanLayer( this.getSceneNode( _tag ) ); }, } );