Files
AppleHillsProduction/Assets/Scripts/UI/CardSystem/CardAlbumUI.cs
2025-10-14 15:54:11 +02:00

189 lines
5.6 KiB
C#

using System;
using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using UnityEngine;
using UnityEngine.UI;
namespace AppleHills.UI.CardSystem
{
/// <summary>
/// Main UI controller for the card album system.
/// Manages the backpack icon and navigation between card system pages.
/// </summary>
public class CardAlbumUI : MonoBehaviour
{
[Header("UI References")]
[SerializeField] private GameObject backpackIcon;
[SerializeField] private UIPage mainMenuPage;
[SerializeField] private UIPage albumViewPage;
[SerializeField] private UIPage boosterOpeningPage;
[Header("UI Elements")]
[SerializeField] private Button backpackButton;
[SerializeField] private Text boosterCountText;
private UIPageController _pageController;
private CardSystemManager _cardManager;
private bool _isInitialized = false;
private void Awake()
{
// Get or create a UI page controller
_pageController = FindAnyObjectByType<UIPageController>();
if (_pageController == null)
{
GameObject controllerObj = new GameObject("UIPageController");
controllerObj.transform.SetParent(transform);
_pageController = controllerObj.AddComponent<UIPageController>();
}
// Initialize pages and hide them
InitializePages();
// Set up backpack button
if (backpackButton != null)
{
backpackButton.onClick.AddListener(OnBackpackButtonClicked);
}
// Initially show only the backpack icon
ShowOnlyBackpackIcon();
}
private void Start()
{
// Get card manager
_cardManager = CardSystemManager.Instance;
// Subscribe to events
if (_cardManager != null)
{
_cardManager.OnBoosterCountChanged += UpdateBoosterCount;
UpdateBoosterCount(_cardManager.GetBoosterPackCount());
}
_isInitialized = true;
}
private void OnDestroy()
{
// Unsubscribe from events
if (_cardManager != null)
{
_cardManager.OnBoosterCountChanged -= UpdateBoosterCount;
}
// Clean up button listeners
if (backpackButton != null)
{
backpackButton.onClick.RemoveListener(OnBackpackButtonClicked);
}
}
/// <summary>
/// Initializes all UI pages and sets them up with proper callbacks
/// </summary>
private void InitializePages()
{
// Ensure all pages are inactive at start
if (mainMenuPage != null)
{
mainMenuPage.gameObject.SetActive(false);
}
if (albumViewPage != null)
{
albumViewPage.gameObject.SetActive(false);
}
if (boosterOpeningPage != null)
{
boosterOpeningPage.gameObject.SetActive(false);
}
// Set up page changed callback
if (_pageController != null)
{
_pageController.OnPageChanged += OnPageChanged;
}
}
/// <summary>
/// Handles click on the backpack icon
/// </summary>
private void OnBackpackButtonClicked()
{
// If no pages are open, push the main menu
if (_pageController.CurrentPage == null)
{
_pageController.PushPage(mainMenuPage);
}
else if (_pageController.CurrentPage == mainMenuPage)
{
// If main menu is open, pop it
_pageController.PopPage();
}
}
/// <summary>
/// Handles changes to the current page
/// </summary>
private void OnPageChanged(UIPage newPage)
{
// Hide/show backpack icon based on current page
if (newPage == null)
{
ShowOnlyBackpackIcon();
}
else
{
backpackIcon.SetActive(false);
}
}
/// <summary>
/// Shows only the backpack icon, hiding all other UI elements
/// </summary>
private void ShowOnlyBackpackIcon()
{
backpackIcon.SetActive(true);
}
/// <summary>
/// Opens the album view page
/// </summary>
public void OpenAlbumView()
{
_pageController.PushPage(albumViewPage);
}
/// <summary>
/// Opens the booster opening page if boosters are available
/// </summary>
public void OpenBoosterPack()
{
if (_cardManager != null && _cardManager.GetBoosterPackCount() > 0)
{
_pageController.PushPage(boosterOpeningPage);
}
else
{
Logging.Debug("[CardAlbumUI] No booster packs available");
// TODO: Show "no boosters available" message
}
}
/// <summary>
/// Updates the booster count display
/// </summary>
private void UpdateBoosterCount(int count)
{
if (boosterCountText != null)
{
boosterCountText.text = count.ToString();
}
}
}
}