UIManager.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /** @format */
  2. /**
  3. * UIManager界面管理类
  4. *
  5. * 1.打开界面,根据配置自动加载界面、调用初始化、播放打开动画、隐藏其他界面、屏蔽下方界面点击
  6. * 2.关闭界面,根据配置自动关闭界面、播放关闭动画、恢复其他界面
  7. * 3.切换界面,与打开界面类似,但是是将当前栈顶的界面切换成新的界面(先关闭再打开)
  8. * 4.提供界面缓存功能
  9. *
  10. */
  11. import {BaseUI, UIShowTypes} from '../ui/BaseUI'
  12. import {IUIConfig, UICfg} from '../config/UICfg'
  13. import {ccUtils} from '../utils/ccUtils'
  14. import {UI} from '../enums/UI'
  15. import {Data, Mgr} from '../GameControl'
  16. import {Log} from '../utils/LogUtils'
  17. import {IOperateNeed} from '../interface/GlobalInterface'
  18. import {IMessage, IReward, IRewardNty, ItemUICfg} from '../interface/UIInterface'
  19. import {SOUND} from '../enums/Sound'
  20. /** UI栈结构体 */
  21. export interface UIInfo {
  22. uiID: number
  23. baseUI: BaseUI
  24. uiArgs: any
  25. uiCfg: IUIConfig
  26. preventNode?: cc.Node
  27. aniNode?: cc.Node
  28. zOrder?: number
  29. isClose?: boolean
  30. hideTime?: number
  31. prefab?: cc.Prefab
  32. }
  33. export interface GroupUIInfo {
  34. uiID: number
  35. uiArgs?: any
  36. }
  37. export class UIManager {
  38. /** 是否正在关闭UI */
  39. private isClosing = false
  40. /** 是否正在打开UI */
  41. private isOpening = false
  42. /** UI界面缓存(key为UIId,value为BaseUI节点)*/
  43. private UICache: {[UIId: number]: UIInfo} = {}
  44. /** UI界面栈({UIID + BaseUI + UIArgs}数组)*/
  45. private UIStack: UIInfo[] = []
  46. /** UI待打开列表 */
  47. private UIOpenQueue: UIInfo[] = []
  48. /** UI待关闭列表 */
  49. private UICloseQueue: number[] = []
  50. /** UI配置 */
  51. private releaseTimeID: any
  52. /** 打开一组UI */
  53. private groupUIs: GroupUIInfo[] = []
  54. private curGroupUI: GroupUIInfo = null
  55. public prefabBundle: cc.AssetManager.Bundle = null
  56. /** UI打开前回调 */
  57. public uiOpenBeforeDelegate: (uiID: number, preUIId: number) => void = null
  58. /** UI打开回调 */
  59. public uiOpenDelegate: (uiID: number, preUIId: number) => void = null
  60. /** UI关闭回调 */
  61. public uiCloseDelegate: (uiID: number) => void = null
  62. public async init() {
  63. if (this.releaseTimeID) clearInterval(this.releaseTimeID)
  64. this.releaseTimeID = setInterval(this.cleanUI.bind(this), 15000)
  65. Data.main.texBundle = await ccUtils.getBundleAsync('texture')
  66. this.prefabBundle = await ccUtils.getBundleAsync('prefab')
  67. //加载全局通用预制体
  68. this.prefabBundle.loadDir('items', cc.Prefab, (error, assets: cc.Prefab[]) => {
  69. if (error) {
  70. Log.error(error)
  71. } else {
  72. for (let j = 0; j < assets.length; j++) {
  73. Data.main.itemsPrefabMap.set(assets[j].name, assets[j])
  74. let pool = Data.main.itemsPoolMap.get(assets[j].name)
  75. if (!pool) {
  76. pool = new cc.NodePool()
  77. Data.main.itemsPoolMap.set(assets[j].name, pool)
  78. }
  79. }
  80. }
  81. })
  82. return this.prefabBundle != null
  83. }
  84. /****************** 私有方法,UIManager内部的功能和基础规则 *******************/
  85. /**
  86. * 添加防触摸层
  87. * @param zOrder 屏蔽层的层级
  88. */
  89. private preventTouch(zOrder: number) {
  90. let node = new cc.Node()
  91. node.name = 'preventTouch'
  92. node.setContentSize(cc.winSize)
  93. node.on(
  94. cc.Node.EventType.TOUCH_START,
  95. (event: cc.Event.EventCustom) => {
  96. event.stopPropagation()
  97. },
  98. node,
  99. )
  100. let child = cc.director.getScene().getChildByName('Canvas')
  101. child.addChild(node, zOrder)
  102. return node
  103. }
  104. /** 自动执行下一个待关闭或待打开的界面 */
  105. private autoExecNextUI() {
  106. // 逻辑上是先关后开
  107. if (this.UICloseQueue.length > 0) {
  108. let closeUI = this.UICloseQueue[0]
  109. this.UICloseQueue.splice(0, 1)
  110. this.hide(closeUI)
  111. } else if (this.UIOpenQueue.length > 0) {
  112. let uiQueueInfo = this.UIOpenQueue[0]
  113. this.UIOpenQueue.splice(0, 1)
  114. this.show(uiQueueInfo.uiID, uiQueueInfo.uiArgs)
  115. }
  116. }
  117. /**
  118. * 自动检测动画组件以及特定动画,如存在则播放动画,无论动画是否播放,都执行回调
  119. * @param aniName 动画名
  120. * @param aniOverCallback 动画播放完成回调
  121. */
  122. private autoExecAnimation(baseUI: BaseUI, aniName: string, aniOverCallback: () => void) {
  123. // 暂时先省略动画播放的逻辑
  124. let node = baseUI.node
  125. switch (aniName) {
  126. case 'uiOpen':
  127. if (
  128. baseUI.showType >= UIShowTypes.UIAddition &&
  129. ![UI.CurrencyUI, UI.LoadingUI, UI.TipUI, UI.PowerUpUI, UI.GameLoadingUI].includes(baseUI.uiID)
  130. ) {
  131. Mgr.audio.playSFX(SOUND.uiPop)
  132. node.setScale(0)
  133. node.opacity = 0
  134. cc.tween(node)
  135. .to(0.3, {scale: 1.1}, {easing: 'backOut'}) // 缩放动作,0.3秒内从 0 缩放到 1,使用 backOut 缓动效果
  136. .to(0.3, {opacity: 255}) // 渐显动作,0.1秒内从透明度 0 到 255
  137. .union()
  138. .to(0.1, {scale: 1}) // 缩小一点,形成回弹效果
  139. .call(aniOverCallback)
  140. .start()
  141. } else {
  142. aniOverCallback()
  143. }
  144. break
  145. case 'uiClose':
  146. aniOverCallback()
  147. break
  148. }
  149. }
  150. /**
  151. * 异步加载一个UI的prefab,成功加载了一个prefab之后
  152. * @param uiID 界面id
  153. * @param processCallback 加载进度回调
  154. * @param completeCallback 加载完成回调
  155. * @param uiArgs 初始化参数
  156. */
  157. private getOrCreateUI(
  158. uiID: number,
  159. processCallback: any,
  160. completeCallback: (baseUI: BaseUI, prefab?: cc.Prefab) => void,
  161. uiArgs: any,
  162. ): void {
  163. // 如果找到缓存对象,则直接返回
  164. let baseUI: BaseUI
  165. let cacheUIInfo = this.UICache[uiID]
  166. if (cacheUIInfo) {
  167. completeCallback(cacheUIInfo.baseUI)
  168. return
  169. }
  170. // 找到UI配置
  171. let uiPath = UICfg.get(uiID).url
  172. if (null == uiPath) {
  173. cc.log(`getOrCreateUI ${uiID} faile, prefab conf not found!`)
  174. completeCallback(null)
  175. return
  176. }
  177. this.prefabBundle.load(uiPath, cc.Prefab, processCallback, (err: Error, prefab: cc.Prefab) => {
  178. // 检查加载资源错误
  179. if (err) {
  180. cc.error(`getOrCreateUI loadRes ${uiID} faile, path: ${uiPath} error: ${err}`)
  181. completeCallback(null)
  182. return
  183. }
  184. // 检查实例化错误
  185. let uiNode: cc.Node = cc.instantiate(prefab)
  186. if (null == uiNode) {
  187. cc.error(`getOrCreateUI instantiate ${uiID} faile, path: ${uiPath}`)
  188. completeCallback(null)
  189. cc.assetManager.releaseAsset(prefab)
  190. return
  191. }
  192. // 检查组件获取错误
  193. baseUI = uiNode.getComponent(BaseUI)
  194. if (null == baseUI) {
  195. cc.error(`getOrCreateUI getComponent ${uiID} faile, path: ${uiPath}`)
  196. uiNode.destroy()
  197. completeCallback(null)
  198. cc.assetManager.releaseAsset(prefab)
  199. return
  200. }
  201. baseUI.uiID = uiID
  202. baseUI.init(uiArgs)
  203. completeCallback(baseUI, prefab)
  204. })
  205. }
  206. /**
  207. * UI被打开时回调,对UI进行初始化设置,刷新其他界面的显示,并根据
  208. * @param uiID 哪个界面被打开了
  209. * @param baseUI 界面对象
  210. * @param uiInfo 界面栈对应的信息结构
  211. * @param uiArgs 界面初始化参数
  212. */
  213. private onUIOpen(uiID: number, baseUI: BaseUI, uiInfo: UIInfo, uiArgs: any) {
  214. if (null == baseUI) {
  215. return
  216. }
  217. // 激活界面
  218. uiInfo.baseUI = baseUI
  219. baseUI.node.active = true
  220. let realZIndex = uiInfo.zOrder + baseUI.showType
  221. baseUI.node.zIndex = realZIndex
  222. if (baseUI.prevent) {
  223. let blockInputEvents = baseUI.node.getChildByName('blockInputEvents')
  224. if (!blockInputEvents) {
  225. blockInputEvents = new cc.Node()
  226. blockInputEvents.name = 'blockInputEvents'
  227. blockInputEvents.setContentSize(cc.winSize)
  228. blockInputEvents.addComponent(cc.BlockInputEvents)
  229. baseUI.node.addChild(blockInputEvents, -9999)
  230. }
  231. // 这里直接给node添加BlockInputEvents导致子节点setSwallowTouches无法吞噬,改成上面的实现方式
  232. // baseUI.node.setContentSize(cc.winSize)
  233. // if (!baseUI.node.getComponent(cc.BlockInputEvents)) baseUI.node.addComponent(cc.BlockInputEvents)
  234. }
  235. // 快速关闭界面的设置,绑定界面中的background,实现快速关闭
  236. if (baseUI.quickClose) {
  237. let backGround = baseUI.node.getChildByName('background')
  238. if (!backGround) {
  239. backGround = new cc.Node()
  240. backGround.name = 'background'
  241. backGround.setContentSize(cc.winSize)
  242. baseUI.node.addChild(backGround, -1)
  243. }
  244. backGround.targetOff(cc.Node.EventType.TOUCH_START)
  245. backGround.on(
  246. cc.Node.EventType.TOUCH_START,
  247. (event: cc.Event.EventCustom) => {
  248. event.stopPropagation()
  249. this.hideBaseUI(baseUI)
  250. },
  251. backGround,
  252. )
  253. }
  254. // 添加到场景中
  255. let child = cc.director.getScene().getChildByName('Canvas')
  256. child.addChild(baseUI.node)
  257. // 从那个界面打开的
  258. let fromUIID = 0
  259. if (this.UIStack.length > 1) {
  260. fromUIID = this.UIStack[this.UIStack.length - 2].uiID
  261. }
  262. // 打开界面之前回调
  263. if (this.uiOpenBeforeDelegate) {
  264. this.uiOpenBeforeDelegate(uiID, fromUIID)
  265. }
  266. // 执行onOpen回调 下一帧执行保证onLoad onEnable先执行
  267. baseUI.scheduleOnce(() => {
  268. if (!uiInfo.isClose && baseUI.node.activeInHierarchy) baseUI.onShow(uiArgs, fromUIID)
  269. })
  270. this.autoExecAnimation(baseUI, 'uiOpen', () => {
  271. baseUI.onOpenAniOver()
  272. if (this.uiOpenDelegate) {
  273. this.uiOpenDelegate(uiID, fromUIID)
  274. }
  275. })
  276. }
  277. /** 打开界面并添加到界面栈中 */
  278. public show(uiID: number, uiArgs: any = null, progressCallback: Function = null): void {
  279. let uiInfo: UIInfo = {
  280. uiCfg: UICfg.get(uiID),
  281. uiID: uiID,
  282. uiArgs: uiArgs,
  283. baseUI: null,
  284. }
  285. if (this.isOpening || this.isClosing) {
  286. // 插入待打开队列
  287. let openIndex = this.UIOpenQueue.findIndex(item => item.uiID == uiID)
  288. if (openIndex >= 0) this.UIOpenQueue.splice(openIndex, 1)
  289. this.UIOpenQueue.push(uiInfo)
  290. return
  291. }
  292. let uiInfoTmp = this.getUIInfo(uiID)
  293. if (uiInfoTmp) {
  294. // 重复打开了同一个界面
  295. this.hide(uiID)
  296. this.show(uiID, uiArgs)
  297. return
  298. }
  299. // 设置UI的zOrder
  300. uiInfo.zOrder = this.UIStack.length
  301. this.UIStack.push(uiInfo)
  302. // 先屏蔽点击
  303. uiInfo.preventNode = this.preventTouch(10000)
  304. this.isOpening = true
  305. // 预加载资源,并在资源加载完成后自动打开界面
  306. this.getOrCreateUI(
  307. uiID,
  308. progressCallback,
  309. (baseUI: BaseUI, prefab: cc.Prefab): void => {
  310. if (uiInfo.preventNode) {
  311. uiInfo.preventNode.destroy()
  312. uiInfo.preventNode = null
  313. }
  314. // 如果界面已经被关闭或创建失败
  315. if (uiInfo.isClose || null == baseUI) {
  316. cc.log(`getOrCreateUI ${uiID} faile!
  317. close state : ${uiInfo.isClose} , baseUI : ${baseUI}`)
  318. this.isOpening = false
  319. return
  320. }
  321. if (prefab) uiInfo.prefab = prefab
  322. this.onUIOpen(uiID, baseUI, uiInfo, uiArgs)
  323. this.isOpening = false
  324. this.autoExecNextUI()
  325. },
  326. uiArgs,
  327. )
  328. }
  329. /** 显示一组UI */
  330. showUIs(UIs: GroupUIInfo[]) {
  331. if (!UIs) return
  332. UIs = UIs.filter(value => value)
  333. if (UIs.length <= 0) return
  334. this.groupUIs = UIs
  335. this.curGroupUI = this.groupUIs.splice(0, 1)[0]
  336. this.show(this.curGroupUI.uiID, this.curGroupUI.uiArgs)
  337. }
  338. /** 替换栈顶界面 */
  339. public replace(uiID: number, uiArgs: any = null) {
  340. this.hideBaseUI(this.UIStack[this.UIStack.length - 1].baseUI)
  341. this.show(uiID, uiArgs)
  342. }
  343. /**
  344. * 关闭当前界面
  345. * @param closeUI 要关闭的界面
  346. */
  347. public hideBaseUI(closeUI?: BaseUI) {
  348. let uiCount = this.UIStack.length
  349. if (uiCount < 1 || this.isClosing || this.isOpening) {
  350. if (closeUI) {
  351. // 插入待关闭队列
  352. let closeIndex = this.UICloseQueue.findIndex(v => v == closeUI.uiID)
  353. if (closeIndex >= 0) this.UICloseQueue.splice(closeIndex, 1)
  354. this.UICloseQueue.push(closeUI.uiID)
  355. }
  356. return
  357. }
  358. let uiInfo: UIInfo
  359. if (closeUI) {
  360. let isSplice = false
  361. for (let index = this.UIStack.length - 1; index >= 0; index--) {
  362. let ui = this.UIStack[index]
  363. if (ui.baseUI === closeUI) {
  364. uiInfo = ui
  365. isSplice = true
  366. this.UIStack.splice(index, 1)
  367. break
  368. }
  369. }
  370. if (isSplice) this.resetUIStackZIndex()
  371. // 找不到这个UI
  372. if (uiInfo === undefined) {
  373. let index = this.UIOpenQueue.findIndex(value => value.uiID == closeUI.uiID)
  374. if (index >= 0) {
  375. this.UIOpenQueue.splice(index, 1)
  376. }
  377. this.autoExecNextUI()
  378. return
  379. }
  380. } else {
  381. uiInfo = this.UIStack.pop()
  382. }
  383. // 关闭当前界面
  384. let uiID = uiInfo.uiID
  385. let baseUI = uiInfo.baseUI
  386. uiInfo.isClose = true
  387. // 回收遮罩层
  388. if (uiInfo.preventNode) {
  389. uiInfo.preventNode.destroy()
  390. uiInfo.preventNode = null
  391. }
  392. if (null == baseUI) {
  393. return
  394. }
  395. this.isClosing = true
  396. let preUIInfo = null
  397. for (let i = uiCount - 2; i >= 0; i--) {
  398. if (this.UIStack[i] && this.UIStack[i].baseUI.showType == baseUI.showType) {
  399. preUIInfo = this.UIStack[i]
  400. break
  401. }
  402. }
  403. let close = () => {
  404. this.isClosing = false
  405. // 显示之前的界面
  406. if (preUIInfo && preUIInfo.baseUI && preUIInfo.baseUI.node.activeInHierarchy) {
  407. // 回调onTop onTop只是当前层级生效
  408. preUIInfo.baseUI.onTop(uiID)
  409. }
  410. if (this.uiCloseDelegate) {
  411. this.uiCloseDelegate(uiID)
  412. }
  413. this.releaseUI(uiInfo)
  414. baseUI.onHide()
  415. this.autoExecNextUI()
  416. if (this.groupUIs.length > 0) {
  417. this.curGroupUI = this.groupUIs.splice(0, 1)[0]
  418. this.show(this.curGroupUI.uiID, this.curGroupUI.uiArgs)
  419. }
  420. }
  421. // 执行关闭动画
  422. this.autoExecAnimation(baseUI, 'uiClose', close)
  423. }
  424. /**
  425. * 关闭当前界面
  426. * @param uiID 要关闭的界面
  427. */
  428. public hide(uiID: UI) {
  429. let uiCount = this.UIStack.length
  430. if (uiCount < 1 || this.isClosing || this.isOpening) {
  431. if (uiID) {
  432. // 插入待关闭队列
  433. const openIndex = this.UIOpenQueue.findIndex(v => v.uiID == uiID)
  434. if (openIndex != -1) {
  435. this.UIOpenQueue.splice(openIndex, 1)
  436. } else {
  437. this.UICloseQueue.push(uiID)
  438. }
  439. }
  440. return
  441. }
  442. if (this.getUI(uiID)) this.hideBaseUI(this.getUI(uiID))
  443. }
  444. /** 关闭所有界面 */
  445. public closeAll(remainUIs: UI[] = []) {
  446. // 不播放动画,也不清理缓存
  447. let remainUIInfo = []
  448. for (const uiInfo of this.UIStack) {
  449. if (remainUIs.includes(uiInfo.uiID)) {
  450. remainUIInfo.push(uiInfo)
  451. continue
  452. }
  453. uiInfo.isClose = true
  454. if (uiInfo.preventNode) {
  455. uiInfo.preventNode.destroy()
  456. uiInfo.preventNode = null
  457. }
  458. if (uiInfo.baseUI) {
  459. //uiInfo.baseUI.decAllRef()
  460. this.releaseUI(uiInfo)
  461. uiInfo.baseUI.onHide()
  462. }
  463. }
  464. this.UIOpenQueue = []
  465. this.UICloseQueue = []
  466. this.UIStack = remainUIInfo
  467. this.resetUIStackZIndex()
  468. this.isOpening = false
  469. this.isClosing = false
  470. this.curGroupUI = null
  471. this.groupUIs = []
  472. }
  473. releaseUI(uiInfo: UIInfo) {
  474. let baseUI = uiInfo.baseUI
  475. baseUI.node.parent = null
  476. this.UICache[uiInfo.uiID] = uiInfo
  477. if (!baseUI.cache) {
  478. uiInfo.hideTime = Date.now()
  479. }
  480. }
  481. cleanUI() {
  482. let now = Date.now()
  483. for (let key in this.UICache) {
  484. let uiInfo = this.UICache[key]
  485. if (uiInfo) {
  486. let node = uiInfo.baseUI.node
  487. let hideTime = uiInfo.hideTime
  488. let prefab = uiInfo.prefab
  489. if (!node.parent && hideTime > 0 && now - hideTime > 15000 && !this.isClosing && !this.isOpening) {
  490. console.log('UIMgr releaseUI:', node.name)
  491. uiInfo.baseUI.decAllRef()
  492. node.destroy()
  493. cc.assetManager.releaseAsset(prefab)
  494. delete this.UICache[key]
  495. }
  496. }
  497. }
  498. }
  499. resetUIStackZIndex() {
  500. for (let i = 0; i < this.UIStack.length; i++) {
  501. let uiInfo = this.UIStack[i]
  502. uiInfo.zOrder = i
  503. uiInfo.baseUI.node.zIndex = i + uiInfo.baseUI.showType
  504. }
  505. }
  506. /******************** UI的便捷接口 *******************/
  507. //可能存在当时UI正在加载node,没有baseUI,顶层不是那么精确
  508. public isTopUI(uiID, isCurLayer: boolean = true): boolean {
  509. let curUIs = this.UIStack
  510. let uiInfo = curUIs.find(v => v.uiID == uiID)
  511. if (!uiInfo) return false
  512. if (uiInfo && isCurLayer) {
  513. curUIs = this.UIStack.filter(v => v.baseUI && uiInfo.baseUI && v.baseUI.showType == uiInfo.baseUI.showType)
  514. }
  515. return curUIs[curUIs.length - 1]?.uiID == uiID
  516. }
  517. public getUI(uiID: number): BaseUI {
  518. for (let index = 0; index < this.UIStack.length; index++) {
  519. const element = this.UIStack[index]
  520. if (uiID == element.uiID) {
  521. return element.baseUI
  522. }
  523. }
  524. return null
  525. }
  526. //返回uiID,可能存在当时UI正在加载node,没有baseUI
  527. public getTopUI(): number {
  528. if (this.UIStack.length > 0) {
  529. return this.UIStack[this.UIStack.length - 1].uiID
  530. }
  531. return null
  532. }
  533. public getUIInfo(uiID: number): UIInfo {
  534. for (let index = 0; index < this.UIStack.length; index++) {
  535. let element = this.UIStack[index]
  536. if (uiID == element.uiID) {
  537. return element
  538. }
  539. }
  540. return null
  541. }
  542. public getGroupUIInfo(uiID: number, args?: any): GroupUIInfo {
  543. return {uiID, uiArgs: args}
  544. }
  545. public getRewardGroupUIInfo(rewardNty: IRewardNty): GroupUIInfo | null {
  546. let hasReward = false
  547. for (let key in rewardNty) {
  548. hasReward ||= Array.isArray(rewardNty[key]) && rewardNty[key].length > 0
  549. }
  550. if (hasReward) {
  551. let rewardArgs: IReward = {idNumArr: Mgr.goods.getGoodsListByRewardNty(rewardNty)}
  552. return {uiID: UI.RewardUI, uiArgs: rewardArgs}
  553. } else {
  554. return null
  555. }
  556. }
  557. public callOnShow(uiID: UI, args: any) {
  558. let uiInfo = this.getUIInfo(uiID)
  559. if (uiInfo && uiInfo.baseUI) {
  560. uiInfo.baseUI.onShow(args, null)
  561. }
  562. }
  563. /******************** 通用UI的操作接口 *******************/
  564. public tip(text: string = '') {
  565. if (text == '') {
  566. return
  567. }
  568. this.hide(UI.TipUI)
  569. this.show(UI.TipUI, text)
  570. }
  571. /**
  572. * 显示网络请求等待响应屏蔽触摸的界面
  573. * @param str 显示的文本
  574. * @param isDelay 是否延时显示可见的屏蔽层,延时中为透明的屏蔽层
  575. * @param cutNet 超时后是否重启游戏
  576. */
  577. public showLoading(str: string = '', isDelay: boolean = true, cutNet: boolean = false, timeOut: boolean = false) {
  578. this.show(UI.LoadingUI, {str, isDelay, cutNet, timeOut})
  579. }
  580. /**
  581. * 隐藏网络请求等待响应屏蔽触摸的界面
  582. */
  583. public hideLoading() {
  584. this.hide(UI.LoadingUI)
  585. }
  586. public message(text: string = '', sureFunc: Function, isHideCancel: boolean = false, sureLb: string = '') {
  587. if (text == '') {
  588. return
  589. }
  590. let args: IMessage = {
  591. sureFuc: sureFunc,
  592. tip: text,
  593. isHideCancel,
  594. sureLb,
  595. }
  596. this.show(UI.MessageUI, args)
  597. }
  598. public fadeShow(uiID: number, uiArgs: any = null, inOrOut = true) {
  599. this.show(UI.FadeInOutUI, {
  600. inOrOut,
  601. aniFinishFuc: () => {},
  602. })
  603. if (uiID > 0) this.show(uiID, uiArgs)
  604. }
  605. public showReward(rewardNty: IRewardNty, itemUICfgArr?: ItemUICfg[]) {
  606. if (!rewardNty) return
  607. let hasReward = false
  608. for (let key in rewardNty) {
  609. hasReward ||= Array.isArray(rewardNty[key]) && rewardNty[key].length > 0
  610. }
  611. if (hasReward) {
  612. let rewardArgs: IReward = {idNumArr: Mgr.goods.getGoodsListByRewardNty(rewardNty), itemUICfgArr}
  613. this.show(UI.RewardUI, rewardArgs)
  614. }
  615. }
  616. //传入needNum表示根据needGoods道具ID判断是否显示UI。不传值默认显示UI
  617. public showObtain(needGoods: number | IOperateNeed, needNum: number = 0): boolean {
  618. let operateNeed: IOperateNeed
  619. let showObtain = true
  620. if (typeof needGoods == 'number') {
  621. operateNeed = {
  622. canUp: false,
  623. isMax: false,
  624. need: [
  625. {
  626. id: needGoods,
  627. num: Data.user.goods.get(needGoods) ? Data.user.goods.get(needGoods) : 0,
  628. need: needNum,
  629. },
  630. ],
  631. }
  632. } else {
  633. operateNeed = needGoods
  634. }
  635. if (needNum > 0) {
  636. let hasNum = Data.user.goods.get(operateNeed.need[0]?.id)
  637. showObtain = hasNum < needNum
  638. }
  639. if (showObtain) this.show(UI.ObtainChannelUI, operateNeed)
  640. return showObtain
  641. }
  642. }