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) { this.backdrop = backdrop; this.enlargedContainer = enlargedContainer; this.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; } } }