324 lines
11 KiB
C#
324 lines
11 KiB
C#
using System.Collections.Generic;
|
|
using AppleHills.Data.CardSystem;
|
|
using Core;
|
|
using Data.CardSystem;
|
|
using Pixelplacement;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AppleHills.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;
|
|
|
|
// Runtime references
|
|
private CardSystemManager _cardManager;
|
|
private List<CardUIElement> _displayedCards = new List<CardUIElement>();
|
|
|
|
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>();
|
|
}
|
|
|
|
/// <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
|
|
CreateCardStack(collectedCards);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates UI elements for the player's collected cards
|
|
/// </summary>
|
|
private void CreateCardStack(List<CardData> cards)
|
|
{
|
|
if (albumGrid == null || cardPrefab == null) return;
|
|
|
|
// Stack offset for visual effect
|
|
Vector3 stackOffset = new Vector3(5f, -5f, 0f);
|
|
Vector3 basePosition = Vector3.zero;
|
|
|
|
// Sort cards by collection index
|
|
cards.Sort((a, b) => a.CollectionIndex.CompareTo(b.CollectionIndex));
|
|
|
|
// Create card UI elements
|
|
for (int i = 0; i < cards.Count; i++)
|
|
{
|
|
GameObject cardObj = Instantiate(cardPrefab, albumGrid.transform);
|
|
CardUIElement cardUI = cardObj.GetComponent<CardUIElement>();
|
|
|
|
if (cardUI != null)
|
|
{
|
|
// Position in stack
|
|
cardObj.GetComponent<RectTransform>().anchoredPosition = basePosition + (stackOffset * i);
|
|
|
|
// Set up card data
|
|
cardUI.SetupCard(cards[i]);
|
|
|
|
// Add drag handlers
|
|
SetupCardDragHandlers(cardUI);
|
|
|
|
// Add to tracked cards
|
|
_displayedCards.Add(cardUI);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets up drag and drop handlers for a card
|
|
/// </summary>
|
|
private void SetupCardDragHandlers(CardUIElement cardUI)
|
|
{
|
|
// Add click listener to handle card clicks
|
|
Button cardButton = cardUI.GetComponent<Button>();
|
|
if (cardButton != null)
|
|
{
|
|
cardButton.onClick.AddListener(() => OnCardClicked(cardUI));
|
|
}
|
|
|
|
// Subscribe to the card's own click event
|
|
cardUI.OnCardClicked += OnCardClicked;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles when a card is clicked (simplistic alternative to drag and drop)
|
|
/// </summary>
|
|
private void OnCardClicked(CardUIElement cardUI)
|
|
{
|
|
CardData cardData = cardUI.GetCardData();
|
|
|
|
// Currently, clicking a card does not perform any action
|
|
// This can be expanded to show card details or other interactions
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears all cards from the album
|
|
/// </summary>
|
|
private void ClearAlbum()
|
|
{
|
|
// Clear card UI elements
|
|
foreach (var card in _displayedCards)
|
|
{
|
|
if (card != null && card.gameObject != null)
|
|
{
|
|
Destroy(card.gameObject);
|
|
}
|
|
}
|
|
_displayedCards.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initialize filtering UI and set up listeners
|
|
/// </summary>
|
|
private void InitializeFilters()
|
|
{
|
|
// Setup zone filter
|
|
if (zoneFilterDropdown != null)
|
|
{
|
|
zoneFilterDropdown.ClearOptions();
|
|
|
|
// Add "All Zones" option
|
|
List<string> zoneOptions = new List<string> { "All Zones" };
|
|
|
|
// Add each zone
|
|
foreach (CardZone zone in System.Enum.GetValues(typeof(CardZone)))
|
|
{
|
|
zoneOptions.Add(zone.ToString());
|
|
}
|
|
|
|
zoneFilterDropdown.AddOptions(zoneOptions);
|
|
zoneFilterDropdown.onValueChanged.AddListener(OnZoneFilterChanged);
|
|
}
|
|
|
|
// Setup rarity filter
|
|
if (rarityFilterDropdown != null)
|
|
{
|
|
rarityFilterDropdown.ClearOptions();
|
|
|
|
// Add "All Rarities" option
|
|
List<string> rarityOptions = new List<string> { "All Rarities" };
|
|
|
|
// Add each rarity
|
|
foreach (CardRarity rarity in System.Enum.GetValues(typeof(CardRarity)))
|
|
{
|
|
rarityOptions.Add(rarity.ToString());
|
|
}
|
|
|
|
rarityFilterDropdown.AddOptions(rarityOptions);
|
|
rarityFilterDropdown.onValueChanged.AddListener(OnRarityFilterChanged);
|
|
}
|
|
|
|
// Setup reset button
|
|
if (resetFiltersButton != null)
|
|
{
|
|
resetFiltersButton.onClick.AddListener(ResetFilters);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when zone filter dropdown changes
|
|
/// </summary>
|
|
private void OnZoneFilterChanged(int index)
|
|
{
|
|
ApplyFilters();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when rarity filter dropdown changes
|
|
/// </summary>
|
|
private void OnRarityFilterChanged(int index)
|
|
{
|
|
ApplyFilters();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reset all filters to default values
|
|
/// </summary>
|
|
private void ResetFilters()
|
|
{
|
|
if (zoneFilterDropdown != null)
|
|
zoneFilterDropdown.value = 0;
|
|
|
|
if (rarityFilterDropdown != null)
|
|
rarityFilterDropdown.value = 0;
|
|
|
|
ApplyFilters();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Apply current filter settings to the displayed cards
|
|
/// </summary>
|
|
private void ApplyFilters()
|
|
{
|
|
if (_cardManager == null) return;
|
|
|
|
// Get all cards
|
|
List<CardData> allCards = _cardManager.GetAllCollectedCards();
|
|
List<CardData> filteredCards = new List<CardData>(allCards);
|
|
|
|
// Apply zone filter
|
|
if (zoneFilterDropdown != null && zoneFilterDropdown.value > 0)
|
|
{
|
|
CardZone selectedZone = (CardZone)(zoneFilterDropdown.value - 1);
|
|
filteredCards = filteredCards.FindAll(card => card.Zone == selectedZone);
|
|
}
|
|
|
|
// Apply rarity filter
|
|
if (rarityFilterDropdown != null && rarityFilterDropdown.value > 0)
|
|
{
|
|
CardRarity selectedRarity = (CardRarity)(rarityFilterDropdown.value - 1);
|
|
filteredCards = filteredCards.FindAll(card => card.Rarity == selectedRarity);
|
|
}
|
|
|
|
// Update the displayed cards
|
|
ClearAlbum();
|
|
CreateCardStack(filteredCards);
|
|
}
|
|
|
|
// Override for transition animations
|
|
protected override void DoTransitionIn(System.Action onComplete)
|
|
{
|
|
// Reset the canvas group
|
|
if (canvasGroup != null)
|
|
{
|
|
canvasGroup.alpha = 0f;
|
|
Tween.Value(0f, 1f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete);
|
|
}
|
|
else
|
|
{
|
|
// Simple scale animation if no canvas group
|
|
transform.localScale = Vector3.zero;
|
|
Tween.LocalScale(transform, Vector3.one, transitionDuration, 0f, Tween.EaseOutBack, Tween.LoopType.None, null, onComplete);
|
|
}
|
|
}
|
|
|
|
protected override void DoTransitionOut(System.Action onComplete)
|
|
{
|
|
// Simple fade animation
|
|
if (canvasGroup != null)
|
|
{
|
|
Tween.Value(canvasGroup.alpha, 0f, (value) => canvasGroup.alpha = value, transitionDuration, 0f, Tween.EaseInOut, Tween.LoopType.None, null, onComplete);
|
|
}
|
|
else
|
|
{
|
|
Tween.LocalScale(transform, Vector3.zero, transitionDuration, 0f, Tween.EaseInBack, Tween.LoopType.None, null, onComplete);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
// Clean up filter listeners
|
|
if (zoneFilterDropdown != null)
|
|
zoneFilterDropdown.onValueChanged.RemoveListener(OnZoneFilterChanged);
|
|
|
|
if (rarityFilterDropdown != null)
|
|
rarityFilterDropdown.onValueChanged.RemoveListener(OnRarityFilterChanged);
|
|
|
|
if (resetFiltersButton != null)
|
|
resetFiltersButton.onClick.RemoveListener(ResetFilters);
|
|
|
|
// Clean up card listeners
|
|
foreach (var card in _displayedCards)
|
|
{
|
|
if (card != null)
|
|
{
|
|
// Unsubscribe from card events
|
|
card.OnCardClicked -= OnCardClicked;
|
|
|
|
// Remove button listeners
|
|
Button cardButton = card.GetComponent<Button>();
|
|
if (cardButton != null)
|
|
{
|
|
cardButton.onClick.RemoveAllListeners();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|