NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/UI_ItemSourcePanelControlle...

244 lines
7.3 KiB
C#

using cfg;
using Cysharp.Threading.Tasks;
using Framework;
using Gameplay;
using Gameplay.Level;
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_ItemSourcePanelController : UIWindow
{
//UI GameObj
///Auto Gen Start///
public Button Button_Close;
public TMP_Text Text_ItemName;
public Image Image_ItemIcon;
public TMP_Text Text_ItemType;
public TMP_Text Text_ItemNum;
public TMP_Text Text_ItemDesc;
public RecycleView ScrollView_Source;
public TMP_Text Text_Source;
public Image Image_TopBlackBar;
public Image Image_BottomBlackBar;
///Auto Gen End///
class ItemSourceTab : UIGameObjectWrapper
{
public TMP_Text Text_Source;
public Button Button_Jump;
public TMP_Text Text_GoTo;
public Action CloseAction;
int JumpID;
public ItemSourceTab(GameObject root, Action closeAction, bool isScaleShow = true) : base(root, isScaleShow)
{
Text_Source = GetComponent<TMP_Text>("Text_Source");
Button_Jump = GetComponent<Button>("Button");
Text_GoTo = GetComponent<TMP_Text>("Button/Text_GoTo");
CloseAction = closeAction;
CommonUtils.BindButton(Button_Jump, JumpAction);
}
public void SetInfo(int ID)
{
JumpID = ID;
if (JumpID > 100000)//TO Level
{
Text_Source.text = string.Format("{0} {1}", LevelManager.Instance.LevelIDToNum(JumpID), LevelManager.Instance.LevelIDToName(JumpID));
if (LevelManager.Instance.CheckLevelUnlock(JumpID))
{
Button_Jump.interactable = true;
Text_Source.color = Color.white;
//CommonUtils.GetComponent<Image>(item.gameObject, "icon").color = new Color(1, 1, 1, 1);
Text_GoTo.text = CommonUtils.GetLocalizeText("Package_JumpTo");
}
else
{
Button_Jump.interactable = false;
Text_Source.color = new Color(.2f, .2f, .2f, 1);
//CommonUtils.GetComponent<Image>(item.gameObject, "icon").color = new Color(1, 1, 1, .5f);
Text_GoTo.text = CommonUtils.GetLocalizeText("Package_Lock");
}
}
else//TO Shop
{
var data = ShopManager.Instance.GetShopData(JumpID);
Text_Source.text = CommonUtils.GetLocalizeText(data.Name);
//var btn = item.gameObject.GetComponent<Button>();
Button_Jump.interactable = true;
Text_Source.color = Color.white;
//CommonUtils.GetComponent<Image>(item.gameObject, "icon").color = new Color(1, 1, 1, 1);
Text_GoTo.text = CommonUtils.GetLocalizeText("Package_JumpTo");
}
}
void JumpAction()
{
if (JumpID > 100000)
{
JumpToLevel(JumpID);
}
else
{
JumpToShop(JumpID);
}
}
void JumpToLevel(int level)
{
if (LevelManager.Instance.CheckLevelUnlock(level))
{
SignUpManager.Instance.DoNotShowDailyAccumulate = true;
JumpToManager.JumpFunc<int, Action>(cfg.JumpFuncType.Level_Detail, level, () =>
{
CloseAction?.Invoke();
});
}
else
DebugUtil.Log("level is locked: " + level);
}
void JumpToShop(int ShopId)
{
var shopCfg = TableManager.Instance.Tables.Shop;
if (!shopCfg.DataMap.ContainsKey(ShopId))
{
DebugUtil.Log("Jump to shop detail failed, shop id not found: " + ShopId);
return;
}
SignUpManager.Instance.DoNotShowDailyAccumulate = true;
JumpToManager.JumpFunc<int, Action>(JumpFuncType.Item_Shop, ShopId, () =>
{
CloseAction?.Invoke();
});
}
}
class OpenData
{
public int itemID;
public Action CloseCallback;
public bool HideUseBtn;
public OpenData(int itemID, Action CloseCallback, bool hideUseBtn)
{
this.HideUseBtn = hideUseBtn;
this.itemID = itemID;
this.CloseCallback = CloseCallback;
}
}
int ItemID;
Action onWindowCloseCallback;
bool hideUseBtn;
Dictionary<int, ItemSourceTab> dicScrollItem = new();
public static UniTask<UIWindow> Open(int itemID, Action JumpCloseCallback, bool hideUseBtn = false)
{
CommonUtils.CaptureScreenshot();
var data = new OpenData(itemID, JumpCloseCallback, hideUseBtn);
return UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_ItemSourcePanel, data);
}
// deal with input data
public override void PreLoad(object data = null)
{
var openData = (OpenData)data;
ItemID = openData.itemID;
onWindowCloseCallback = openData.CloseCallback;
hideUseBtn = openData.HideUseBtn;
var spritePath = CommonUtils.GetItemPath(ItemID);
AssetManager.Instance.PostPreload<Sprite>(spritePath).Forget();
}
// Use this for initialization
public override void OnInit()
{
//bind UI GameObj
UI_ItemSourcePanelBinder.GetComponents(this);
var dataItem = CategoryManager.Instance.GetItemData(ItemID);
Text_ItemName.text = CommonUtils.GetLocalizeText(dataItem.NameLocal);
Text_ItemType.text = CommonUtils.GetLocalizeText(dataItem.IntroductionTitleKey);
Text_ItemDesc.text = CommonUtils.GetLocalizeText(dataItem.IntroductionKey);
if (dataItem.HideStockNumber)
{
Text_ItemNum.gameObject.SetActive(false);
}
else
{
Text_ItemNum.text = CategoryManager.Instance.GetItemCount(ItemID).ToString();
}
var spritePath = CommonUtils.GetItemPath(ItemID);
Image_ItemIcon.sprite = AssetManager.Instance.GetPreLoadResult<Sprite>(spritePath);
AddBgClose(OnCloseClick);
BindButton(Button_Close, OnCloseClick);
ScrollView_Source.Init(RefreshItem);
ScrollView_Source.ShowList(dataItem.Acquisition.Length);
}
private void RefreshItem(GameObject cell, int index)
{
if (!dicScrollItem.TryGetValue(cell.GetInstanceID(), out ItemSourceTab item))
{
item = new ItemSourceTab(cell, () =>
{
CloseWindow();
onWindowCloseCallback?.Invoke();
});
dicScrollItem.Add(cell.GetInstanceID(), item);
}
var dataItem = CategoryManager.Instance.GetItemData(ItemID);
item.SetInfo(dataItem.Acquisition[index]);
}
//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();
var spritePath = CommonUtils.GetItemPath(ItemID);
AssetManager.Instance.Unload(spritePath);
}
protected override void OnCloseClick()
{
CloseWindow();
base.OnCloseClick();
}
}