Code up the card part
This commit is contained in:
121
Assets/Scripts/UI/CardSystem/DragDrop/CardDraggableVisual.cs
Normal file
121
Assets/Scripts/UI/CardSystem/DragDrop/CardDraggableVisual.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using AppleHills.Data.CardSystem;
|
||||
using UI.DragAndDrop.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI.CardSystem.DragDrop
|
||||
{
|
||||
/// <summary>
|
||||
/// Visual representation for CardDraggable.
|
||||
/// Uses the existing CardDisplay component to render the card.
|
||||
/// </summary>
|
||||
public class CardDraggableVisual : DraggableVisual
|
||||
{
|
||||
[Header("Card Visual Components")]
|
||||
[SerializeField] private CardDisplay cardDisplay;
|
||||
[SerializeField] private Transform shadowTransform;
|
||||
[SerializeField] private float shadowOffset = 20f;
|
||||
|
||||
private Vector3 _shadowInitialPosition;
|
||||
private CardDraggable _cardDraggable;
|
||||
|
||||
public CardDisplay CardDisplay => cardDisplay;
|
||||
|
||||
public override void Initialize(DraggableObject parent)
|
||||
{
|
||||
base.Initialize(parent);
|
||||
|
||||
_cardDraggable = parent as CardDraggable;
|
||||
|
||||
// Get CardDisplay component if not assigned
|
||||
if (cardDisplay == null)
|
||||
{
|
||||
cardDisplay = GetComponentInChildren<CardDisplay>();
|
||||
}
|
||||
|
||||
// Initialize shadow
|
||||
if (shadowTransform != null)
|
||||
{
|
||||
_shadowInitialPosition = shadowTransform.localPosition;
|
||||
}
|
||||
|
||||
// Subscribe to card data changes
|
||||
if (_cardDraggable != null)
|
||||
{
|
||||
_cardDraggable.OnCardDataChanged += HandleCardDataChanged;
|
||||
|
||||
// Initial card setup
|
||||
if (_cardDraggable.CardData != null && cardDisplay != null)
|
||||
{
|
||||
cardDisplay.SetupCard(_cardDraggable.CardData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void UpdateVisualContent()
|
||||
{
|
||||
// CardDisplay handles its own rendering, no need to update every frame
|
||||
// This is called every frame but we only update when card data changes
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refresh the card display with current data
|
||||
/// </summary>
|
||||
public void RefreshCardDisplay()
|
||||
{
|
||||
if (cardDisplay != null && _cardDraggable != null && _cardDraggable.CardData != null)
|
||||
{
|
||||
cardDisplay.SetupCard(_cardDraggable.CardData);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleCardDataChanged(CardDraggable draggable, CardData data)
|
||||
{
|
||||
RefreshCardDisplay();
|
||||
}
|
||||
|
||||
protected override void OnPointerDownVisual()
|
||||
{
|
||||
base.OnPointerDownVisual();
|
||||
|
||||
// Move shadow down when pressed
|
||||
if (shadowTransform != null)
|
||||
{
|
||||
shadowTransform.localPosition = _shadowInitialPosition + (-Vector3.up * shadowOffset);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnPointerUpVisual(bool longPress)
|
||||
{
|
||||
base.OnPointerUpVisual(longPress);
|
||||
|
||||
// Restore shadow position
|
||||
if (shadowTransform != null)
|
||||
{
|
||||
shadowTransform.localPosition = _shadowInitialPosition;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDragStartedVisual()
|
||||
{
|
||||
base.OnDragStartedVisual();
|
||||
// Card-specific visual effects when dragging starts
|
||||
}
|
||||
|
||||
protected override void OnDragEndedVisual()
|
||||
{
|
||||
base.OnDragEndedVisual();
|
||||
// Card-specific visual effects when dragging ends
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
base.OnDestroy();
|
||||
|
||||
if (_cardDraggable != null)
|
||||
{
|
||||
_cardDraggable.OnCardDataChanged -= HandleCardDataChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user