2025-10-28 14:31:17 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using TMPro;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
2025-10-29 11:19:17 +01:00
|
|
|
|
using Core.SaveLoad;
|
|
|
|
|
|
using Unity.Android.Gradle;
|
2025-10-28 14:31:17 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Levels
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// UI overlay for confirming a level switch. Displays level info and handles confirm/cancel actions.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class LevelSwitchMenu : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
[Header("UI References")]
|
|
|
|
|
|
public Image iconImage;
|
|
|
|
|
|
public TMP_Text levelNameText;
|
|
|
|
|
|
public Button confirmButton;
|
|
|
|
|
|
public Button cancelButton;
|
|
|
|
|
|
public Button minigameButton;
|
|
|
|
|
|
public GameObject padlockIcon;
|
|
|
|
|
|
public Button restartButton;
|
|
|
|
|
|
public GameObject popupConfirmMenu;
|
|
|
|
|
|
public Button popupConfirmButton;
|
|
|
|
|
|
public Button popupCancelButton;
|
|
|
|
|
|
public Image tintTargetImage;
|
|
|
|
|
|
public Color popupTintColor = new Color(0.5f, 0.5f, 0.5f, 1f); // grey by default
|
|
|
|
|
|
private Color _originalTintColor;
|
|
|
|
|
|
private Action _onRestart;
|
|
|
|
|
|
|
|
|
|
|
|
private Action _onLevelConfirm;
|
|
|
|
|
|
private Action _onMinigameConfirm;
|
|
|
|
|
|
private Action _onCancel;
|
|
|
|
|
|
private LevelSwitchData _switchData;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize the menu with data and callbacks.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Setup(LevelSwitchData switchData, Action onLevelConfirm, Action onMinigameConfirm, Action onCancel, Action onRestart = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_switchData = switchData;
|
|
|
|
|
|
_onLevelConfirm = onLevelConfirm;
|
|
|
|
|
|
_onMinigameConfirm = onMinigameConfirm;
|
|
|
|
|
|
_onCancel = onCancel;
|
|
|
|
|
|
_onRestart = onRestart;
|
2025-10-29 11:19:17 +01:00
|
|
|
|
if(switchData != null)
|
2025-10-28 14:31:17 +01:00
|
|
|
|
{
|
2025-10-29 11:19:17 +01:00
|
|
|
|
if (iconImage)
|
|
|
|
|
|
{
|
|
|
|
|
|
iconImage.sprite = switchData.menuSprite != null
|
|
|
|
|
|
? switchData.menuSprite
|
|
|
|
|
|
: switchData.mapSprite;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (levelNameText) levelNameText.text = switchData?.targetLevelSceneName ?? "";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Logging.LogWarning("[LevelSwitchMenu] No level data is assigned!");
|
2025-10-28 14:31:17 +01:00
|
|
|
|
}
|
2025-10-29 11:19:17 +01:00
|
|
|
|
if (confirmButton) confirmButton.onClick.AddListener(OnConfirmClicked);
|
|
|
|
|
|
if (cancelButton) cancelButton.onClick.AddListener(OnCancelClicked);
|
|
|
|
|
|
if (minigameButton) minigameButton.onClick.AddListener(OnMinigameClicked);
|
2025-10-28 14:31:17 +01:00
|
|
|
|
if (restartButton) restartButton.onClick.AddListener(OnRestartClicked);
|
|
|
|
|
|
if (popupConfirmMenu) popupConfirmMenu.SetActive(false);
|
|
|
|
|
|
if (tintTargetImage) _originalTintColor = tintTargetImage.color;
|
|
|
|
|
|
if (popupConfirmButton) popupConfirmButton.onClick.AddListener(OnPopupConfirmClicked);
|
|
|
|
|
|
if (popupCancelButton) popupCancelButton.onClick.AddListener(OnPopupCancelClicked);
|
2025-10-29 11:19:17 +01:00
|
|
|
|
|
|
|
|
|
|
// --- Minigame unlock state logic ---
|
|
|
|
|
|
if (SaveLoadManager.Instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SaveLoadManager.Instance.IsSaveDataLoaded)
|
|
|
|
|
|
{
|
|
|
|
|
|
ApplyMinigameUnlockStateIfAvailable();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
SaveLoadManager.Instance.OnLoadCompleted += OnSaveDataLoadedHandler;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-28 14:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (confirmButton) confirmButton.onClick.RemoveListener(OnConfirmClicked);
|
|
|
|
|
|
if (cancelButton) cancelButton.onClick.RemoveListener(OnCancelClicked);
|
|
|
|
|
|
if (minigameButton) minigameButton.onClick.RemoveListener(OnMinigameClicked);
|
|
|
|
|
|
if (restartButton) restartButton.onClick.RemoveListener(OnRestartClicked);
|
|
|
|
|
|
if (popupConfirmButton) popupConfirmButton.onClick.RemoveListener(OnPopupConfirmClicked);
|
|
|
|
|
|
if (popupCancelButton) popupCancelButton.onClick.RemoveListener(OnPopupCancelClicked);
|
2025-10-29 11:19:17 +01:00
|
|
|
|
if (SaveLoadManager.Instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SaveLoadManager.Instance.OnLoadCompleted -= OnSaveDataLoadedHandler;
|
|
|
|
|
|
}
|
2025-10-28 14:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnConfirmClicked()
|
|
|
|
|
|
{
|
|
|
|
|
|
_onLevelConfirm?.Invoke();
|
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnMinigameClicked()
|
|
|
|
|
|
{
|
|
|
|
|
|
_onMinigameConfirm?.Invoke();
|
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnCancelClicked()
|
|
|
|
|
|
{
|
|
|
|
|
|
_onCancel?.Invoke();
|
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnRestartClicked()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (popupConfirmMenu) popupConfirmMenu.SetActive(true);
|
|
|
|
|
|
if (tintTargetImage) tintTargetImage.color = popupTintColor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnPopupCancelClicked()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (popupConfirmMenu) popupConfirmMenu.SetActive(false);
|
|
|
|
|
|
if (tintTargetImage) tintTargetImage.color = _originalTintColor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnPopupConfirmClicked()
|
|
|
|
|
|
{
|
|
|
|
|
|
_onRestart?.Invoke();
|
|
|
|
|
|
if (popupConfirmMenu) popupConfirmMenu.SetActive(false);
|
|
|
|
|
|
if (tintTargetImage) tintTargetImage.color = _originalTintColor;
|
|
|
|
|
|
}
|
2025-10-29 11:19:17 +01:00
|
|
|
|
|
|
|
|
|
|
private void ApplyMinigameUnlockStateIfAvailable()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (minigameButton == null || padlockIcon == null || _switchData == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
var data = SaveLoadManager.Instance?.currentSaveData;
|
|
|
|
|
|
string minigameName = _switchData.targetMinigameSceneName;
|
|
|
|
|
|
bool unlocked = data?.unlockedMinigames != null && !string.IsNullOrEmpty(minigameName) && data.unlockedMinigames.Contains(minigameName);
|
|
|
|
|
|
minigameButton.interactable = unlocked;
|
|
|
|
|
|
padlockIcon.SetActive(!unlocked);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnSaveDataLoadedHandler(string slot)
|
|
|
|
|
|
{
|
|
|
|
|
|
ApplyMinigameUnlockStateIfAvailable();
|
|
|
|
|
|
if (SaveLoadManager.Instance != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SaveLoadManager.Instance.OnLoadCompleted -= OnSaveDataLoadedHandler;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-28 14:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|