460 lines
9.6 KiB
JavaScript
460 lines
9.6 KiB
JavaScript
|
|
/*******************************************************************************
|
|
*
|
|
* Nx杂项方法
|
|
*
|
|
*
|
|
*
|
|
* 2021.12.10
|
|
******************************************************************************/
|
|
|
|
const NxDefine = require( "nx.define" );
|
|
const NxObject = require( "nx.object" );
|
|
|
|
// 全局方法注入
|
|
const importNxFuncs = function() {
|
|
|
|
// =========================================================================
|
|
//
|
|
// 日志快捷接口
|
|
//
|
|
// =========================================================================
|
|
|
|
nx.debug = function( key, ...args ) { nx.logger.debug( key, ...args ); };
|
|
nx.info = function( key, ...args ) { nx.logger.info( key, ...args ); };
|
|
nx.warn = function( key, ...args ) { nx.logger.warn( key, ...args ); };
|
|
nx.error = function( key, ...args ) { nx.logger.error( key, ...args ); };
|
|
|
|
// =========================================================================
|
|
//
|
|
// 消息提示快捷接口[外部重载]
|
|
//
|
|
// =========================================================================
|
|
|
|
// 警告框
|
|
nx.alert = function( _text, _key, _cb ) {
|
|
|
|
if( !nx.msgUI ) {
|
|
alert( _text || "" );
|
|
return;
|
|
}
|
|
|
|
_key = _key || "confirm";
|
|
nx.mbox( _text, [ _key ], _cb );
|
|
};
|
|
|
|
// 确认框
|
|
nx.mbox = function( _text, _keys, _cb, _tag ) {
|
|
|
|
if( !nx.msgUI ) {
|
|
let ret = confirm( _text );
|
|
_cb && _cb( ret ? "yes" : "no" );
|
|
return;
|
|
}
|
|
|
|
// 选项参数补全
|
|
if( nx.dt.arrEmpty( _keys ) ) {
|
|
_keys = [ 'ok' ];
|
|
}
|
|
|
|
// 弹窗
|
|
nx.msgUI.popBox( _text, _keys, _cb, _tag );
|
|
};
|
|
|
|
// 轻态提示
|
|
nx.tbox = function( _text ) {
|
|
|
|
if( !nx.msgUI ) {
|
|
nx.debug( _text );
|
|
return;
|
|
}
|
|
|
|
nx.msgUI.popTip( _text || "" );
|
|
};
|
|
|
|
// 跑马灯提示
|
|
nx.marquee = function( _text, _times = 1 ) {
|
|
|
|
if( nx.msgUI ) {
|
|
nx.msgUI.marquee( _text, _times );
|
|
}
|
|
};
|
|
|
|
// =========================================================================
|
|
//
|
|
// 错误对象操作
|
|
// { code: 0, desc: "" }
|
|
// =========================================================================
|
|
|
|
// 生成一个错误
|
|
nx.genError = function( _code, _desc, _log = false ) {
|
|
|
|
if( _code == null || _code == undefined ) {
|
|
nx.error( "无效错误" );
|
|
}
|
|
|
|
let err = ( _code == 0 ) ? null : {
|
|
code: _code,
|
|
desc: _desc || ( "Error" + _code )
|
|
};
|
|
|
|
// 打印输出
|
|
if( err && _log ) {
|
|
nx.error( nx.fmtError( err ) );
|
|
}
|
|
|
|
return err;
|
|
};
|
|
|
|
// 文本化错误
|
|
nx.fmtError = function( _e ) {
|
|
if( !_e ) return "";
|
|
let key = _e.desc || ( "Error" + _e.code );
|
|
return nx.text.getKey( key );
|
|
};
|
|
|
|
// 错误轻态提示
|
|
nx.tipError = function( _e ) {
|
|
if( _e ) {
|
|
nx.tbox( _e.desc || ( "Error" + _e.code ) );
|
|
}
|
|
};
|
|
|
|
// 错误弹窗提示
|
|
nx.boxError = function( _e ) {
|
|
if( _e ) {
|
|
nx.mbox( _e.desc || ( "Error" + _e.code ) );
|
|
}
|
|
};
|
|
|
|
// =========================================================================
|
|
//
|
|
// 窗口快捷接口
|
|
//
|
|
// =========================================================================
|
|
|
|
// 预加载窗口
|
|
nx.preloadWindow = function( _rkey, _cb ) {
|
|
|
|
nx.error( "方法暂未实现!" );
|
|
// // 解析键
|
|
// let args = nx.res.decodeRK( _rkey );
|
|
// if( args.bundle == "" || args.bundle == 'resources' ) {
|
|
// nx.wndMgr.preloadWnd( args.path, _cb );
|
|
// return;
|
|
// }
|
|
|
|
// // 外包需先走包加载
|
|
// nx.res.loadBundle( args.bundle, ( _e, _p ) => {
|
|
|
|
// if( _e ) {
|
|
// nx.dt.fnInvoke( _cb, _e );
|
|
// return;
|
|
// }
|
|
|
|
// nx.wndMgr.preloadWnd( args.path, _cb );
|
|
// } );
|
|
|
|
};
|
|
|
|
// 新建窗口
|
|
nx.createWindow = function( _rkey, _cb, _args, _showWait ) {
|
|
|
|
nx.error( "方法暂未实现!" );
|
|
|
|
// // 解析键
|
|
// let args = nx.res.decodeRK( _rkey );
|
|
// if( _rkey.indexOf( '|' ) > 0 && args.bundle != 'resources' ) {
|
|
|
|
// // 外包需先走包加载
|
|
// nx.res.loadBundle( args.bundle, ( _e, _p ) => {
|
|
|
|
// if( _e ) {
|
|
// nx.dt.fnInvoke( _cb, _e );
|
|
// return;
|
|
// }
|
|
|
|
// nx.createWindow( args.path, _cb, _args, _showWait );
|
|
// } );
|
|
|
|
// return;
|
|
// }
|
|
|
|
// // 如果在新建队列,则忽略
|
|
// nx.createQueue = nx.createQueue || [];
|
|
// if( nx.dt.arrMember( nx.createQueue, args.path ) ) {
|
|
// nx.error( nx.text.getKey( "ErrRepeatCreateWnd" ) + args.path );
|
|
// nx.dt.fnInvoke( _cb, nx.genError( ERR.COM.RepeatCreateInst ) );
|
|
// return;
|
|
// }
|
|
|
|
// // 创建
|
|
// nx.createQueue.push( _rkey );
|
|
// nx.wndMgr.create( args.path, _args, ( _e, _p ) => {
|
|
|
|
// nx.dt.arrDelete( nx.createQueue, ( _t ) => {
|
|
// return _t == _rkey;
|
|
// }, true );
|
|
|
|
// nx.dt.fnInvoke( _cb, _e, _p );
|
|
|
|
// }, _showWait );
|
|
|
|
};
|
|
|
|
// 查找窗口
|
|
nx.findWindow = function( _wid ) {
|
|
nx.error( "方法暂未实现!" );
|
|
|
|
// let wnd = nx.wndMgr.find( _wid );
|
|
// return wnd ? wnd.wcmp : null;
|
|
};
|
|
|
|
// 关闭窗口
|
|
nx.closeWindow = function( _wid ) {
|
|
nx.error( "方法暂未实现!" );
|
|
// nx.wndMgr.close( _wid );
|
|
};
|
|
|
|
// 关闭全部窗口
|
|
nx.closeWindowAll = function( _incmain ) {
|
|
nx.error( "方法暂未实现!" );
|
|
// nx.wndMgr.closeAll( _incmain );
|
|
};
|
|
|
|
// =========================================================================
|
|
//
|
|
// 场景快捷接口
|
|
//
|
|
// =========================================================================
|
|
|
|
// 预加载场景
|
|
nx.preloadScene = function( _rkey, _cb ) {
|
|
|
|
nx.error( "方法暂未实现!" );
|
|
|
|
// // 解析键
|
|
// let args = nx.res.decodeRK( _rkey );
|
|
// if( args.bundle == "" || args.bundle == 'resources' ) {
|
|
// nx.sceneMgr.preloadScene( args.path, _cb );
|
|
// return;
|
|
// }
|
|
|
|
// // 外包需先走包加载
|
|
// nx.res.loadBundle( args.bundle, ( _e, _p ) => {
|
|
|
|
// if( _e ) {
|
|
// nx.dt.fnInvoke( _cb, _e );
|
|
// return;
|
|
// }
|
|
|
|
// nx.sceneMgr.preloadScene( args.path, _cb );
|
|
// } );
|
|
|
|
};
|
|
|
|
// 新建场景
|
|
nx.createScene = function( _rkey, _cb, _args, _showWait ) {
|
|
|
|
nx.error( "方法暂未实现!" );
|
|
|
|
// // 解析键
|
|
// let args = nx.res.decodeRK( _rkey );
|
|
// if( _rkey.indexOf( '|' ) > 0 && args.bundle != 'resources' ) {
|
|
|
|
// // 外包需先走包加载
|
|
// nx.res.loadBundle( args.bundle, ( _e, _p ) => {
|
|
|
|
// if( _e ) {
|
|
// nx.dt.fnInvoke( _cb, _e );
|
|
// return;
|
|
// }
|
|
|
|
// nx.createScene( args.path, _cb, _args, _showWait );
|
|
// } );
|
|
|
|
// return;
|
|
// }
|
|
|
|
// // 如果在新建队列,则忽略
|
|
// nx.createQueue = nx.createQueue || [];
|
|
// if( nx.dt.arrMember( nx.createQueue, args.path ) ) {
|
|
// nx.error( nx.text.getKey( "ErrRepeatCreateScene" ) + args.path );
|
|
// nx.dt.fnInvoke( _cb, nx.genError( ERR.COM.RepeatCreateInst ) );
|
|
// return;
|
|
// }
|
|
|
|
// // 创建
|
|
// nx.createQueue.push( _rkey );
|
|
// nx.sceneMgr.create( args.path, _args, ( _e, _p ) => {
|
|
|
|
// nx.dt.arrDelete( nx.createQueue, ( _t ) => {
|
|
// return _t == _rkey;
|
|
// }, true );
|
|
|
|
// nx.dt.fnInvoke( _cb, _e, _p );
|
|
|
|
// }, _showWait );
|
|
|
|
};
|
|
|
|
// 查找场景
|
|
nx.getScene = function() {
|
|
return nx.sceneMgr.getScene();
|
|
};
|
|
|
|
// 关闭场景
|
|
nx.closeScene = function() {
|
|
nx.sceneMgr.close();
|
|
};
|
|
|
|
// =========================================================================
|
|
//
|
|
// 本地化快捷接口
|
|
//
|
|
// =========================================================================
|
|
|
|
// 获取当前系统语言
|
|
nx.getLocLanguage = function() {
|
|
return nx.frame.vget( "LocalLanguage" );
|
|
};
|
|
|
|
// 改变当前系统语言
|
|
nx.setLocLanguage = function( _lcode ) {
|
|
|
|
if( nx.dt.strEmpty( _lcode ) ) {
|
|
nx.error( "[语言]设定失败,无效参数." );
|
|
return;
|
|
}
|
|
|
|
let old = nx.getLocLanguage();
|
|
if( old != _lcode ) {
|
|
nx.frame.vset( "LocalLanguage", _lcode );
|
|
nx.storage.set( NxDefine.LK_Language, _lcode );
|
|
nx.debug( "[语言]改变:", _lcode );
|
|
}
|
|
|
|
};
|
|
|
|
// 获取当前方言
|
|
nx.getLocVoice = function() {
|
|
return nx.frame.vget( "LocalVoice" );
|
|
};
|
|
|
|
// 设置当前方言
|
|
nx.setLocVoice = function( _vtype ) {
|
|
|
|
if( nx.dt.strEmpty( _vtype ) ) {
|
|
nx.error( "[方言]设定失败,无效参数." );
|
|
return;
|
|
}
|
|
|
|
nx.frame.vset( "LocalVoice", _vtype );
|
|
nx.storage.set( "LKUsedLocVoice", _vtype );
|
|
nx.debug( "[方言]更变:", _vtype );
|
|
};
|
|
|
|
// =========================================================================
|
|
//
|
|
// 系统快捷接口
|
|
//
|
|
// =========================================================================
|
|
|
|
// [外部重载] 退出
|
|
nx.appQuit = function( _text, _tag ) {
|
|
nx.warn( "请外部重载!" );
|
|
// cc.director.end();
|
|
cc.game.end();
|
|
};
|
|
|
|
// 游戏重启
|
|
nx.restart = function() {
|
|
|
|
// SP:桥接模块清理一下
|
|
if( nx && nx.bridge && nx.bridge.mods ) {
|
|
nx.bridge.mods.uninstallAll();
|
|
}
|
|
|
|
cc.audioEngine.stopAll();
|
|
cc.game.restart();
|
|
};
|
|
|
|
// 提示重启
|
|
nx.tipRestart = function( _msg ) {
|
|
|
|
_msg = _msg || "ErrUnknown";
|
|
nx.mbox( _msg, [ "retry" ], ( _key, _box ) => {
|
|
nx.restart();
|
|
} );
|
|
|
|
};
|
|
|
|
// 获取当前平台
|
|
nx.getPlatform = function() {
|
|
|
|
let pt = "unknown";
|
|
switch( cc.sys.os ) {
|
|
case cc.sys.OS_ANDROID: pt = "android"; break;
|
|
case cc.sys.OS_IOS:
|
|
case cc.sys.OS_OSX: pt = "ios"; break;
|
|
default: break;
|
|
}
|
|
|
|
if( cc.sys.isBrowser ) {
|
|
pt += "-web";
|
|
}
|
|
|
|
return pt;
|
|
|
|
};
|
|
|
|
// 控制台输出指定对象的指定类型成员
|
|
nx.dumpMembers = function( _object, _typename ) {
|
|
|
|
if( !_object ) {
|
|
console.log( "空对象!" );
|
|
return;
|
|
}
|
|
|
|
let count = 0;
|
|
for( let k in _object ) {
|
|
var ele = _object[ k ];
|
|
if( !_typename || typeof ( ele ) == _typename ) {
|
|
console.log( "[%s]member(%d):%s", _typename || "all", ++count, k );
|
|
}
|
|
}
|
|
};
|
|
|
|
};
|
|
|
|
var NxMisc = cc.Class( {
|
|
|
|
extends: NxObject,
|
|
|
|
name: "NxMisc",
|
|
|
|
// 初始化
|
|
initialize: function( _args ) {
|
|
|
|
// USPER
|
|
if( !this._super( _args ) ) {
|
|
return false;
|
|
}
|
|
|
|
// 注入
|
|
importNxFuncs();
|
|
|
|
return true;
|
|
},
|
|
|
|
// 销毁
|
|
uninitialize: function() {
|
|
|
|
// USPER
|
|
return this._super();
|
|
},
|
|
|
|
} );
|
|
|
|
// 模块导出
|
|
module.exports = NxMisc; |