Files
test/Assets/PerfectWorld/Scripts/UI/ShopDetailPanel.cs
T
2025-10-16 10:25:49 +07:00

220 lines
6.5 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ShopDetailPanel : MonoBehaviour
{
[Header("UI Components")]
public TextMeshProUGUI itemNameText;
public TextMeshProUGUI itemDescriptionText;
public Image itemIconImage;
public TextMeshProUGUI itemPriceText;
public TextMeshProUGUI itemQuantityText;
public TextMeshProUGUI itemGiftText;
[Header("Buy Options")]
public Button[] buyOptionButtons; // Up to 4 buy options
public TextMeshProUGUI[] buyOptionPriceTexts;
public TextMeshProUGUI[] buyOptionStatusTexts;
[Header("Action Buttons")]
public Button buyButton;
public Button closeButton;
private GShopItem currentItem;
private ShopUIManager shopManager;
void Start()
{
SetupEventListeners();
}
void SetupEventListeners()
{
if (buyButton != null)
buyButton.onClick.AddListener(OnBuyClicked);
if (closeButton != null)
closeButton.onClick.AddListener(OnCloseClicked);
// Setup buy option buttons
for (int i = 0; i < buyOptionButtons.Length; i++)
{
int optionIndex = i; // Capture for closure
if (buyOptionButtons[i] != null)
{
buyOptionButtons[i].onClick.AddListener(() => OnBuyOptionSelected(optionIndex));
}
}
}
public void SetupDetailPanel(GShopItem item, ShopUIManager manager)
{
currentItem = item;
shopManager = manager;
UpdateDisplay();
}
void UpdateDisplay()
{
if (currentItem.id == 0)
return;
// Set basic item info
if (itemNameText != null)
itemNameText.text = currentItem.name;
if (itemDescriptionText != null)
itemDescriptionText.text = currentItem.desc;
if (itemQuantityText != null)
itemQuantityText.text = $"Quantity: {currentItem.num}";
// Load icon
if (itemIconImage != null)
{
LoadItemIcon(itemIconImage, currentItem.name);
}
// Update buy options
UpdateBuyOptions();
// Update gift info
UpdateGiftInfo();
}
void UpdateBuyOptions()
{
for (int i = 0; i < buyOptionButtons.Length && i < currentItem.buy.Length; i++)
{
var buyOption = currentItem.buy[i];
// Show/hide buy option based on price
bool hasValidPrice = buyOption.price > 0;
if (buyOptionButtons[i] != null)
{
buyOptionButtons[i].gameObject.SetActive(hasValidPrice);
}
if (hasValidPrice)
{
// Set price text
if (i < buyOptionPriceTexts.Length && buyOptionPriceTexts[i] != null)
{
buyOptionPriceTexts[i].text = buyOption.price.ToString();
}
// Set status text
if (i < buyOptionStatusTexts.Length && buyOptionStatusTexts[i] != null)
{
string statusText = GetStatusText(buyOption.status);
buyOptionStatusTexts[i].text = statusText;
}
}
}
}
string GetStatusText(uint status)
{
switch (status)
{
case 0: return "";
case 1: return "HOT";
case 2: return "NEW";
case 3: return "RECOMMENDED";
case 4: return "10% OFF";
case 5: return "20% OFF";
case 6: return "30% OFF";
case 7: return "40% OFF";
case 8: return "50% OFF";
case 9: return "60% OFF";
case 10: return "70% OFF";
case 11: return "80% OFF";
case 12: return "90% OFF";
case 13: return "SOLD OUT";
default: return "";
}
}
void UpdateGiftInfo()
{
if (itemGiftText != null)
{
if (currentItem.idGift > 0)
{
itemGiftText.text = $"Gift: {currentItem.giftNum}x (ID: {currentItem.idGift})";
itemGiftText.gameObject.SetActive(true);
}
else
{
itemGiftText.gameObject.SetActive(false);
}
}
}
void LoadItemIcon(Image iconImage, string itemName)
{
// TODO: Implement icon loading based on item name
// This is where you'd load the appropriate icon sprite
Debug.Log($"Loading detail icon for item: {itemName}");
// You can implement icon loading like this:
// string iconPath = $"Icons/{itemName}"; // Adjust path as needed
// Sprite iconSprite = Resources.Load<Sprite>(iconPath);
// if (iconSprite != null)
// iconImage.sprite = iconSprite;
}
void OnBuyOptionSelected(int optionIndex)
{
if (currentItem.buy == null || optionIndex >= currentItem.buy.Length)
return;
var selectedOption = currentItem.buy[optionIndex];
if (selectedOption.price > 0 && selectedOption.status != 13) // Not sold out
{
// TODO: Implement purchase with specific buy option
Debug.Log($"Selected buy option {optionIndex}: Price={selectedOption.price}, Status={selectedOption.status}");
// Update main price display
if (itemPriceText != null)
itemPriceText.text = $"Price: {selectedOption.price}";
}
}
void OnBuyClicked()
{
// TODO: Implement purchase logic
Debug.Log($"Attempting to buy item: {currentItem.name} (ID: {currentItem.id})");
// Close panel after purchase attempt
OnCloseClicked();
}
void OnCloseClicked()
{
gameObject.SetActive(false);
}
void OnDestroy()
{
// Clean up event listeners
if (buyButton != null)
buyButton.onClick.RemoveListener(OnBuyClicked);
if (closeButton != null)
closeButton.onClick.RemoveListener(OnCloseClicked);
for (int i = 0; i < buyOptionButtons.Length; i++)
{
if (buyOptionButtons[i] != null)
{
int optionIndex = i; // Capture for closure
buyOptionButtons[i].onClick.RemoveListener(() => OnBuyOptionSelected(optionIndex));
}
}
}
}