125 lines
3.7 KiB
C#
Raw Normal View History

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-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();
}
}
private void InitPauseMenus()
{
PopupMenu = MenuApi.MakeInputFieldPopup("IP Address", "IP Address", "Connect", "Cancel");
PopupMenu.OnPopupConfirm += Connect;
InfoPopup = MenuApi.MakeInfoPopup("you forgot to set the text!!!", "you dumpty!!!");
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-24 22:30:53 +01:00
PopupMenu = MenuApi.MakeInputFieldPopup("IP Address", "IP Address", "Connect", "Cancel");
PopupMenu.OnPopupConfirm += Connect;
2021-08-26 08:51:33 +01:00
InfoPopup = MenuApi.MakeInfoPopup("you forgot to set the text!!!", "you dumpty!!!");
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;
}
InfoPopup.SetUpPopup($"Client Disconnected. Reason : {error}", InputLibrary.menuConfirm, InputLibrary.cancel, new ScreenPrompt("OK"), null, true, false);
InfoPopup.EnableMenu(true);
DisconnectButton.gameObject.SetActive(false);
ClientButton.SetActive(true);
HostButton.gameObject.SetActive(true);
}
2021-06-04 11:31:09 +01:00
}
}