115 lines
3.5 KiB
C#
115 lines
3.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.EventSystems;
|
|
|
|
using RR.Common.Managers;
|
|
using RR.Game2D.Core.Managers;
|
|
using RR.Common.Behaviour.ScriptableObjects;
|
|
|
|
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;
|
|
|
|
SettingsUIList settingsUIList;
|
|
|
|
private SettingsManager settingsManager;
|
|
private GameSettings currentSettings;
|
|
|
|
private void Awake()
|
|
{
|
|
settingsManager = SettingsManager.Instance;
|
|
|
|
if (settingsManager == null)
|
|
{
|
|
GameManager.Logger.LogError(name, "SettingsManager is null!");
|
|
return;
|
|
}
|
|
|
|
settingsManager.OnSettingsLoaded += UpdateSettings;
|
|
settingsManager.OnSettingsSaved += SaveSettingsHandler;
|
|
|
|
currentSettings = settingsManager.GetCurrentGameSettings();
|
|
|
|
if (saveButton) saveButton.onClick.AddListener(SaveAndClose);
|
|
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
public void Open()
|
|
{
|
|
gameObject.SetActive(true);
|
|
StopAllCoroutines();
|
|
StartCoroutine(Fade(0f, 1f, fadeDuration));
|
|
}
|
|
|
|
private void Close()
|
|
{
|
|
|
|
}
|
|
|
|
private void Save()
|
|
{
|
|
|
|
}
|
|
|
|
public void SaveAndClose()
|
|
{
|
|
Save();
|
|
Close();
|
|
}
|
|
|
|
private void UpdateSettings()
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|