164 lines
4.2 KiB
C#
164 lines
4.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class MenuControls : MonoBehaviour {
|
|
UIDocument menuDoc;
|
|
|
|
[SerializeField]
|
|
UIDocument timingDoc;
|
|
|
|
[SerializeField]
|
|
UIDocument crosshairDoc;
|
|
|
|
[SerializeField]
|
|
UIDocument controlsDoc;
|
|
|
|
[SerializeField]
|
|
ShipControls shipControls;
|
|
|
|
MenuUI menuUi;
|
|
|
|
Volume effectsVolume;
|
|
|
|
[SerializeField]
|
|
TimingUI timingUi;
|
|
|
|
[SerializeField]
|
|
ControlsUI controlsUI;
|
|
|
|
[SerializeField]
|
|
ShipAudio audio;
|
|
|
|
SpaceRaceInput input;
|
|
|
|
PlayerData _player;
|
|
|
|
Toggle muteAudioToggle;
|
|
Toggle invertYToggle;
|
|
Slider viewSensSlider;
|
|
Label viewSensLabel;
|
|
|
|
bool visible = false;
|
|
|
|
void Awake() {
|
|
menuDoc = GetComponent<UIDocument>();
|
|
effectsVolume = GetComponent<Volume>();
|
|
|
|
if (input == null) input = new SpaceRaceInput();
|
|
input.menu.Enable();
|
|
|
|
menuUi = GetComponent<MenuUI>();
|
|
if (!menuUi) {
|
|
Debug.LogError("No MenuUI");
|
|
return;
|
|
}
|
|
|
|
_player = PlayerDataService.Load();
|
|
|
|
SetupActions();
|
|
Hide();
|
|
}
|
|
|
|
void SetupActions() {
|
|
if (input == null)
|
|
input = new SpaceRaceInput();
|
|
|
|
input.menu.Enable();
|
|
input.menu.ToggleMenu.started += OnToggleMenu;
|
|
input.menu.CloseMenu.started += OnCloseMenu;
|
|
}
|
|
|
|
void OnDestroy() {
|
|
input.menu.ToggleMenu.started -= OnToggleMenu;
|
|
input.menu.CloseMenu.started -= OnCloseMenu;
|
|
}
|
|
|
|
void Show() {
|
|
visible = true;
|
|
UnityEngine.Cursor.lockState = CursorLockMode.None;
|
|
menuDoc.enabled = true;
|
|
effectsVolume.enabled = true;
|
|
menuUi.AddTracks();
|
|
menuUi.FocusTracks();
|
|
|
|
BindSettings();
|
|
|
|
// TODO abstract all this cause it's shit
|
|
// take control off the ship
|
|
timingDoc.enabled = false;
|
|
crosshairDoc.enabled = false;
|
|
controlsDoc.enabled = false;
|
|
timingUi.enabled = false;
|
|
controlsUI.enabled = false;
|
|
shipControls.enabled = false;
|
|
}
|
|
|
|
void Hide() {
|
|
visible = false;
|
|
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
|
|
menuDoc.enabled = false;
|
|
effectsVolume.enabled = false;
|
|
|
|
// resume control of the ship
|
|
timingDoc.enabled = true;
|
|
crosshairDoc.enabled = true;
|
|
controlsDoc.enabled = true;
|
|
timingUi.enabled = true;
|
|
controlsUI.enabled = true;
|
|
shipControls.enabled = true;
|
|
}
|
|
|
|
void OnToggleMenu(InputAction.CallbackContext _ctx) {
|
|
audio.PlayNoise();
|
|
visible = !visible;
|
|
|
|
if (visible) {
|
|
Show();
|
|
return;
|
|
}
|
|
|
|
Hide();
|
|
return;
|
|
}
|
|
|
|
void OnCloseMenu(InputAction.CallbackContext _ctx) {
|
|
if (visible) Hide();
|
|
}
|
|
|
|
void BindSettings() {
|
|
Debug.Log("Setting controls change callbacks");
|
|
invertYToggle = menuDoc.rootVisualElement.Q<Toggle>("invertY");
|
|
muteAudioToggle = menuDoc.rootVisualElement.Q<Toggle>("muteAudioToggle");
|
|
viewSensSlider = menuDoc.rootVisualElement.Q<Slider>("viewSens");
|
|
viewSensLabel = menuDoc.rootVisualElement.Q<Label>("viewSensLabel");
|
|
|
|
viewSensSlider.value = _player.Settings.ViewSens;
|
|
invertYToggle.value = _player.Settings.InvertY;
|
|
muteAudioToggle.value = _player.Settings.MuteAudio;
|
|
|
|
viewSensLabel.text = $"{viewSensSlider.value:0.00}";
|
|
|
|
invertYToggle.RegisterValueChangedCallback(evt => {
|
|
_player.Settings.InvertY = evt.newValue;
|
|
shipControls.InvertY = evt.newValue;
|
|
|
|
PlayerDataService.Persist(_player);
|
|
});
|
|
|
|
muteAudioToggle.RegisterValueChangedCallback(evt => {
|
|
_player.Settings.MuteAudio = evt.newValue;
|
|
shipControls.MuteAudio = evt.newValue;
|
|
PlayerDataService.Persist(_player);
|
|
});
|
|
|
|
viewSensSlider.RegisterValueChangedCallback(evt => {
|
|
shipControls.ViewSens = evt.newValue;
|
|
_player.Settings.ViewSens = evt.newValue;
|
|
viewSensLabel.text = $"{evt.newValue:0.00}";
|
|
|
|
PlayerDataService.Persist(_player);
|
|
});
|
|
}
|
|
} |