ShowTipsCtrl.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /// #pkgName FGUI包名
  2. /// #prefabName ui预制名字
  3. /// #viewName uiview名字
  4. /// #UIName ui的名字,prefabName首字母转大写
  5. /// ui逻辑处理类
  6. /// created by chenwb 2024
  7. import { gameMethod } from "../../../../common/gameMethod";
  8. import { FguiEvent } from "../../../../data/const/EventConst";
  9. import FguiMgr from "../../../../frameWork/fgui/FguiMgr";
  10. import { FguiViewCtrl } from "../../../../frameWork/fgui/mvc/FguiViewCtrl";
  11. import EventMng from "../../../../manager/EventMng";
  12. import {ShowTipsItem} from "./ShowTipsItem";
  13. import { ShowTipsVM } from "./ShowTipsVM";
  14. import { ShowTipsView } from "./ShowTipsView";
  15. export class ShowTipsCtrl extends FguiViewCtrl<ShowTipsVM> {
  16. private delayList:string[];
  17. private delayShow:number; // 0.2秒内重复触发的,过滤掉
  18. private showTipsList:ShowTipsItem[];
  19. OnInited(): void {
  20. this.delayShow = 0;
  21. this.delayList = new Array();
  22. this.showTipsList = new Array();
  23. }
  24. OnShow(intent?: any): void {
  25. this.AddListeners();
  26. this.delayShow = 0.2;
  27. this.AddDelayMessage(intent);
  28. this.ShowTipsUI();
  29. }
  30. OnHide(): void {
  31. this.RemoveListeners();
  32. for (let index = 0; index < this.showTipsList.length; index++) {
  33. const element = this.showTipsList[index];
  34. if (element.IsUsing()){
  35. element.ResetUI();
  36. }
  37. }
  38. }
  39. //#region UI事件
  40. private AddListeners() : void {
  41. this.initEvent(FguiEvent.SHOWTIPS, this.AddDelayMessage)
  42. }
  43. private RemoveListeners() : void {
  44. }
  45. private GetShowTipsItem(){
  46. if (gameMethod.isEmpty(this.showTipsList)){
  47. return this.CreateShowTipsItem();
  48. }
  49. let count = this.showTipsList.length;
  50. for (let index = 0; index < count; index++) {
  51. const element = this.showTipsList[index];
  52. if (!element.IsUsing()){
  53. return element;
  54. }
  55. }
  56. return this.CreateShowTipsItem();
  57. }
  58. private CreateShowTipsItem(){
  59. let tipsItem = fgui.UIPackage.createObject("Common","TipsItem", ShowTipsItem) as ShowTipsItem;;
  60. this.showTipsList.push(tipsItem);
  61. if (gameMethod.isEmpty(this.VM.Panel) || gameMethod.isEmpty(tipsItem)) {
  62. //还未初始化完成
  63. return null
  64. }
  65. this.VM.Panel.addChild(tipsItem);
  66. tipsItem.x = this.VM.Panel.width/2;
  67. tipsItem.touchable = false;
  68. return tipsItem;
  69. }
  70. private ShowTipsUI(){
  71. if (gameMethod.isEmpty(this.delayList)){
  72. return;
  73. }
  74. let tipsItem = this.GetShowTipsItem();
  75. if (gameMethod.isEmpty(tipsItem)) {
  76. return
  77. }
  78. this.delayShow = 0.2;
  79. tipsItem.setData({tips:this.delayList.pop(), callback:()=>{
  80. if (this.IsHideUI()){
  81. this.Close();
  82. }
  83. }});
  84. }
  85. // 是否关闭界面
  86. private IsHideUI(){
  87. if (!gameMethod.isEmpty(this.delayList)){
  88. return false;
  89. }
  90. let count = this.showTipsList.length;
  91. for (let index = 0; index < count; index++) {
  92. if (this.showTipsList[index].IsUsing()){
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. private AddDelayMessage(tips:string) {
  99. if (gameMethod.isEmpty(tips)) {
  100. return
  101. }
  102. this.delayShow = 0.2;
  103. if (this.delayShow > 0) {
  104. this.delayList.push(tips)
  105. return
  106. }
  107. }
  108. OnUpdate(dt:number): void {
  109. if (this.delayShow > 0) {
  110. this.delayShow -= dt
  111. if (!gameMethod.isEmpty(this.delayList)) {
  112. this.ShowTipsUI();
  113. }
  114. }
  115. }
  116. private Close(): void{
  117. FguiMgr.Instance.closeUI(ShowTipsView);
  118. }
  119. //#endregion
  120. }