using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class LoginUmaUI : MonoBehaviour
{
public GUISkin skin;
public GameObject soundMenu;
public GameObject musicObject;
enum LoginState
{
Login,
Register,
Authenticating,
CharacterSelect,
CharacterCreate
}
enum CreationState
{
Body,
Head,
Face,
Hair
}
#region fields
LoginState loginState;
CreationState creationState;
// Login fields
string username = "";
string password = "";
// Registration fields
string password2 = "";
string email = "";
string email2 = "";
// Character select fields
CharacterEntry characterSelected = null;
string characterName = "";
string race = "Human";
string gender = "Male";
string dialogMessage = "";
string errorMessage = "";
#endregion fields
// Use this for initialization
void Start ()
{
loginState = LoginState.Login;
EventSystem.RegisterEvent("LOGIN_RESPONSE", this);
EventSystem.RegisterEvent("REGISTER_RESPONSE", this);
// Play music
SoundSystem.LoadSoundSettings();
SoundSystem.PlayMusic(musicObject.GetComponent<AudioSource>());
}
// Update is called once per frame
void Update ()
{
}
void OnGUI ()
{
GUI.skin = skin;
if (errorMessage != "" || dialogMessage != "") {
GUI.enabled = false;
}
if (loginState == LoginState.Login || loginState == LoginState.Authenticating) {
DrawLoginUI();
//DrawSoundMenuButton();
} else if (loginState == LoginState.Register) {
DrawRegisterUI();
}
GUI.enabled = true;
if (errorMessage != "") {
DrawErrorUI();
} else if (dialogMessage != "") {
DrawDialogUI();
}
}
public void OnEvent(EventData eData) {
if (eData.eventType == "LOGIN_RESPONSE") {
dialogMessage = "";
if (eData.eventArgs[0] == "Success") {
Application.LoadLevel("UMA Character Scene");
} else {
errorMessage = eData.eventArgs[0];
}
} else if (eData.eventType == "REGISTER_RESPONSE") {
dialogMessage = "";
if (eData.eventArgs[0] == "Success") {
loginState = LoginState.Login;
errorMessage = "Account created. You can now log in";
} else {
errorMessage = eData.eventArgs[0];
}
}
}
Rect CreateCenteredRect (int width, int height)
{
int left = (Screen.width - width) / 2;
int top = (Screen.height - height) / 2;
Rect centeredRect
= new Rect
(left, top, width, height
); return centeredRect;
}
void DrawLoginUI ()
{
GUILayout.BeginArea(CreateCenteredRect (220, 180), skin.GetStyle("Window"));
GUILayout.Label ("Username:");
username = GUILayout.TextField (username);
GUILayout.Label("Password:");
password = GUILayout.PasswordField(password, '*');
GUILayout.BeginHorizontal();
if (GUILayout.Button("Login")) {
dialogMessage = "Logging in...";
// Verify username and password are entered, then pass them to the client
// Also convert password to md5
Client.Instance.Login(username, Encryption.Md5Sum(password));
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
private bool ValidateEmail (string email)
{
Regex regex
= new Regex
(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); Match match = regex.Match (email);
if (match.Success)
return true;
else
return false;
}
#endregion
void ShowDialog(string message, bool showButton) {
if (showButton) {
errorMessage = message;
dialogMessage = "";
} else {
dialogMessage = message;
errorMessage = "";
}
/*dialogUI.SetActive(true);
dialogString.text = message;
dialogButton.SetActive(showButton);*/
}
}