2025-10-29 09:57:33 +01:00
using System ;
2025-10-30 10:15:53 +01:00
using System.Collections ;
2025-10-29 09:57:33 +01:00
using TMPro ;
using UnityEngine ;
using UnityEngine.UI ;
namespace Levels
{
/// <summary>
2025-10-30 10:15:53 +01:00
/// 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 ;
2025-10-30 10:15:53 +01:00
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 ;
2025-10-30 10:15:53 +01:00
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 ) ;
2025-10-30 10:15:53 +01:00
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 ) ;
2025-10-30 10:15:53 +01:00
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 ) ;
}
2025-10-30 10:15:53 +01:00
private void OnToLeaderboardClicked ( )
2025-10-29 09:57:33 +01:00
{
2025-10-30 10:15:53 +01:00
if ( _activeScrollCoroutine ! = null )
{
StopCoroutine ( _activeScrollCoroutine ) ;
}
_activeScrollCoroutine = StartCoroutine ( ScrollToLeaderboardCoroutine ( ) ) ;
2025-10-29 09:57:33 +01:00
}
2025-10-30 10:15:53 +01:00
private void OnToLevelSelectClicked ( )
2025-10-29 09:57:33 +01:00
{
2025-10-30 10:15:53 +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
}
}
}