Files
AppleHillsProduction/Assets/Scripts/UI/CardSystem/AlbumViewPage.cs

339 lines
11 KiB
C#

using System.Collections.Generic;
using AppleHills.Data.CardSystem;
using Core;
using Data.CardSystem;
using Pixelplacement;
using TMPro;
using UI.Core;
using UnityEngine;
using UnityEngine.UI;
namespace UI.CardSystem
{
/// <summary>
/// UI page for viewing the player's card collection in an album.
/// </summary>
public class AlbumViewPage : UIPage
{
[Header("Album UI Elements")]
[SerializeField] private GridLayoutGroup albumGrid;
[SerializeField] private GameObject cardPrefab;
[Header("Filter UI")]
[SerializeField] private TMP_Dropdown zoneFilterDropdown;
[SerializeField] private TMP_Dropdown rarityFilterDropdown;
[SerializeField] private Button resetFiltersButton;
[SerializeField] private CanvasGroup canvasGroup;
[Header("Navigation")]
[SerializeField] private Button backButton;
// Runtime references
private CardSystemManager _cardManager;
private List<CardDisplay> _displayedCards = new List<CardDisplay>();
private void Awake()
{
_cardManager = CardSystemManager.Instance;
// Make sure we have a CanvasGroup for transitions
if (canvasGroup == null)
canvasGroup = GetComponent<CanvasGroup>();
if (canvasGroup == null)
canvasGroup = gameObject.AddComponent<CanvasGroup>();
// Set up back button
if (backButton != null)
{
backButton.onClick.AddListener(OnBackButtonClicked);
}
}
/// <summary>
/// Sets up the album when the page becomes active
/// </summary>
public override void TransitionIn()
{
base.TransitionIn();
// Initialize the album when the page becomes active
InitializeAlbum();
}
/// <summary>
/// Initializes the album with the player's collection
/// </summary>
private void InitializeAlbum()
{
// Clear any previous setup
ClearAlbum();
// Setup filter UI
InitializeFilters();
// Get all collected cards
List<CardData> collectedCards = _cardManager.GetAllCollectedCards();
// If there are cards to display, create UI elements for them
if (collectedCards.Count > 0)
{
// Create card UI elements
DisplayCards(collectedCards);
}
}
/// <summary>
/// Creates UI elements for the player's collected cards
/// </summary>
private void DisplayCards(List<CardData> cards)
{
if (albumGrid == null || cardPrefab == null) return;
// Sort cards by collection index
cards.Sort((a, b) => a.CollectionIndex.CompareTo(b.CollectionIndex));
// Create card UI elements
foreach (var cardData in cards)
{
GameObject cardObj = Instantiate(cardPrefab, albumGrid.transform);
// Configure the card UI with the card data
// TODO: Update this to use your actual CardDisplay component
// CardUIElement cardUI = cardObj.GetComponent<CardUIElement>();
// if (cardUI != null)
// {
// cardUI.SetupCard(cardData);
// _displayedCards.Add(cardUI);
// }
}
}
/// <summary>
/// Clears all card UI elements from the album
/// </summary>
private void ClearAlbum()
{
foreach (var card in _displayedCards)
{
if (card != null && card.gameObject != null)
Destroy(card.gameObject);
}
_displayedCards.Clear();
}
/// <summary>
/// Initializes filter dropdowns
/// </summary>
private void InitializeFilters()
{
// Setup zone filter dropdown
if (zoneFilterDropdown != null)
{
zoneFilterDropdown.ClearOptions();
// Add "All Zones" option
List<string> zoneOptions = new List<string>() { "All Zones" };
// Add options for each zone
foreach (CardZone zone in System.Enum.GetValues(typeof(CardZone)))
{
zoneOptions.Add(zone.ToString());
}
zoneFilterDropdown.AddOptions(zoneOptions);
zoneFilterDropdown.onValueChanged.AddListener(OnFilterChanged);
}
// Setup rarity filter dropdown
if (rarityFilterDropdown != null)
{
rarityFilterDropdown.ClearOptions();
// Add "All Rarities" option
List<string> rarityOptions = new List<string>() { "All Rarities" };
// Add options for each rarity
foreach (CardRarity rarity in System.Enum.GetValues(typeof(CardRarity)))
{
rarityOptions.Add(rarity.ToString());
}
rarityFilterDropdown.AddOptions(rarityOptions);
rarityFilterDropdown.onValueChanged.AddListener(OnFilterChanged);
}
// Setup reset filters button
if (resetFiltersButton != null)
{
resetFiltersButton.onClick.AddListener(OnResetFiltersClicked);
}
}
/// <summary>
/// Handles changes to the filter dropdowns
/// </summary>
private void OnFilterChanged(int value)
{
ApplyFilters();
}
/// <summary>
/// Resets all filters to their default values
/// </summary>
private void OnResetFiltersClicked()
{
if (zoneFilterDropdown != null)
zoneFilterDropdown.value = 0;
if (rarityFilterDropdown != null)
rarityFilterDropdown.value = 0;
ApplyFilters();
}
/// <summary>
/// Applies the current filter selections to the displayed cards
/// </summary>
private void ApplyFilters()
{
// Clear current cards
ClearAlbum();
// Get selected filters
CardZone? selectedZone = null;
CardRarity? selectedRarity = null;
// Get zone filter value
if (zoneFilterDropdown != null && zoneFilterDropdown.value > 0)
{
selectedZone = (CardZone)(zoneFilterDropdown.value - 1);
}
// Get rarity filter value
if (rarityFilterDropdown != null && rarityFilterDropdown.value > 0)
{
selectedRarity = (CardRarity)(rarityFilterDropdown.value - 1);
}
// Get filtered cards
List<CardData> filteredCards = GetFilteredCards(selectedZone, selectedRarity);
// Create card UI elements for the filtered cards
DisplayCards(filteredCards);
Logging.Debug($"[AlbumViewPage] Applied filters. Showing {filteredCards.Count} cards.");
}
/// <summary>
/// Gets cards filtered by zone and/or rarity
/// </summary>
private List<CardData> GetFilteredCards(CardZone? zone, CardRarity? rarity)
{
List<CardData> result;
// Get all collected cards
if (zone == null && rarity == null)
{
// No filters, return all cards
result = _cardManager.GetAllCollectedCards();
}
else if (zone != null && rarity != null)
{
// Both filters, get cards by zone and rarity
result = _cardManager.GetCardsByZoneAndRarity(zone.Value, rarity.Value);
}
else if (zone != null)
{
// Only zone filter
result = _cardManager.GetCardsByZone(zone.Value);
}
else
{
// Only rarity filter
result = _cardManager.GetCardsByRarity(rarity.Value);
}
return result;
}
/// <summary>
/// Handles click on the back button
/// </summary>
private void OnBackButtonClicked()
{
// Use the UIPageController to go back to the previous page
UIPageController pageController = UIPageController.Instance;
if (pageController != null)
{
pageController.PopPage();
}
}
protected override void DoTransitionIn(System.Action onComplete)
{
// Simple fade in animation
if (canvasGroup != null)
{
canvasGroup.alpha = 0f;
Tween.Value(0f, 1f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete, obeyTimescale: false);
}
else
{
// Fallback if no CanvasGroup
onComplete?.Invoke();
}
}
protected override void DoTransitionOut(System.Action onComplete)
{
// Simple fade out animation
if (canvasGroup != null)
{
Tween.Value(canvasGroup.alpha, 0f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete, obeyTimescale: false);
}
else
{
// Fallback if no CanvasGroup
onComplete?.Invoke();
}
}
private void OnDestroy()
{
// Clean up button listeners
if (backButton != null)
{
backButton.onClick.RemoveListener(OnBackButtonClicked);
}
if (zoneFilterDropdown != null)
{
zoneFilterDropdown.onValueChanged.RemoveListener(OnFilterChanged);
}
if (rarityFilterDropdown != null)
{
rarityFilterDropdown.onValueChanged.RemoveListener(OnFilterChanged);
}
if (resetFiltersButton != null)
{
resetFiltersButton.onClick.RemoveListener(OnResetFiltersClicked);
}
}
private void OnEnable()
{
if (_cardManager == null)
{
_cardManager = CardSystemManager.Instance;
}
}
public void ExitAlbum()
{
UIPageController.Instance.PopPage();
}
}
}