NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/UI_GetItemWindowController.cs

211 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
using Button = UnityEngine.UI.Button;
using Image = UnityEngine.UI.Image;
using Framework;
using System;
using Gameplay;
using Cysharp.Threading.Tasks;
using PhxhSDK;
using cfg.ActorCfg;
using Gameplay.Level;
public class UI_GetItemWindowController : UIWindow
{
//UI GameObj
///Auto Gen Start///
public Image Image_BG;
public Image Image_ItemPic;
public Image Image_Bar;
public TMP_Text Text_ItemNum;
public TMP_Text Text_IteamName;
public TMP_Text Text_Continue;
///Auto Gen End///
private ListItem mItems;
private EShowType _showType;
private GameObject _itemTemp;
private GameObject _scrollView;
private GameObject _scrollContent;
private GameObject _goMaskBg;
private Action _callBackFunc;
public enum EShowType
{
Normal,
StorySettle
}
private struct Param
{
public EShowType type;
public ListItem listItem;
public Action cb;
public Param(EShowType Etype, ListItem list, Action callBack = null)
{
type = Etype;
listItem = list;
cb = callBack;
}
}
/// <summary>
/// 打开奖励获取界面
/// </summary>
public static async UniTask<UIWindow> Open(ItemCounts itemCounts, EShowType type = EShowType.Normal, Action cb = null)
{
ListItem itemList = new(itemCounts);
return await UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_GetItemWindow, new Param(type, itemList, cb));
}
public static async UniTask<UIWindow> Open(ListItem itemList, EShowType type = EShowType.Normal, Action cb = null)
{
return await UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_GetItemWindow, new Param(type, itemList, cb));
}
public static async UniTask<UIWindow> Open(ItemList itemList, EShowType type = EShowType.Normal, Action cb = null)
{
ListItem listItem = new(itemList);
return await UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_GetItemWindow, new Param(type, listItem, cb));
}
/// <summary>
/// 道具
/// </summary>
private class Item : UIGameObjectWrapper
{
/// <summary>
/// 道具名
/// </summary>
private TMP_Text textName;
/// <summary>
/// 通用道具
/// </summary>
private GenericItem genericItem;
public Item(GameObject root) : base(root)
{
textName = GetComponent<TMP_Text>("Text_IteamName");
genericItem = new GenericItem(FindObj("GenericItem"));
}
public void SetInfo(int itemId, int count)
{
DataItem dataItem = TableManager.Instance.Tables.Item.Get(itemId);
if (dataItem == null)
{
DebugUtil.LogError($"获取配置错误 id{itemId}");
return;
}
textName.text = CommonUtils.GetLocalizeText(dataItem.NameLocal);
genericItem.SetInfo(dataItem, count);
IsScaleShow = true;
}
public override void Dispose()
{
genericItem.Dispose();
genericItem = null;
base.Dispose();
}
}
/// <summary>
/// 道具列表
/// </summary>
private List<Item> listItem;
public override void PreLoad(object data = null)
{
if (data != null)
{
var param = (Param)data;
mItems = param.listItem;
_showType = param.type;
_callBackFunc = param.cb;
}
}
public override void OnInit()
{
UI_GetItemWindowBinder.GetComponents(this);
Transform tsItemRoot = FindObj("Image_BG/Goods").transform;
_scrollView = FindObj("Image_BG/ScrollView");
_itemTemp = FindObj("Image_BG/Item");
_scrollContent = FindObj("Image_BG/ScrollView/Viewport/Content");
_goMaskBg = FindObj("Image_BG/ImageMaskBg");
BindButton(GetComponent<Button>("Image_BG"), OnBgClick);
AddBgClose(OnBgClick);
}
protected override void OnShowWindow(object data = null)
{
RefreshMaterial(mItems);
}
public override void OnRelease()
{
foreach (Item item in listItem)
{
item.Dispose();
}
listItem.Clear();
base.OnRelease();
}
private void RefreshMaterial(ListItem mItems)
{
_scrollContent.transform.ClearChilds();
listItem = new(mItems.List.Count);
for (int i = 0; i < mItems.List.Count; ++i)
{
var item = Instantiate(_itemTemp, _scrollContent.transform);
listItem.Add(new Item(item));
}
for (int i = 0; i < listItem.Count; i++)
{
if (i < mItems.Ids.Count)
{
Item item = listItem[i];
item.SetInfo(mItems.Ids[i], mItems.Counts[i]);
item.IsActive = true;
}
else
listItem[i].IsActive = false;
}
_goMaskBg.SetActive(_showType == EShowType.StorySettle);
}
private void OnBgClick()
{
if (_showType == EShowType.StorySettle)
{
LevelManager.Instance.ExitLevelDirectly();
}
_callBackFunc?.Invoke();
CloseWindow();
}
}