Add AlbumViewPage prefab

This commit is contained in:
Michal Adam Pikulski
2025-10-20 08:32:57 +02:00
parent 80005e6b7d
commit 07750dd5ba
3 changed files with 3500 additions and 133 deletions

View File

@@ -3,37 +3,30 @@ 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 and organizing the player's card collection in an album.
/// 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 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 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 Dictionary<int, Transform> _albumSlots = new Dictionary<int, Transform>();
// Drag and drop handling
private CardUIElement _currentlyDraggedCard = null;
private Vector3 _cardOriginalPosition;
private void Awake()
{
@@ -58,7 +51,7 @@ namespace AppleHills.UI.CardSystem
}
/// <summary>
/// Initializes the album with card slots and the player's collection
/// Initializes the album with the player's collection
/// </summary>
private void InitializeAlbum()
{
@@ -71,57 +64,11 @@ namespace AppleHills.UI.CardSystem
// Get all collected cards
List<CardData> collectedCards = _cardManager.GetAllCollectedCards();
// Show/hide empty message based on collection
if (emptyAlbumMessage != null)
// If there are cards to display, create UI elements for them
if (collectedCards.Count > 0)
{
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}";
}
}
// Create card UI elements
CreateCardStack(collectedCards);
}
}
@@ -130,7 +77,7 @@ namespace AppleHills.UI.CardSystem
/// </summary>
private void CreateCardStack(List<CardData> cards)
{
if (cardStackContainer == null || cardPrefab == null) return;
if (albumGrid == null || cardPrefab == null) return;
// Stack offset for visual effect
Vector3 stackOffset = new Vector3(5f, -5f, 0f);
@@ -142,7 +89,7 @@ namespace AppleHills.UI.CardSystem
// Create card UI elements
for (int i = 0; i < cards.Count; i++)
{
GameObject cardObj = Instantiate(cardPrefab, cardStackContainer);
GameObject cardObj = Instantiate(cardPrefab, albumGrid.transform);
CardUIElement cardUI = cardObj.GetComponent<CardUIElement>();
if (cardUI != null)
@@ -158,14 +105,6 @@ namespace AppleHills.UI.CardSystem
// 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);
}
}
}
}
@@ -193,36 +132,8 @@ namespace AppleHills.UI.CardSystem
{
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}");
// Currently, clicking a card does not perform any action
// This can be expanded to show card details or other interactions
}
/// <summary>
@@ -239,16 +150,6 @@ namespace AppleHills.UI.CardSystem
}
}
_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>
@@ -356,7 +257,6 @@ namespace AppleHills.UI.CardSystem
// Update the displayed cards
ClearAlbum();
SetupAlbumSlots();
CreateCardStack(filteredCards);
}
@@ -421,4 +321,3 @@ namespace AppleHills.UI.CardSystem
}
}
}