(Feat) Add Remmaping
This commit is contained in:
@@ -1,68 +1,59 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Ashwild.Settings
|
||||
{
|
||||
/// <summary>
|
||||
/// Binds the audio volume widgets (master, music, SFX) to the <see cref="SettingsManager"/>,
|
||||
/// configuring each slider from the authored <see cref="SettingsDefinition"/>.
|
||||
/// </summary>
|
||||
public class AudioSettingsSubPanel : SettingsSubPanel
|
||||
{
|
||||
[Header("Master")]
|
||||
[SerializeField] private Slider masterSlider;
|
||||
[SerializeField] private TMP_Text masterLabel;
|
||||
#region Serialized Fields
|
||||
|
||||
[Header("Music")]
|
||||
[SerializeField] private Slider musicSlider;
|
||||
[SerializeField] private TMP_Text musicLabel;
|
||||
[Header("Widgets")]
|
||||
[SerializeField] private SettingSliderWidget masterSlider;
|
||||
[SerializeField] private SettingSliderWidget musicSlider;
|
||||
[SerializeField] private SettingSliderWidget sfxSlider;
|
||||
|
||||
[Header("SFX")]
|
||||
[SerializeField] private Slider sfxSlider;
|
||||
[SerializeField] private TMP_Text sfxLabel;
|
||||
#endregion
|
||||
|
||||
private bool wiring;
|
||||
#region Unity Lifecycle
|
||||
|
||||
/// <summary>
|
||||
/// Initializes each volume slider from the definition with its apply action.
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
masterSlider.onValueChanged.AddListener(OnMasterChanged);
|
||||
musicSlider.onValueChanged.AddListener(OnMusicChanged);
|
||||
sfxSlider.onValueChanged.AddListener(OnSfxChanged);
|
||||
SettingsManager s = SettingsManager.Instance;
|
||||
if (s == null || s.Definition == null)
|
||||
{
|
||||
Debug.LogError($"[AudioSettingsSubPanel] SettingsManager/Definition unavailable on '{name}'.", this);
|
||||
return;
|
||||
}
|
||||
SettingsDefinition def = s.Definition;
|
||||
|
||||
masterSlider.Init(def.MasterVolume.Min, def.MasterVolume.Max, def.MasterVolume.LabelScale, def.MasterVolume.LabelFormat, v => s.SetMasterVolume(v));
|
||||
musicSlider.Init(def.MusicVolume.Min, def.MusicVolume.Max, def.MusicVolume.LabelScale, def.MusicVolume.LabelFormat, v => s.SetMusicVolume(v));
|
||||
sfxSlider.Init(def.SfxVolume.Min, def.SfxVolume.Max, def.SfxVolume.LabelScale, def.SfxVolume.LabelFormat, v => s.SetSfxVolume(v));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public API
|
||||
|
||||
/// <summary>
|
||||
/// Pushes the manager's current volumes into the sliders.
|
||||
/// </summary>
|
||||
public override void Refresh()
|
||||
{
|
||||
SettingsManager s = SettingsManager.Instance;
|
||||
if (s == null) return;
|
||||
|
||||
wiring = true;
|
||||
masterSlider.SetValueWithoutNotify(s.MasterVolume);
|
||||
musicSlider.SetValueWithoutNotify(s.MusicVolume);
|
||||
sfxSlider.SetValueWithoutNotify(s.SfxVolume);
|
||||
UpdateLabel(masterLabel, s.MasterVolume);
|
||||
UpdateLabel(musicLabel, s.MusicVolume);
|
||||
UpdateLabel(sfxLabel, s.SfxVolume);
|
||||
wiring = false;
|
||||
masterSlider.SetValue(s.MasterVolume);
|
||||
musicSlider.SetValue(s.MusicVolume);
|
||||
sfxSlider.SetValue(s.SfxVolume);
|
||||
}
|
||||
|
||||
private void OnMasterChanged(float v)
|
||||
{
|
||||
UpdateLabel(masterLabel, v);
|
||||
if (!wiring) SettingsManager.Instance.SetMasterVolume(v);
|
||||
}
|
||||
|
||||
private void OnMusicChanged(float v)
|
||||
{
|
||||
UpdateLabel(musicLabel, v);
|
||||
if (!wiring) SettingsManager.Instance.SetMusicVolume(v);
|
||||
}
|
||||
|
||||
private void OnSfxChanged(float v)
|
||||
{
|
||||
UpdateLabel(sfxLabel, v);
|
||||
if (!wiring) SettingsManager.Instance.SetSfxVolume(v);
|
||||
}
|
||||
|
||||
private static void UpdateLabel(TMP_Text label, float value01)
|
||||
{
|
||||
if (label != null) label.text = $"{Mathf.RoundToInt(value01 * 100f)}%";
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user