CapersProject/Assets/02.Scripts/Ui/Title/TitleOptions.cs

306 lines
9.3 KiB
C#
Raw Normal View History

2024-11-19 07:57:05 +00:00
using System;
2024-11-11 12:23:27 +00:00
using System.Collections;
2024-11-19 13:32:43 +00:00
using System.Linq;
2024-11-11 12:23:27 +00:00
using BlueWater;
2024-10-31 12:17:10 +00:00
using BlueWater.Audios;
2024-11-19 07:57:05 +00:00
using BlueWater.Uis;
2024-11-19 13:32:43 +00:00
using BlueWater.Utility;
2024-11-11 12:23:27 +00:00
using TMPro;
2024-10-31 12:17:10 +00:00
using UnityEngine;
2024-11-19 07:57:05 +00:00
using UnityEngine.EventSystems;
2024-11-17 04:29:57 +00:00
using UnityEngine.InputSystem;
2024-12-03 12:30:09 +00:00
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.ResourceManagement.AsyncOperations;
2024-10-31 12:17:10 +00:00
using UnityEngine.UI;
2024-11-19 13:32:43 +00:00
public enum ScreenMode
{
FullScreen = 0,
BorderlessFullScreen = 1,
Windowed = 2
}
2024-11-19 07:57:05 +00:00
public class TitleOptions : PopupUi
2024-10-31 12:17:10 +00:00
{
2024-11-17 04:29:57 +00:00
[SerializeField]
private GameObject _panel;
2024-11-19 07:57:05 +00:00
2024-10-31 12:17:10 +00:00
[SerializeField]
private Slider _masterVolume;
2024-11-19 07:57:05 +00:00
2024-10-31 12:17:10 +00:00
[SerializeField]
private Slider _bgmVolume;
2024-11-19 07:57:05 +00:00
2024-10-31 12:17:10 +00:00
[SerializeField]
private Slider _sfxVolume;
2024-11-11 12:23:27 +00:00
2024-11-19 13:32:43 +00:00
[SerializeField]
private TMP_Dropdown _screenModeDropdown;
[SerializeField]
private TMP_Dropdown _resolutionDropdown;
2024-11-11 12:23:27 +00:00
[SerializeField]
private TMP_Dropdown _languageDropdown;
2024-11-19 07:57:05 +00:00
2024-10-31 12:17:10 +00:00
private AudioManager _audioManager;
2024-11-19 07:57:05 +00:00
private InputAction _interactionEAction;
private InputAction _closeOptionsAction;
2024-12-03 12:30:09 +00:00
private Coroutine _changedLocaleInstance;
2024-11-19 13:32:43 +00:00
2024-11-19 07:57:05 +00:00
public Action CloseOptions;
2024-10-31 12:17:10 +00:00
private void Start()
{
2024-12-03 12:30:09 +00:00
LocalizationSettings.SelectedLocaleChanged += OnChangedLocale;
2024-11-19 07:57:05 +00:00
_interactionEAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.InteractionE);
_closeOptionsAction = PlayerInputKeyManager.Instance.GetAction(InputActionMaps.TycoonUi, TycoonUiActions.Cancel);
2024-11-19 13:32:43 +00:00
2024-10-31 12:17:10 +00:00
_audioManager = AudioManager.Instance;
2024-11-19 13:32:43 +00:00
InitializeScreenModeDropdown();
InitializeResolutionDropdown();
2024-10-31 12:17:10 +00:00
2024-11-19 13:32:43 +00:00
var masterVolume = ES3.Load(SaveData.MasterVolume, 1f);
2024-10-31 12:17:10 +00:00
SetMasterVolume(masterVolume);
2024-11-19 07:57:05 +00:00
2024-11-19 13:32:43 +00:00
var bgmVolume = ES3.Load(SaveData.BgmVolume, 1f);
2024-10-31 12:17:10 +00:00
SetBgmVolume(bgmVolume);
2024-11-19 07:57:05 +00:00
2024-11-19 13:32:43 +00:00
var sfxVolume = ES3.Load(SaveData.SfxVolume, 1f);
2024-10-31 12:17:10 +00:00
SetSfxVolume(sfxVolume);
2024-11-11 12:23:27 +00:00
StartCoroutine(nameof(InitializeLanguage));
2024-10-31 12:17:10 +00:00
}
2024-11-19 07:57:05 +00:00
private void OnDestroy()
{
2024-12-03 12:30:09 +00:00
LocalizationSettings.SelectedLocaleChanged -= OnChangedLocale;
2024-11-19 07:57:05 +00:00
if (_interactionEAction != null)
{
_interactionEAction.performed -= OnInteractionE;
}
if (_closeOptionsAction != null)
{
_closeOptionsAction.performed -= OnCloseOptions;
}
2024-11-19 13:32:43 +00:00
if (_resolutionDropdown != null)
{
_resolutionDropdown.onValueChanged.RemoveListener(SetResolution);
}
if (_screenModeDropdown != null)
{
_screenModeDropdown.onValueChanged.RemoveListener(SetScreenMode);
}
2024-11-19 07:57:05 +00:00
}
2024-12-03 12:30:09 +00:00
private void OnChangedLocale(Locale locale)
{
Utils.StartUniqueCoroutine(this, ref _changedLocaleInstance, ChangeLocaleCoroutine(locale));
StartCoroutine(ChangeLocaleCoroutine(locale));
}
private IEnumerator ChangeLocaleCoroutine(Locale locale)
{
var loadingOperation = Utils.GetTableAsync();
yield return loadingOperation;
if (loadingOperation.Status == AsyncOperationStatus.Succeeded)
{
var options = Enum.GetNames(typeof(ScreenMode));
var savedScreenMode = ES3.Load(SaveData.ScreenMode, (int)ScreenMode.FullScreen);
_screenModeDropdown.captionText.text = Utils.GetLocalizedString(((ScreenMode)savedScreenMode).ToString());
for (int i = 0; i < _screenModeDropdown.options.Count; i++)
{
_screenModeDropdown.options[i].text = Utils.GetLocalizedString(options[i]);
}
}
}
2024-11-19 07:57:05 +00:00
public override void Open()
{
2024-12-18 16:00:39 +00:00
base.Open();
2024-11-19 07:57:05 +00:00
_panel.SetActive(true);
}
public override void Close()
{
_panel.SetActive(false);
2024-12-18 16:00:39 +00:00
base.Close();
2024-11-19 07:57:05 +00:00
}
public override void EnableInput()
{
_interactionEAction.performed += OnInteractionE;
_closeOptionsAction.performed += OnCloseOptions;
}
public override void DisableInput()
{
_interactionEAction.performed -= OnInteractionE;
_closeOptionsAction.performed -= OnCloseOptions;
}
2024-11-19 13:32:43 +00:00
private void InitializeScreenModeDropdown()
{
_screenModeDropdown.ClearOptions();
var options = Enum.GetNames(typeof(ScreenMode));
var localizedOptions = options
.Select(Utils.GetLocalizedString)
.ToList();
_screenModeDropdown.AddOptions(localizedOptions);
var savedScreenMode = ES3.Load(SaveData.ScreenMode, (int)ScreenMode.FullScreen);
_screenModeDropdown.value = savedScreenMode;
_screenModeDropdown.onValueChanged.AddListener(SetScreenMode);
}
private void InitializeResolutionDropdown()
{
_resolutionDropdown.ClearOptions();
var resolutions = Screen.resolutions;
var resolutionOptions = resolutions
.Select(element => $"{element.width}x{element.height}")
.Distinct()
.ToList();
// 현재 해상도 가져오기
2024-11-19 14:38:45 +00:00
string currentResolutionString = $"{Screen.width}x{Screen.height}";
2024-11-19 13:32:43 +00:00
// 현재 해상도가 리스트에 없으면 추가
if (!resolutionOptions.Contains(currentResolutionString))
{
resolutionOptions.Add(currentResolutionString);
}
_resolutionDropdown.AddOptions(resolutionOptions);
// 저장된 해상도 설정
var savedResolution = ES3.Load(SaveData.Resolution, defaultValue: currentResolutionString);
var savedIndex = resolutionOptions.IndexOf(savedResolution);
if (savedIndex == -1)
{
// 저장된 값이 드롭다운에 없으면 추가
resolutionOptions.Add(savedResolution);
savedIndex = resolutionOptions.Count - 1;
}
_resolutionDropdown.value = savedIndex;
_resolutionDropdown.onValueChanged.AddListener(SetResolution);
}
2024-11-19 07:57:05 +00:00
2024-10-31 12:17:10 +00:00
public void SetMasterVolume(float value)
{
_audioManager.SetMasterVolume(value);
_masterVolume.value = value;
2024-11-19 13:32:43 +00:00
ES3.Save(SaveData.MasterVolume, value);
2024-10-31 12:17:10 +00:00
}
2024-11-19 07:57:05 +00:00
2024-10-31 12:17:10 +00:00
public void SetBgmVolume(float value)
{
_audioManager.SetBgmVolume(value);
_bgmVolume.value = value;
2024-11-19 13:32:43 +00:00
ES3.Save(SaveData.BgmVolume, value);
2024-10-31 12:17:10 +00:00
}
2024-11-19 07:57:05 +00:00
2024-10-31 12:17:10 +00:00
public void SetSfxVolume(float value)
{
_audioManager.SetSfxVolume(value);
_sfxVolume.value = value;
2024-11-19 13:32:43 +00:00
ES3.Save(SaveData.SfxVolume, value);
2024-10-31 12:17:10 +00:00
}
2024-11-11 12:23:27 +00:00
private IEnumerator InitializeLanguage()
{
yield return new WaitUntil(() => LocalizationManager.Instance.IsInitialized);
2024-11-28 23:07:50 +00:00
_languageDropdown.ClearOptions();
var localeOptions = Enum.GetValues(typeof(LocaleType))
.Cast<LocaleType>()
.Select(locale => LocalizationManager.Instance.GetLocaleDisplayName(locale))
.ToList();
_languageDropdown.AddOptions(localeOptions);
2024-11-19 07:57:05 +00:00
2024-11-11 12:23:27 +00:00
var languageIndex = LocalizationManager.Instance.GetCurrentLocaleIndex();
_languageDropdown.value = languageIndex;
}
public void ChangeLanguage(int localeType)
{
_languageDropdown.value = localeType;
LocalizationManager.Instance.ChangeLocale((LocaleType)localeType);
}
2024-11-19 07:57:05 +00:00
public void OnInteractionE(InputAction.CallbackContext context)
2024-11-17 04:29:57 +00:00
{
2024-11-19 07:57:05 +00:00
var current = EventSystem.current.currentSelectedGameObject;
if (!current) return;
var currenButton = current.GetComponent<Button>();
currenButton.onClick.Invoke();
2024-11-17 04:29:57 +00:00
}
2024-11-19 07:57:05 +00:00
public void OnCloseOptions(InputAction.CallbackContext context)
2024-11-17 04:29:57 +00:00
{
2024-11-19 07:57:05 +00:00
CloseOptions?.Invoke();
2024-11-17 04:29:57 +00:00
}
2024-11-19 13:32:43 +00:00
public void SetScreenMode(int screenModeIndex)
{
switch ((ScreenMode)screenModeIndex)
{
case ScreenMode.FullScreen:
Screen.fullScreenMode = FullScreenMode.FullScreenWindow;
break;
case ScreenMode.BorderlessFullScreen:
if (Application.platform == RuntimePlatform.WindowsPlayer)
{
Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen;
}
else if (Application.platform == RuntimePlatform.OSXPlayer)
{
Screen.fullScreenMode = FullScreenMode.MaximizedWindow;
}
break;
case ScreenMode.Windowed:
Screen.fullScreenMode = FullScreenMode.Windowed;
break;
default:
throw new ArgumentOutOfRangeException(nameof(screenModeIndex), screenModeIndex, null);
}
ES3.Save(SaveData.ScreenMode, screenModeIndex);
EventManager.InvokeChangedDisplay();
}
public void SetResolution(int resolutionIndex)
{
var resolutionString = _resolutionDropdown.options[resolutionIndex].text;
SetResolution(resolutionString);
}
private void SetResolution(string resolutionString)
{
var parts = resolutionString.Split(new[] { 'x', '@', ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 2 &&
int.TryParse(parts[0], out var width) &&
int.TryParse(parts[1], out var height))
{
2024-11-19 14:38:45 +00:00
string saveResolution = $"{width}x{height}";
2024-11-19 13:32:43 +00:00
Screen.SetResolution(width, height, Screen.fullScreenMode);
2024-11-19 14:38:45 +00:00
ES3.Save(SaveData.Resolution, saveResolution);
2024-11-19 13:32:43 +00:00
EventManager.InvokeChangedDisplay();
}
}
2024-11-19 07:57:05 +00:00
}