304 lines
8.9 KiB
C#
304 lines
8.9 KiB
C#
using System;
|
|
using AppleHills.Data.CardSystem;
|
|
using Core;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
namespace UI.CardSystem
|
|
{
|
|
/// <summary>
|
|
/// Displays a single card using the new visual system with frames, overlays, backgrounds, and zone shapes.
|
|
/// </summary>
|
|
public class CardDisplay : MonoBehaviour
|
|
{
|
|
[Header("UI Elements")]
|
|
[SerializeField] private TextMeshProUGUI cardNameText;
|
|
[SerializeField] private Image cardImage;
|
|
[SerializeField] private Image frameImage;
|
|
[SerializeField] private Image overlayImage;
|
|
[SerializeField] private Image backgroundImage;
|
|
[SerializeField] private Image zoneShapeImage;
|
|
|
|
[Header("Card Data")]
|
|
[SerializeField] private CardData cardData;
|
|
|
|
[Header("Visual Settings")]
|
|
[SerializeField] private CardVisualConfig visualConfig;
|
|
|
|
#if UNITY_EDITOR
|
|
[Header("Editor Tools")]
|
|
[SerializeField] private CardDefinition editorCardDefinition;
|
|
#endif
|
|
|
|
// Events
|
|
public event Action<CardDisplay> OnCardClicked;
|
|
|
|
/// <summary>
|
|
/// Sets up the card display with the given card data
|
|
/// </summary>
|
|
public void SetupCard(CardData data)
|
|
{
|
|
if (data == null)
|
|
{
|
|
Logging.Warning("[CardDisplay] Attempted to setup card with null data");
|
|
return;
|
|
}
|
|
|
|
cardData = data;
|
|
UpdateCardVisuals();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates all visual elements based on current card data
|
|
/// </summary>
|
|
private void UpdateCardVisuals()
|
|
{
|
|
if (cardData == null)
|
|
{
|
|
ClearCard();
|
|
return;
|
|
}
|
|
|
|
if (visualConfig == null)
|
|
{
|
|
Logging.Warning("[CardDisplay] No CardVisualConfig assigned. Visuals may not display correctly.");
|
|
}
|
|
|
|
// Update text
|
|
UpdateCardName();
|
|
|
|
// Update main card image
|
|
UpdateCardImage();
|
|
|
|
// Update rarity-based visuals (frame and overlay)
|
|
UpdateRarityVisuals();
|
|
|
|
// Update zone-based visuals (background and shape)
|
|
UpdateZoneVisuals();
|
|
|
|
Logging.Debug($"[CardDisplay] Updated visuals for card: {cardData.Name} (Rarity: {cardData.Rarity}, Zone: {cardData.Zone})");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the card name text
|
|
/// </summary>
|
|
private void UpdateCardName()
|
|
{
|
|
if (cardNameText != null)
|
|
{
|
|
cardNameText.text = cardData.Name ?? "Unknown Card";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the main card image
|
|
/// </summary>
|
|
private void UpdateCardImage()
|
|
{
|
|
if (cardImage != null)
|
|
{
|
|
if (cardData.CardImage != null)
|
|
{
|
|
cardImage.sprite = cardData.CardImage;
|
|
cardImage.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
cardImage.sprite = null;
|
|
cardImage.enabled = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates frame and overlay based on card rarity
|
|
/// </summary>
|
|
private void UpdateRarityVisuals()
|
|
{
|
|
if (visualConfig == null) return;
|
|
|
|
// Update frame
|
|
if (frameImage != null)
|
|
{
|
|
Sprite frameSprite = visualConfig.GetRarityFrame(cardData.Rarity);
|
|
if (frameSprite != null)
|
|
{
|
|
frameImage.sprite = frameSprite;
|
|
frameImage.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
frameImage.enabled = false;
|
|
}
|
|
}
|
|
|
|
// Update overlay (can be null for some rarities)
|
|
if (overlayImage != null)
|
|
{
|
|
Sprite overlaySprite = visualConfig.GetRarityOverlay(cardData.Rarity);
|
|
if (overlaySprite != null)
|
|
{
|
|
overlayImage.sprite = overlaySprite;
|
|
overlayImage.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
overlayImage.enabled = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates background and zone shape based on card zone and rarity
|
|
/// Special handling for Legendary cards
|
|
/// </summary>
|
|
private void UpdateZoneVisuals()
|
|
{
|
|
if (visualConfig == null) return;
|
|
|
|
// Update background
|
|
// Legendary cards get special background, others get zone background
|
|
if (backgroundImage != null)
|
|
{
|
|
Sprite bgSprite = visualConfig.GetBackground(cardData.Zone, cardData.Rarity);
|
|
if (bgSprite != null)
|
|
{
|
|
backgroundImage.sprite = bgSprite;
|
|
backgroundImage.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
backgroundImage.enabled = false;
|
|
}
|
|
}
|
|
|
|
// Update zone shape
|
|
// Legendary cards don't show shapes
|
|
if (zoneShapeImage != null)
|
|
{
|
|
Sprite shapeSprite = visualConfig.GetZoneShape(cardData.Zone, cardData.Rarity);
|
|
if (shapeSprite != null)
|
|
{
|
|
zoneShapeImage.sprite = shapeSprite;
|
|
zoneShapeImage.enabled = true;
|
|
}
|
|
else
|
|
{
|
|
zoneShapeImage.enabled = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears all visual elements
|
|
/// </summary>
|
|
private void ClearCard()
|
|
{
|
|
if (cardNameText != null) cardNameText.text = "";
|
|
if (cardImage != null) cardImage.enabled = false;
|
|
if (frameImage != null) frameImage.enabled = false;
|
|
if (overlayImage != null) overlayImage.enabled = false;
|
|
if (backgroundImage != null) backgroundImage.enabled = false;
|
|
if (zoneShapeImage != null) zoneShapeImage.enabled = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when the card is clicked
|
|
/// </summary>
|
|
public void OnClick()
|
|
{
|
|
OnCardClicked?.Invoke(this);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the card data associated with this display
|
|
/// </summary>
|
|
public CardData GetCardData()
|
|
{
|
|
return cardData;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the visual config reference
|
|
/// </summary>
|
|
public void SetVisualConfig(CardVisualConfig config)
|
|
{
|
|
visualConfig = config;
|
|
if (cardData != null)
|
|
{
|
|
UpdateCardVisuals();
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
/// <summary>
|
|
/// Editor-only: Creates card data from the assigned definition and displays it
|
|
/// </summary>
|
|
public void PreviewFromDefinition()
|
|
{
|
|
if (editorCardDefinition == null)
|
|
{
|
|
Logging.Warning("[CardDisplay] Cannot preview: No card definition assigned in editor");
|
|
return;
|
|
}
|
|
|
|
cardData = editorCardDefinition.CreateCardData();
|
|
UpdateCardVisuals();
|
|
|
|
Logging.Debug($"[CardDisplay] Previewing card from definition: {cardData.Name}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Editor-only: Clears the preview
|
|
/// </summary>
|
|
public void ClearPreview()
|
|
{
|
|
cardData = null;
|
|
ClearCard();
|
|
Logging.Debug("[CardDisplay] Preview cleared");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
[CustomEditor(typeof(CardDisplay))]
|
|
public class CardDisplayEditor : Editor
|
|
{
|
|
public override void OnInspectorGUI()
|
|
{
|
|
DrawDefaultInspector();
|
|
|
|
CardDisplay display = (CardDisplay)target;
|
|
|
|
EditorGUILayout.Space();
|
|
EditorGUILayout.LabelField("Editor Preview Tools", EditorStyles.boldLabel);
|
|
|
|
EditorGUILayout.BeginHorizontal();
|
|
if (GUILayout.Button("Preview Card"))
|
|
{
|
|
display.PreviewFromDefinition();
|
|
EditorUtility.SetDirty(display);
|
|
}
|
|
|
|
if (GUILayout.Button("Clear Preview"))
|
|
{
|
|
display.ClearPreview();
|
|
EditorUtility.SetDirty(display);
|
|
}
|
|
EditorGUILayout.EndHorizontal();
|
|
|
|
EditorGUILayout.Space();
|
|
EditorGUILayout.HelpBox(
|
|
"Assign a Card Definition in 'Editor Tools' section, then click 'Preview Card' to see how it will look at runtime.",
|
|
MessageType.Info
|
|
);
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|