SanAndreasUnity/Assets/Scripts/UI/JoinGameWindow.cs
2020-05-31 19:09:42 +02:00

104 lines
2.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Utilities;
using SanAndreasUnity.Net;
namespace SanAndreasUnity.UI
{
public class JoinGameWindow : PauseMenuWindow
{
string m_ipStr = "127.0.0.1";
string m_portStr = NetManager.defaultListenPortNumber.ToString();
JoinGameWindow()
{
// set default parameters
this.windowName = "Join Game";
this.useScrollView = true;
}
void Start ()
{
// adjust rect
this.windowRect = GUIUtils.GetCenteredRect(new Vector2(550, 300));
}
void Update()
{
if (PauseMenu.IsOpened)
this.IsOpened = false;
}
protected override void OnWindowGUI ()
{
GUILayout.Label ("IP:");
m_ipStr = GUILayout.TextField(m_ipStr, GUILayout.Width(200));
GUILayout.Label ("Port:");
m_portStr = GUILayout.TextField(m_portStr, GUILayout.Width(100));
GUILayout.Space(40);
// 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);
// button for connecting/disconnecting
string buttonText = "Connect";
System.Action buttonAction = this.Connect;
if (NetStatus.IsClientConnecting())
{
buttonText = "Disconnect";
buttonAction = this.Disconnect;
}
else if (NetStatus.IsClientConnected())
{
GUI.enabled = false;
buttonText = "Connected";
buttonAction = () => {};
}
if (GUILayout.Button(buttonText, GUILayout.MinWidth(80), GUILayout.Height(30), GUILayout.ExpandWidth(false)))
buttonAction();
}
void Connect()
{
try
{
NetManager.StartClient(m_ipStr, ushort.Parse(m_portStr));
}
catch (System.Exception ex)
{
Debug.LogException(ex);
MessageBox.Show("Error", ex.ToString());
}
}
void Disconnect()
{
NetManager.StopNetwork();
}
}
}