129 lines
4.6 KiB
C#
129 lines
4.6 KiB
C#
using Core;
|
|
using UI.CardSystem.StateMachine;
|
|
using UnityEngine;
|
|
|
|
namespace UI.CardSystem
|
|
{
|
|
/// <summary>
|
|
/// Manages card enlarge system: backdrop display and card reparenting.
|
|
/// Created and owned by AlbumViewPage (not a Unity component).
|
|
/// </summary>
|
|
public class CardEnlargeController
|
|
{
|
|
private readonly GameObject _backdrop;
|
|
private readonly Transform _enlargedContainer;
|
|
|
|
/// <summary>
|
|
/// Constructor - called by AlbumViewPage's lazy property
|
|
/// </summary>
|
|
public CardEnlargeController(GameObject backdrop, Transform enlargedContainer)
|
|
{
|
|
_backdrop = backdrop;
|
|
_enlargedContainer = enlargedContainer;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Subscribe to a placed card's enlarged state events
|
|
/// </summary>
|
|
public void RegisterCard(StateMachine.Card card)
|
|
{
|
|
if (card == null) return;
|
|
|
|
var enlargeState = card.GetStateComponent<StateMachine.States.CardAlbumEnlargedState>(CardStateNames.AlbumEnlarged);
|
|
if (enlargeState != null)
|
|
{
|
|
enlargeState.OnEnlargeRequested += OnCardEnlargeRequested;
|
|
enlargeState.OnShrinkRequested += OnCardShrinkRequested;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unsubscribe from a card's enlarged state events
|
|
/// </summary>
|
|
public void UnregisterCard(StateMachine.Card card)
|
|
{
|
|
if (card == null) return;
|
|
|
|
var enlargeState = card.GetStateComponent<StateMachine.States.CardAlbumEnlargedState>(CardStateNames.AlbumEnlarged);
|
|
if (enlargeState != null)
|
|
{
|
|
enlargeState.OnEnlargeRequested -= OnCardEnlargeRequested;
|
|
enlargeState.OnShrinkRequested -= OnCardShrinkRequested;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clean up enlarged card state (on page close)
|
|
/// </summary>
|
|
public void CleanupEnlargedState()
|
|
{
|
|
// Hide backdrop if visible
|
|
if (_backdrop != null && _backdrop.activeSelf)
|
|
{
|
|
_backdrop.SetActive(false);
|
|
}
|
|
|
|
// If there's an enlarged card in the container, return it to its slot
|
|
if (_enlargedContainer != null && _enlargedContainer.childCount > 0)
|
|
{
|
|
for (int i = _enlargedContainer.childCount - 1; i >= 0; i--)
|
|
{
|
|
Transform cardTransform = _enlargedContainer.GetChild(i);
|
|
var card = cardTransform.GetComponent<StateMachine.Card>();
|
|
var state = cardTransform.GetComponentInChildren<StateMachine.States.CardAlbumEnlargedState>(true);
|
|
if (card != null && state != null)
|
|
{
|
|
Transform originalParent = state.GetOriginalParent();
|
|
if (originalParent != null)
|
|
{
|
|
cardTransform.SetParent(originalParent, true);
|
|
cardTransform.localPosition = state.GetOriginalLocalPosition();
|
|
cardTransform.localRotation = state.GetOriginalLocalRotation();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#region Event Handlers
|
|
|
|
private void OnCardEnlargeRequested(StateMachine.States.CardAlbumEnlargedState state)
|
|
{
|
|
if (state == null) return;
|
|
|
|
// Show backdrop
|
|
if (_backdrop != null)
|
|
{
|
|
_backdrop.SetActive(true);
|
|
}
|
|
|
|
// Reparent card root to enlarged container preserving world transform
|
|
if (_enlargedContainer != null)
|
|
{
|
|
var ctx = state.GetComponentInParent<StateMachine.CardContext>();
|
|
if (ctx != null)
|
|
{
|
|
ctx.RootTransform.SetParent(_enlargedContainer, true);
|
|
ctx.RootTransform.SetAsLastSibling();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnCardShrinkRequested(StateMachine.States.CardAlbumEnlargedState state)
|
|
{
|
|
if (state == null) return;
|
|
|
|
// Hide backdrop; state will animate back to slot and reparent on completion
|
|
if (_backdrop != null)
|
|
{
|
|
_backdrop.SetActive(false);
|
|
}
|
|
|
|
// Do not reparent here; reverse animation is orchestrated by the state
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|