105 lines
2.6 KiB
C#
105 lines
2.6 KiB
C#
using BrewMonster;
|
|
using BrewMonster.Scripts.Managers;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using static CSNetwork.Common.ExpTypes;
|
|
|
|
public class ProduceItemPanel : MonoBehaviour
|
|
{
|
|
[Header("UI")]
|
|
public Image icon;
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (loadIconCoroutine != null)
|
|
StopCoroutine(loadIconCoroutine);
|
|
|
|
if (cachedButton != null)
|
|
cachedButton.onClick.RemoveListener(OnClicked);
|
|
}
|
|
}
|