107 lines
3.3 KiB
C#
107 lines
3.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// ChangeUserScreen scene manager
|
|
/// </summary>
|
|
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>
|
|
/// UI Reference to the error GameObject to display an error message
|
|
/// </summary>
|
|
public GameObject error;
|
|
|
|
/// <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()
|
|
{
|
|
PersistentDataController.GetInstance().Load();
|
|
error.SetActive(false);
|
|
DisplayUsers();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add all users to the container to display them
|
|
/// </summary>
|
|
private void DisplayUsers()
|
|
{
|
|
foreach (Transform child in usersContainer)
|
|
Destroy(child.gameObject);
|
|
|
|
List<User> users = UserList.GetUsers();
|
|
currentUserIndex = UserList.IndexOf(UserList.GetCurrentUser().GetUsername());
|
|
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 user content
|
|
UserCard card = instance.GetComponent<UserCard>();
|
|
card.user = user;
|
|
card.selectUser = () => UpdateSelection(x);
|
|
card.updateUserCardContainer = DisplayUsers;
|
|
card.displayError = () => error.SetActive(true);
|
|
|
|
// Store reference to image for fancy coloring
|
|
Image background = instance.GetComponent<Image>();
|
|
userBackgrounds.Add(background);
|
|
// Set background color
|
|
background.color = i == currentUserIndex ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
}
|
|
}
|
|
|
|
/// <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 = new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Select the current selected user and return to the StartScreenScene
|
|
/// </summary>
|
|
public void IChooseYou()
|
|
{
|
|
UserList.ChangeCurrentUser(currentUserIndex);
|
|
SystemController.GetInstance().BackToPreviousScene();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Callback to load the UserCreationScreen scene
|
|
/// </summary>
|
|
public void GotoUserCreation()
|
|
{
|
|
SystemController.GetInstance().LoadNextScene("Accounts/Scenes/UserCreationScreen");
|
|
}
|
|
}
|