140 lines
4.6 KiB
C#
140 lines
4.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// UserCreationScreen scene manager
|
|
/// </summary>
|
|
public class UserCreationScreen : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// Maximum lenght of a username
|
|
/// </summary>
|
|
private const int MAX_USERNAME_LENGTH = 12;
|
|
|
|
/// <summary>
|
|
/// Reference to the error message to display when a certain usernmae is invalid or already exists
|
|
/// </summary>
|
|
public GameObject errorMessage;
|
|
|
|
/// <summary>
|
|
/// Reference to the input text field for username
|
|
/// </summary>
|
|
public TMP_InputField inputName;
|
|
|
|
/// <summary>
|
|
/// Reference to the avatar-list container
|
|
/// </summary>
|
|
public Transform avatarsContainer;
|
|
|
|
/// <summary>
|
|
/// Avatar prefab
|
|
/// </summary>
|
|
public GameObject avatarPrefab;
|
|
|
|
/// <summary>
|
|
/// Current selected avatar
|
|
/// </summary>
|
|
private int selectedAvatar = 0;
|
|
|
|
/// <summary>
|
|
/// List of references to avatar background sprites (so we can color them nicely)
|
|
/// </summary>
|
|
private List<Image> avatars = new List<Image>();
|
|
|
|
/// <summary>
|
|
/// Reference to the back button, so we can deactivate it when the user cannot go back (when no user is found at startup)
|
|
/// </summary>
|
|
public GameObject backButton;
|
|
|
|
/// <summary>
|
|
/// Boolean used to check whether the user can go back to the previous scene
|
|
/// </summary>
|
|
public static bool canGoBack = true;
|
|
|
|
|
|
/// <summary>
|
|
/// Start is called before the first frame update
|
|
/// </summary>
|
|
void Start()
|
|
{
|
|
errorMessage.SetActive(false);
|
|
backButton.SetActive(canGoBack);
|
|
|
|
// Reset to default value
|
|
UserCreationScreen.canGoBack = true;
|
|
|
|
for (int i = 0; i < UserList.AVATARS.Count; i++)
|
|
{
|
|
// Create instance of prefab
|
|
GameObject instance = GameObject.Instantiate(avatarPrefab, avatarsContainer);
|
|
|
|
// Store value of i so we can use it the callback (else it would get the value of sprites.Count)
|
|
int x = i;
|
|
// Add onClick callback
|
|
instance.GetComponent<Button>().onClick.AddListener(() => UpdateAvatar(x));
|
|
|
|
// Store reference to image for fancy coloring
|
|
Image background = instance.GetComponent<Image>();
|
|
avatars.Add(background);
|
|
// Set background color
|
|
background.color = selectedAvatar == i ? Color.blue : Color.gray;
|
|
// Find correct component for setting the sprite
|
|
instance.transform.Find("Image").GetComponent<Image>().sprite = UserList.AVATARS[i];
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update the current selected avatar
|
|
/// </summary>
|
|
/// <param name="newAvatar">Index to the new avatar in the <c>this.avatars</c> list</param>
|
|
private void UpdateAvatar(int newAvatar)
|
|
{
|
|
avatars[selectedAvatar].color = Color.gray;
|
|
selectedAvatar = newAvatar;
|
|
avatars[selectedAvatar].color = Color.blue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if a given string is a correct username (using Regex)
|
|
/// </summary>
|
|
/// <param name="username">The username to be checked</param>
|
|
/// <returns><c>true</c> if the username was valid, <c>false</c> otherwise</returns>
|
|
static public bool IsValidUsername(string username)
|
|
{
|
|
return new Regex($@"^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]{{1,{MAX_USERNAME_LENGTH}}}$").IsMatch(username);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new user
|
|
/// (this method will be called by a button callback)
|
|
/// </summary>
|
|
public void CreateUser()
|
|
{
|
|
string username = inputName.text;
|
|
if (IsValidUsername(username))
|
|
{
|
|
if (UserList.GetUserByUsername(username) == null)
|
|
{
|
|
// Create a new entry in the UserList ScriptableObject
|
|
UserList.AddUser(username, UserList.AVATARS[selectedAvatar]);
|
|
SystemController.GetInstance().BackToPreviousScene();
|
|
}
|
|
// Warn user that username already exists
|
|
else
|
|
{
|
|
errorMessage.SetActive(true);
|
|
errorMessage.GetComponent<TMP_Text>().text = "Deze gebruikersnaam bestaat al! Kies een andere.";
|
|
}
|
|
}
|
|
// Warn user that username is invalid
|
|
else
|
|
{
|
|
errorMessage.SetActive(true);
|
|
errorMessage.GetComponent<TMP_Text>().text = "Je gebruikersnaam moet bestaan uit minimum 1 en maximum 12 letters (a-z en A-Z) of cijfers (0-9).";
|
|
}
|
|
}
|
|
}
|