Files
AppleHillsProduction/Assets/Scripts/Minigames/StatueDressup/Controllers/PhotoEnlargeController.cs

164 lines
5.8 KiB
C#
Raw Normal View History

2025-11-26 17:11:02 +01:00
using Core;
using UnityEngine;
using UnityEngine.EventSystems;
using Utils;
namespace Minigames.StatueDressup.Controllers
{
/// <summary>
/// Manages enlarged photo preview display.
/// Handles backdrop, preview spawning, and click-to-dismiss.
/// Similar to CardEnlargeController but simpler (no state machine).
/// </summary>
public class PhotoEnlargeController
{
private readonly GameObject _backdrop;
private readonly Transform _enlargedContainer;
private readonly float _animationDuration;
private GameObject _currentEnlargedPreview;
private PhotoGridItem _currentSourceItem;
/// <summary>
/// Constructor
/// </summary>
public PhotoEnlargeController(GameObject backdrop, Transform enlargedContainer, float animationDuration = 0.3f)
{
_backdrop = backdrop;
_enlargedContainer = enlargedContainer;
_animationDuration = animationDuration;
}
/// <summary>
/// Check if a photo is currently enlarged
/// </summary>
public bool IsPhotoEnlarged => _currentEnlargedPreview != null;
/// <summary>
/// Enlarge a photo from a grid item
/// </summary>
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<UnityEngine.UI.Image>();
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<EventTrigger>();
if (clickHandler == null)
{
clickHandler = _currentEnlargedPreview.AddComponent<EventTrigger>();
}
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");
}
/// <summary>
/// Shrink the currently enlarged photo back to grid
/// </summary>
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");
}
/// <summary>
/// Cleanup - call when gallery is closed
/// </summary>
public void Cleanup()
{
if (_currentEnlargedPreview != null)
{
Object.Destroy(_currentEnlargedPreview);
_currentEnlargedPreview = null;
}
if (_backdrop != null)
{
_backdrop.SetActive(false);
}
_currentSourceItem = null;
}
}
}