474 lines
14 KiB
C#
474 lines
14 KiB
C#
using BrewMonster.Assets.PerfectWorld.Scripts.UI;
|
|
using BrewMonster.Common;
|
|
using BrewMonster.Managers;
|
|
using BrewMonster.Scripts;
|
|
using BrewMonster.Scripts.Managers;
|
|
using CSNetwork.GPDataType;
|
|
using Cysharp.Threading.Tasks.Triggers;
|
|
using PerfectWorld.Scripts.Managers;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using static BrewMonster.Scripts.EC_Inventory;
|
|
|
|
namespace BrewMonster.UI
|
|
{
|
|
public class DlgPetHatch : AUIDialog
|
|
{
|
|
[Header("Component")]
|
|
[SerializeField] private TextMeshProUGUI m_pTxtName;
|
|
[SerializeField] private TextMeshProUGUI m_pTxtGold;
|
|
[SerializeField] private Image m_pImg_Item;
|
|
[SerializeField] private GameObject m_pTxtBindTip;
|
|
|
|
[Header("Buttons")]
|
|
[SerializeField] private Button m_btnConfirm;
|
|
[SerializeField] private Button m_btnCancel;
|
|
[SerializeField] private Button m_btnClose;
|
|
[SerializeField] private Button m_btnUseItem;
|
|
|
|
[Header("Defautl Icon")]
|
|
[SerializeField] private Sprite m_defaultIcon;
|
|
[SerializeField] private GameObject m_itemInfo;
|
|
|
|
private int m_nSlot = 1;
|
|
private EC_IvtrPetEgg m_pCurrentEgg = null;
|
|
|
|
private float m_doubleClickTime = 1f;
|
|
|
|
public override void Awake()
|
|
{
|
|
base.Awake();
|
|
RegisterClick(m_pImg_Item.transform, OnClickEggSlot);
|
|
}
|
|
|
|
public override void OnEnable()
|
|
{
|
|
base.OnEnable();
|
|
ClearEgg();
|
|
if (m_btnConfirm != null)
|
|
m_btnConfirm.onClick.AddListener(OnCommandConfirm);
|
|
if (m_btnCancel != null)
|
|
m_btnCancel.onClick.AddListener(OnCommandCancel);
|
|
if (m_btnClose != null)
|
|
m_btnClose.onClick.AddListener(OnCommandCancel);
|
|
if (m_btnUseItem != null)
|
|
m_btnUseItem.onClick.AddListener(OnClickUseItem);
|
|
}
|
|
|
|
public override void OnDisable()
|
|
{
|
|
base.OnDisable();
|
|
RestoreInventoryColor();
|
|
if (m_btnConfirm != null)
|
|
m_btnConfirm.onClick.RemoveListener(OnCommandConfirm);
|
|
if (m_btnCancel != null)
|
|
m_btnCancel.onClick.RemoveListener(OnCommandCancel);
|
|
if (m_btnClose != null)
|
|
m_btnClose.onClick.RemoveListener(OnCommandCancel);
|
|
if (m_btnUseItem != null)
|
|
m_btnUseItem.onClick.RemoveListener(OnClickUseItem);
|
|
}
|
|
|
|
public override void Show(bool value)
|
|
{
|
|
base.Show(value);
|
|
if (value)
|
|
{
|
|
OnShowDialog();
|
|
}
|
|
}
|
|
|
|
private void OnShowDialog()
|
|
{
|
|
ClearEgg();
|
|
}
|
|
|
|
private void SetEggInternal(EC_IvtrPetEgg petEgg, int slotIndex)
|
|
{
|
|
if (petEgg == null)
|
|
return;
|
|
|
|
if (m_nSlot >= 0)
|
|
{
|
|
var previousBtn = FindInventoryButtonBySlot(m_nSlot);
|
|
SetInventorySlotGray(previousBtn, false);
|
|
}
|
|
|
|
if (m_pCurrentEgg != null)
|
|
m_pCurrentEgg.Freeze(false);
|
|
|
|
m_pCurrentEgg = petEgg;
|
|
m_nSlot = slotIndex;
|
|
|
|
petEgg.Freeze(true);
|
|
SetDataPtr(petEgg, "");
|
|
UpdateEggUI(petEgg);
|
|
|
|
var btn = FindInventoryButtonBySlot(slotIndex);
|
|
SetInventorySlotGray(btn, true);
|
|
}
|
|
|
|
public void SetEgg(EC_IvtrItem pItem, int nSlot)
|
|
{
|
|
if (!(pItem is EC_IvtrPetEgg petEgg))
|
|
return;
|
|
|
|
SetEggInternal((EC_IvtrPetEgg)pItem, nSlot);
|
|
}
|
|
|
|
private void RegisterClick(Transform target, Action<PointerEventData> callback)
|
|
{
|
|
if (target == null)
|
|
return;
|
|
|
|
var trigger = target.GetComponent<EventTrigger>();
|
|
if (trigger == null)
|
|
trigger = target.gameObject.AddComponent<EventTrigger>();
|
|
|
|
var existingClick = trigger.triggers.Find(e => e.eventID == EventTriggerType.PointerClick);
|
|
if (existingClick != null)
|
|
trigger.triggers.Remove(existingClick);
|
|
|
|
var entry = new EventTrigger.Entry
|
|
{
|
|
eventID = EventTriggerType.PointerClick
|
|
};
|
|
entry.callback.AddListener((data) => { callback((PointerEventData)data); });
|
|
trigger.triggers.Add(entry);
|
|
}
|
|
|
|
private void OnClickEggSlot(PointerEventData eventData)
|
|
{
|
|
if (m_pCurrentEgg != null)
|
|
{
|
|
ReturnItemToInventory(m_nSlot);
|
|
ClearEgg();
|
|
}
|
|
}
|
|
|
|
private void OnDropEgg(PointerEventData eventData)
|
|
{
|
|
if (eventData.pointerDrag == null)
|
|
return;
|
|
|
|
var btn = eventData.pointerDrag.GetComponent<Button>();
|
|
if(btn == null)
|
|
return;
|
|
|
|
int slotIndex = btn.transform.GetSiblingIndex();
|
|
|
|
var item = GetItemFromDrag(eventData);
|
|
if (item == null)
|
|
return;
|
|
|
|
if(item.GetClassID() != (int)EC_IvtrItem.InventoryClassId.ICID_PETEGG)
|
|
{
|
|
return;
|
|
}
|
|
|
|
EC_IvtrItem detailedItem = EC_IvtrItem.CreateItem(item.m_tid, item.m_expire_date, item.m_iCount);
|
|
if(item.Content != null && item.Content.Length > 0)
|
|
{
|
|
detailedItem.SetItemInfo(item.Content, item.Content.Length);
|
|
}
|
|
else
|
|
{
|
|
detailedItem.GetDetailDataFromLocal();
|
|
}
|
|
|
|
//EC_IvtrPetEgg petEgg = detailedItem as EC_IvtrPetEgg;
|
|
EC_IvtrPetEgg petEgg = item as EC_IvtrPetEgg;
|
|
if(petEgg == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (m_nSlot >= 0)
|
|
{
|
|
var previosBtn = FindInventoryButtonBySlot(m_nSlot);
|
|
SetInventorySlotGray(previosBtn, false);
|
|
}
|
|
|
|
if(m_pCurrentEgg != null)
|
|
m_pCurrentEgg.Freeze(false);
|
|
|
|
m_pCurrentEgg = petEgg;
|
|
m_nSlot = slotIndex;
|
|
SetDataPtr(petEgg, "");
|
|
petEgg.Freeze(true);
|
|
UpdateEggUI(petEgg);
|
|
SetInventorySlotGray(btn, true);
|
|
}
|
|
|
|
private EC_IvtrItem GetItemFromDrag(PointerEventData eventData)
|
|
{
|
|
if (eventData.pointerDrag == null)
|
|
return null;
|
|
|
|
var btn = eventData.pointerDrag.GetComponent<Button>();
|
|
if(btn == null)
|
|
return null;
|
|
|
|
int slotIndex = btn.transform.GetSiblingIndex();
|
|
|
|
var host = CECGameRun.Instance?.GetHostPlayer();
|
|
if(host == null)
|
|
return null;
|
|
|
|
var inv = host.GetInventory(0);
|
|
if (inv == null)
|
|
return null;
|
|
|
|
return inv.GetItem(slotIndex, false);
|
|
}
|
|
|
|
private Button FindInventoryButtonBySlot(int slot)
|
|
{
|
|
var inventoryUI = FindFirstObjectByType<EC_InventoryUI>();
|
|
if(inventoryUI == null)
|
|
return null;
|
|
|
|
var field = typeof(EC_InventoryUI)
|
|
.GetField("inventoryPackButtons", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
|
|
if(field == null)
|
|
return null;
|
|
|
|
var list = field.GetValue(inventoryUI) as System.Collections.Generic.List<Button>;
|
|
if(list == null || slot < 0 || slot >= list.Count)
|
|
return null;
|
|
|
|
return list[slot];
|
|
}
|
|
|
|
private void SetInventorySlotGray(Button btn, bool gray)
|
|
{
|
|
if (btn == null)
|
|
return;
|
|
var img = btn.GetComponent<Image>();
|
|
if (img == null)
|
|
return;
|
|
img.color = gray ? Color.gray : Color.white;
|
|
}
|
|
|
|
private void SetSlotIcon(Image slot, EC_IvtrItem item)
|
|
{
|
|
if (slot == null || item == null)
|
|
{
|
|
BMLogger.LogWarning("DlgPetHatch: SetSlotIcon - slot or item is null");
|
|
return;
|
|
}
|
|
|
|
var sprite = EC_IvtrItemUtils.Instance.ResolveItemIconSprite(item.m_tid);
|
|
if (sprite == null)
|
|
{
|
|
BMLogger.LogWarning($"DlgPetHatch: No sprite found for item tid: {item.m_tid}");
|
|
return;
|
|
}
|
|
|
|
BMLogger.Log($"DlgPetHatch: Setting icon for {item.GetName()}");
|
|
slot.sprite = sprite;
|
|
slot.enabled = true;
|
|
slot.color = Color.white;
|
|
slot.SetNativeSize();
|
|
}
|
|
|
|
private void ClearSlotIcon(Image slot)
|
|
{
|
|
if (slot == null)
|
|
return;
|
|
|
|
slot.sprite = m_defaultIcon;
|
|
slot.enabled = m_defaultIcon != null;
|
|
}
|
|
|
|
private void ReturnItemToInventory(int slotIndex)
|
|
{
|
|
if (slotIndex < 0)
|
|
return;
|
|
|
|
var btn = FindInventoryButtonBySlot(slotIndex);
|
|
SetInventorySlotGray(btn, false);
|
|
}
|
|
|
|
private void RestoreInventoryColor()
|
|
{
|
|
if (m_nSlot >= 0)
|
|
{
|
|
var btn = FindInventoryButtonBySlot(m_nSlot);
|
|
SetInventorySlotGray(btn, false);
|
|
}
|
|
}
|
|
|
|
private void UpdateEggUI(EC_IvtrPetEgg pEgg)
|
|
{
|
|
if(pEgg == null)
|
|
return;
|
|
|
|
if (m_pTxtBindTip != null)
|
|
{
|
|
bool isBound = (pEgg.GetProcType() & (int)EC_IvtrItem.ProcType.PROC_BIND) != 0;
|
|
m_pTxtBindTip.SetActive(isBound);
|
|
|
|
if (isBound)
|
|
{
|
|
BMLogger.Log("DlgPetHatch: Pet egg is bound - displaying warning");
|
|
}
|
|
}
|
|
|
|
if(m_pTxtName != null)
|
|
{
|
|
m_pTxtName.text = pEgg.GetName();
|
|
}
|
|
|
|
if (m_pTxtGold != null)
|
|
{
|
|
try
|
|
{
|
|
var essence = pEgg.GetDBEssence();
|
|
if (essence.id != 0)
|
|
{
|
|
m_pTxtGold.SetText(essence.money_hatched.ToString());
|
|
}
|
|
else
|
|
{
|
|
m_pTxtGold.SetText("0");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
BMLogger.LogError($"DlgPetHatch: Failed to get essence data for pet egg - {ex.Message}");
|
|
m_pTxtGold.SetText("0");
|
|
}
|
|
}
|
|
SetSlotIcon(m_pImg_Item, pEgg);
|
|
}
|
|
|
|
private void ClearEgg()
|
|
{
|
|
if (m_nSlot >= 0)
|
|
{
|
|
ReturnItemToInventory(m_nSlot);
|
|
}
|
|
|
|
if(m_pTxtBindTip != null)
|
|
m_pTxtBindTip.SetActive(false);
|
|
|
|
if (m_pCurrentEgg != null)
|
|
{
|
|
m_pCurrentEgg.Freeze(false);
|
|
m_pCurrentEgg = null;
|
|
}
|
|
|
|
SetDataPtr(null, "");
|
|
ClearSlotIcon(m_pImg_Item);
|
|
|
|
if (m_pTxtName != null)
|
|
m_pTxtName.text = "";
|
|
|
|
if(m_pTxtGold != null)
|
|
m_pTxtGold.text = "0";
|
|
|
|
m_nSlot = -1;
|
|
}
|
|
|
|
private void OnCommandConfirm()
|
|
{
|
|
EC_IvtrItem pItem = (EC_IvtrItem)(GetDataPtr(""));
|
|
if (pItem != null)
|
|
{
|
|
pItem.Freeze(false);
|
|
GetHostPlayer().HatchPet(m_nSlot);
|
|
SetDataPtr(null, "");
|
|
GetGameUIMan().EndNPCService();
|
|
GetGameUIMan().GetDialog("Win_Inventory")?.Show(false);
|
|
GetHostPlayer().GetPack(Inventory_type.IVTRTYPE_PACK).UnfreezeAllItems();
|
|
CloseDialogue();
|
|
}
|
|
}
|
|
|
|
private void OnCommandCancel()
|
|
{
|
|
RestoreInventoryColor();
|
|
ClearEgg();
|
|
GetGameUIMan().EndNPCService();
|
|
GetGameUIMan().GetDialog("Win_Inventory")?.Show(false);
|
|
GetHostPlayer().GetPack(Inventory_type.IVTRTYPE_PACK).UnfreezeAllItems();
|
|
CloseDialogue();
|
|
}
|
|
|
|
//public void SetEgg(EC_IvtrItem pItem, int nSlot)
|
|
//{
|
|
// if (pItem == null || !(pItem is EC_IvtrPetEgg))
|
|
// return;
|
|
|
|
// ClearEgg();
|
|
|
|
// EC_IvtrPetEgg petEgg = (EC_IvtrPetEgg)pItem;
|
|
|
|
// m_pCurrentEgg = petEgg;
|
|
// m_nSlot = nSlot;
|
|
|
|
// petEgg.Freeze(true);
|
|
// UpdateEggUI(petEgg);
|
|
|
|
// var btn = FindInventoryButtonBySlot(nSlot);
|
|
// SetInventorySlotGray(btn, true);
|
|
//}
|
|
|
|
private bool TryGetSelectedInventoryItem(out EC_IvtrItem item, out int slot)
|
|
{
|
|
item = null;
|
|
slot = -1;
|
|
var inventoryUI = FindFirstObjectByType<EC_InventoryUI>();
|
|
if (inventoryUI == null)
|
|
return false;
|
|
|
|
var type = typeof(EC_InventoryUI);
|
|
var packageField = type.GetField("currentSelectedPackage", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
var slotField = type.GetField("currentSelectedSlot", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
var itemField = type.GetField("currentSelectedItem", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
|
|
|
if (packageField == null || slotField == null || itemField == null)
|
|
return false;
|
|
|
|
var selectedPackage = (byte)packageField.GetValue(inventoryUI);
|
|
if (selectedPackage != (byte)Inventory_type.IVTRTYPE_PACK)
|
|
return false;
|
|
|
|
slot = (int)slotField.GetValue(inventoryUI);
|
|
item = itemField.GetValue(inventoryUI) as EC_IvtrItem;
|
|
|
|
return item != null && slot >= 0;
|
|
}
|
|
|
|
private void OnClickUseItem()
|
|
{
|
|
if (!TryGetSelectedInventoryItem(out var selectedItem, out var selectedSlot))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!(selectedItem is EC_IvtrPetEgg selectedEgg))
|
|
{
|
|
return;
|
|
}
|
|
|
|
SetEggInternal(selectedEgg, selectedSlot);
|
|
if (m_itemInfo != null)
|
|
m_itemInfo.SetActive(false);
|
|
}
|
|
|
|
|
|
private void OnDestroy()
|
|
{
|
|
ClearEgg();
|
|
RestoreInventoryColor();
|
|
}
|
|
}
|
|
}
|