Iplements and integrates GameManager and UIController.

This commit is contained in:
Jonas Luz Jr. 2025-11-23 16:40:06 -03:00
parent dd5aef15b1
commit 4c077514e3
13 changed files with 228 additions and 7 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2ef7622eea79d2543b4f52a08c14c5f7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,3 +1,5 @@
using System;
using System.Collections;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
@ -51,6 +53,7 @@ namespace PPGIA.X540.Project3.API
[Header("API State Information")] [Header("API State Information")]
[SerializeField] [SerializeField]
private Session _session; private Session _session;
public bool IsSessionActive => _session != null;
#endregion ------------------------------------------------------------ #endregion ------------------------------------------------------------
#region -- Other Properties & Methods --------------------------------- #region -- Other Properties & Methods ---------------------------------
@ -88,7 +91,7 @@ namespace PPGIA.X540.Project3.API
} }
[ContextMenu("Session/Initiate Session")] [ContextMenu("Session/Initiate Session")]
public void InitiateSession() public void InitiateSession(Action sessionStartedCallback = null)
{ {
StopAllCoroutines(); StopAllCoroutines();
@ -100,15 +103,18 @@ namespace PPGIA.X540.Project3.API
var body = request.downloadHandler?.text ?? string.Empty; var body = request.downloadHandler?.text ?? string.Empty;
var session = JsonUtility.FromJson<Session>(body); var session = JsonUtility.FromJson<Session>(body);
_session = session; _session = session;
sessionStartedCallback?.Invoke();
})); }));
} }
[ContextMenu("Session/Close Session")] [ContextMenu("Session/Close Session")]
public void CloseSession() public void CloseSession(Action sessionClosedCallback = null)
{ {
if (_session == null) if (_session == null)
{ {
Debug.LogWarning("No active session to close."); Debug.LogWarning("No active session to close.");
sessionClosedCallback?.Invoke();
return; return;
} }
@ -121,11 +127,14 @@ namespace PPGIA.X540.Project3.API
{ {
Debug.Log("Session closed successfully."); Debug.Log("Session closed successfully.");
_session = null; _session = null;
sessionClosedCallback?.Invoke();
})); }));
} }
[ContextMenu("Chat/Send Message")] [ContextMenu("Chat/Send Message")]
public void SendChatMessage() public void SendChatMessage(string message = null,
Action<string> responseReceivedCallback = null,
Action speechFinishedCallback = null)
{ {
// Ensure there is an active session // Ensure there is an active session
if (_session == null) if (_session == null)
@ -134,6 +143,8 @@ namespace PPGIA.X540.Project3.API
return; return;
} }
if (message != null) _query = message;
StopAllCoroutines(); StopAllCoroutines();
// Build the endpoint URL and payload // Build the endpoint URL and payload
@ -146,6 +157,10 @@ namespace PPGIA.X540.Project3.API
{ {
var body = request.downloadHandler?.text ?? string.Empty; var body = request.downloadHandler?.text ?? string.Empty;
var response = ApiModel.FromJson<ChatServiceResponse>(body); var response = ApiModel.FromJson<ChatServiceResponse>(body);
var chatResponse = response?.Message;
responseReceivedCallback?.Invoke(chatResponse);
var audioUrl = response?.AudioUrl; var audioUrl = response?.AudioUrl;
if (string.IsNullOrEmpty(audioUrl)) if (string.IsNullOrEmpty(audioUrl))
{ {
@ -162,10 +177,25 @@ namespace PPGIA.X540.Project3.API
Debug.LogError("AudioClip is null after download."); Debug.LogError("AudioClip is null after download.");
return; return;
} }
StartCoroutine(PlayAudioAndNotifyCoroutine(
audioClip, speechFinishedCallback));
}));
}));
}
_audioSource?.PlayOneShot(audioClip); private IEnumerator PlayAudioAndNotifyCoroutine(
})); AudioClip audioClip, Action onComplete)
})); {
if (_audioSource == null || audioClip == null)
{
onComplete?.Invoke();
yield break;
}
_audioSource.PlayOneShot(audioClip);
yield return new WaitForSeconds(audioClip.length);
onComplete?.Invoke();
} }
#endregion -- API Calls ------------------------------------------------ #endregion -- API Calls ------------------------------------------------

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8a2376aa5b49eeb41aac7df48220349f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,72 @@
using UnityEngine;
using PPGIA.X540.Project3.API;
namespace PPGIA.X540.Project3
{
public class AppManager : MonoBehaviour
{
[Header("References")]
[SerializeField]
private UIController _uiController;
[SerializeField]
private ApiClientManager _apiManager;
private void Awake()
{
if (_uiController == null)
_uiController = GetComponent<UIController>();
if (_apiManager == null)
_apiManager = GetComponent<ApiClientManager>();
}
void Start()
{
_apiManager.CloseSession(
() => _uiController.SessionActive = _apiManager.IsSessionActive);
}
private void OnEnable()
{
_uiController.OnSessionButtonClicked += HandleSessionButtonClicked;
_uiController.OnSendChatButtonClicked += HandleSendChatButtonClicked;
}
private void OnDisable()
{
_uiController.OnSessionButtonClicked -= HandleSessionButtonClicked;
_uiController.OnSendChatButtonClicked -= HandleSendChatButtonClicked;
}
private void HandleSessionButtonClicked()
{
if (!_apiManager.IsSessionActive)
{
_apiManager.InitiateSession(
() => _uiController.SessionActive = _apiManager.IsSessionActive);
}
else
{
_apiManager.CloseSession(
() => _uiController.SessionActive = _apiManager.IsSessionActive
);
}
}
private void HandleSendChatButtonClicked(string message)
{
_apiManager.SendChatMessage(message,
(responseMessage) =>
{
_uiController.ChatOutput += $"User: {message}\n";
_uiController.ChatOutput += $"Bot: {responseMessage}\n";
},
() =>
{
// Speech finished callback (optional)
});
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7a1ca38af0a96524893a60d620fa5791

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2accbadde24a9c647b17ab58a3139bfa
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,91 @@
using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace PPGIA.X540.Project3
{
[RequireComponent(typeof(UIController))]
public class UIController : MonoBehaviour
{
private UIDocument _uiDocument;
private VisualElement _root;
private readonly string[] _sessionButtonLabels = {
"Iniciar Sessão",
"Encerrar Sessão"
};
private Button _sessionButton;
private Button _sendChatButton;
private int _currentSessionState = 0;
private TextField _chatInputField;
private TextField _chatOutputField;
public string ChatOutput
{
get => _chatOutputField.value;
set => _chatOutputField.value = value;
}
public bool SessionActive {
get => _currentSessionState == 1;
set
{
_currentSessionState = value ? 1 : 0;
UpdateStateForSession();
}
}
public Action OnSessionButtonClicked { get; set; }
public Action<string> OnSendChatButtonClicked { get; set; }
public float Progress { get; set; }
private void Awake()
{
_uiDocument = GetComponent<UIDocument>();
_root = _uiDocument.rootVisualElement;
_sessionButton = _root.Q<Button>("B_Session");
_sendChatButton = _root.Q<Button>("B_SendChat");
_chatInputField = _root.Q<TextField>("TF_ChatInput");
_chatOutputField = _root.Q<TextField>("TF_ChatOutput");
SessionActive = false;
}
void OnEnable()
{
_sessionButton.clicked += OnSessionButtonClickedInternal;
_sendChatButton.clicked += OnSendChatButtonClickedInternal;
}
void OnDisable()
{
_sessionButton.clicked -= OnSessionButtonClickedInternal;
_sendChatButton.clicked -= OnSendChatButtonClickedInternal;
}
private void UpdateStateForSession()
{
_sessionButton.text = _sessionButtonLabels[_currentSessionState];
var enable = _currentSessionState == 1;
_chatInputField.SetEnabled(enable);
_sendChatButton.SetEnabled(enable);
}
private void OnSessionButtonClickedInternal()
{
OnSessionButtonClicked?.Invoke();
// SessionActive state will be updated externally
}
private void OnSendChatButtonClickedInternal()
{
OnSendChatButtonClicked?.Invoke(_chatInputField.value);
_chatInputField.value = string.Empty;
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 177140e4f5a62c145a88b74bb8f02f59