Files
test/Assets/PerfectWorld/Scripts/UI/Dialogs/Setting/SettingSound.cs
T
2026-05-15 20:23:46 +07:00

224 lines
8.1 KiB
C#

using BrewMonster.Assets.PerfectWorld.Scripts.UI;
using BrewMonster.Common;
using BrewMonster.Managers;
using BrewMonster.Network;
using BrewMonster.Scripts;
using BrewMonster.Scripts.Managers;
using BrewMonster.UI;
using CSNetwork.GPDataType;
using Cysharp.Threading.Tasks.Triggers;
using PerfectWorld.Scripts.Managers;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace BrewMonster
{
public class SettingSound : MonoBehaviour
{
[Header("Sound Settings")]
[SerializeField] private Toggle tog_bgm;
[SerializeField] private Toggle tog_sfx;
[SerializeField] private Toggle tog_master;
[SerializeField] private RectTransform handle_bmg;
[SerializeField] private RectTransform handle_sfx;
[SerializeField] private RectTransform handle_master;
[SerializeField] private TextMeshProUGUI txt_bgm;
[SerializeField] private TextMeshProUGUI txt_sfx;
[SerializeField] private TextMeshProUGUI txt_master;
[Header("Sprite")]
[SerializeField] private Sprite switch_off;
[SerializeField] private Sprite switch_on;
[Header("Switch Config")]
private float slideDuration = 0.15f;
private float handleOffPositionX = -88.3f;
private float handleOnPositionX = -27.8f;
private float textOnPositionX = -27.8f;
private float textOffPositionX = 30f;
private Color textOnColor = new Color(0.96f, 0.84f, 0.61f);
private Color textOffColor = new Color(0.6f, 0.6f, 0.6f);
[Header("slider Text Level")]
[SerializeField] private TextMeshProUGUI txt_level_of_bgm;
[SerializeField] private TextMeshProUGUI txt_level_of_sfx;
[SerializeField] private TextMeshProUGUI txt_level_of_master;
private Coroutine bgmCoroutine;
private Coroutine sfxCoroutine;
private Coroutine masterCoroutine;
[SerializeField] private SliderController slideCtrlBgrMusic;
[SerializeField] private SliderController slideCtrlSFX;
[SerializeField] private SliderController slideCtrlMaster;
private void OnEnable()
{
tog_bgm.isOn = EC_Game.GetActiveBgrMusic();
UpdateSwitchInstant(tog_bgm, handle_bmg, txt_bgm);
tog_sfx.isOn = EC_Game.GetActiveSoundSFX();
UpdateSwitchInstant(tog_sfx, handle_sfx, txt_sfx);
tog_master.isOn = EC_Game.GetActiveSoundMaster();
UpdateSwitchInstant(tog_master, handle_master, txt_master);
tog_bgm.onValueChanged.AddListener(OnBgmChanged);
tog_sfx.onValueChanged.AddListener(OnSfxChanged);
tog_master.onValueChanged.AddListener(OnMasterChanged);
slideCtrlBgrMusic.InitSlide(EC_Game.GetVolumeBgrMusic());
slideCtrlSFX.InitSlide(EC_Game.GetVolumeSoundSFX());
slideCtrlMaster.InitSlide(EC_Game.GetVolumeSoundMaster());
}
private void OnDisable()
{
tog_bgm.onValueChanged.RemoveListener(OnBgmChanged);
tog_sfx.onValueChanged.RemoveListener(OnSfxChanged);
tog_master.onValueChanged.RemoveListener(OnMasterChanged);
}
private void OnBgmChanged(bool isOn)
{
if (bgmCoroutine != null)
{
StopCoroutine(bgmCoroutine);
}
bgmCoroutine = StartCoroutine(SlideSwitchCoroutine(tog_bgm, handle_bmg, txt_bgm));
// TODO: Add logic to change BGM volume or mute state
//EC_Game.SetActiveBgrMusic(isOn);
}
private void OnSfxChanged(bool isOn)
{
if (sfxCoroutine != null)
{
StopCoroutine(sfxCoroutine);
}
sfxCoroutine = StartCoroutine(SlideSwitchCoroutine(tog_sfx, handle_sfx, txt_sfx));
// TODO: Add logic to change SFX volume or mute state
//EC_Game.SetActiveSoundSFX(isOn);
}
private void OnMasterChanged(bool isOn)
{
if (masterCoroutine != null)
{
StopCoroutine(masterCoroutine);
}
masterCoroutine = StartCoroutine(SlideSwitchCoroutine(tog_master, handle_master, txt_master));
// TODO: Add logic to change Master volume or mute state
//EC_Game.SetActiveSoundMaster(isOn);
}
private void UpdateSwitchInstant(Toggle toggle, RectTransform handle, TextMeshProUGUI statusText)
{
bool isOn = toggle.isOn;
// Logic circle switch
float handleTargetX = isOn ? handleOnPositionX : handleOffPositionX;
handle.anchoredPosition = new Vector2(handleTargetX, handle.anchoredPosition.y);
// Logic text switch
if (statusText != null)
{
float textTargetX = isOn ? textOnPositionX : textOffPositionX;
statusText.rectTransform.anchoredPosition = new Vector2(textTargetX, statusText.rectTransform.anchoredPosition.y);
statusText.text = isOn ? "Mở" : "Tắt";
statusText.color = isOn ? textOnColor : textOffColor;
}
// Logic background switch
if (handle.TryGetComponent<Image>(out var handleImage))
{
handleImage.sprite = isOn ? switch_on : switch_off;
}
}
private IEnumerator SlideSwitchCoroutine(Toggle toggle, RectTransform handle, TextMeshProUGUI statusText)
{
bool isOn = toggle.isOn;
// Get info target
float handleTargetX = isOn ? handleOnPositionX : handleOffPositionX;
float handleStartX = handle.anchoredPosition.x;
float textTargetX = isOn ? textOnPositionX : textOffPositionX;
float textStartX = statusText != null ? statusText.rectTransform.anchoredPosition.x : 0f;
// Update text and background immediately
if (statusText != null)
{
statusText.text = isOn ? "Mở" : "Tắt";
statusText.color = isOn ? textOnColor : textOffColor;
}
if (handle.TryGetComponent<Image>(out var handleImage))
{
handleImage.sprite = isOn ? switch_on : switch_off;
}
float time = 0f;
while (time < slideDuration)
{
time += Time.deltaTime;
float t = Mathf.Clamp01(time / slideDuration);
float smoothT = Mathf.SmoothStep(0f, 1f, t);
// Move handle
float currentHandleX = Mathf.Lerp(handleStartX, handleTargetX, smoothT);
handle.anchoredPosition = new Vector2(currentHandleX, handle.anchoredPosition.y);
// Move text
if (statusText != null)
{
float currentTextX = Mathf.Lerp(textStartX, textTargetX, smoothT);
statusText.rectTransform.anchoredPosition = new Vector2(currentTextX, statusText.rectTransform.anchoredPosition.y);
}
yield return null;
}
// Position it correctly in the last frame to avoid errors.
handle.anchoredPosition = new Vector2(handleTargetX, handle.anchoredPosition.y);
if (statusText != null)
{
statusText.rectTransform.anchoredPosition = new Vector2(textTargetX, statusText.rectTransform.anchoredPosition.y);
}
}
public int GetVolumeBgrMusic()
{
return slideCtrlBgrMusic.GetValueCurrentSlide();
}
public int GetVolumeSFX()
{
return slideCtrlSFX.GetValueCurrentSlide();
}
public int GetVolumeMaster()
{
return slideCtrlMaster.GetValueCurrentSlide();
}
public bool GetActiveBgrMusic()
{
return tog_bgm.isOn;
}
public bool GetActiveSFX()
{
return tog_sfx.isOn;
}
public bool GetActiveSoundMaster()
{
return tog_master.isOn;
}
}
}