Refactors UI interaction and state management
Improves UI element handling by encapsulating UI logic. Enhances state management for session activity and input enablement through properties. Introduces event handlers for button clicks to decouple UI interactions.
This commit is contained in:
parent
15db99b032
commit
6c1ed05bd7
|
|
@ -12,36 +12,72 @@ namespace PPGIA.X540.Project3
|
||||||
private UIDocument _uiDocument;
|
private UIDocument _uiDocument;
|
||||||
private VisualElement _root;
|
private VisualElement _root;
|
||||||
|
|
||||||
private readonly string[] _sessionButtonLabels = {
|
#region -- Fields & Properties ----------------------------------------
|
||||||
"Iniciar Sessão",
|
// Buttons ------------------------------------------------------------
|
||||||
"Encerrar Sessão"
|
private readonly string[] _sessionButtonLabels = {
|
||||||
|
"Iniciar Sessão",
|
||||||
|
"Encerrar Sessão"
|
||||||
};
|
};
|
||||||
private Button _sessionButton;
|
private Button _sessionButton;
|
||||||
private Button _sendChatButton;
|
private Button _sendChatButton;
|
||||||
|
|
||||||
private int _currentSessionState = 0;
|
private int _currentSessionState = 0;
|
||||||
|
public bool SessionActive
|
||||||
|
{
|
||||||
|
get => _currentSessionState == 1;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_currentSessionState = value ? 1 : 0;
|
||||||
|
_sessionButton.text = _sessionButtonLabels[_currentSessionState];
|
||||||
|
InputEnabled = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Chat Fields --------------------------------------------------------
|
||||||
private TextField _chatInputField;
|
private TextField _chatInputField;
|
||||||
private TextField _chatOutputField;
|
public string ChatInput
|
||||||
|
{
|
||||||
|
get => _chatInputField.value;
|
||||||
|
set => _chatInputField.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TextField _chatOutputField;
|
||||||
public string ChatOutput
|
public string ChatOutput
|
||||||
{
|
{
|
||||||
get => _chatOutputField.value;
|
get => _chatOutputField.value;
|
||||||
set => _chatOutputField.value = value;
|
set => _chatOutputField.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SessionActive {
|
public bool InputEnabled
|
||||||
get => _currentSessionState == 1;
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var value = _chatInputField.enabledSelf;
|
||||||
|
_sendChatButton.SetEnabled(value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
_currentSessionState = value ? 1 : 0;
|
_chatInputField.SetEnabled(value);
|
||||||
UpdateStateForSession();
|
_sendChatButton.SetEnabled(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Action OnSessionButtonClicked { get; set; }
|
// Progress Bar -------------------------------------------------------
|
||||||
public Action<string> OnSendChatButtonClicked { get; set; }
|
private ProgressBar _progressBar;
|
||||||
public float Progress { get; set; }
|
public float Progress
|
||||||
|
{
|
||||||
|
get => _progressBar.value;
|
||||||
|
set => _progressBar.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event Handlers -----------------------------------------------------
|
||||||
|
public event Action OnSessionButtonClicked;
|
||||||
|
public event Action<string> OnSendChatButtonClicked;
|
||||||
|
|
||||||
|
#endregion ------------------------------------------------------------
|
||||||
|
|
||||||
|
#region -- MonoBehaviour Methods --------------------------------------
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
_uiDocument = GetComponent<UIDocument>();
|
_uiDocument = GetComponent<UIDocument>();
|
||||||
|
|
@ -51,12 +87,11 @@ namespace PPGIA.X540.Project3
|
||||||
_sendChatButton = _root.Q<Button>("B_SendChat");
|
_sendChatButton = _root.Q<Button>("B_SendChat");
|
||||||
_chatInputField = _root.Q<TextField>("TF_ChatInput");
|
_chatInputField = _root.Q<TextField>("TF_ChatInput");
|
||||||
_chatOutputField = _root.Q<TextField>("TF_ChatOutput");
|
_chatOutputField = _root.Q<TextField>("TF_ChatOutput");
|
||||||
|
_progressBar = _root.Q<ProgressBar>("PB_Progress");
|
||||||
SessionActive = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnEnable()
|
void OnEnable()
|
||||||
{
|
{
|
||||||
_sessionButton.clicked += OnSessionButtonClickedInternal;
|
_sessionButton.clicked += OnSessionButtonClickedInternal;
|
||||||
_sendChatButton.clicked += OnSendChatButtonClickedInternal;
|
_sendChatButton.clicked += OnSendChatButtonClickedInternal;
|
||||||
}
|
}
|
||||||
|
|
@ -66,20 +101,12 @@ namespace PPGIA.X540.Project3
|
||||||
_sessionButton.clicked -= OnSessionButtonClickedInternal;
|
_sessionButton.clicked -= OnSessionButtonClickedInternal;
|
||||||
_sendChatButton.clicked -= OnSendChatButtonClickedInternal;
|
_sendChatButton.clicked -= OnSendChatButtonClickedInternal;
|
||||||
}
|
}
|
||||||
|
#endregion ------------------------------------------------------------
|
||||||
private void UpdateStateForSession()
|
|
||||||
{
|
|
||||||
_sessionButton.text = _sessionButtonLabels[_currentSessionState];
|
|
||||||
|
|
||||||
var enable = _currentSessionState == 1;
|
|
||||||
_chatInputField.SetEnabled(enable);
|
|
||||||
_sendChatButton.SetEnabled(enable);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnSessionButtonClickedInternal()
|
private void OnSessionButtonClickedInternal()
|
||||||
{
|
{
|
||||||
OnSessionButtonClicked?.Invoke();
|
OnSessionButtonClicked?.Invoke();
|
||||||
// SessionActive state will be updated externally
|
// SessionActive state should be updated externally, no logic here.
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnSendChatButtonClickedInternal()
|
private void OnSendChatButtonClickedInternal()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue