using System; using System.Collections; using TMPro; using UnityEngine; using UnityEngine.UI; namespace Levels { /// /// UI overlay for confirming a level switch. Displays level info and handles confirm/cancel actions, and supports scrolling between level info and leaderboard. /// 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; private Action _onLevelConfirm; private Action _onCancel; private LevelSwitchData _switchData; private Coroutine _activeScrollCoroutine; /// /// Initialize the menu with data and callbacks. /// 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; } 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; } } private void OnConfirmClicked() { _onLevelConfirm?.Invoke(); Destroy(gameObject); } private void OnCancelClicked() { _onCancel?.Invoke(); Destroy(gameObject); } private void OnToLeaderboardClicked() { if (_activeScrollCoroutine != null) { StopCoroutine(_activeScrollCoroutine); } _activeScrollCoroutine = StartCoroutine(ScrollToLeaderboardCoroutine()); } private void OnToLevelSelectClicked() { 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; } } }