Iplements and integrates GameManager and UIController.
This commit is contained in:
parent
dd5aef15b1
commit
4c077514e3
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2ef7622eea79d2543b4f52a08c14c5f7
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
|
||||
using UnityEngine;
|
||||
|
|
@ -51,6 +53,7 @@ namespace PPGIA.X540.Project3.API
|
|||
[Header("API State Information")]
|
||||
[SerializeField]
|
||||
private Session _session;
|
||||
public bool IsSessionActive => _session != null;
|
||||
#endregion ------------------------------------------------------------
|
||||
|
||||
#region -- Other Properties & Methods ---------------------------------
|
||||
|
|
@ -88,7 +91,7 @@ namespace PPGIA.X540.Project3.API
|
|||
}
|
||||
|
||||
[ContextMenu("Session/Initiate Session")]
|
||||
public void InitiateSession()
|
||||
public void InitiateSession(Action sessionStartedCallback = null)
|
||||
{
|
||||
StopAllCoroutines();
|
||||
|
||||
|
|
@ -100,15 +103,18 @@ namespace PPGIA.X540.Project3.API
|
|||
var body = request.downloadHandler?.text ?? string.Empty;
|
||||
var session = JsonUtility.FromJson<Session>(body);
|
||||
_session = session;
|
||||
|
||||
sessionStartedCallback?.Invoke();
|
||||
}));
|
||||
}
|
||||
|
||||
[ContextMenu("Session/Close Session")]
|
||||
public void CloseSession()
|
||||
{
|
||||
public void CloseSession(Action sessionClosedCallback = null)
|
||||
{
|
||||
if (_session == null)
|
||||
{
|
||||
Debug.LogWarning("No active session to close.");
|
||||
sessionClosedCallback?.Invoke();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -121,11 +127,14 @@ namespace PPGIA.X540.Project3.API
|
|||
{
|
||||
Debug.Log("Session closed successfully.");
|
||||
_session = null;
|
||||
sessionClosedCallback?.Invoke();
|
||||
}));
|
||||
}
|
||||
|
||||
[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
|
||||
if (_session == null)
|
||||
|
|
@ -134,6 +143,8 @@ namespace PPGIA.X540.Project3.API
|
|||
return;
|
||||
}
|
||||
|
||||
if (message != null) _query = message;
|
||||
|
||||
StopAllCoroutines();
|
||||
|
||||
// Build the endpoint URL and payload
|
||||
|
|
@ -146,13 +157,17 @@ namespace PPGIA.X540.Project3.API
|
|||
{
|
||||
var body = request.downloadHandler?.text ?? string.Empty;
|
||||
var response = ApiModel.FromJson<ChatServiceResponse>(body);
|
||||
|
||||
var chatResponse = response?.Message;
|
||||
responseReceivedCallback?.Invoke(chatResponse);
|
||||
|
||||
var audioUrl = response?.AudioUrl;
|
||||
if (string.IsNullOrEmpty(audioUrl))
|
||||
{
|
||||
Debug.LogWarning("No audio URL in response.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Debug.Log($"Downloading audio from: {audioUrl}");
|
||||
StartCoroutine(ApiClient.DownloadAudioCoroutine(
|
||||
audioUrl, _timeoutInSeconds, (audioClip) =>
|
||||
|
|
@ -162,11 +177,26 @@ namespace PPGIA.X540.Project3.API
|
|||
Debug.LogError("AudioClip is null after download.");
|
||||
return;
|
||||
}
|
||||
|
||||
_audioSource?.PlayOneShot(audioClip);
|
||||
StartCoroutine(PlayAudioAndNotifyCoroutine(
|
||||
audioClip, speechFinishedCallback));
|
||||
}));
|
||||
}));
|
||||
}
|
||||
|
||||
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 ------------------------------------------------
|
||||
|
||||
[ContextMenu("Debug/Play or Stop Test Audio")]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8a2376aa5b49eeb41aac7df48220349f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -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)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7a1ca38af0a96524893a60d620fa5791
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2accbadde24a9c647b17ab58a3139bfa
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 177140e4f5a62c145a88b74bb8f02f59
|
||||
Loading…
Reference in New Issue