425 lines
15 KiB
C#
425 lines
15 KiB
C#
using System.Collections.Generic;
|
|
using AppleHills.Data.CardSystem;
|
|
using Core;
|
|
using Data.CardSystem;
|
|
using Pixelplacement;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AppleHills.UI.CardSystem
|
|
{
|
|
/// <summary>
|
|
/// UI page for viewing and organizing the player's card collection in an album.
|
|
/// </summary>
|
|
public class AlbumViewPage : UIPage
|
|
{
|
|
[Header("Album UI Elements")]
|
|
[SerializeField] private GridLayoutGroup albumGrid;
|
|
[SerializeField] private RectTransform cardStackContainer;
|
|
[SerializeField] private GameObject emptyAlbumMessage;
|
|
[SerializeField] private GameObject cardSlotPrefab;
|
|
[SerializeField] private GameObject cardPrefab;
|
|
|
|
[Header("Filter UI")]
|
|
[SerializeField] private Dropdown zoneFilterDropdown;
|
|
[SerializeField] private Dropdown rarityFilterDropdown;
|
|
[SerializeField] private Button resetFiltersButton;
|
|
[SerializeField] private CanvasGroup canvasGroup;
|
|
|
|
// Runtime references
|
|
private CardSystemManager _cardManager;
|
|
private List<CardUIElement> _displayedCards = new List<CardUIElement>();
|
|
private Dictionary<int, Transform> _albumSlots = new Dictionary<int, Transform>();
|
|
|
|
// Drag and drop handling
|
|
private CardUIElement _currentlyDraggedCard = null;
|
|
private Vector3 _cardOriginalPosition;
|
|
|
|
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 card slots and 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();
|
|
|
|
// Show/hide empty message based on collection
|
|
if (emptyAlbumMessage != null)
|
|
{
|
|
emptyAlbumMessage.SetActive(collectedCards.Count == 0);
|
|
}
|
|
|
|
if (collectedCards.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Set up the album slots
|
|
SetupAlbumSlots();
|
|
|
|
// Create card UI elements for the stack
|
|
CreateCardStack(collectedCards);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets up empty slots in the album grid
|
|
/// </summary>
|
|
private void SetupAlbumSlots()
|
|
{
|
|
if (albumGrid == null || cardSlotPrefab == null) return;
|
|
|
|
// Create predefined slots in the album
|
|
// For a simple implementation, we'll create a 5x5 grid of slots
|
|
int slotsPerZone = 5; // 5 slots per zone (one row)
|
|
int totalZones = System.Enum.GetValues(typeof(CardZone)).Length;
|
|
|
|
for (int zone = 0; zone < totalZones; zone++)
|
|
{
|
|
for (int i = 0; i < slotsPerZone; i++)
|
|
{
|
|
// Create a slot at this position
|
|
GameObject slotObj = Instantiate(cardSlotPrefab, albumGrid.transform);
|
|
|
|
// Calculate the collection index for this slot
|
|
int collectionIndex = zone * 100 + i; // Zone*100 + position to ensure unique indices
|
|
|
|
// Store the slot reference
|
|
_albumSlots[collectionIndex] = slotObj.transform;
|
|
|
|
// Set the slot label (optional)
|
|
Text slotLabel = slotObj.GetComponentInChildren<Text>();
|
|
if (slotLabel != null)
|
|
{
|
|
CardZone zoneEnum = (CardZone)zone;
|
|
slotLabel.text = $"{zoneEnum} #{i+1}";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates UI elements for the player's collected cards
|
|
/// </summary>
|
|
private void CreateCardStack(List<CardData> cards)
|
|
{
|
|
if (cardStackContainer == 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, cardStackContainer);
|
|
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);
|
|
|
|
// Check if this card should be placed in a slot already
|
|
int collectionIndex = cards[i].CollectionIndex;
|
|
if (_albumSlots.TryGetValue(collectionIndex, out Transform slot))
|
|
{
|
|
// Card has a designated slot, place it there
|
|
PlaceCardInSlot(cardUI, slot);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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();
|
|
|
|
// Find the slot for this card based on collection index
|
|
if (_albumSlots.TryGetValue(cardData.CollectionIndex, out Transform slot))
|
|
{
|
|
// Place the card in its slot
|
|
PlaceCardInSlot(cardUI, slot);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Places a card in a specific album slot
|
|
/// </summary>
|
|
private void PlaceCardInSlot(CardUIElement cardUI, Transform slotTransform)
|
|
{
|
|
if (cardUI == null || slotTransform == null) return;
|
|
|
|
// Save original parent to revert if needed
|
|
Transform originalParent = cardUI.transform.parent;
|
|
Vector3 originalPosition = cardUI.transform.position;
|
|
Vector3 originalScale = cardUI.transform.localScale;
|
|
|
|
// Animate card moving to slot
|
|
cardUI.transform.SetParent(slotTransform);
|
|
|
|
// Use tween to animate the placement
|
|
Tween.Position(cardUI.transform, slotTransform.position, 0.3f, 0f);
|
|
Tween.LocalScale(cardUI.transform, Vector3.one, 0.3f, 0f);
|
|
|
|
// Update the card's internal state (if needed)
|
|
CardData cardData = cardUI.GetCardData();
|
|
Logging.Debug($"[AlbumViewPage] Placed card {cardData.Name} in slot {cardData.CollectionIndex}");
|
|
}
|
|
|
|
/// <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();
|
|
|
|
// Clear album slots
|
|
foreach (var slotPair in _albumSlots)
|
|
{
|
|
if (slotPair.Value != null && slotPair.Value.gameObject != null)
|
|
{
|
|
Destroy(slotPair.Value.gameObject);
|
|
}
|
|
}
|
|
_albumSlots.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();
|
|
SetupAlbumSlots();
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|