22using UnityEngine . UIElements ;
33using Unity . BossRoom . Utils ;
44using Unity . BossRoom . Audio ;
5+ using Unity . BossRoom . Gameplay . UI ; // Reference for UIQuitPanel
6+ using VContainer ; // Needed for the dependency injection setup
57
68namespace Unity . BossRoom . Gameplay . UI
79{
@@ -14,23 +16,34 @@ public class UISettingsCanvas : MonoBehaviour
1416 [ SerializeField ]
1517 UIDocument uiDocument ; // Reference to the UIDocument asset
1618
19+ // Panels and Buttons
1720 VisualElement settingsPanelRoot ;
1821 VisualElement quitPanelRoot ;
1922 Button settingsButton ;
2023 Button quitButton ;
24+ Button qualityButton ;
25+ Button confirmQuitButton ;
26+ Button closeButton ;
27+ Button cancelButton ;
2128 Slider masterVolumeSlider ;
2229 Slider musicVolumeSlider ;
30+ UIQuitPanel uiQuitPanel ;
2331
2432 void Awake ( )
2533 {
2634 // Retrieve the root VisualElement from the UIDocument
2735 var root = uiDocument . rootVisualElement ;
28-
36+ // get UIQuitPanel component which is attached to the same GameObject
37+ uiQuitPanel = GetComponent < UIQuitPanel > ( ) ;
2938 // Query the panels by their names or assigned USS classes
3039 settingsPanelRoot = root . Q < VisualElement > ( "settingsPanelRoot" ) ;
3140 quitPanelRoot = root . Q < VisualElement > ( "quitPanelRoot" ) ;
3241 quitButton = root . Q < Button > ( "quitButton" ) ;
3342 settingsButton = root . Q < Button > ( "settingsButton" ) ;
43+ qualityButton = root . Q < Button > ( "qualityButton" ) ;
44+ closeButton = root . Q < Button > ( "closeButton" ) ;
45+ cancelButton = root . Q < Button > ( "cancelButton" ) ;
46+ confirmQuitButton = root . Q < Button > ( "confirmButton" ) ;
3447 masterVolumeSlider = root . Q < Slider > ( "masterVolume" ) ;
3548 musicVolumeSlider = root . Q < Slider > ( "musicVolume" ) ;
3649
@@ -39,32 +52,45 @@ void Awake()
3952
4053 settingsButton . SetEnabled ( true ) ;
4154 quitButton . SetEnabled ( true ) ;
42- // Bind the buttons to their respective method using new input system
55+ qualityButton . SetEnabled ( true ) ;
56+ confirmQuitButton . SetEnabled ( true ) ;
57+ closeButton . SetEnabled ( true ) ;
58+ cancelButton . SetEnabled ( true ) ;
59+ masterVolumeSlider . SetEnabled ( true ) ;
60+ musicVolumeSlider . SetEnabled ( true ) ;
61+
62+ musicVolumeSlider . value = ClientPrefs . GetMusicVolume ( ) ;
63+ masterVolumeSlider . value = ClientPrefs . GetMasterVolume ( ) ;
64+
65+ // Bind buttons to their respective methods
4366 settingsButton . clicked += OnClickSettingsButton ;
4467 quitButton . clicked += OnClickQuitButton ;
45-
46- // Bind the sliders to their respective methods
68+ qualityButton . clicked += SetQualitySettings ;
69+ confirmQuitButton . clicked += ExecuteQuitAction ;
70+ closeButton . clicked += OnClickCloseButton ;
71+ cancelButton . clicked += OnClickCancelButton ;
72+
73+ // Bind sliders to their respective methods
4774 masterVolumeSlider . value = ClientPrefs . GetMasterVolume ( ) ;
4875 masterVolumeSlider . RegisterValueChangedCallback ( evt => OnMasterVolumeSliderChanged ( evt . newValue ) ) ;
4976 musicVolumeSlider . value = ClientPrefs . GetMusicVolume ( ) ;
5077 musicVolumeSlider . RegisterValueChangedCallback ( evt => OnMusicVolumeSliderChanged ( evt . newValue ) ) ;
51-
52-
5378 }
5479
80+ /// <summary>
81+ /// Ensures all panels are hidden when this component is initialized.
82+ /// </summary>
5583 void DisablePanels ( )
5684 {
5785 settingsPanelRoot . style . display = DisplayStyle . None ;
5886 quitPanelRoot . style . display = DisplayStyle . None ;
5987 }
6088
6189 /// <summary>
62- /// Called directly by the settings button in the UI Through a manual event binding
90+ /// Called when the Settings button is pressed. Toggles the display of the settings panel.
6391 /// </summary>
6492 public void OnClickSettingsButton ( )
6593 {
66- Debug . Log ( "Settings button pressed" ) ;
67- // settingsButton is pressed
6894 if ( settingsPanelRoot != null )
6995 {
7096 bool isVisible = settingsPanelRoot . style . display == DisplayStyle . Flex ;
@@ -78,7 +104,7 @@ public void OnClickSettingsButton()
78104 }
79105
80106 /// <summary>
81- /// Called directly by the quit button in the UI manual button here
107+ /// Called when the Quit button is pressed. Toggles the display of the quit panel.
82108 /// </summary>
83109 public void OnClickQuitButton ( )
84110 {
@@ -94,17 +120,91 @@ public void OnClickQuitButton()
94120 }
95121 }
96122
97- private void OnMasterVolumeSliderChanged ( float newValue )
123+ float SliderToDecibel ( float sliderValue )
124+ {
125+ float minDB = - 40f ; // Silent floor
126+ float maxDB = 0f ; // Maximum volume
127+ sliderValue = Mathf . Pow ( Mathf . Clamp ( sliderValue , 0.0001f , 1f ) , 2.0f ) ; // Exponential curve
128+ return Mathf . Lerp ( minDB , maxDB , sliderValue ) ;
129+ }
130+
131+ /// <summary>
132+ /// Called when the Master Volume slider's value is adjusted.
133+ /// </summary>
134+ /// <param name="newValue">New slider value.</param>
135+ void OnMasterVolumeSliderChanged ( float newValue )
98136 {
137+ // newValue = Mathf.Clamp(newValue, 0, 100);
99138 ClientPrefs . SetMasterVolume ( newValue ) ;
100139 AudioMixerConfigurator . Instance . Configure ( ) ;
140+ Debug . Log ( "Master Volume set to: " + newValue ) ;
101141 }
102142
103- private void OnMusicVolumeSliderChanged ( float newValue )
143+ /// <summary>
144+ /// Called when the Music Volume slider's value is adjusted.
145+ /// </summary>
146+ /// <param name="newValue">New slider value.</param>
147+ void OnMusicVolumeSliderChanged ( float newValue )
104148 {
149+ //float dB= SliderToDecibel(newValue);
150+ // clamp the value to the range [0, 1]
151+ //newValue = Mathf.Clamp(newValue, 0, 1);
105152 ClientPrefs . SetMusicVolume ( newValue ) ;
106153 AudioMixerConfigurator . Instance . Configure ( ) ;
154+ Debug . Log ( "Music Volume set to: " + newValue ) ;
155+ }
156+
157+ /// <summary>
158+ /// Called when the Quality Settings button is pressed. Updates the quality level.
159+ /// </summary>
160+ public void SetQualitySettings ( )
161+ {
162+ var qualityLevels = QualitySettings . names . Length - 1 ;
163+ var currentLevel = QualitySettings . GetQualityLevel ( ) ;
164+
165+ if ( currentLevel < qualityLevels )
166+ {
167+ QualitySettings . IncreaseLevel ( ) ;
168+ }
169+ else
170+ {
171+ QualitySettings . SetQualityLevel ( 0 ) ;
172+ }
173+
174+ // Dynamically update the button text with the current quality level
175+ qualityButton . text = QualitySettings . names [ QualitySettings . GetQualityLevel ( ) ] ;
176+ }
177+
178+ /// <summary>
179+ /// Hook for the Quit Panel's Quit functionality. Delegates actions to UIQuitPanel.
180+ /// </summary>
181+ void ExecuteQuitAction ( )
182+ {
183+ Debug . Log ( "Confirm button pressed" ) ;
184+
185+ if ( uiQuitPanel != null )
186+ {
187+ uiQuitPanel . Quit ( ) ;
188+ Debug . Log ( "Quit executed." ) ;
189+ }
190+ else
191+ {
192+ Debug . LogError ( "UIQuitPanel is not assigned!" ) ;
193+ }
194+ }
195+
196+ void OnClickCloseButton ( )
197+ {
198+ // Close the settings panel
199+ settingsPanelRoot . style . display = DisplayStyle . None ;
200+ }
201+
202+ void OnClickCancelButton ( )
203+ {
204+ // Close the quit panel
205+ quitPanelRoot . style . display = DisplayStyle . None ;
107206 }
108207 }
109208}
110209
210+
0 commit comments