143 lines
4.9 KiB
C#
143 lines
4.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using RR.Common.Controls;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UI;
|
|
|
|
public class AdminStatsWindowUIController : MonoBehaviour
|
|
{
|
|
[Header("Тексты статистики")]
|
|
[SerializeField] private string gamesPlayedTextPrefix = "Игр сыграно: ";
|
|
[SerializeField] private TextMeshProUGUI gamesPlayedText;
|
|
[SerializeField] private string gamesPlayedNulTextPrefix = "Игр сыграно (обнуляемый): ";
|
|
[SerializeField] private TextMeshProUGUI gamesPlayedNulText;
|
|
[SerializeField] private string prizesGivenTextPrefix = "Призов выдано: ";
|
|
[SerializeField] private TextMeshProUGUI prizesGivenText;
|
|
[SerializeField] private string prizesGivenNulTextPrefix = "Призов выдано (обнуляемый): ";
|
|
[SerializeField] private TextMeshProUGUI prizesGivenNulText;
|
|
|
|
[Header("Кнопки")]
|
|
[SerializeField] private Button resetGamesPlayedNulButton;
|
|
[SerializeField] private Button resetPrizesGivenNulButton;
|
|
[SerializeField] private Button closeButton;
|
|
|
|
[Header("UI")]
|
|
[SerializeField] private CanvasGroup canvasGroup;
|
|
[SerializeField] private float fadeDuration = 0.15f;
|
|
|
|
private string _gamesPlayedKey;
|
|
private string _gamesPlayedNulKey;
|
|
private string _prizesGivenKey;
|
|
private string _prizesGivenNulKey;
|
|
|
|
private void Awake()
|
|
{
|
|
_gamesPlayedKey = PrizeSystem.key_starts;
|
|
_gamesPlayedNulKey = PrizeSystem.key_startsNul;
|
|
_prizesGivenKey = PrizeSystem.key_prizesgiven;
|
|
_prizesGivenNulKey = PrizeSystem.key_prizesgivenNul;
|
|
|
|
if (resetGamesPlayedNulButton) resetGamesPlayedNulButton.onClick.AddListener(() => ResetPlayerPrefsStat(_gamesPlayedNulKey));
|
|
|
|
if (resetPrizesGivenNulButton) resetPrizesGivenNulButton.onClick.AddListener( () => ResetPlayerPrefsStat(_prizesGivenNulKey));
|
|
|
|
if (closeButton) closeButton.onClick.AddListener(Toggle);
|
|
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (resetGamesPlayedNulButton) resetGamesPlayedNulButton.onClick.RemoveListener(() => ResetPlayerPrefsStat(_gamesPlayedNulKey));
|
|
|
|
if (resetPrizesGivenNulButton) resetPrizesGivenNulButton.onClick.RemoveListener( () => ResetPlayerPrefsStat(_prizesGivenNulKey));
|
|
|
|
if (closeButton) closeButton.onClick.RemoveListener(Toggle);
|
|
}
|
|
|
|
public void Toggle()
|
|
{
|
|
if (gameObject.activeSelf)
|
|
{
|
|
Close();
|
|
}
|
|
else
|
|
{
|
|
Open();
|
|
}
|
|
}
|
|
|
|
private void Open()
|
|
{
|
|
gameObject.SetActive(true);
|
|
UpdateText();
|
|
StopAllCoroutines();
|
|
StartCoroutine(Fade(0f, 1f, fadeDuration));
|
|
}
|
|
|
|
private void Close()
|
|
{
|
|
StopAllCoroutines();
|
|
StartCoroutine(Fade(1f, 0f, fadeDuration, () => { gameObject.SetActive(false); }));
|
|
}
|
|
|
|
private void UpdateText()
|
|
{
|
|
if (gamesPlayedText)
|
|
{
|
|
int gamesPlayed = PlayerPrefs.GetInt(_gamesPlayedKey, 0);
|
|
gamesPlayedText.text = gamesPlayedTextPrefix + gamesPlayed;
|
|
}
|
|
|
|
if (gamesPlayedNulText)
|
|
{
|
|
int gamesPlayedNul = PlayerPrefs.GetInt(_gamesPlayedNulKey, 0);
|
|
gamesPlayedNulText.text = gamesPlayedNulTextPrefix + gamesPlayedNul;
|
|
}
|
|
|
|
if (prizesGivenText)
|
|
{
|
|
int prizesGiven = PlayerPrefs.GetInt(_prizesGivenKey, 0);
|
|
prizesGivenText.text = prizesGivenTextPrefix + prizesGiven;
|
|
}
|
|
|
|
if (prizesGivenNulText)
|
|
{
|
|
int prizesGivenNul = PlayerPrefs.GetInt(_prizesGivenNulKey, 0);
|
|
prizesGivenNulText.text = prizesGivenNulTextPrefix + prizesGivenNul;
|
|
}
|
|
}
|
|
|
|
private void ResetPlayerPrefsStat(string key)
|
|
{
|
|
PlayerPrefs.SetInt(key, 0);
|
|
UpdateText();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Метод для плавного перехода (затухания)
|
|
/// </summary>
|
|
/// <param name="from">Начальная прозрачность</param>
|
|
/// <param name="to">Конечная прозрачность</param>
|
|
/// <param name="time">Время перехода</param>
|
|
/// <param name="onDone">Действие при выполнении перехода</param>
|
|
private IEnumerator Fade(float from, float to, float time, System.Action onDone = null)
|
|
{
|
|
if (!canvasGroup) { onDone?.Invoke(); yield break; }
|
|
float t = 0f;
|
|
canvasGroup.alpha = from;
|
|
canvasGroup.blocksRaycasts = true;
|
|
while (t < time)
|
|
{
|
|
t += Time.unscaledDeltaTime;
|
|
canvasGroup.alpha = Mathf.Lerp(from, to, t / time);
|
|
yield return null;
|
|
}
|
|
canvasGroup.alpha = to;
|
|
canvasGroup.blocksRaycasts = to > 0.99f;
|
|
onDone?.Invoke();
|
|
}
|
|
}
|