Update
This commit is contained in:
90
Assets/Scripts/UI/LoginController.cs
Normal file
90
Assets/Scripts/UI/LoginController.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
public class LoginController : BaseUIController
|
||||
{
|
||||
private TextField _nameInput;
|
||||
private Label _errorLabel;
|
||||
private Button _confirmBtn;
|
||||
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
|
||||
_nameInput = root.Q<TextField>("UsernameInput");
|
||||
_errorLabel = root.Q<Label>("ErrorMsg");
|
||||
_confirmBtn = root.Q<Button>("ConfirmBtn");
|
||||
|
||||
if (_confirmBtn != null)
|
||||
_confirmBtn.clicked += OnConfirmClicked;
|
||||
}
|
||||
|
||||
private async void OnConfirmClicked()
|
||||
{
|
||||
string username = _nameInput.value.Trim();
|
||||
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
ShowError("Name cannot be empty!");
|
||||
return;
|
||||
}
|
||||
|
||||
_confirmBtn.SetEnabled(false);
|
||||
_confirmBtn.text = "CHECKING...";
|
||||
|
||||
// 1. Kiểm tra trùng tên trên Firebase
|
||||
bool isTaken = await FirebaseService.IsUsernameTaken(username);
|
||||
|
||||
if (isTaken)
|
||||
{
|
||||
ShowError("This name is already taken!");
|
||||
_confirmBtn.SetEnabled(true);
|
||||
_confirmBtn.text = "CONFIRM";
|
||||
}
|
||||
else
|
||||
{
|
||||
// 2. Đăng ký user mới
|
||||
bool success = await FirebaseService.RegisterUser(username);
|
||||
if (success)
|
||||
{
|
||||
// 3. Lưu lại và đóng popup
|
||||
PlayerPrefs.SetString("Username", username);
|
||||
PlayerPrefs.Save();
|
||||
Debug.Log($"[Login] Registered as {username}");
|
||||
|
||||
// Thông báo cho UIManager biết đã login xong
|
||||
uiManager.OnLoginSuccess();
|
||||
await PlayTransitionOut();
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowError("Connection error!");
|
||||
_confirmBtn.SetEnabled(true);
|
||||
_confirmBtn.text = "CONFIRM";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowError(string msg)
|
||||
{
|
||||
if (_errorLabel == null) return;
|
||||
_errorLabel.text = msg;
|
||||
_errorLabel.style.display = DisplayStyle.Flex;
|
||||
}
|
||||
|
||||
public override async Task PlayTransitionIn()
|
||||
{
|
||||
// Login hiện ra như một overlay trung tâm
|
||||
if (root != null)
|
||||
{
|
||||
root.style.translate = new StyleTranslate(new Translate(0, 0));
|
||||
root.style.opacity = 0;
|
||||
}
|
||||
Show();
|
||||
await PrimeTween.Tween.Custom(0f, 1f, duration: 0.5f, onValueChange: val => root.style.opacity = val);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user