2021-08-26 08:51:33 +01:00
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using QuantumUNET;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
2021-08-24 21:08:55 +01:00
|
|
|
|
using UnityEngine.UI;
|
2021-06-04 11:31:09 +01:00
|
|
|
|
|
|
|
|
|
namespace QSB.Menus
|
|
|
|
|
{
|
|
|
|
|
class MenuManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
private IMenuAPI MenuApi => QSBCore.MenuApi;
|
2021-08-24 21:08:55 +01:00
|
|
|
|
private PopupMenu PopupMenu;
|
2021-08-24 22:30:53 +01:00
|
|
|
|
private Button HostButton;
|
|
|
|
|
private GameObject ClientButton;
|
2021-08-24 21:08:55 +01:00
|
|
|
|
private Button DisconnectButton;
|
2021-08-26 08:51:33 +01:00
|
|
|
|
private PopupMenu InfoPopup;
|
2021-08-26 14:56:42 +01:00
|
|
|
|
private bool _addedPauseLock;
|
2021-06-04 11:31:09 +01:00
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
MakeTitleMenus();
|
2021-08-26 08:51:33 +01:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 14:56:42 +01:00
|
|
|
|
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()
|
2021-08-26 08:51:33 +01:00
|
|
|
|
{
|
|
|
|
|
PopupMenu = MenuApi.MakeInputFieldPopup("IP Address", "IP Address", "Connect", "Cancel");
|
|
|
|
|
PopupMenu.OnPopupConfirm += Connect;
|
|
|
|
|
|
2021-08-26 14:56:42 +01:00
|
|
|
|
InfoPopup = MenuApi.MakeInfoPopup("DEFAULT TEXT", "you forgor 💀");
|
|
|
|
|
InfoPopup.OnDeactivateMenu += OnCloseInfoPopup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitPauseMenus()
|
|
|
|
|
{
|
|
|
|
|
CreateCommonPopups();
|
2021-08-26 08:51:33 +01:00
|
|
|
|
|
|
|
|
|
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;
|
2021-06-04 11:31:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MakeTitleMenus()
|
|
|
|
|
{
|
2021-08-26 14:56:42 +01:00
|
|
|
|
CreateCommonPopups();
|
2021-08-26 08:51:33 +01:00
|
|
|
|
|
2021-08-24 22:30:53 +01:00
|
|
|
|
HostButton = MenuApi.TitleScreen_MakeSimpleButton("MULTIPLAYER (HOST)");
|
|
|
|
|
HostButton.onClick.AddListener(Host);
|
|
|
|
|
|
|
|
|
|
ClientButton = MenuApi.TitleScreen_MakeMenuOpenButton("MULTIPLAYER (CONNECT)", PopupMenu);
|
2021-08-24 21:08:55 +01:00
|
|
|
|
|
|
|
|
|
DisconnectButton = MenuApi.TitleScreen_MakeSimpleButton("DISCONNECT");
|
|
|
|
|
DisconnectButton.onClick.AddListener(Disconnect);
|
2021-08-24 22:30:53 +01:00
|
|
|
|
DisconnectButton.gameObject.SetActive(false);
|
|
|
|
|
DisconnectButton.GetComponent<CanvasGroup>().alpha = 1f;
|
2021-06-04 11:31:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 21:08:55 +01:00
|
|
|
|
private void Disconnect()
|
2021-06-04 11:31:09 +01:00
|
|
|
|
{
|
2021-08-24 21:08:55 +01:00
|
|
|
|
QSBNetworkManager.Instance.StopHost();
|
|
|
|
|
DisconnectButton.gameObject.SetActive(false);
|
2021-08-24 22:30:53 +01:00
|
|
|
|
ClientButton.SetActive(true);
|
|
|
|
|
HostButton.gameObject.SetActive(true);
|
2021-06-04 11:31:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 21:08:55 +01:00
|
|
|
|
private void Host()
|
2021-06-04 11:31:09 +01:00
|
|
|
|
{
|
2021-08-24 21:08:55 +01:00
|
|
|
|
QSBNetworkManager.Instance.StartHost();
|
|
|
|
|
DisconnectButton.gameObject.SetActive(true);
|
2021-08-24 22:30:53 +01:00
|
|
|
|
DisconnectButton.GetComponent<CanvasGroup>().alpha = 1f;
|
|
|
|
|
ClientButton.SetActive(false);
|
|
|
|
|
HostButton.gameObject.SetActive(false);
|
2021-06-04 11:31:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 21:08:55 +01:00
|
|
|
|
private void Connect()
|
2021-06-04 11:31:09 +01:00
|
|
|
|
{
|
2021-08-24 21:08:55 +01:00
|
|
|
|
QSBNetworkManager.Instance.networkAddress = (PopupMenu as PopupInputMenu).GetInputText();
|
2021-06-04 11:31:09 +01:00
|
|
|
|
QSBNetworkManager.Instance.StartClient();
|
2021-08-26 08:51:33 +01:00
|
|
|
|
DisconnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = "CONNECTING... (STOP)";
|
2021-08-24 21:08:55 +01:00
|
|
|
|
DisconnectButton.gameObject.SetActive(true);
|
2021-08-24 22:30:53 +01:00
|
|
|
|
DisconnectButton.GetComponent<CanvasGroup>().alpha = 1f;
|
|
|
|
|
ClientButton.SetActive(false);
|
|
|
|
|
HostButton.gameObject.SetActive(false);
|
2021-06-04 11:31:09 +01:00
|
|
|
|
}
|
2021-08-26 08:51:33 +01:00
|
|
|
|
|
|
|
|
|
private void OnConnected()
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"ON CONNECTED");
|
|
|
|
|
DisconnectButton.transform.GetChild(0).GetChild(1).GetComponent<Text>().text = "DISCONNECT";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisconnected(NetworkError error)
|
|
|
|
|
{
|
|
|
|
|
if (error == NetworkError.Ok)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 14:56:42 +01:00
|
|
|
|
OpenInfoPopup($"Client Disconnected. Reason : {error}", "OK");
|
2021-08-26 08:51:33 +01:00
|
|
|
|
|
|
|
|
|
DisconnectButton.gameObject.SetActive(false);
|
|
|
|
|
ClientButton.SetActive(true);
|
|
|
|
|
HostButton.gameObject.SetActive(true);
|
|
|
|
|
}
|
2021-06-04 11:31:09 +01:00
|
|
|
|
}
|
|
|
|
|
}
|