Files
AppleHillsProduction/Assets/Scripts/Levels/MinigameSwitchMenu.cs

132 lines
5.2 KiB
C#
Raw Normal View History

2025-10-29 09:57:33 +01:00
using System;
using System.Collections;
2025-10-29 09:57:33 +01:00
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Levels
{
/// <summary>
/// UI overlay for confirming a level switch. Displays level info and handles confirm/cancel actions, and supports scrolling between level info and leaderboard.
2025-10-29 09:57:33 +01:00
/// </summary>
public class MinigameSwitchMenu : MonoBehaviour
{
[Header("UI References")]
public Image iconImage;
public TMP_Text levelNameText;
public Button confirmButton;
public Button cancelButton;
public Button toLeaderboardButton;
public Button toLevelSelectButton;
public ScrollRect scrollView;
public float scrollDuration = 1f;
2025-10-29 09:57:33 +01:00
private Action _onLevelConfirm;
private Action _onCancel;
private LevelSwitchData _switchData;
private Coroutine _activeScrollCoroutine;
2025-10-29 09:57:33 +01:00
/// <summary>
/// Initialize the menu with data and callbacks.
/// </summary>
public void Setup(LevelSwitchData switchData, Action onLevelConfirm, Action onCancel)
{
_switchData = switchData;
_onLevelConfirm = onLevelConfirm;
_onCancel = onCancel;
if (iconImage) iconImage.sprite = switchData?.menuSprite ?? switchData?.mapSprite;
if (levelNameText) levelNameText.text = switchData?.targetLevelSceneName ?? "";
if (confirmButton) confirmButton.onClick.AddListener(OnConfirmClicked);
if (cancelButton) cancelButton.onClick.AddListener(OnCancelClicked);
if (toLeaderboardButton) toLeaderboardButton.onClick.AddListener(OnToLeaderboardClicked);
if (toLevelSelectButton) toLevelSelectButton.onClick.AddListener(OnToLevelSelectClicked);
// Show only the toLeaderboardButton at start
if (toLeaderboardButton) toLeaderboardButton.gameObject.SetActive(true);
if (toLevelSelectButton) toLevelSelectButton.gameObject.SetActive(false);
// Initialize scroll view to start at left (level info view)
if (scrollView) scrollView.horizontalNormalizedPosition = 0f;
2025-10-29 09:57:33 +01:00
}
private void OnDestroy()
{
if (confirmButton) confirmButton.onClick.RemoveListener(OnConfirmClicked);
if (cancelButton) cancelButton.onClick.RemoveListener(OnCancelClicked);
if (toLeaderboardButton) toLeaderboardButton.onClick.RemoveListener(OnToLeaderboardClicked);
if (toLevelSelectButton) toLevelSelectButton.onClick.RemoveListener(OnToLevelSelectClicked);
if (_activeScrollCoroutine != null)
{
StopCoroutine(_activeScrollCoroutine);
_activeScrollCoroutine = null;
}
2025-10-29 09:57:33 +01:00
}
private void OnConfirmClicked()
{
_onLevelConfirm?.Invoke();
Destroy(gameObject);
}
private void OnCancelClicked()
{
_onCancel?.Invoke();
Destroy(gameObject);
}
private void OnToLeaderboardClicked()
2025-10-29 09:57:33 +01:00
{
if (_activeScrollCoroutine != null)
{
StopCoroutine(_activeScrollCoroutine);
}
_activeScrollCoroutine = StartCoroutine(ScrollToLeaderboardCoroutine());
2025-10-29 09:57:33 +01:00
}
private void OnToLevelSelectClicked()
2025-10-29 09:57:33 +01:00
{
if (_activeScrollCoroutine != null)
{
StopCoroutine(_activeScrollCoroutine);
}
_activeScrollCoroutine = StartCoroutine(ScrollToLevelSelectCoroutine());
}
private IEnumerator ScrollToLeaderboardCoroutine()
{
if (toLeaderboardButton) toLeaderboardButton.gameObject.SetActive(false);
float elapsed = 0f;
float startPos = scrollView.horizontalNormalizedPosition;
float targetPos = 0.95f;
while (elapsed < scrollDuration)
{
elapsed += Time.deltaTime;
float t = Mathf.Clamp01(elapsed / scrollDuration);
float smoothT = Mathf.SmoothStep(0f, 1f, t);
scrollView.horizontalNormalizedPosition = Mathf.Lerp(startPos, targetPos, smoothT);
yield return null;
}
scrollView.horizontalNormalizedPosition = targetPos;
if (toLevelSelectButton) toLevelSelectButton.gameObject.SetActive(true);
_activeScrollCoroutine = null;
}
private IEnumerator ScrollToLevelSelectCoroutine()
{
if (toLevelSelectButton) toLevelSelectButton.gameObject.SetActive(false);
float elapsed = 0f;
float startPos = scrollView.horizontalNormalizedPosition;
float targetPos = 0f;
while (elapsed < scrollDuration)
{
elapsed += Time.deltaTime;
float t = Mathf.Clamp01(elapsed / scrollDuration);
float smoothT = Mathf.SmoothStep(0f, 1f, t);
scrollView.horizontalNormalizedPosition = Mathf.Lerp(startPos, targetPos, smoothT);
yield return null;
}
scrollView.horizontalNormalizedPosition = targetPos;
if (toLeaderboardButton) toLeaderboardButton.gameObject.SetActive(true);
_activeScrollCoroutine = null;
2025-10-29 09:57:33 +01:00
}
}
}