123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /// #pkgName FGUI包名
- /// #panelName UIPanel名字
- /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
- /// 该脚本由模板创建
- /// created by cb 2024
- using FairyGUI;
- using FL.Data;
- using System;
- using XGame.Framework.UI;
- namespace FL.FGUI
- {
- public class UseItemParam
- {
- public int id;
- public int showNum;
- public int maxNum; // -1表示无上限
- public long endTime; // 结束时间戳
- public Action<int> onPromiseCallback; // 确定按钮回调函数
- public Action onCancelCallback; // 取消按钮回调函数
- }
- /// <summary>
- /// UI逻辑处理类
- /// </summary>
- /// <typeparam name=""></typeparam>
- public partial class CommonUseItemCtrl : UIController<CommonUseItemVM>
- {
- private bool _bPromiseCallback;
- private int _showCount;
- private long _openUIServerTime; // 打开此界面时所处的当前的时间戳(用于加速类道具打开此界面停留时间比较久时)
- private UseItemParam _useItemParam;
- private const int stayTime = 3000; // 停留时间超过30秒
- protected override void OnEnable(object intent)
- {
- _bPromiseCallback = false;
- AddUIListenres();
- AddEventListeners();
- if (intent != null)
- {
- _useItemParam = (UseItemParam)intent;
- }
- _openUIServerTime = Context.Time.GetNowTime();
- ShowUI();
- }
- protected override void OnDisable()
- {
- RemoveUIListenres();
- RemoveEventListeners();
- if (_bPromiseCallback)
- {
- if (_useItemParam?.onPromiseCallback != null) _useItemParam.onPromiseCallback(_showCount);
- }
- else
- {
- if (_useItemParam?.onCancelCallback != null) _useItemParam.onCancelCallback();
- }
- _bPromiseCallback = false;
- _showCount = 0;
- _openUIServerTime = 0;
- _useItemParam = null;
- }
- #region UI事件
- private void AddUIListenres()
- {
- VM.UseBtn.onClick.Add(OnClickUseBtn);
- VM.AddBtn.onClick.Add(OnClickAddBtn);
- VM.SubBtn.onClick.Add(OnClickSubBtn);
- }
- private void RemoveUIListenres()
- {
- VM.UseBtn.onClick.Remove(OnClickUseBtn);
- VM.AddBtn.onClick.Remove(OnClickAddBtn);
- VM.SubBtn.onClick.Remove(OnClickSubBtn);
- }
- private void OnClickUseBtn(EventContext context)
- {
- if (_useItemParam?.endTime > 0)
- {
- XGame.Log.Warn($"当前服务器时间:{Context.Time.GetNowTime()},_openUIServerTime:{_openUIServerTime}");
- if (Context.Time.GetNowTime() - _openUIServerTime > stayTime)
- {
- var needNum = GetRealNeed();
- if (needNum != _useItemParam.maxNum)
- {
- RefreshNeedTimeCard(needNum);
- return;
- }
- }
- }
- _bPromiseCallback = true;
- Context.ClosePanel();
- }
- private void OnClickAddBtn(EventContext context)
- {
- long itemCount = ItemData.Instance.GetItemNum(_useItemParam.id); // 当前拥有的道具数量
- var count = _showCount + 1;
- if (count <= itemCount)
- {
- if (_useItemParam.maxNum > 0 && count <= _useItemParam.maxNum)
- {
- _showCount = count;
- }
- ShowCount();
- }
- }
- private void OnClickSubBtn(EventContext context)
- {
- var count = _showCount - 1;
- if (count >= 0)
- {
- _showCount = count;
- ShowCount();
- }
- }
- #endregion
- #region 事件监听
- private void AddEventListeners()
- {
- }
- private void RemoveEventListeners()
- {
- }
- #endregion
- private void ShowUI()
- {
- long itemCount = ItemData.Instance.GetItemNum(_useItemParam.id); // 当前拥有的道具数量
- _showCount = itemCount < _useItemParam.showNum ? (int)itemCount : _useItemParam.showNum;
- ShowCount();
- }
- /// <summary>
- /// 显示数量按钮UI状态
- /// </summary>
- private void ShowBtnUI()
- {
- }
- private void ShowCount()
- {
- VM.NumLabel.text = _showCount.ToString();
- }
- // 获取实际需要的加速道具数量
- private int GetRealNeed()
- {
- return 1;
- }
- //时间变化,刷新卡所需数量重置
- private void RefreshNeedTimeCard(int maxNum)
- {
- _useItemParam.maxNum = maxNum;
- _useItemParam.showNum = _useItemParam.maxNum > 1 ? _useItemParam.maxNum - 1 : 0;
- _openUIServerTime = Context.Time.GetNowTime();
- ShowUI();
- }
- }
- }
|