SanAndreasUnity/Assets/Scripts/UI/JoinGameWindow.cs

152 lines
3.5 KiB
C#
Raw Normal View History

2019-04-23 18:58:43 +00:00
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Net;
2019-11-12 13:43:22 +00:00
using System.Linq;
2019-04-23 18:58:43 +00:00
namespace SanAndreasUnity.UI
{
public class JoinGameWindow : PauseMenuWindow
{
[SerializeField] string m_ipStr = "127.0.0.1";
2019-06-25 21:04:30 +00:00
string m_portStr = NetManager.defaultListenPortNumber.ToString();
2019-04-23 18:58:43 +00:00
2019-11-12 13:43:22 +00:00
string[] m_tabNames = new string[]{"Direct", "LAN"};
int m_currentTabIndex = 0;
Mirror.NetworkDiscoveryHUD m_netDiscoveryHUD;
2019-04-23 18:58:43 +00:00
JoinGameWindow()
{
// set default parameters
this.windowName = "Join Game";
this.useScrollView = true;
}
void Start ()
{
// adjust rect
2019-11-12 13:43:22 +00:00
float width = Mathf.Min(650, Screen.width * 0.9f);
this.windowRect = GUIUtils.GetCenteredRect(new Vector2(width, 400));
m_netDiscoveryHUD = Mirror.NetworkManager.singleton.GetComponentOrThrow<Mirror.NetworkDiscoveryHUD>();
m_netDiscoveryHUD.connectAction = this.ConnectFromDiscovery;
m_netDiscoveryHUD.drawGUI = false;
2019-04-23 18:58:43 +00:00
}
void Update()
{
if (PauseMenu.IsOpened)
this.IsOpened = false;
}
2019-04-23 18:58:43 +00:00
protected override void OnWindowGUI ()
{
2019-11-12 13:43:22 +00:00
m_currentTabIndex = GUIUtils.TabsControl(m_currentTabIndex, m_tabNames);
2019-04-23 18:58:43 +00:00
2019-11-12 13:43:22 +00:00
GUILayout.Space(20);
if (0 == m_currentTabIndex)
{
GUILayout.Label ("IP:");
m_ipStr = GUILayout.TextField(m_ipStr, GUILayout.Width(200));
GUILayout.Label ("Port:");
m_portStr = GUILayout.TextField(m_portStr, GUILayout.Width(100));
}
else if (1 == m_currentTabIndex)
{
m_netDiscoveryHUD.width = (int) this.WindowSize.x - 30;
m_netDiscoveryHUD.DisplayServers();
}
2019-04-23 18:58:43 +00:00
}
protected override void OnWindowGUIAfterContent()
{
// label with status
string strStatus = "Disconnected";
if (NetStatus.IsClientConnecting())
{
strStatus = "Connecting.";
for (int i = 0; i < ((int)Time.realtimeSinceStartup) % 3; i++)
strStatus += ".";
}
else if (NetStatus.IsClientConnected())
{
strStatus = "Connected";
}
GUILayout.Label("Status: " + strStatus);
2019-11-12 13:43:22 +00:00
// button for connecting/disconnecting/refreshing LAN
string buttonText = "Connect";
2019-11-12 13:43:22 +00:00
System.Action buttonAction = this.ConnectDirectly;
if (NetStatus.IsClientConnecting())
{
buttonText = "Disconnect";
buttonAction = this.Disconnect;
}
else if (NetStatus.IsClientConnected())
{
GUI.enabled = false;
buttonText = "Connected";
buttonAction = () => {};
}
2019-11-12 13:43:22 +00:00
else
{
if (1 == m_currentTabIndex)
{
GUI.enabled = ! m_netDiscoveryHUD.IsRefreshing;
buttonText = m_netDiscoveryHUD.IsRefreshing ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh LAN";
buttonAction = () => m_netDiscoveryHUD.Refresh();
}
}
2019-07-28 22:26:56 +00:00
if (GUIUtils.ButtonWithCalculatedSize(buttonText, 80, 30))
buttonAction();
2019-04-23 18:58:43 +00:00
}
2019-11-12 13:43:22 +00:00
void ConnectDirectly()
{
this.Connect(m_ipStr, ushort.Parse(m_portStr));
}
void ConnectFromDiscovery(Mirror.NetworkDiscovery.DiscoveryInfo info)
{
this.Connect(info.EndPoint.Address.ToString(), ushort.Parse( info.KeyValuePairs[Mirror.NetworkDiscovery.kPortKey] ));
}
void Connect(string ip, ushort port)
2019-04-23 18:58:43 +00:00
{
2019-04-23 19:04:48 +00:00
try
{
2019-11-12 13:43:22 +00:00
NetManager.StartClient(ip, port);
2019-04-23 19:04:48 +00:00
}
catch (System.Exception ex)
{
2019-06-25 21:09:34 +00:00
Debug.LogException(ex);
2019-04-23 19:04:48 +00:00
MessageBox.Show("Error", ex.ToString());
}
2019-04-23 18:58:43 +00:00
}
void Disconnect()
{
NetManager.StopNetwork();
}
2019-04-23 18:58:43 +00:00
}
}