// 大资源更新弹窗 const JMButton = require('JMButton'); const UpdateProgress = require('UpdateProgress'); cc.Class({ editor: { menu: 'Launch/HotUpdateAlert' }, extends: cc.Component, properties: { contentLabel: cc.Label, wifiNode: cc.Node, updateProgress: UpdateProgress, countdownLabel: cc.Label, leftBtn: JMButton, rightBtn: JMButton }, onLoad () { this.initStatusInfo(); this.countdownLabel.node.active = false; this._countDownFinish = false; // 弹框居中展示 let y = cc.winSize.height / 2; let x = cc.winSize.width / 2; let newVec2 = this.node.convertToNodeSpaceAR(cc.v2(x, y)); this.node.y = newVec2.y; this.node.x = newVec2.x; }, onEnable () { cc.game.on('e_mgr_launch_update_progression', this._handleUpdateProgression, this); cc.game.on('e_mgr_launch_update_finished', this._handleUpdateFinished, this); cc.game.on('e_mgr_launch_update_failed', this._handleUpdateFailed, this); this.reloadView(); }, onDisable () { cc.game.targetOff(this); }, initStatusInfo () { this.status = undefined; let remoteUpdateInfo = G.RemoteUpdateInfoMgr.remoteUpdateInfo; this.isForceUpdate = remoteUpdateInfo.newResForce; // 非强更按钮布局与回调 this.forceBtnMap = { [JMC.UPDATE_STATUS.UNSTART]: [ {active: true, text: '退出游戏', cb: this.exitGameOnClicked}, {active: true, text: '立即更新', cb: this.startDownloadOnClicked} ], [JMC.UPDATE_STATUS.DOWNLOADING]: [ {active: false}, {active: false} ], [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: [ {active: true, text: '退出游戏', cb: this.exitGameOnClicked}, {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked} ], [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: [ {active: true, text: '重启游戏', cb: this.restartOnClicked}, {active: false} ], [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: [ {active: true, text: '退出游戏', cb: this.exitGameOnClicked}, {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked} ] }; // 非强更按钮布局与回调 this.unforceBtnMap = { [JMC.UPDATE_STATUS.UNSTART]: [ {active: true, text: '暂不更新', cb: this.skipUpdateOnClicked}, {active: true, text: '立即更新', cb: this.startDownloadOnClicked} ], [JMC.UPDATE_STATUS.DOWNLOADING]: [ {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked, countdown: true}, {active: false} ], [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: [ {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked}, {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked} ], [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: [ {active: true, text: '重启游戏', cb: this.restartOnClicked}, {active: false} ], [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: [ {active: true, text: '隐藏弹窗', cb: this.hideUpdateOnClicked}, {active: true, text: '继续下载', cb: this.resumeDownloadOnClicked} ] }; // 弹框描述内容 this.infoMap = { [JMC.UPDATE_STATUS.UNSTART]: `发现更新,更新大小:${G.UpdateUtils.bytesToMb(G.HotUpdateMgr.realTotalBytes).toFixed(1)}M,是否立即更新?`, [JMC.UPDATE_STATUS.DOWNLOADING]: '下载中,请保持网络畅通', [JMC.UPDATE_STATUS.DOWNLOAD_PAUSE]: '检测到网络变更,下载被暂停', [JMC.UPDATE_STATUS.DOWNLOAD_FINISHED]: '下载完成,立即启动游戏', [JMC.UPDATE_STATUS.DOWNLOAD_FAILED]: '下载失败,请保持网络畅通再试' }; }, reloadView () { let status = G.HotUpdateMgr.status; // 刷新非WiFi的警告 this.wifiNode.active = (cc.sys.getNetworkType() == cc.sys.NetworkType.WWAN) ? true : false; // 刷新进度条 this.updateProgress.setDownloadStatus(status); this.updateProgress.setProgress(G.HotUpdateMgr.downloadedBytes, G.HotUpdateMgr.realTotalBytes); // 以下是状态更新了才刷新 if (this.status === status) return; this.status = status; this._stStatusChange(status); // 更新弹框描述 this.contentLabel.string = this.infoMap[status]; // 更新按钮 let btnMap = this.isForceUpdate ? this.forceBtnMap : this.unforceBtnMap; let btnInfo = btnMap[status]; // 设置左边的按钮 let leftBtnInfo = btnInfo[0]; if (leftBtnInfo.active) { this.leftBtn.node.active = true; this.leftBtn.title = leftBtnInfo.text; let cb = this.leftBtn.node.cb; if (cb) { this.leftBtn.node.off('click', cb, this); } this.leftBtn.node.cb = leftBtnInfo.cb; this.leftBtn.node.on('click', leftBtnInfo.cb, this); if (leftBtnInfo.countdown && !this._countDownFinish) { this.countdownLabel.node.active = true; let times = 10; this.leftBtn.interactable = false; this.schedule( () => { times--; this.countdownLabel.string = `${times}秒后可点击`; if (times <= 0) { this.leftBtn.interactable = true; this.countdownLabel.node.active = false; this._countDownFinish = true; } }, 1, 10 ); } } else { this.leftBtn.node.active = false; } // 设置右侧按钮 let rightBtnInfo = btnInfo[1]; if (rightBtnInfo.active) { this.rightBtn.node.active = true; this.rightBtn.title = rightBtnInfo.text; let cb = this.rightBtn.node.cb; if (cb) { this.rightBtn.node.off('click', cb, this); } this.rightBtn.node.cb = rightBtnInfo.cb; this.rightBtn.node.on('click', rightBtnInfo.cb, this); } else { this.rightBtn.node.active = false; } }, // 退出游戏 exitGameOnClicked () { if (!G.MiddleAuth.exitGame()) { cc.game.end(); } }, // 立即更新 startDownloadOnClicked () { G.HotUpdateMgr.gotoUpdate(); }, // 继续下载 resumeDownloadOnClicked () { G.HotUpdateMgr.gotoUpdate(); }, // 重启游戏 restartOnClicked () { G.HotUpdateMgr.restartApp(); }, // 暂不更新 skipUpdateOnClicked () { cc.game.emit('e_launch_update_show_progress'); // 在非更新场景时,展示顶部进度条 cc.game.emit('e_launch_enter_scene'); // 在更新场景时,进入下一个场景 this.node.active = false; }, // 隐藏弹窗 hideUpdateOnClicked () { cc.game.emit('e_launch_update_show_progress'); // 在非更新场景时,展示顶部进度条 cc.game.emit('e_launch_enter_scene'); // 在更新场景时,进入下一个场景 this.node.active = false; }, _handleUpdateProgression () { this.reloadView(); }, _handleUpdateFinished () { this.reloadView(); }, _handleUpdateFailed () { this.reloadView(); } });