Resolve WES-56 "Account switching"
This commit is contained in:
committed by
Jelle De Geest
parent
0ea743354b
commit
b6b863183e
90
Assets/Accounts/Scripts/ChangeUserScreen.cs
Normal file
90
Assets/Accounts/Scripts/ChangeUserScreen.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ChangeUserScreen : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// Prefab of the user selection
|
||||
/// </summary>
|
||||
public GameObject userPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the container to list all users
|
||||
/// </summary>
|
||||
public Transform usersContainer;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the user list
|
||||
/// </summary>
|
||||
public UserList userList;
|
||||
|
||||
/// <summary>
|
||||
/// Index of the current selected user in the UserList
|
||||
/// </summary>
|
||||
private int currentUserIndex;
|
||||
|
||||
/// <summary>
|
||||
/// List of references to avatar background sprites (so we can color them nicely)
|
||||
/// </summary>
|
||||
private List<Image> userBackgrounds = new List<Image>();
|
||||
|
||||
/// <summary>
|
||||
/// Start is called before the first frame update
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
List<User> users = userList.GetUsers();
|
||||
currentUserIndex = userList.GetCurrentUserIndex();
|
||||
for (int i = 0; i < users.Count; i++)
|
||||
{
|
||||
User user = users[i];
|
||||
|
||||
// Create instance of prefab
|
||||
GameObject instance = GameObject.Instantiate(userPrefab, usersContainer);
|
||||
|
||||
// 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(() => UpdateSelection(x));
|
||||
// Set username
|
||||
instance.GetComponentInChildren<TMP_Text>().text = user.username;
|
||||
|
||||
// Store reference to image for fancy coloring
|
||||
Image background = instance.GetComponent<Image>();
|
||||
userBackgrounds.Add(background);
|
||||
// Set background color
|
||||
background.color = i == currentUserIndex ? Color.blue : Color.gray;
|
||||
// Find correct component for setting the sprite
|
||||
foreach (Image img in background.GetComponentsInChildren<Image>())
|
||||
if (img != background)
|
||||
{
|
||||
img.sprite = user.avatar;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the current selected user
|
||||
/// </summary>
|
||||
/// <param name="index">Index to the user in the <c>this.userBackgrounds</c> list</param>
|
||||
private void UpdateSelection(int index)
|
||||
{
|
||||
userBackgrounds[currentUserIndex].color = Color.gray;
|
||||
currentUserIndex = index;
|
||||
userBackgrounds[currentUserIndex].color = Color.blue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Select the current selected user and return to the StartScreenScene
|
||||
/// </summary>
|
||||
public void IChooseYou()
|
||||
{
|
||||
userList.ChangeCurrentUser(currentUserIndex);
|
||||
userList.Save();
|
||||
SceneManager.LoadScene("Common/Scenes/StartScreen");
|
||||
}
|
||||
}
|
||||
11
Assets/Accounts/Scripts/ChangeUserScreen.cs.meta
Normal file
11
Assets/Accounts/Scripts/ChangeUserScreen.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a7f31ce7a41c294b95cb1f9fbec207f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -15,6 +15,11 @@ public class UserCreationScreen : MonoBehaviour
|
||||
/// </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>
|
||||
@@ -57,6 +62,8 @@ public class UserCreationScreen : MonoBehaviour
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
errorMessage.SetActive(false);
|
||||
|
||||
for (int i = 0; i < sprites.Count; i++)
|
||||
{
|
||||
// Create instance of prefab
|
||||
@@ -100,7 +107,7 @@ public class UserCreationScreen : MonoBehaviour
|
||||
/// <returns><c>true</c> if the username was valid, <c>false</c> otherwise</returns>
|
||||
static public bool IsValidUsername(string username)
|
||||
{
|
||||
return new Regex($@"^[abcdefghijklmnopqrstuvwxyz]{{1,{MAX_USERNAME_LENGTH}}}$").IsMatch(username);
|
||||
return new Regex($@"^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]{{1,{MAX_USERNAME_LENGTH}}}$").IsMatch(username);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -115,16 +122,22 @@ public class UserCreationScreen : MonoBehaviour
|
||||
if (users.GetUserByUsername(username) == null)
|
||||
{
|
||||
// Create a new entry in the UserList ScriptableObject
|
||||
users.CreateAndAddNewUser(username, sprites[selectedAvatar]);
|
||||
users.ChangeCurrentUser(users.CreateAndAddNewUser(username, sprites[selectedAvatar]));
|
||||
// TODO: change scene, for now just change to StartScreen
|
||||
SceneManager.LoadScene("Common/Scenes/StartScreen");
|
||||
}
|
||||
// TODO: give more feedback to user
|
||||
// Warn user that username already exists
|
||||
else Debug.LogWarning($"Username '{username}' already exists!");
|
||||
else
|
||||
{
|
||||
errorMessage.SetActive(true);
|
||||
errorMessage.GetComponent<TMP_Text>().text = "Deze gebruikersnaam bestaat al! Kies een andere.";
|
||||
}
|
||||
}
|
||||
// TODO: give more feedback to user
|
||||
// Warn user that username is invalid
|
||||
else Debug.LogWarning($"Invalid username '{username}'!");
|
||||
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).";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ public class UserList : ScriptableObject
|
||||
/// The index of the current/last logged in user in the <c>storedUsers</c> list
|
||||
/// </summary>
|
||||
public int currentUserIndex;
|
||||
|
||||
/// <summary>
|
||||
/// A list containing all users (which can be serialized)
|
||||
/// </summary>
|
||||
@@ -105,6 +106,33 @@ public class UserList : ScriptableObject
|
||||
return storedUserList.storedUsers[storedUserList.currentUserIndex];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the index in the userlist of the current playing user
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetCurrentUserIndex()
|
||||
{
|
||||
return storedUserList.currentUserIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change the current user
|
||||
/// </summary>
|
||||
/// <param name="index">Index of the user in the userlist</param>
|
||||
public void ChangeCurrentUser(int index)
|
||||
{
|
||||
storedUserList.currentUserIndex = index;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change the current user
|
||||
/// </summary>
|
||||
/// <param name="user">Reference to the user in the userlist</param>
|
||||
public void ChangeCurrentUser(User user)
|
||||
{
|
||||
storedUserList.currentUserIndex = storedUserList.storedUsers.IndexOf(user);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save the users
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user