166 lines
3.9 KiB
C#
166 lines
3.9 KiB
C#
using HurricaneVR.Framework.ControllerInput;
|
|
using HurricaneVR.Framework.Core.UI;
|
|
using Sirenix.OdinInspector;
|
|
using TMPro;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using Zenject;
|
|
|
|
public class HandMenuUI : NetworkBehaviour
|
|
{
|
|
[Inject]
|
|
[ReadOnly]
|
|
private HVRInputModule uiInput;
|
|
|
|
[Inject]
|
|
[ReadOnly]
|
|
private SceneManager sceneManager;
|
|
|
|
[SerializeField]
|
|
private GameObject anchor;
|
|
|
|
[SerializeField]
|
|
private Transform lookAt;
|
|
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
private Canvas canvas;
|
|
|
|
[SerializeField]
|
|
private TextMeshProUGUI titleText;
|
|
|
|
[SerializeField]
|
|
private TextMeshProUGUI roomCodeText;
|
|
|
|
[SerializeField]
|
|
private Button restartButton;
|
|
|
|
[SerializeField]
|
|
private Button quitButton;
|
|
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
private TextMeshProUGUI quitButtonText;
|
|
|
|
[SerializeField]
|
|
private Button closeButton;
|
|
|
|
[SerializeField]
|
|
private Button settingsButton;
|
|
|
|
[SerializeField]
|
|
private Button startButton;
|
|
|
|
[SerializeField]
|
|
private Button backButton;
|
|
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
private bool isSettingsVisible = false;
|
|
|
|
[SerializeField]
|
|
private GameObject mainMenu;
|
|
|
|
[SerializeField]
|
|
private GameObject settings;
|
|
|
|
private float forwardOffset = 0.7f;
|
|
private float upOffset = -0.3f;
|
|
|
|
private void Start()
|
|
{
|
|
if (uiInput == null) return;
|
|
|
|
canvas = GetComponent<Canvas>();
|
|
uiInput?.AddCanvas(canvas);
|
|
|
|
canvas.enabled = false;
|
|
|
|
quitButtonText = quitButton.GetComponentInChildren<TextMeshProUGUI>();
|
|
|
|
UpdateTextsAndButtons();
|
|
|
|
sceneManager.SceneLoaded.AddListener(() => UpdateTextsAndButtons());
|
|
closeButton.onClick.AddListener(() => ToggleVisibility());
|
|
quitButton.onClick.AddListener(() => QuitClicked());
|
|
backButton.onClick.AddListener(() => BackClicked());
|
|
settingsButton.onClick.AddListener(() => SettingsClicked());
|
|
}
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
base.OnNetworkSpawn();
|
|
|
|
// Player components need to be injected manualy again, because NetworkManager doesn't inject them.
|
|
gameObject.Inject();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
CheckInput();
|
|
}
|
|
|
|
private void UpdatePosition()
|
|
{
|
|
var targetPosition = lookAt.position + (lookAt.forward * forwardOffset);
|
|
transform.position = new Vector3(targetPosition.x, lookAt.position.y + upOffset, targetPosition.z);
|
|
|
|
var lookAtPositon = new Vector3(lookAt.position.x, transform.position.y, lookAt.position.z);
|
|
transform.rotation = Quaternion.LookRotation(transform.position - lookAtPositon);
|
|
}
|
|
|
|
private void CheckInput()
|
|
{
|
|
if (HVRInputManager.Instance.LeftController.PrimaryButtonState.JustActivated)
|
|
{
|
|
ToggleVisibility();
|
|
}
|
|
}
|
|
|
|
private void UpdateTextsAndButtons()
|
|
{
|
|
titleText.text = sceneManager.LoadedScene.name;
|
|
quitButtonText.text = sceneManager.IsInLobby ? "Quit" : "Give Up";
|
|
backButton.gameObject.SetActive(isSettingsVisible);
|
|
mainMenu.SetActive(!isSettingsVisible);
|
|
settings.SetActive(isSettingsVisible);
|
|
restartButton.gameObject.SetActive(!sceneManager.IsInLobby);
|
|
startButton.gameObject.SetActive(!sceneManager.IsInLobby);
|
|
}
|
|
|
|
private void ToggleVisibility()
|
|
{
|
|
canvas.enabled = !canvas.enabled;
|
|
|
|
if (canvas.enabled)
|
|
{
|
|
UpdatePosition();
|
|
}
|
|
}
|
|
|
|
private void BackClicked()
|
|
{
|
|
isSettingsVisible = false;
|
|
UpdateTextsAndButtons();
|
|
}
|
|
|
|
private void SettingsClicked()
|
|
{
|
|
isSettingsVisible = true;
|
|
UpdateTextsAndButtons();
|
|
}
|
|
|
|
private void QuitClicked()
|
|
{
|
|
if (sceneManager.IsInLobby)
|
|
{
|
|
Application.Quit();
|
|
}
|
|
else
|
|
{
|
|
sceneManager.SwitchToLobbyLevel();
|
|
}
|
|
}
|
|
}
|