mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-09 21:59:16 +00:00
199 lines
5.3 KiB
C#
199 lines
5.3 KiB
C#
using QSB.Player;
|
|
using QSB.Utility;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UI;
|
|
|
|
namespace QSB.Menus
|
|
{
|
|
class MenuManager : MonoBehaviour
|
|
{
|
|
public static MenuManager Instance;
|
|
|
|
private IMenuAPI MenuApi => QSBCore.MenuApi;
|
|
private PopupMenu PopupMenu;
|
|
private Button HostButton;
|
|
private GameObject ClientButton;
|
|
private Button DisconnectButton;
|
|
private PopupMenu InfoPopup;
|
|
private bool _addedPauseLock;
|
|
|
|
public void Start()
|
|
{
|
|
Instance = this;
|
|
MakeTitleMenus();
|
|
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
|
|
QSBNetworkManager.Instance.OnClientConnected += OnConnected;
|
|
QSBNetworkManager.Instance.OnClientDisconnected += OnDisconnected;
|
|
}
|
|
|
|
void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isUniverse)
|
|
{
|
|
if (isUniverse)
|
|
{
|
|
InitPauseMenus();
|
|
return;
|
|
}
|
|
|
|
if (newScene == OWScene.TitleScreen)
|
|
{
|
|
MakeTitleMenus();
|
|
}
|
|
}
|
|
|
|
private void OpenInfoPopup(string message, string buttonText)
|
|
{
|
|
InfoPopup.SetUpPopup(message, InputLibrary.menuConfirm, InputLibrary.cancel, new ScreenPrompt(buttonText), null, true, false);
|
|
|
|
OWTime.Pause(OWTime.PauseType.System);
|
|
OWInput.ChangeInputMode(InputMode.Menu);
|
|
|
|
var pauseCommandListener = Locator.GetPauseCommandListener();
|
|
if (pauseCommandListener != null)
|
|
{
|
|
pauseCommandListener.AddPauseCommandLock();
|
|
_addedPauseLock = true;
|
|
}
|
|
|
|
InfoPopup.EnableMenu(true);
|
|
}
|
|
|
|
private void OnCloseInfoPopup()
|
|
{
|
|
var pauseCommandListener = Locator.GetPauseCommandListener();
|
|
if (pauseCommandListener != null && _addedPauseLock)
|
|
{
|
|
pauseCommandListener.RemovePauseCommandLock();
|
|
_addedPauseLock = false;
|
|
}
|
|
OWTime.Unpause(OWTime.PauseType.System);
|
|
OWInput.RestorePreviousInputs();
|
|
}
|
|
|
|
private void CreateCommonPopups()
|
|
{
|
|
PopupMenu = MenuApi.MakeInputFieldPopup("IP Address", "IP Address", "Connect", "Cancel");
|
|
PopupMenu.OnPopupConfirm += Connect;
|
|
|
|
InfoPopup = MenuApi.MakeInfoPopup("", "");
|
|
InfoPopup.OnDeactivateMenu += OnCloseInfoPopup;
|
|
}
|
|
|
|
private void InitPauseMenus()
|
|
{
|
|
CreateCommonPopups();
|
|
|
|
HostButton = MenuApi.PauseMenu_MakeSimpleButton("MULTIPLAYER (HOST)");
|
|
HostButton.onClick.AddListener(Host);
|
|
|
|
ClientButton = MenuApi.PauseMenu_MakeMenuOpenButton("MULTIPLAYER (CONNECT)", PopupMenu);
|
|
|
|
DisconnectButton = MenuApi.PauseMenu_MakeSimpleButton("DISCONNECT");
|
|
DisconnectButton.onClick.AddListener(Disconnect);
|
|
DisconnectButton.gameObject.SetActive(false);
|
|
DisconnectButton.GetComponent<CanvasGroup>().alpha = 1f;
|
|
}
|
|
|
|
private void MakeTitleMenus()
|
|
{
|
|
CreateCommonPopups();
|
|
|
|
HostButton = MenuApi.TitleScreen_MakeSimpleButton("MULTIPLAYER (HOST)");
|
|
HostButton.onClick.AddListener(Host);
|
|
|
|
ClientButton = MenuApi.TitleScreen_MakeMenuOpenButton("MULTIPLAYER (CONNECT)", PopupMenu);
|
|
|
|
DisconnectButton = MenuApi.TitleScreen_MakeSimpleButton("DISCONNECT");
|
|
DisconnectButton.onClick.AddListener(Disconnect);
|
|
DisconnectButton.gameObject.SetActive(false);
|
|
DisconnectButton.GetComponent<CanvasGroup>().alpha = 1f;
|
|
}
|
|
|
|
private void Disconnect()
|
|
{
|
|
QSBNetworkManager.Instance.StopHost();
|
|
DisconnectButton.gameObject.SetActive(false);
|
|
ClientButton.SetActive(true);
|
|
HostButton.gameObject.SetActive(true);
|
|
}
|
|
|
|
private void Host()
|
|
{
|
|
QSBNetworkManager.Instance.StartHost();
|
|
DisconnectButton.gameObject.SetActive(true);
|
|
DisconnectButton.GetComponent<CanvasGroup>().alpha = 1f;
|
|
ClientButton.SetActive(false);
|
|
HostButton.gameObject.SetActive(false);
|
|
}
|
|
|
|
private void Connect()
|
|
{
|
|
QSBNetworkManager.Instance.networkAddress = (PopupMenu as PopupInputMenu).GetInputText();
|
|
QSBNetworkManager.Instance.StartClient();
|
|
DisconnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = "CONNECTING... (STOP)";
|
|
DisconnectButton.gameObject.SetActive(true);
|
|
DisconnectButton.GetComponent<CanvasGroup>().alpha = 1f;
|
|
ClientButton.SetActive(false);
|
|
HostButton.gameObject.SetActive(false);
|
|
}
|
|
|
|
private void OnConnected()
|
|
{
|
|
var text = QSBCore.IsHost
|
|
? "STOP HOSTING"
|
|
: "DISCONNECT";
|
|
DisconnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = text;
|
|
}
|
|
|
|
public void OnKicked(KickReason reason)
|
|
{
|
|
string text;
|
|
switch (reason)
|
|
{
|
|
case KickReason.QSBVersionNotMatching:
|
|
text = "Server refused connection as QSB version does not match.";
|
|
break;
|
|
case KickReason.GameVersionNotMatching:
|
|
text = "Server refused connection as Outer Wilds version does not match.";
|
|
break;
|
|
case KickReason.None:
|
|
text = "Kicked from server. No reason given.";
|
|
break;
|
|
default:
|
|
text = $"Kicked from server. KickReason:{reason}";
|
|
break;
|
|
}
|
|
|
|
OpenInfoPopup(text, "OK");
|
|
|
|
DisconnectButton.gameObject.SetActive(false);
|
|
ClientButton.SetActive(true);
|
|
HostButton.gameObject.SetActive(true);
|
|
}
|
|
|
|
private void OnDisconnected(NetworkError error)
|
|
{
|
|
if (error == NetworkError.Ok)
|
|
{
|
|
return;
|
|
}
|
|
|
|
string text;
|
|
switch (error)
|
|
{
|
|
case NetworkError.Timeout:
|
|
text = "Connection timed out. Either the server does not exist, or it has stopped responding.";
|
|
break;
|
|
default:
|
|
text = $"Disconnected due to error. NetworkError:{error}";
|
|
break;
|
|
}
|
|
|
|
OpenInfoPopup(text, "OK");
|
|
|
|
DisconnectButton.gameObject.SetActive(false);
|
|
ClientButton.SetActive(true);
|
|
HostButton.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
} |