567 lines
14 KiB
C#
567 lines
14 KiB
C#
using cfg.ActorCfg;
|
|
using Cysharp.Threading.Tasks;
|
|
using Framework;
|
|
using Gameplay;
|
|
using PhxhSDK;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using Button = UnityEngine.UI.Button;
|
|
using Image = UnityEngine.UI.Image;
|
|
|
|
public class UI_GiftItemController : UIWindow
|
|
{
|
|
/// <summary>
|
|
/// 界面类型,包含普通礼包界面和自选礼包界面
|
|
/// </summary>
|
|
enum PanelContent
|
|
{
|
|
None,
|
|
DirectOpen,
|
|
OptionalContent
|
|
}
|
|
|
|
#region UI
|
|
//UI GameObj
|
|
///Auto Gen Start///
|
|
public Button Button_Close_Normal;
|
|
public TMP_Text Text_ItemName_Normal;
|
|
public Image Image_ItemIcon_Normal;
|
|
public TMP_Text Text_Num_Normal;
|
|
public TMP_Text Text_UsingIntroduce_Normal;
|
|
public RecycleView ScrollView_ItemList_normal;
|
|
public Button Button_Use_Normal;
|
|
public Button Button_Close_Special;
|
|
public TMP_Text Text_ItemName_Special;
|
|
public Image Image_ItemIcon_Special;
|
|
public TMP_Text Text_Num_Special;
|
|
public TMP_Text Text_UsingIntroduce_Special;
|
|
public RecycleView ScrollView_ItemList_special;
|
|
public TMP_Text Text_GainDescript;
|
|
public Button Button_Decrease;
|
|
public Button Button_Add;
|
|
public TMP_Text Text_UseNum;
|
|
public Button Button_Zero;
|
|
public TMP_Text Text_Zero;
|
|
public Button Button_Max;
|
|
public TMP_Text Text_Max;
|
|
public Button Button_Use_Special;
|
|
public Image Image_TopBlackBar;
|
|
public Image Image_BottomBlackBar;
|
|
|
|
///Auto Gen End///
|
|
|
|
#endregion
|
|
|
|
#region class
|
|
class OpenData
|
|
{
|
|
public int itemID;
|
|
public int useCount;
|
|
|
|
public OpenData(int itemID, int count)
|
|
{
|
|
this.itemID = itemID;
|
|
this.useCount = count;
|
|
}
|
|
}
|
|
|
|
class ChooseItem
|
|
{
|
|
/// <summary>
|
|
/// 选中数量
|
|
/// </summary>
|
|
public int count;
|
|
|
|
/// <summary>
|
|
/// 礼包道具
|
|
/// </summary>
|
|
public cfg.item.ItemCounts item;
|
|
public ChooseItem(cfg.item.ItemCounts item, int count)
|
|
{
|
|
this.item = item;
|
|
this.count = count;
|
|
}
|
|
}
|
|
|
|
class RewardItemInfo
|
|
{
|
|
public int ID;
|
|
public int Count;
|
|
public RewardItemInfo(int id,int count)
|
|
{
|
|
ID = id;
|
|
Count = count;
|
|
}
|
|
}
|
|
|
|
class NormalItem : UIGameObjectWrapper
|
|
{
|
|
public Image itemIcon;
|
|
public Image selectedIcon;
|
|
public TMP_Text itemCount;
|
|
public TMP_Text Num;
|
|
|
|
public Button Button_decrease;
|
|
public Button Button_add;
|
|
|
|
private ChooseItem data;
|
|
private bool isPatchUse;
|
|
|
|
private Func<int, int, bool> buttonCallback;
|
|
|
|
public NormalItem(GameObject root, Func<int, int, bool> callback, bool isPatchUse, bool isScaleShow = true) : base(root, isScaleShow)
|
|
{
|
|
itemIcon = GetComponent<Image>("ItemBg/ItemIcon");
|
|
selectedIcon = GetComponent<Image>("ItemBg/ImageItemChoose");
|
|
itemCount = GetComponent<TMP_Text>("ItemBg/ImageCount/TextCount");
|
|
Num = GetComponent<TMP_Text>("NumBg/Num");
|
|
Button_decrease = GetComponent<Button>("Decrease");
|
|
Button_add = GetComponent<Button>("Add");
|
|
|
|
buttonCallback = callback;
|
|
this.isPatchUse = isPatchUse;
|
|
|
|
CommonUtils.BindButton(Button_decrease, DecreaseCountClick);
|
|
CommonUtils.BindButton(Button_add, AddCountClick);
|
|
if (!isPatchUse)
|
|
{
|
|
FindObj("ItemBg").AddComponent<Button>();
|
|
var itemButton = GetComponent<Button>("ItemBg");
|
|
CommonUtils.BindButton(itemButton, AddCountClick);
|
|
}
|
|
|
|
FindObj("Decrease").gameObject.SetActive(isPatchUse);
|
|
FindObj("Add").gameObject.SetActive(isPatchUse);
|
|
FindObj("NumBg").gameObject.SetActive(isPatchUse);
|
|
if (isPatchUse)
|
|
{
|
|
selectedIcon.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public async UniTask SetInfo(ChooseItem item)
|
|
{
|
|
data = item;
|
|
var itemID = item.item.Id;
|
|
var itemData = CategoryManager.Instance.GetItemData(itemID);
|
|
itemIcon.sprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(CommonUtils.GetItemPath(itemID));
|
|
itemCount.text = item.item.Count.ToString();
|
|
|
|
if (isPatchUse)
|
|
{
|
|
Num.text = item.count.ToString();
|
|
itemCount.text = (item.item.Count * item.count).ToString();
|
|
}
|
|
else
|
|
selectedIcon.gameObject.SetActive(item.count > 0);
|
|
}
|
|
|
|
void AddCountClick()
|
|
{
|
|
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
|
if (buttonCallback == null)
|
|
return;
|
|
if (buttonCallback.Invoke(data.item.Id, 1))
|
|
{
|
|
Num.text = data.count.ToString();
|
|
itemCount.text = (data.item.Count * data.count).ToString();
|
|
}
|
|
}
|
|
|
|
void DecreaseCountClick()
|
|
{
|
|
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
|
if (buttonCallback == null)
|
|
return;
|
|
if (data.count > 0)
|
|
{
|
|
if (buttonCallback.Invoke(data.item.Id, -1))
|
|
{
|
|
Num.text = data.count.ToString();
|
|
itemCount.text = (data.item.Count * data.count).ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class SpecialItem : UIGameObjectWrapper
|
|
{
|
|
public Image itemIcon;
|
|
public TMP_Text itemCount;
|
|
|
|
public SpecialItem(GameObject root, bool isScaleShow = true) : base(root, isScaleShow)
|
|
{
|
|
itemIcon = GetComponent<Image>("ItemIcon");
|
|
itemCount = GetComponent<TMP_Text>("ImageCount/TextCount");
|
|
}
|
|
|
|
public async UniTask SetInfo(RewardItemInfo data, int useCount)
|
|
{
|
|
var dataItem = CategoryManager.Instance.GetItemData(data.ID);
|
|
itemIcon.sprite = await AssetManager.Instance.LoadAssetAsync<Sprite>(CommonUtils.GetItemPath(data.ID));
|
|
itemCount.text = (data.Count * useCount).ToString();
|
|
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
private int ItemID;
|
|
private int ItemUseMaxCount;
|
|
private int UseCount = 0;
|
|
|
|
private PanelContent panelContent = PanelContent.None;
|
|
|
|
private bool isPatchUse = false;
|
|
private bool PatchUse
|
|
{
|
|
get { return isPatchUse; }
|
|
set
|
|
{
|
|
isPatchUse = value;
|
|
}
|
|
}
|
|
|
|
private Dictionary<int, NormalItem> dicNormalItem = new();
|
|
private Dictionary<int, SpecialItem> dicSpecialItem = new();
|
|
private List<ChooseItem> selections = new();
|
|
private List<RewardItemInfo> rewardsItem = new();
|
|
|
|
|
|
private GameObject NormalNode;//自选型使用礼包
|
|
private GameObject SpecialNode;//普通使用礼包
|
|
|
|
|
|
public static UniTask<UIWindow> Open(int itemID, int maxUseCount = 1)
|
|
{
|
|
CommonUtils.CaptureScreenshot();
|
|
var data = new OpenData(itemID, maxUseCount);
|
|
return UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_GiftItem, data);
|
|
}
|
|
|
|
// deal with input data
|
|
public override void PreLoad(object data = null)
|
|
{
|
|
if (data is OpenData inputdata)
|
|
{
|
|
ItemID = inputdata.itemID;
|
|
ItemUseMaxCount = inputdata.useCount;
|
|
|
|
var spritePath = CommonUtils.GetItemPath(ItemID);
|
|
AssetManager.Instance.PostPreload<Sprite>(spritePath).Forget();
|
|
}
|
|
}
|
|
|
|
// Use this for initialization
|
|
public override void OnInit()
|
|
{
|
|
//bind UI GameObj
|
|
UI_GiftItemBinder.GetComponents(this);
|
|
|
|
NormalNode = FindObj("MainNode/NormalNode");
|
|
SpecialNode = FindObj("MainNode/SpecialNode");
|
|
|
|
var dataItem = CategoryManager.Instance.GetItemData(ItemID);
|
|
var dataType = (ItemUseType)dataItem.Param1;
|
|
|
|
if (ItemUseMaxCount == 1)
|
|
{
|
|
PatchUse = false;
|
|
}
|
|
else
|
|
{
|
|
PatchUse = true;
|
|
}
|
|
|
|
if (dataType == ItemUseType.Chest)
|
|
{
|
|
panelContent = PanelContent.DirectOpen;
|
|
NormalNode.SetActive(false);
|
|
SpecialNode.SetActive(true);
|
|
UseCount = 1;
|
|
InitSpecialPanel();
|
|
}
|
|
else if (dataType == ItemUseType.SelectableChest)
|
|
{
|
|
panelContent = PanelContent.OptionalContent;
|
|
NormalNode.SetActive(true);
|
|
SpecialNode.SetActive(false);
|
|
|
|
selections = new List<ChooseItem>();
|
|
var itemData = CategoryManager.Instance.GetItemData(ItemID);
|
|
foreach (var item in itemData.Selections)
|
|
{
|
|
selections.Add(new ChooseItem(item, 0));
|
|
}
|
|
InitNormalPanel();
|
|
}
|
|
else
|
|
{
|
|
DebugUtil.LogError($"UI_GiftItemController: {dataType} is not supported");
|
|
}
|
|
|
|
BindButton(Button_Close_Normal, OnCloseClick);
|
|
BindButton(Button_Close_Special, OnCloseClick);
|
|
}
|
|
|
|
//invoke when open window
|
|
protected override void OnShowWindow(object data = null)
|
|
{
|
|
|
|
}
|
|
|
|
//invoke when reload window
|
|
protected override void OnReloadWindow(object data = null)
|
|
{
|
|
|
|
}
|
|
|
|
//invoke when close window
|
|
protected override void OnHideWindow()
|
|
{
|
|
|
|
}
|
|
|
|
//invoke when destroy
|
|
public override void OnRelease()
|
|
{
|
|
base.OnRelease();
|
|
}
|
|
|
|
protected override void OnCloseClick()
|
|
{
|
|
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
|
CloseWindow();
|
|
}
|
|
|
|
#region 自选礼包界面
|
|
void InitNormalPanel()
|
|
{
|
|
var itemData = CategoryManager.Instance.GetItemData(ItemID);
|
|
Text_ItemName_Normal.text = CommonUtils.GetLocalizeText(itemData.NameLocal);
|
|
Image_ItemIcon_Normal.sprite = AssetManager.Instance.GetPreLoadResult<Sprite>(CommonUtils.GetItemPath(ItemID));
|
|
|
|
Text_Num_Normal.text = $"兑换: {UseCount}/{ItemUseMaxCount}";
|
|
Text_Num_Normal.transform.parent.gameObject.SetActive(PatchUse);
|
|
|
|
ScrollView_ItemList_normal.Init(RefreshNormalItem);
|
|
ScrollView_ItemList_normal.ShowList(selections.Count);
|
|
if (selections.Count < 6)
|
|
{
|
|
ScrollView_ItemList_normal.ScrollRect.horizontal = false;
|
|
}
|
|
BindButton(Button_Use_Normal, OptionalUseClick);
|
|
}
|
|
|
|
void OptionalUseClick()
|
|
{
|
|
List<uint> items = new();
|
|
List<uint> counts = new();
|
|
foreach (var item in selections)
|
|
{
|
|
if (item.count > 0)
|
|
{
|
|
items.Add((uint)item.item.Id);
|
|
counts.Add((uint)item.count);
|
|
}
|
|
}
|
|
|
|
if (items.Count > 0)
|
|
{
|
|
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
|
CategoryManager.Instance.UseItem(ItemID, counts.ToArray(), items.ToArray());
|
|
CloseWindow();
|
|
}
|
|
else
|
|
{
|
|
CommonUtils.ShowMessageTips("请至少选择一个道具兑换!");
|
|
}
|
|
}
|
|
|
|
void RefreshNormalPanelInfo()
|
|
{
|
|
var sum = 0;
|
|
foreach (var item in selections)
|
|
{
|
|
sum += item.count;
|
|
}
|
|
UseCount = sum;
|
|
Text_Num_Normal.text = $"兑换: {UseCount}/{ItemUseMaxCount}";
|
|
}
|
|
|
|
private void RefreshNormalItem(GameObject cell, int index)
|
|
{
|
|
if (!dicNormalItem.TryGetValue(cell.GetInstanceID(), out NormalItem item))
|
|
{
|
|
item = new NormalItem(cell, OnNormalItemClick, PatchUse);
|
|
dicNormalItem.Add(cell.GetInstanceID(), item);
|
|
}
|
|
var dataItem = CategoryManager.Instance.GetItemData(ItemID);
|
|
item.SetInfo(selections[index]).Forget();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 自选礼包内道具数量点击事件
|
|
/// </summary>
|
|
/// <param name="itemID"></param>
|
|
/// <param name="count"></param>
|
|
/// <returns></returns>
|
|
bool OnNormalItemClick(int itemID, int count)
|
|
{
|
|
var sum = 0;
|
|
ChooseItem data = null;
|
|
foreach (var item in selections)
|
|
{
|
|
if (!PatchUse)
|
|
item.count = 0;
|
|
|
|
sum += item.count;
|
|
if (item.item.Id == itemID)
|
|
{
|
|
data = item;
|
|
if (!PatchUse)
|
|
item.count = 1;
|
|
}
|
|
}
|
|
|
|
if (PatchUse)
|
|
{
|
|
var changeSum = sum + count;
|
|
if (changeSum >= 0 && changeSum <= ItemUseMaxCount)
|
|
{
|
|
if (data != null)
|
|
{
|
|
data.count += count;
|
|
if (data.count < 0)
|
|
data.count = 0;
|
|
}
|
|
RefreshNormalPanelInfo();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ScrollView_ItemList_normal.ShowList(selections.Count);
|
|
return false;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 普通礼包界面
|
|
void InitSpecialPanel()
|
|
{
|
|
var itemData = CategoryManager.Instance.GetItemData(ItemID);
|
|
Text_ItemName_Special.text = CommonUtils.GetLocalizeText(itemData.NameLocal);
|
|
Image_ItemIcon_Special.sprite = AssetManager.Instance.GetPreLoadResult<Sprite>(CommonUtils.GetItemPath(ItemID));
|
|
|
|
Text_Num_Special.text = $"持有: {UseCount}/{ItemUseMaxCount}";
|
|
Text_UseNum.text = UseCount.ToString();
|
|
FindObj("MainNode/SpecialNode/ImageMiddleBg/NumNode").SetActive(PatchUse);
|
|
|
|
var rewardId = itemData.Param2;
|
|
var rewards = TableManager.Instance.Tables.Rewards.DataList;
|
|
rewardsItem.Clear();
|
|
foreach (var reward in rewards)
|
|
{
|
|
if (reward.Id == rewardId)
|
|
{
|
|
var item = new RewardItemInfo(reward.ItemID, reward.MaxCount);
|
|
rewardsItem.Add(item);
|
|
}
|
|
}
|
|
|
|
ScrollView_ItemList_special.Init(RefreshSpecialItem);
|
|
ScrollView_ItemList_special.ShowList(rewardsItem.Count);
|
|
if(rewardsItem.Count < 6)
|
|
{
|
|
ScrollView_ItemList_special.ScrollRect.horizontal = false;
|
|
}
|
|
|
|
BindButton(Button_Decrease, DecreaseClick);
|
|
BindButton(Button_Add, AddClick);
|
|
BindButton(Button_Zero, MinClick);
|
|
BindButton(Button_Max, MaxClick);
|
|
BindButton(Button_Use_Special, UseClick);
|
|
}
|
|
|
|
void AddClick()
|
|
{
|
|
var maxCount = CategoryManager.Instance.GetItemCount(ItemID);
|
|
if (UseCount < maxCount)
|
|
{
|
|
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
|
UseCount++;
|
|
Text_UseNum.text = UseCount.ToString();
|
|
Text_Num_Special.text = $"持有: {UseCount}/{ItemUseMaxCount}";
|
|
|
|
ScrollView_ItemList_special.ShowList(rewardsItem.Count);
|
|
}
|
|
else
|
|
{
|
|
CommonUtils.ShowMessageTips("道具数量已达上限");
|
|
}
|
|
}
|
|
|
|
void DecreaseClick()
|
|
{
|
|
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
|
if (UseCount > 1)
|
|
{
|
|
UseCount--;
|
|
Text_UseNum.text = UseCount.ToString();
|
|
Text_Num_Special.text = $"持有: {UseCount}/{ItemUseMaxCount}";
|
|
ScrollView_ItemList_special.ShowList(rewardsItem.Count);
|
|
}
|
|
|
|
}
|
|
|
|
void MinClick()
|
|
{
|
|
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
|
UseCount = 1;
|
|
Text_UseNum.text = UseCount.ToString();
|
|
Text_Num_Special.text = $"持有: {UseCount}/{ItemUseMaxCount}";
|
|
ScrollView_ItemList_special.ShowList(rewardsItem.Count);
|
|
}
|
|
|
|
void MaxClick()
|
|
{
|
|
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
|
UseCount = (int)CategoryManager.Instance.GetItemCount(ItemID);
|
|
Text_UseNum.text = UseCount.ToString();
|
|
Text_Num_Special.text = $"持有: {UseCount}/{ItemUseMaxCount}";
|
|
ScrollView_ItemList_special.ShowList(rewardsItem.Count);
|
|
}
|
|
|
|
void UseClick()
|
|
{
|
|
if (UseCount > 0)
|
|
{
|
|
AudioManager.Instance.PlayAudio(Framework.Constants.Sound.COMMON_CLICK);
|
|
CategoryManager.Instance.UseItem(ItemID, UseCount);
|
|
CloseWindow();
|
|
}
|
|
else
|
|
{
|
|
CommonUtils.ShowMessageTips("请选择正确的使用数量!");
|
|
}
|
|
}
|
|
|
|
private void RefreshSpecialItem(GameObject cell, int index)
|
|
{
|
|
if (!dicSpecialItem.TryGetValue(cell.GetInstanceID(), out SpecialItem item))
|
|
{
|
|
item = new SpecialItem(cell);
|
|
dicSpecialItem.Add(cell.GetInstanceID(), item);
|
|
}
|
|
var dataItem = CategoryManager.Instance.GetItemData(ItemID);
|
|
item.SetInfo(rewardsItem[index], UseCount).Forget();
|
|
}
|
|
#endregion
|
|
} |