537 lines
17 KiB
C#
537 lines
17 KiB
C#
|
||
/****************************************************
|
||
文件:Assets/Code/Scripts/Gameplay/UI/UI_BuyEnergyController.cs
|
||
作者:yuntao-Hu
|
||
时间:2024/5
|
||
描述:补充体力UI界面
|
||
说明:
|
||
*****************************************************/
|
||
|
||
|
||
using cfg.ActorCfg;
|
||
using Cysharp.Threading.Tasks;
|
||
using Framework;
|
||
using Gameplay;
|
||
using Gameplay.Common;
|
||
using Gameplay.Net;
|
||
using PhxhSDK;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using Button = UnityEngine.UI.Button;
|
||
using Image = UnityEngine.UI.Image;
|
||
using ScrollView = PhxhSDK.PhxhScrollView;
|
||
|
||
|
||
public class UI_BuyEnergyController : UIWindow
|
||
{
|
||
#region Temporary_Constant
|
||
private static List<int> energyID = new List<int>() { GLConfig.Inst.data.EnergyPointID, 6 };//4:pve体力 6:pvp体力
|
||
//TODO 后续将扩展至globalCfg
|
||
|
||
private const int DEFAULT_USAGE_AMOUNT = 1;//默认使用数目,使用时的最小量
|
||
#endregion
|
||
|
||
#region UI GameObj
|
||
//UI GameObj
|
||
///Auto Gen Start///
|
||
public Image Image_BG;
|
||
public TMP_Text Text_Title;
|
||
public TMP_Text Text_Currency;
|
||
public Button Button_Close;
|
||
public Image Image_DetailBG;
|
||
public TMP_Text Text_Detail;
|
||
public Image Image_BtnBar;
|
||
public TMP_InputField Text_NumInputField;
|
||
public Button Button_Min;
|
||
public Image Image_Min;
|
||
public TMP_Text Text_Min;
|
||
public Button Button_Minus;
|
||
public Button Button_Max;
|
||
public Image Image_Max;
|
||
public TMP_Text Text_Max;
|
||
public Button Button_Plus;
|
||
public Image Image_ItemBG;
|
||
public Image Image_ItemRect;
|
||
public Image Image_MovePoint;
|
||
public Image Image_Arrow;
|
||
public TMP_Text Text_NowCount;
|
||
public TMP_Text Text_AfterCount;
|
||
public Button Button_Return;
|
||
public TMP_Text Text_Return;
|
||
public Button Button_Buy;
|
||
public TMP_Text Text_Buy;
|
||
|
||
///Auto Gen End///
|
||
private RawImage rawImageGaussianBlurMask;
|
||
#endregion
|
||
|
||
|
||
private int curEnergyItemID;
|
||
#region class
|
||
|
||
class MethodButtonObj : UIGameObjectWrapper
|
||
{
|
||
private GameObject chosenCircle;
|
||
private TextMeshProUGUI itemAmountText;
|
||
private Image buttonIcon;
|
||
|
||
//TODO 考虑后续,将补充体力方式改为json配置后,可能需要调整传参
|
||
public MethodButtonObj(GameObject root, int i, int itemID, bool isScaleShow = true) : base(root, i, isScaleShow)
|
||
{
|
||
chosenCircle = FindObj("ChooseRect");
|
||
itemAmountText = GetComponent<TextMeshProUGUI>("ImageCount/TextCount");
|
||
buttonIcon = GetComponent<Image>("ItemIcon");
|
||
RefreshIcon(itemID);
|
||
}
|
||
|
||
public void BindListener(Action callback)
|
||
{
|
||
BindButton(GameObject.GetComponent<Button>(), callback);
|
||
}
|
||
|
||
async void RefreshIcon(int itemID)
|
||
{
|
||
var spritePath = CommonUtils.GetItemPath(itemID);
|
||
buttonIcon.sprite = await LoadAssetAsync<Sprite>(spritePath);
|
||
//TODO release
|
||
}
|
||
|
||
public void SetChosen(bool isChosen)
|
||
{
|
||
chosenCircle.SetActive(isChosen);
|
||
}
|
||
|
||
public void SetItemAmount(uint amount)
|
||
{
|
||
itemAmountText.text = amount.ToString();
|
||
}
|
||
|
||
public void SetActive(bool isActive)
|
||
{
|
||
GameObject.SetActive(isActive);
|
||
}
|
||
|
||
public override void Dispose()
|
||
{
|
||
base.Dispose();
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Button_Listener
|
||
public void OnClickClose()
|
||
{
|
||
CloseWindow();
|
||
}
|
||
|
||
public void OnMinClick()
|
||
{
|
||
SetUsageAmountAndRefresh(DEFAULT_USAGE_AMOUNT);
|
||
}
|
||
|
||
public void OnMinusClick()
|
||
{
|
||
if (m_UsageAmount > DEFAULT_USAGE_AMOUNT)
|
||
{
|
||
m_UsageAmount--;
|
||
SetUsageAmountAndRefresh(m_UsageAmount);
|
||
}
|
||
}
|
||
|
||
public void OnMaxClick()
|
||
{
|
||
int itemID = EnergyManager.Instance.GetItemIDBuyEnergy((int)m_RechargeMethod, curEnergyItemID);
|
||
int upperValue = EnergyManager.Instance.MaxConsumeAmountWithoutConvert(itemID, out _);
|
||
SetUsageAmountAndRefresh(Math.Max(DEFAULT_USAGE_AMOUNT, upperValue));
|
||
}
|
||
|
||
public void OnPlusClick()
|
||
{
|
||
int itemID = EnergyManager.Instance.GetItemIDBuyEnergy((int)m_RechargeMethod, curEnergyItemID);
|
||
int upperValue = EnergyManager.Instance.MaxConsumeAmountWithoutConvert(itemID, out _);
|
||
if (m_UsageAmount < upperValue)
|
||
{
|
||
m_UsageAmount++;
|
||
SetUsageAmountAndRefresh(m_UsageAmount);
|
||
}
|
||
}
|
||
|
||
public void OnReturnClick()
|
||
{
|
||
CloseWindow();
|
||
}
|
||
|
||
public void OnBuyClick()
|
||
{
|
||
int itemID = EnergyManager.Instance.GetItemIDBuyEnergy((int)m_RechargeMethod, curEnergyItemID);
|
||
int upperValue = EnergyManager.Instance.MaxConsumeAmountWithoutConvert(itemID, out bool itemLimited);
|
||
if (!ValueValidator.IsIdValid(itemID))
|
||
{
|
||
DebugUtil.LogError("补充体力方式对应的物品ID不存在,请检查配置!");
|
||
return;
|
||
}
|
||
|
||
if (m_UsageAmount <= upperValue && m_UsageAmount > 0)
|
||
{
|
||
EnergyManager.Instance.BuyEnergy(itemID, (uint)m_UsageAmount, curEnergyItemID);
|
||
CloseWindow();
|
||
}
|
||
else
|
||
{
|
||
|
||
DebugUtil.Log("购买体力数目超出可选择范围!");
|
||
if (m_UsageAmount > upperValue)
|
||
{
|
||
if (itemLimited)
|
||
{
|
||
DataItem dataItem = TableManager.Instance.Tables.Item.Get(itemID);
|
||
string itemName = CommonUtils.GetLocalizeText(dataItem.NameLocal);
|
||
CommonUtils.ShowMessageTips(CommonUtils.GetLocalizeText("BuyEnergy_failed_itemLack", itemName));// + CommonUtils.GetLocalizeText("BuyEnergy_failed")
|
||
}
|
||
else
|
||
{
|
||
CommonUtils.ShowMessageTips(CommonUtils.GetLocalizeText("BuyEnergy_failed_overflow"));// + CommonUtils.GetLocalizeText("BuyEnergy_failed")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
private List<MethodButtonObj> rechargeMethodButtons = new List<MethodButtonObj>();
|
||
|
||
private uint m_CurrEnergy;
|
||
private int m_UsageAmount;
|
||
|
||
private int m_RechargeMethod;
|
||
|
||
#region UIWindowDelegrate
|
||
|
||
/// <summary>
|
||
/// 打开购买体力界面
|
||
/// </summary>
|
||
public static async UniTask<UIWindow> Open(int Energy)
|
||
{
|
||
await CommonUtils.CaptureScreenshot();
|
||
if (!energyID.Contains(Energy))
|
||
{
|
||
DebugUtil.LogError("该物品并非体力类物品");
|
||
return default;
|
||
}
|
||
return await UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_BuyEnergy, Energy);
|
||
}
|
||
|
||
// deal with input data
|
||
public override void PreLoad(object data = null)
|
||
{
|
||
curEnergyItemID = (int)data;
|
||
}
|
||
|
||
// Use this for initialization
|
||
public override void OnInit()
|
||
{
|
||
//bind UI GameObj
|
||
UI_BuyEnergyBinder.GetComponents(this);
|
||
|
||
rawImageGaussianBlurMask = GetComponent<RawImage>("GaussianBlurMask");
|
||
rawImageGaussianBlurMask.texture = CommonUtils.GetScreenTexture2D();
|
||
|
||
BindButton();
|
||
BindInputFieldListener();
|
||
SetTextAndEnergyIcon();
|
||
|
||
m_CurrEnergy = EnergyManager.Instance.GetNowEnergy(curEnergyItemID);
|
||
EventManager.Instance.Register<uint>(EventManager.EventName.ItemChange, OnItemChange);
|
||
}
|
||
|
||
async void SetTextAndEnergyIcon()
|
||
{
|
||
DataItem data = CategoryManager.Instance.GetItemData(curEnergyItemID);
|
||
if (data != null)
|
||
{
|
||
Image_MovePoint.sprite = await LoadAssetAsync<Sprite>(data.IconPath);
|
||
}
|
||
Text_Title.text = "补充" + CommonUtils.GetLocalizeText(CategoryManager.Instance.GetItemData(curEnergyItemID).NameLocal);
|
||
}
|
||
//invoke when open window
|
||
protected override void OnShowWindow(object data = null)
|
||
{
|
||
base.OnShowWindow(data);
|
||
bool chooseMethod = false;
|
||
for (int i = 0; i < EnergyManager.Instance.ItemIDBuyEnergyCount(curEnergyItemID) - 1; i++)
|
||
{
|
||
int itemID = EnergyManager.Instance.GetItemIDBuyEnergy(m_RechargeMethod, curEnergyItemID);
|
||
if (!ValueValidator.IsIdValid(itemID))
|
||
{
|
||
DebugUtil.LogError("补充体力方式对应的物品ID不存在,请检查配置!");
|
||
continue;
|
||
}
|
||
if (isItemRemained(itemID))
|
||
{
|
||
if (!chooseMethod)
|
||
{
|
||
OnChooseMethod(i);
|
||
chooseMethod = true;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HideMethodButton(i);
|
||
}
|
||
}
|
||
if (!chooseMethod)
|
||
{
|
||
OnChooseMethod(EnergyManager.Instance.ItemIDBuyEnergyCount(curEnergyItemID) - 1);
|
||
}
|
||
|
||
SetUsageAmountAndRefresh(amount: DEFAULT_USAGE_AMOUNT);
|
||
}
|
||
|
||
//invoke when reload window
|
||
protected override void OnReloadWindow(object data = null)
|
||
{
|
||
base.OnReloadWindow(data);
|
||
}
|
||
|
||
//invoke when close window
|
||
protected override void OnHideWindow()
|
||
{
|
||
base.OnHideWindow();
|
||
}
|
||
|
||
//invoke when destroy
|
||
public override void OnRelease()
|
||
{
|
||
foreach (var btn in rechargeMethodButtons)
|
||
{
|
||
btn.Dispose();
|
||
}
|
||
EventManager.Instance.Unregister<uint>(EventManager.EventName.ItemChange, OnItemChange);
|
||
base.OnRelease();
|
||
}
|
||
#endregion
|
||
|
||
private void OnItemChange(uint itemId)
|
||
{
|
||
if ((int)itemId != curEnergyItemID)
|
||
return;
|
||
|
||
m_CurrEnergy = EnergyManager.Instance.GetNowEnergy();
|
||
|
||
int itemID = EnergyManager.Instance.GetItemIDBuyEnergy((int)m_RechargeMethod, curEnergyItemID);
|
||
if (!ValueValidator.IsIdValid(itemID))
|
||
{
|
||
DebugUtil.LogError("补充体力方式对应的物品ID不存在,请检查配置!");
|
||
return;
|
||
}
|
||
|
||
int consume = EnergyManager.Instance.GetConvertConsumes(itemID, m_UsageAmount);
|
||
int recharge = EnergyManager.Instance.GetConvertRecharges(itemID, m_UsageAmount);
|
||
RefreshDetailText(consume, recharge);
|
||
}
|
||
|
||
void BindInputFieldListener()
|
||
{
|
||
Text_NumInputField.onEndEdit.AddListener(SnapInputToStep);
|
||
}
|
||
/// <summary>
|
||
/// 由于货币输入框输入的数值不一定是阶梯值,需要对输入的数值做向下取整到阶梯值
|
||
/// </summary>
|
||
/// <param name="inputAmount"></param>
|
||
void SnapInputToStep(string inputAmount)
|
||
{
|
||
int parsedValue;
|
||
if (int.TryParse(inputAmount, out parsedValue))
|
||
{
|
||
// 对输入的数据做向下取整到阶梯值
|
||
int snappedValue = 0;
|
||
int itemID = EnergyManager.Instance.GetItemIDBuyEnergy((int)m_RechargeMethod, curEnergyItemID);
|
||
|
||
if (EnergyManager.Instance.GetConvertConsumes(itemID) != 0)
|
||
snappedValue = parsedValue / EnergyManager.Instance.GetConvertConsumes(itemID);
|
||
else
|
||
DebugUtil.LogError("补充体力方式对应的转化比例为0,请检查配置内是否包含该方式!");
|
||
|
||
int upperValue = EnergyManager.Instance.MaxConsumeAmountWithoutConvert(itemID, out _);
|
||
|
||
if (snappedValue > upperValue)
|
||
{
|
||
snappedValue = upperValue;
|
||
}
|
||
if (snappedValue < DEFAULT_USAGE_AMOUNT)
|
||
{
|
||
snappedValue = DEFAULT_USAGE_AMOUNT;
|
||
}
|
||
|
||
m_UsageAmount = snappedValue;
|
||
SetUsageAmountAndRefresh(m_UsageAmount);
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("货币输入内容格式不正确,已舍弃错误内容,请检查输入");
|
||
SetUsageAmountAndRefresh(m_UsageAmount);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 绑定按钮
|
||
/// </summary>
|
||
void BindButton()
|
||
{
|
||
InitBindMethodButton();
|
||
|
||
Button_Buy.onClick.AddListener(OnBuyClick);
|
||
Button_Close.onClick.AddListener(OnClickClose);
|
||
Button_Min.onClick.AddListener(OnMinClick);
|
||
Button_Minus.onClick.AddListener(OnMinusClick);
|
||
Button_Max.onClick.AddListener(OnMaxClick);
|
||
Button_Plus.onClick.AddListener(OnPlusClick);
|
||
Button_Return.onClick.AddListener(OnReturnClick);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绑定补充体力方式选择按钮
|
||
/// </summary>
|
||
void InitBindMethodButton()
|
||
{
|
||
//体力补充方式选择按钮
|
||
ScrollView methodButtonView = Image_ItemBG.GetComponentInChildren<ScrollView>();
|
||
if (methodButtonView == null)
|
||
{
|
||
DebugUtil.LogError("体力补充方式按钮列表获取失败!");
|
||
return;
|
||
}
|
||
else
|
||
{
|
||
GameObject buttonPrefab = null;
|
||
foreach (Transform buttonGameObj in methodButtonView.content)
|
||
{
|
||
buttonGameObj.gameObject.SetActive(false);
|
||
|
||
if (buttonGameObj.GetComponent<Button>() != null)
|
||
buttonPrefab = buttonGameObj.gameObject;
|
||
}
|
||
|
||
for (int i = 0; i < EnergyManager.Instance.ItemIDBuyEnergyCount(curEnergyItemID); i++)
|
||
{
|
||
int itemID = EnergyManager.Instance.GetItemIDBuyEnergy(i, curEnergyItemID);
|
||
if (!ValueValidator.IsIdValid(itemID))
|
||
{
|
||
DebugUtil.LogError("补充体力方式对应的物品ID不存在,请检查配置!");
|
||
continue;
|
||
}
|
||
var methodButton = new MethodButtonObj(buttonPrefab, i, itemID);
|
||
methodButton.SetActive(true);
|
||
rechargeMethodButtons.Add(methodButton);
|
||
|
||
int methodIndex = i;
|
||
methodButton.BindListener(() =>
|
||
{
|
||
if (methodIndex != m_RechargeMethod)
|
||
OnChooseMethod(methodIndex);
|
||
});
|
||
|
||
|
||
uint itemAmount = CategoryManager.Instance.GetItemCount(itemID);
|
||
methodButton.SetItemAmount(itemAmount);
|
||
}
|
||
}
|
||
}
|
||
|
||
void HideMethodButton(int method)
|
||
{
|
||
if (method < rechargeMethodButtons.Count)
|
||
{
|
||
rechargeMethodButtons[method].SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("不存在" + method.ToString() + "对应的按钮!");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 体力药是否剩余(可用于判断初始化时的补充体力方式)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
bool isItemRemained(int ItemID)
|
||
{
|
||
if (CategoryManager.Instance.GetItemCount(ItemID) > 0)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 选中补充体力方式Button
|
||
/// </summary>
|
||
/// <param name="chosenMethod"></param>
|
||
void OnChooseMethod(int chosenMethod)
|
||
{
|
||
MethodButtonObj preChosenButton = rechargeMethodButtons[m_RechargeMethod];
|
||
MethodButtonObj curChosenButton = rechargeMethodButtons[chosenMethod];
|
||
|
||
m_RechargeMethod = chosenMethod;
|
||
|
||
preChosenButton.SetChosen(false);
|
||
curChosenButton.SetChosen(true);
|
||
|
||
//切换按钮后,重置界面内相关数目显示
|
||
SetUsageAmountAndRefresh(amount: DEFAULT_USAGE_AMOUNT);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设定使用数目,并刷新界面内相关数目显示
|
||
/// </summary>
|
||
/// <param name="amount"></param>
|
||
void SetUsageAmountAndRefresh(int amount)
|
||
{
|
||
m_UsageAmount = amount;
|
||
int itemID = EnergyManager.Instance.GetItemIDBuyEnergy((int)m_RechargeMethod, curEnergyItemID);
|
||
if (!ValueValidator.IsIdValid(itemID))
|
||
{
|
||
DebugUtil.LogError("补充体力方式对应的物品ID不存在,请检查配置!");
|
||
return;
|
||
}
|
||
|
||
int consume = EnergyManager.Instance.GetConvertConsumes(itemID, m_UsageAmount);
|
||
int recharge = EnergyManager.Instance.GetConvertRecharges(itemID, m_UsageAmount);
|
||
RefreshDetailText(consume, recharge);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刷新文字显示
|
||
/// </summary>
|
||
/// <param name="consume">实际道具消耗显示数</param>
|
||
/// <param name="recharge">转化体力数量</param>
|
||
void RefreshDetailText(int consume, int recharge)
|
||
{
|
||
uint curEnergy = m_CurrEnergy;
|
||
|
||
Text_NumInputField.text = consume.ToString();
|
||
Text_NowCount.text = curEnergy.ToString();
|
||
Text_AfterCount.text = (curEnergy + recharge).ToString();
|
||
|
||
int itemID = EnergyManager.Instance.GetItemIDBuyEnergy((int)m_RechargeMethod, curEnergyItemID);
|
||
if (!ValueValidator.IsIdValid(itemID))
|
||
{
|
||
DebugUtil.LogError("补充体力方式对应的物品ID不存在,请检查配置!");
|
||
return;
|
||
}
|
||
|
||
DataItem dataItem = CategoryManager.Instance.GetItemData(itemID);
|
||
if (dataItem != null)
|
||
{
|
||
string itemName = consume + " " + CommonUtils.GetLocalizeText(dataItem.NameLocal);
|
||
string energyName = recharge + " " + CommonUtils.GetLocalizeText(CategoryManager.Instance.GetItemData(curEnergyItemID).NameLocal);
|
||
Text_Detail.text = CommonUtils.GetLocalizeText("BuyEnergy_tips", itemName, energyName);
|
||
}
|
||
}
|
||
}
|