2024-08-11 07:20:58 +08:00
|
|
|
/*
|
|
|
|
* author: mark joshwel
|
|
|
|
* date: 11/8/2024
|
|
|
|
* description: option menu script for handling credits menu button functions
|
|
|
|
*/
|
|
|
|
|
2024-08-11 16:27:33 +08:00
|
|
|
using Unity.Mathematics;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Audio;
|
2024-08-11 07:20:58 +08:00
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// class managing the credits menu and button function invocations
|
|
|
|
/// </summary>
|
|
|
|
public class ScreenOptionsMenu : CommonMenu
|
|
|
|
{
|
2024-08-11 16:27:33 +08:00
|
|
|
public AudioMixer mixer;
|
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// button to return to the main menu
|
|
|
|
/// </summary>
|
2024-08-11 19:57:50 +08:00
|
|
|
private Button _buttonReturn;
|
2024-08-11 07:20:58 +08:00
|
|
|
|
2024-08-11 14:52:08 +08:00
|
|
|
/// <summary>
|
|
|
|
/// slider for master (music + sfx) volume
|
|
|
|
/// </summary>
|
2024-08-11 19:57:50 +08:00
|
|
|
private Slider _sliderAudioMaster;
|
2024-08-11 14:52:08 +08:00
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// slider for music volume
|
|
|
|
/// </summary>
|
2024-08-11 19:57:50 +08:00
|
|
|
private Slider _sliderAudioMusic;
|
2024-08-11 07:20:58 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// slider for sfx volume
|
|
|
|
/// </summary>
|
2024-08-11 19:57:50 +08:00
|
|
|
private Slider _sliderAudioSfx;
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
{
|
|
|
|
}
|
2024-08-11 15:51:21 +08:00
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// function to associate a display state with the menu,
|
|
|
|
/// and subscribe button events to their respective functions
|
|
|
|
/// </summary>
|
|
|
|
public override void OnEnable()
|
|
|
|
{
|
|
|
|
// set the associated state and call the base OnEnable
|
|
|
|
associatedState = GameManager.DisplayState.ScreenOptionsMenu;
|
|
|
|
base.OnEnable();
|
|
|
|
|
|
|
|
// get the start button from the ui root and subscribe appropriate functions
|
2024-08-11 19:57:50 +08:00
|
|
|
_buttonReturn = UI.Q<Button>("ButtonReturn");
|
|
|
|
_buttonReturn.clicked += PlayClick;
|
|
|
|
_buttonReturn.clicked += OverlayPauseMenu.OptionReturnToMainMenu;
|
2024-08-11 07:20:58 +08:00
|
|
|
|
2024-08-11 14:52:08 +08:00
|
|
|
// TODO: and set the initial value to the current music volume
|
2024-08-11 19:57:50 +08:00
|
|
|
// get the music slider from the ui root and subscribe appropriate functions
|
|
|
|
_sliderAudioMaster = UI.Q<Slider>("MasterSlider");
|
|
|
|
_sliderAudioMaster.RegisterCallback<ChangeEvent<float>>(OptionSetMasterVolume);
|
2024-08-11 15:51:21 +08:00
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
// TODO: and set the initial value to the current music volume
|
2024-08-11 19:57:50 +08:00
|
|
|
// get the music slider from the ui root and subscribe appropriate functions
|
|
|
|
_sliderAudioMusic = UI.Q<Slider>("MusicSlider");
|
|
|
|
_sliderAudioMusic.RegisterCallback<ChangeEvent<float>>(OptionSetMusicVolume);
|
2024-08-11 16:27:33 +08:00
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
// TODO: and set the initial value to the current sfx volume
|
2024-08-11 19:57:50 +08:00
|
|
|
// get the sfx slider from the ui root and subscribe appropriate functions
|
|
|
|
_sliderAudioSfx = UI.Q<Slider>("SFXSlider");
|
|
|
|
_sliderAudioSfx.RegisterCallback<ChangeEvent<float>>(OptionSetSfxVolume);
|
2024-08-11 16:27:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static float ConvertVolume(float linearVolume)
|
|
|
|
{
|
|
|
|
return Mathf.Log10(math.max(0.0000001f, linearVolume / 100)) * 20;
|
|
|
|
}
|
|
|
|
|
2024-08-11 14:52:08 +08:00
|
|
|
/// <summary>
|
|
|
|
/// handle music volume slider change,
|
|
|
|
/// sets the music channel volume in the audio manager appropriately
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="evt">change event</param>
|
|
|
|
private void OptionSetMasterVolume(ChangeEvent<float> evt)
|
|
|
|
{
|
2024-08-11 16:27:33 +08:00
|
|
|
mixer.SetFloat("MasterVolume", ConvertVolume(evt.newValue));
|
2024-08-11 14:52:08 +08:00
|
|
|
}
|
2024-08-11 15:51:21 +08:00
|
|
|
|
2024-08-11 07:20:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// handle music volume slider change,
|
|
|
|
/// sets the music channel volume in the audio manager appropriately
|
|
|
|
/// </summary>
|
2024-08-11 14:52:08 +08:00
|
|
|
/// <param name="evt">change event</param>
|
2024-08-11 07:20:58 +08:00
|
|
|
private void OptionSetMusicVolume(ChangeEvent<float> evt)
|
|
|
|
{
|
2024-08-11 16:27:33 +08:00
|
|
|
mixer.SetFloat("MusicVolume", ConvertVolume(evt.newValue));
|
2024-08-11 07:20:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// handle sfx volume slider change,
|
|
|
|
/// sets the sfx channel volume in the audio manager appropriately
|
|
|
|
/// </summary>
|
2024-08-11 14:52:08 +08:00
|
|
|
/// <param name="evt">change event</param>
|
2024-08-11 07:20:58 +08:00
|
|
|
private void OptionSetSfxVolume(ChangeEvent<float> evt)
|
|
|
|
{
|
2024-08-11 16:27:33 +08:00
|
|
|
mixer.SetFloat("SFXVolume", ConvertVolume(evt.newValue));
|
2024-08-11 07:20:58 +08:00
|
|
|
}
|
|
|
|
}
|