| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using UnityEngine.Audio; |
| | 5 | |
|
| | 6 | | namespace DCL.SettingsCommon |
| | 7 | | { |
| | 8 | | public class Settings |
| | 9 | | { |
| 1919 | 10 | | public static Settings i { get; private set; } |
| | 11 | |
|
| | 12 | | public event Action OnResetAllSettings; |
| | 13 | |
|
| 0 | 14 | | public QualitySettingsData qualitySettingsPresets => qualitySettingsPreset; |
| | 15 | |
|
| | 16 | | public readonly ISettingsRepository<QualitySettings> qualitySettings; |
| | 17 | | public readonly ISettingsRepository<GeneralSettings> generalSettings; |
| | 18 | | public readonly ISettingsRepository<AudioSettings> audioSettings; |
| | 19 | |
|
| | 20 | | private readonly QualitySettingsData qualitySettingsPreset; |
| | 21 | | private readonly AudioMixer audioMixer; |
| | 22 | |
|
| | 23 | | private bool isDisposed; |
| | 24 | |
|
| | 25 | | public static void CreateSharedInstance(ISettingsFactory settingsFactory) |
| | 26 | | { |
| 640 | 27 | | if (i != null && !i.isDisposed) return; |
| 640 | 28 | | i = settingsFactory.Build(); |
| 640 | 29 | | } |
| | 30 | |
|
| 640 | 31 | | public Settings(QualitySettingsData qualitySettingsPreset, |
| | 32 | | AudioMixer audioMixer, |
| | 33 | | ISettingsRepository<QualitySettings> graphicsQualitySettingsRepository, |
| | 34 | | ISettingsRepository<GeneralSettings> generalSettingsRepository, |
| | 35 | | ISettingsRepository<AudioSettings> audioSettingsRepository) |
| | 36 | | { |
| 640 | 37 | | this.qualitySettingsPreset = qualitySettingsPreset; |
| 640 | 38 | | this.audioMixer = audioMixer; |
| 640 | 39 | | qualitySettings = graphicsQualitySettingsRepository; |
| 640 | 40 | | generalSettings = generalSettingsRepository; |
| 640 | 41 | | audioSettings = audioSettingsRepository; |
| | 42 | |
|
| 640 | 43 | | SubscribeToVirtualAudioMixerEvents(); |
| 640 | 44 | | } |
| | 45 | |
|
| | 46 | | public void Dispose() |
| | 47 | | { |
| 640 | 48 | | UnsubscribeFromVirtualAudioMixerEvents(); |
| 640 | 49 | | isDisposed = true; |
| 640 | 50 | | } |
| | 51 | |
|
| | 52 | | public void LoadDefaultSettings() |
| | 53 | | { |
| 25 | 54 | | qualitySettings.Reset(); |
| 25 | 55 | | generalSettings.Reset(); |
| 25 | 56 | | audioSettings.Reset(); |
| 25 | 57 | | } |
| | 58 | |
|
| | 59 | | public void ResetAllSettings() |
| | 60 | | { |
| 0 | 61 | | LoadDefaultSettings(); |
| 0 | 62 | | SaveSettings(); |
| 0 | 63 | | OnResetAllSettings?.Invoke(); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void SubscribeToVirtualAudioMixerEvents() |
| | 67 | | { |
| 640 | 68 | | DataStore.i.virtualAudioMixer.voiceChatVolume.OnChange += ApplyVoiceChatSettings; |
| 640 | 69 | | DataStore.i.virtualAudioMixer.musicVolume.OnChange += ApplyMusicVolume; |
| 640 | 70 | | DataStore.i.virtualAudioMixer.avatarSFXVolume.OnChange += ApplyAvatarSFXVolume; |
| 640 | 71 | | DataStore.i.virtualAudioMixer.uiSFXVolume.OnChange += ApplyUISFXVolume; |
| 640 | 72 | | } |
| | 73 | |
|
| | 74 | | private void UnsubscribeFromVirtualAudioMixerEvents() |
| | 75 | | { |
| 640 | 76 | | DataStore.i.virtualAudioMixer.voiceChatVolume.OnChange -= ApplyVoiceChatSettings; |
| 640 | 77 | | DataStore.i.virtualAudioMixer.musicVolume.OnChange -= ApplyMusicVolume; |
| 640 | 78 | | DataStore.i.virtualAudioMixer.avatarSFXVolume.OnChange -= ApplyAvatarSFXVolume; |
| 640 | 79 | | DataStore.i.virtualAudioMixer.uiSFXVolume.OnChange -= ApplyUISFXVolume; |
| 640 | 80 | | } |
| | 81 | |
|
| | 82 | | public void ApplyMasterVolume() |
| | 83 | | { |
| | 84 | | // Update the "All" mixer group |
| 2 | 85 | | audioMixer.SetFloat("AllBusVolume", Utils.ToAudioMixerGroupVolume(audioSettings.Data.masterVolume)); |
| | 86 | |
|
| | 87 | | // Update voice chat volume, as it does not pass through the AudioMixer |
| 2 | 88 | | ApplyVoiceChatSettings(); |
| 2 | 89 | | } |
| | 90 | |
|
| | 91 | | public void ApplyVoiceChatSettings(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) |
| | 92 | | { |
| 6 | 93 | | AudioSettings audioSettingsData = audioSettings.Data; |
| 6 | 94 | | float calculatedVolume = Utils.ToVolumeCurve(DataStore.i.virtualAudioMixer.voiceChatVolume.Get() * audioSett |
| 6 | 95 | | WebInterface.ApplySettings(calculatedVolume, (int)generalSettings.Data.voiceChatAllow); |
| 6 | 96 | | } |
| | 97 | |
|
| 4 | 98 | | public void ApplyAvatarSFXVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { audioM |
| | 99 | |
|
| 4 | 100 | | public void ApplyUISFXVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { audioMixer |
| | 101 | |
|
| 4 | 102 | | public void ApplyMusicVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { audioMixer |
| | 103 | |
|
| | 104 | | public void SaveSettings() |
| | 105 | | { |
| 1 | 106 | | generalSettings.Save(); |
| 1 | 107 | | qualitySettings.Save(); |
| 1 | 108 | | audioSettings.Save(); |
| 1 | 109 | | PlayerPrefsUtils.Save(); |
| 1 | 110 | | } |
| | 111 | | } |
| | 112 | | } |