feat(ui): implement login UI validation logic
This commit is contained in:
parent
d1745e1af3
commit
ea005e4477
2 changed files with 50 additions and 14 deletions
|
|
@ -451,8 +451,9 @@ MonoBehaviour:
|
|||
m_Script: {fileID: 11500000, guid: 3955dbe39e234b7a84bffd5f053855ff, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
connectButton: {fileID: 0}
|
||||
sendButton: {fileID: 0}
|
||||
loginButton: {fileID: 489976505}
|
||||
usernameInputField: {fileID: 535676077}
|
||||
passwordInputField: {fileID: 234502803}
|
||||
--- !u!1 &184757597
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
|
@ -1346,7 +1347,7 @@ MonoBehaviour:
|
|||
m_PressedTrigger: Pressed
|
||||
m_SelectedTrigger: Selected
|
||||
m_DisabledTrigger: Disabled
|
||||
m_Interactable: 1
|
||||
m_Interactable: 0
|
||||
m_TargetGraphic: {fileID: 489976506}
|
||||
m_OnClick:
|
||||
m_PersistentCalls:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using _Hub.Scripts.Network;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
|
|
@ -7,26 +9,59 @@ namespace _Hub.Scripts.UI
|
|||
{
|
||||
public class LoginUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Button connectButton;
|
||||
[SerializeField] private Button sendButton;
|
||||
#region Variables
|
||||
|
||||
private const int _maxUsernameLength = 15;
|
||||
private const int _maxPasswordLength = 15;
|
||||
[SerializeField] private Button loginButton;
|
||||
[SerializeField] private TMP_InputField usernameInputField;
|
||||
[SerializeField] private TMP_InputField passwordInputField;
|
||||
private string _username = String.Empty;
|
||||
private string _password = String.Empty;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Unity
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
connectButton.onClick.AddListener(OnConnectButton);
|
||||
sendButton.onClick.AddListener(OnSendButton);
|
||||
loginButton.onClick.AddListener(OnLoginClick);
|
||||
usernameInputField.onValueChanged.AddListener(UpdateUsername);
|
||||
passwordInputField.onValueChanged.AddListener(UpdatePassword);
|
||||
}
|
||||
|
||||
|
||||
private void OnConnectButton()
|
||||
private void OnLoginClick()
|
||||
{
|
||||
NetworkClient.Instance.Connect();
|
||||
Debug.Log("Connect Button");
|
||||
}
|
||||
|
||||
private void OnSendButton()
|
||||
private void UpdateUsername(string username)
|
||||
{
|
||||
NetworkClient.Instance.SendServer("This is what i send");
|
||||
Debug.Log("Send Button");
|
||||
}
|
||||
_username = username;
|
||||
Debug.Log("Username:" + username);
|
||||
ValidateAndUpdateUI();
|
||||
}
|
||||
|
||||
private void UpdatePassword(string password)
|
||||
{
|
||||
_password = password;
|
||||
Debug.Log("Password:" + password);
|
||||
ValidateAndUpdateUI();
|
||||
}
|
||||
|
||||
private void ValidateAndUpdateUI()
|
||||
{
|
||||
var usernameRegex = Regex.Match(_username, "^[a-zA-Z0-9]+$");
|
||||
var interactable = (!string.IsNullOrWhiteSpace(_username) && !string.IsNullOrWhiteSpace(_password) &&
|
||||
(_maxUsernameLength >= _username.Length && _maxPasswordLength >= _password.Length) &&
|
||||
usernameRegex.Success);
|
||||
EnableLoginButton(interactable);
|
||||
}
|
||||
|
||||
private void EnableLoginButton(bool interactable)
|
||||
{
|
||||
loginButton.interactable = interactable;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue