using UnityEngine;
using System.Net;
using TNet;
public class UIDirectConnect : MonoBehaviour
{
public UIButton button;
public UILabel buttonText;
public UILabel infoLabel;
public UIInput serverAddress;
public UILabel internalIP;
public UILabel externalIP;
public UIPanel channelListWindow;
public GameObject clearButton;
bool mConnecting = false;
bool mIpResolved = false;
IPAddress mLocal;
IPAddress mExternal;
void Start ()
{
#if UNITY_WEBPLAYER
Tools.ipCheckerUrl = "http://starlink.tasharen.com/ip.php";
infoLabel.text = Localization.Localize("Direct Note 3");
#else
infoLabel.text = Localization.Localize("Direct Note 2");
#endif
internalIP.text = Localization.Localize("Searching");
externalIP.text = Localization.Localize("Searching");
UIEventListener.Get(button.gameObject).onClick = OnButtonClick;
UIEventListener.Get(serverAddress.gameObject).onSelect = OnSelectInput;
UIEventListener.Get(clearButton).onClick = OnClear;
serverAddress.onSubmit = Connect;
Tools.ResolveIPs(OnResolvedIPs);
UpdateButtonText();
}
void OnClear (GameObject go)
{
serverAddress.text = "";
UpdateButtonText();
}
void OnSelectInput (GameObject go, bool selected)
{
UpdateButtonText();
}
void OnResolvedIPs (IPAddress local, IPAddress ext)
{
mLocal = local;
mExternal = ext;
mIpResolved = true;
}
void Update ()
{
if (mIpResolved)
{
internalIP.text = mLocal.ToString();
externalIP.text = mExternal.ToString();
mIpResolved = false;
}
}
void OnEnable ()
{
TNManager.Disconnect();
UpdateButtonText();
}
void OnButtonClick (GameObject go)
{
if (TNServerInstance.isActive)
{
TNServerInstance.Stop();
UpdateButtonText();
}
else if (!string.IsNullOrEmpty(serverAddress.text))
{
Connect(serverAddress.text);
}
#if !UNITY_WEBPLAYER
else
{
TNServerInstance.serverName = "Direct Server";
if (TNServerInstance.Start(5127))
{
TNManager.Connect("127.0.0.1", 5127);
}
else
{
UIMessageBox.Show(Localization.Localize("Unable to Start"), Localization.Localize("Unable to Start Info"));
}
Connect(serverAddress.text);
UpdateButtonText();
}
#endif
}
void Connect (string val)
{
if (!string.IsNullOrEmpty(val))
{
mConnecting = true;
TNManager.Connect(val, 5127);
UpdateButtonText();
}
}
void OnNetworkConnect (bool success, string errmsg)
{
if (!success)
{
UIMessageBox.Show(Localization.Localize("Unable to Connect"), errmsg);
}
mConnecting = false;
UpdateButtonText();
if (success)
{
UIWindow.Show(channelListWindow);
}
}
void UpdateButtonText ()
{
if (mConnecting)
{
button.isEnabled = false;
buttonText.text = Localization.Localize("Connecting");
}
else if (TNServerInstance.isActive)
{
button.isEnabled = true;
buttonText.text = Localization.Localize("Stop");
}
#if UNITY_WEBPLAYER
else if (string.IsNullOrEmpty(serverAddress.text))
{
buttonText.text = Localization.Localize("Connect");
button.isEnabled = false;
}
#else
else if (string.IsNullOrEmpty(serverAddress.text))
{
button.isEnabled = true;
buttonText.text = Localization.Localize("Start");
}
#endif
else
{
button.isEnabled = true;
buttonText.text = Localization.Localize("Connect");
}
}
}