140 lines
4.3 KiB
C#
140 lines
4.3 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.InputSystem;
|
|
using RR.Common.Managers;
|
|
using RR.Game2D.Core.Managers;
|
|
|
|
namespace RR.Common.UI
|
|
{
|
|
[RequireComponent(typeof(CanvasGroup), typeof(SettingsUIList))]
|
|
public class SettingsUI : MonoBehaviour
|
|
{
|
|
[Header("Управление")]
|
|
[SerializeField] private InputActionReference triggerKey;
|
|
[SerializeField] private Button saveButton;
|
|
|
|
[Header("UI")]
|
|
[SerializeField] private CanvasGroup canvasGroup;
|
|
[SerializeField] private Transform content;
|
|
[SerializeField] private float fadeDuration = 0.15f;
|
|
|
|
private SettingsUIList settingsUIList;
|
|
|
|
private SettingsManager settingsManager;
|
|
|
|
private void Awake()
|
|
{
|
|
settingsManager = SettingsManager.Instance;
|
|
|
|
if (triggerKey != null) triggerKey.action.started += Toggle;
|
|
|
|
if (settingsManager == null)
|
|
{
|
|
GameManager.Logger.LogError(name, "SettingsManager is null!");
|
|
return;
|
|
}
|
|
|
|
settingsManager.OnSettingsLoaded += UpdateSettings;
|
|
settingsManager.OnSettingsSaved += SaveSettingsHandler;
|
|
|
|
if (saveButton) saveButton.onClick.AddListener(SaveAndClose);
|
|
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (settingsManager != null)
|
|
{
|
|
settingsManager.OnSettingsLoaded -= UpdateSettings;
|
|
settingsManager.OnSettingsSaved -= SaveSettingsHandler;
|
|
}
|
|
|
|
if (triggerKey != null) triggerKey.action.started -= Toggle;
|
|
|
|
if (saveButton) saveButton.onClick.RemoveListener(SaveAndClose);
|
|
}
|
|
|
|
// private void Update()
|
|
// {
|
|
// if (!gameObject.activeSelf && triggerKey != null && triggerKey.action.WasPerformedThisFrame())
|
|
// {
|
|
// Open();
|
|
// }
|
|
// }
|
|
|
|
private void Toggle(InputAction.CallbackContext ctx)
|
|
{
|
|
if (gameObject.activeSelf)
|
|
{
|
|
SaveAndClose();
|
|
}
|
|
else
|
|
{
|
|
Open();
|
|
}
|
|
}
|
|
|
|
private void Open()
|
|
{
|
|
gameObject.SetActive(true);
|
|
StopAllCoroutines();
|
|
StartCoroutine(Fade(0f, 1f, fadeDuration));
|
|
}
|
|
|
|
private void Close()
|
|
{
|
|
StartCoroutine(Fade(1f, 0f, fadeDuration, () => { gameObject.SetActive(false); }));
|
|
}
|
|
|
|
private void Save()
|
|
{
|
|
UpdateSettings();
|
|
settingsUIList.SaveSettings();
|
|
}
|
|
|
|
private void SaveAndClose()
|
|
{
|
|
Save();
|
|
Close();
|
|
}
|
|
|
|
private void UpdateSettings()
|
|
{
|
|
if (settingsUIList == null) settingsUIList = content.GetComponent<SettingsUIList>();
|
|
if (settingsUIList != null) settingsUIList.UpdateSettings(settingsManager.GetCurrentGameSettings());
|
|
}
|
|
|
|
private void SaveSettingsHandler()
|
|
{
|
|
GameManager.Logger.Log(name, "Saved settings");
|
|
}
|
|
|
|
/// <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();
|
|
}
|
|
}
|
|
}
|
|
|