CommonUseItemCtrl.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using FL.Data;
  8. using System;
  9. using XGame.Framework.UI;
  10. namespace FL.FGUI
  11. {
  12. public class UseItemParam
  13. {
  14. public int id;
  15. public int showNum;
  16. public int maxNum; // -1表示无上限
  17. public long endTime; // 结束时间戳
  18. public Action<int> onPromiseCallback; // 确定按钮回调函数
  19. public Action onCancelCallback; // 取消按钮回调函数
  20. }
  21. /// <summary>
  22. /// UI逻辑处理类
  23. /// </summary>
  24. /// <typeparam name=""></typeparam>
  25. public partial class CommonUseItemCtrl : UIController<CommonUseItemVM>
  26. {
  27. private bool _bPromiseCallback;
  28. private int _showCount;
  29. private long _openUIServerTime; // 打开此界面时所处的当前的时间戳(用于加速类道具打开此界面停留时间比较久时)
  30. private UseItemParam _useItemParam;
  31. private const int stayTime = 3000; // 停留时间超过30秒
  32. protected override void OnEnable(object intent)
  33. {
  34. _bPromiseCallback = false;
  35. AddUIListenres();
  36. AddEventListeners();
  37. if (intent != null)
  38. {
  39. _useItemParam = (UseItemParam)intent;
  40. }
  41. _openUIServerTime = Context.Time.GetNowTime();
  42. ShowItemUI();
  43. ShowUI();
  44. }
  45. protected override void OnDisable()
  46. {
  47. RemoveUIListenres();
  48. RemoveEventListeners();
  49. if (_bPromiseCallback)
  50. {
  51. if (_useItemParam?.onPromiseCallback != null) _useItemParam.onPromiseCallback(_showCount);
  52. }
  53. else
  54. {
  55. if (_useItemParam?.onCancelCallback != null) _useItemParam.onCancelCallback();
  56. }
  57. _bPromiseCallback = false;
  58. _showCount = 0;
  59. _openUIServerTime = 0;
  60. _useItemParam = null;
  61. }
  62. #region UI事件
  63. private void AddUIListenres()
  64. {
  65. VM.UseBtn.onClick.Add(OnClickUseBtn);
  66. VM.AddBtn.onClick.Add(OnClickAddBtn);
  67. VM.SubBtn.onClick.Add(OnClickSubBtn);
  68. }
  69. private void RemoveUIListenres()
  70. {
  71. VM.UseBtn.onClick.Remove(OnClickUseBtn);
  72. VM.AddBtn.onClick.Remove(OnClickAddBtn);
  73. VM.SubBtn.onClick.Remove(OnClickSubBtn);
  74. }
  75. private void OnClickUseBtn(EventContext context)
  76. {
  77. if (_useItemParam?.endTime > 0)
  78. {
  79. XGame.Log.Warn($"当前服务器时间:{Context.Time.GetNowTime()},_openUIServerTime:{_openUIServerTime}");
  80. if (Context.Time.GetNowTime() - _openUIServerTime > stayTime)
  81. {
  82. var needNum = GetRealNeed();
  83. if (needNum != _useItemParam.maxNum)
  84. {
  85. RefreshNeedTimeCard(needNum);
  86. return;
  87. }
  88. }
  89. }
  90. _bPromiseCallback = true;
  91. Context.ClosePanel();
  92. }
  93. private void OnClickAddBtn(EventContext context)
  94. {
  95. long itemCount = ItemData.Instance.GetItemNum(_useItemParam.id); // 当前拥有的道具数量
  96. var count = _showCount + 1;
  97. if (count <= itemCount)
  98. {
  99. if (_useItemParam.maxNum > 0 && count <= _useItemParam.maxNum)
  100. {
  101. _showCount = count;
  102. }
  103. ShowCount();
  104. }
  105. }
  106. private void OnClickSubBtn(EventContext context)
  107. {
  108. var count = _showCount - 1;
  109. if (count >= 0)
  110. {
  111. _showCount = count;
  112. ShowCount();
  113. }
  114. }
  115. #endregion
  116. #region 事件监听
  117. private void AddEventListeners()
  118. {
  119. }
  120. private void RemoveEventListeners()
  121. {
  122. }
  123. #endregion
  124. private void ShowItemUI()
  125. {
  126. UiParam _uiParam = new UiParam();
  127. _uiParam.bShowBtn = true;
  128. _uiParam.bShowName = true;
  129. _uiParam.bShowCount = true;
  130. VM.ItemBtn.Ctrl.ShowAnalogData(_useItemParam.id, _uiParam);
  131. }
  132. private void ShowUI()
  133. {
  134. long itemCount = ItemData.Instance.GetItemNum(_useItemParam.id); // 当前拥有的道具数量
  135. _showCount = itemCount < _useItemParam.showNum ? (int)itemCount : _useItemParam.showNum;
  136. ShowCount();
  137. }
  138. /// <summary>
  139. /// 显示数量按钮UI状态
  140. /// </summary>
  141. private void ShowBtnUI()
  142. {
  143. }
  144. private void ShowCount()
  145. {
  146. VM.NumLabel.text = _showCount.ToString();
  147. }
  148. // 获取实际需要的加速道具数量
  149. private int GetRealNeed()
  150. {
  151. var needTime = DragonEggData.Instance.UpgradeEndTime - Context.Time.GetNowTime();
  152. return DragonEggService.Instance.GetAccelerationCardNum(DragonEggData.Instance.AccelerateId, needTime);
  153. }
  154. //时间变化,刷新卡所需数量重置
  155. private void RefreshNeedTimeCard(int maxNum)
  156. {
  157. _useItemParam.maxNum = maxNum;
  158. _useItemParam.showNum = _useItemParam.maxNum > 1 ? _useItemParam.maxNum - 1 : 0;
  159. _openUIServerTime = Context.Time.GetNowTime();
  160. ShowUI();
  161. }
  162. }
  163. }