using Core; using UnityEngine; using UnityEngine.EventSystems; using Utils; namespace Minigames.StatueDressup.Controllers { /// /// Manages enlarged photo preview display. /// Handles backdrop, preview spawning, and click-to-dismiss. /// Similar to CardEnlargeController but simpler (no state machine). /// public class PhotoEnlargeController { private readonly GameObject _backdrop; private readonly Transform _enlargedContainer; private readonly float _animationDuration; private GameObject _currentEnlargedPreview; private PhotoGridItem _currentSourceItem; /// /// Constructor /// public PhotoEnlargeController(GameObject backdrop, Transform enlargedContainer, float animationDuration = 0.3f) { _backdrop = backdrop; _enlargedContainer = enlargedContainer; _animationDuration = animationDuration; } /// /// Check if a photo is currently enlarged /// public bool IsPhotoEnlarged => _currentEnlargedPreview != null; /// /// Enlarge a photo from a grid item /// public void EnlargePhoto(PhotoGridItem sourceItem, GameObject previewPrefab, Texture2D photoTexture, float enlargedScale) { if (sourceItem == null || previewPrefab == null || photoTexture == null) { Logging.Error("[PhotoEnlargeController] Invalid parameters for EnlargePhoto"); return; } // Don't allow multiple enlargements if (_currentEnlargedPreview != null) { Logging.Warning("[PhotoEnlargeController] Photo already enlarged"); return; } _currentSourceItem = sourceItem; // Show backdrop if (_backdrop != null) { _backdrop.SetActive(true); } // Spawn preview clone _currentEnlargedPreview = Object.Instantiate(previewPrefab, _enlargedContainer); _currentEnlargedPreview.transform.SetAsLastSibling(); // Position at source item's world position _currentEnlargedPreview.transform.position = sourceItem.transform.position; _currentEnlargedPreview.transform.localScale = sourceItem.transform.localScale; // Set photo texture on preview var previewImage = _currentEnlargedPreview.GetComponent(); if (previewImage != null) { // Create sprite from texture Sprite photoSprite = Sprite.Create( photoTexture, new Rect(0, 0, photoTexture.width, photoTexture.height), new Vector2(0.5f, 0.5f) ); previewImage.sprite = photoSprite; } // Add click handler to preview var clickHandler = _currentEnlargedPreview.GetComponent(); if (clickHandler == null) { clickHandler = _currentEnlargedPreview.AddComponent(); } var pointerClickEntry = new EventTrigger.Entry { eventID = EventTriggerType.PointerClick }; pointerClickEntry.callback.AddListener((_) => { ShrinkPhoto(); }); clickHandler.triggers.Add(pointerClickEntry); // Animate to center and scale up TweenAnimationUtility.AnimateLocalPosition(_currentEnlargedPreview.transform, Vector3.zero, _animationDuration); TweenAnimationUtility.AnimateScale(_currentEnlargedPreview.transform, Vector3.one * enlargedScale, _animationDuration); // Play audio feedback AudioManager.Instance.LoadAndPlayUIAudio("card_albumdrop_deep", false); Logging.Debug("[PhotoEnlargeController] Photo enlarged"); } /// /// Shrink the currently enlarged photo back to grid /// public void ShrinkPhoto() { if (_currentEnlargedPreview == null || _currentSourceItem == null) { Logging.Warning("[PhotoEnlargeController] No photo to shrink"); return; } // Hide backdrop if (_backdrop != null) { _backdrop.SetActive(false); } // Get target position from source item Vector3 targetWorldPos = _currentSourceItem.transform.position; Vector3 targetScale = _currentSourceItem.transform.localScale; GameObject previewToDestroy = _currentEnlargedPreview; // Animate back to source position Pixelplacement.Tween.Position(previewToDestroy.transform, targetWorldPos, _animationDuration, 0f, Pixelplacement.Tween.EaseInOut); TweenAnimationUtility.AnimateScale(previewToDestroy.transform, targetScale, _animationDuration, () => { // Destroy preview after animation Object.Destroy(previewToDestroy); }); // Clear references _currentEnlargedPreview = null; _currentSourceItem = null; Logging.Debug("[PhotoEnlargeController] Photo shrunk"); } /// /// Cleanup - call when gallery is closed /// public void Cleanup() { if (_currentEnlargedPreview != null) { Object.Destroy(_currentEnlargedPreview); _currentEnlargedPreview = null; } if (_backdrop != null) { _backdrop.SetActive(false); } _currentSourceItem = null; } } }