Files
test/Assets/PerfectWorld/Scripts/UI/ProduceItemPanel.cs
T
2026-04-02 17:57:25 +07:00

110 lines
2.8 KiB
C#

using BrewMonster;
using BrewMonster.Scripts.Managers;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using static CSNetwork.Common.ExpTypes;
public class ProduceItemPanel : MonoBehaviour
{
[Header("UI")]
public Image icon;
public TextMeshProUGUI text_name;
public TextMeshProUGUI text_level;
private uint recipeId;
private Coroutine loadIconCoroutine;
private DlgProduce ownerDlg;
private Button cachedButton;
public void Setup(uint recipeId, DlgProduce dlg)
{
this.recipeId = recipeId;
this.ownerDlg = dlg;
SetupClickHandler();
LoadOutputIcon();
}
void SetupClickHandler()
{
if (cachedButton == null)
{
cachedButton = GetComponent<Button>();
if (cachedButton == null)
cachedButton = GetComponentInChildren<Button>();
if (cachedButton == null && icon != null)
cachedButton = icon.gameObject.AddComponent<Button>();
}
if (cachedButton != null)
{
cachedButton.onClick.RemoveAllListeners();
cachedButton.onClick.AddListener(OnClicked);
}
}
void OnClicked()
{
Debug.Log($"[ProduceItemPanel] Click recipe {recipeId}");
if (ownerDlg != null)
ownerDlg.ShowRecipeMaterials(recipeId);
}
void LoadOutputIcon()
{
if (icon == null || recipeId == 0)
return;
if (loadIconCoroutine != null)
StopCoroutine(loadIconCoroutine);
loadIconCoroutine = StartCoroutine(LoadIconCoroutine());
}
IEnumerator LoadIconCoroutine()
{
yield return null;
var edm = ElementDataManProvider.GetElementDataMan();
if (edm == null)
yield break;
DATA_TYPE dt = DATA_TYPE.DT_INVALID;
object recipeData = edm.get_data_ptr(recipeId, ID_SPACE.ID_SPACE_RECIPE, ref dt);
if (recipeData is not RECIPE_ESSENCE recipe)
yield break;
if (recipe.targets == null || recipe.targets.Length == 0)
yield break;
uint outputItemId = recipe.targets[0].id_to_make;
if (outputItemId == 0)
yield break;
Sprite sp = EC_IvtrItemUtils.Instance.ResolveItemIconSprite((int)outputItemId);
if (sp != null)
{
icon.sprite = sp;
icon.enabled = true;
icon.color = Color.white;
icon.preserveAspect = true;
text_level.text = $"Cấp {recipe.recipe_level}";
text_name.text = EC_IvtrItemUtils.Instance.ResolveItemName((int)outputItemId);
}
}
void OnDestroy()
{
if (loadIconCoroutine != null)
StopCoroutine(loadIconCoroutine);
if (cachedButton != null)
cachedButton.onClick.RemoveListener(OnClicked);
}
}