Files
AppleHillsProduction/Assets/Scripts/Minigames/DivingForPictures/PictureCamera/Viewfinder.cs

143 lines
4.4 KiB
C#
Raw Normal View History

using Core;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Input;
namespace Minigames.DivingForPictures.PictureCamera
{
/// <summary>
/// Attached to the viewfinder UI prefab. Handles tap detection and other viewfinder-specific operations.
/// </summary>
public class Viewfinder : MonoBehaviour, ITouchInputConsumer
{
[SerializeField]
private Image[] viewfinderImages; // Array of Image components to tint based on proximity
// Events
public event System.Action OnViewfinderTapped;
public event System.Action OnViewfinderHoldStarted;
public event System.Action OnViewfinderHoldEnded;
// State
private bool isActive = true;
private RectTransform _rectTransform;
private CameraViewfinderManager _viewfinderManager;
private void Awake()
{
_rectTransform = GetComponent<RectTransform>();
}
/// <summary>
/// Initializes the viewfinder with a reference to its manager
/// </summary>
/// <param name="manager">Reference to the CameraViewfinderManager</param>
public void Initialize(CameraViewfinderManager manager)
{
_viewfinderManager = manager;
if (_viewfinderManager != null)
{
_viewfinderManager.OnProximityUpdated += UpdateProximityColor;
}
}
private void OnDisable()
{
// Unsubscribe from events when disabled
if (_viewfinderManager != null)
{
_viewfinderManager.OnProximityUpdated -= UpdateProximityColor;
}
}
/// <summary>
/// Updates the color of all viewfinder images based on proximity value
/// </summary>
/// <param name="proximity">Proximity value between 0 and 1 (0=far, 1=close)</param>
private void UpdateProximityColor(float proximity)
{
// Lerp between green (close) and red (far)
Color tintColor = Color.Lerp(Color.red, Color.green, proximity);
// Apply the color to all referenced image components
if (viewfinderImages != null && viewfinderImages.Length > 0)
{
foreach (Image image in viewfinderImages)
{
if (image != null)
{
image.color = tintColor;
}
}
}
}
/// <summary>
/// Enable or disable input reception
/// </summary>
public void SetActive(bool active)
{
isActive = active;
}
#region ITouchInputConsumer Implementation
public void SetupInputOverride()
{
InputManager.Instance.RegisterOverrideConsumer(this);
}
public void RemoveInputOverride()
{
InputManager.Instance.UnregisterOverrideConsumer(this);
}
public void OnTap(Vector2 position)
{
if (isActive)
{
// Fire the tap event that PhotoSequenceController will listen to
OnViewfinderTapped?.Invoke();
Logging.Debug($"[MDPI] Viewfinder OnTap: {position}");
}
}
public void OnHoldStart(Vector2 position)
{
if (isActive)
{
// Fire the hold start event
OnViewfinderHoldStarted?.Invoke();
Logging.Debug($"[MDPI] Viewfinder OnHoldStart: {position}");
}
}
public void OnHoldMove(Vector2 position)
{
// We can use this for any continuous effects during hold if needed in the future
}
public void OnHoldEnd(Vector2 position)
{
if (isActive)
{
// Fire the hold end event
OnViewfinderHoldEnded?.Invoke();
Logging.Debug($"[MDPI] Viewfinder OnHoldEnd: {position}");
}
}
#endregion
/// <summary>
/// Check if a point is within the viewfinder's bounds
/// </summary>
public bool ContainsPoint(Vector2 screenPoint)
{
return RectTransformUtility.RectangleContainsScreenPoint(_rectTransform, screenPoint);
}
}
}