Files
2026-03-12 17:13:31 +07:00

189 lines
5.4 KiB
C#

using BrewMonster.Network;
using BrewMonster.Scripts.Pet;
using CSNetwork.GPDataType;
using ModelRenderer.Scripts.GameData;
using System;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BrewMonster.UI
{
public class CDlgPetList : AUIDialog
{
public const int CDLGPETLIST_SLOT_MAX = 20;
[Header("Prefab Setup")]
[SerializeField] private DlgPetListBox m_prefabPetSlot;
[SerializeField] private Transform m_container;
[Header("Button")]
[SerializeField] private Button m_btnClose;
[SerializeField] private Button m_btnHome;
[SerializeField] private Button m_btnOut;
[Header("References")]
[SerializeField] private DlgPetDetail m_dlgPetDetail;
private List<DlgPetListBox> m_spawnedSlots = new List<DlgPetListBox>();
private int m_nPageIndex = 0;
private int m_currentSelectSlotIndex = -1;
private CECPetCorral m_cachedPetCorral;
public void OnInitDialog()
{
if (!IsShow()) Show(true);
m_btnClose.onClick.RemoveAllListeners();
m_btnClose.onClick.AddListener(() =>
{
m_dlgPetDetail.ClearPetDetail();
CloseDialogue();
});
if (m_btnHome != null)
{
m_btnHome.onClick.RemoveAllListeners();
m_btnHome.onClick.AddListener(() =>
{
if (m_currentSelectSlotIndex >= 0)
{
OnCommandRecall();
}
});
}
if(m_btnOut != null)
{
m_btnOut.onClick.RemoveAllListeners();
m_btnOut.onClick.AddListener(() =>
{
if (m_currentSelectSlotIndex >= 0)
{
OnCommandSummon(m_currentSelectSlotIndex);
}
});
}
RefreshUI();
}
public void RefreshUI()
{
UpdateList();
UpdateButtonStates();
}
public void UpdateList()
{
m_cachedPetCorral = GetHostPlayer()?.GetPetCorral();
if (m_cachedPetCorral == null) return;
int operatingState = GetHostPlayer().IsOperatingPet();
while (m_spawnedSlots.Count < CDLGPETLIST_SLOT_MAX)
{
DlgPetListBox newSlot = Instantiate(m_prefabPetSlot, m_container);
m_spawnedSlots.Add(newSlot);
}
for (int i = 0; i < CDLGPETLIST_SLOT_MAX; i++)
{
int actualSlotIndex = i + (m_nPageIndex * CDLGPETLIST_SLOT_MAX);
CECPetData pPet = m_cachedPetCorral.GetPetData(actualSlotIndex);
bool isActivePet = (m_cachedPetCorral.GetActivePetIndex() == actualSlotIndex);
m_spawnedSlots[i].SetData(
pPet,
actualSlotIndex,
isActivePet,
operatingState,
OnSlotClicked
);
}
}
private void OnSlotClicked(int slotIndex, bool isActivePet)
{
if (m_cachedPetCorral == null)
{
m_cachedPetCorral = GetHostPlayer()?.GetPetCorral();
}
if(m_cachedPetCorral == null)
return;
CECPetData pPetData = m_cachedPetCorral.GetPetData(slotIndex);
if (pPetData != null)
{
m_currentSelectSlotIndex = slotIndex;
UpdateButtonStates();
if (m_dlgPetDetail != null)
{
m_dlgPetDetail.UpdatePet(slotIndex);
}
}
}
private void UpdateButtonStates()
{
if (m_currentSelectSlotIndex < 0)
{
if(m_btnHome != null)
m_btnHome.interactable = false;
if(m_btnOut != null)
m_btnOut.interactable = false;
return;
}
if (m_cachedPetCorral == null)
{
m_cachedPetCorral = GetHostPlayer()?.GetPetCorral();
}
if(m_cachedPetCorral == null)
return;
int operatingState = GetHostPlayer().IsOperatingPet();
bool isActivePet = (m_cachedPetCorral.GetActivePetIndex() == m_currentSelectSlotIndex);
bool canOperate = (operatingState == 0);
if(m_btnHome != null)
m_btnHome.interactable = isActivePet && canOperate;
if(m_btnOut != null)
m_btnOut.interactable = !isActivePet && canOperate;
}
public void OnCommandSummon(int nSlot)
{
if (nSlot < 0)
return;
var actionSwitcher = GetHostPlayer().GetActionSwitcher();
if (actionSwitcher != null && actionSwitcher.OnFlyToRideAction(nSlot))
return;
GetHostPlayer().SummonPet(nSlot);
CloseDialogue();
}
public void OnCommandRecall()
{
GetHostPlayer().RecallPet();
CloseDialogue();
}
public void OnPetDataChanged()
{
m_cachedPetCorral = null;
if(IsShow())
RefreshUI();
}
}
}