UP-Viagg-io/Viagg-io/Assets/afca/ViaggioAI/Scripts/ViaggioAI/ViaggioAIDebugPanelControll...

221 lines
6.2 KiB
C#
Executable File

using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
public class ViaggioAIDebugPanelController : MonoBehaviour
{
#region Inspector Properties
[Header("Scene Objects")]
[SerializeField]
private TMP_Text stateTextMesh;
[SerializeField]
private TMP_Text lastThrownEventTextMesh;
[SerializeField]
private TMP_Text recognziedTextTextMesh;
[SerializeField]
private TMP_Text possibleIntentsTextMesh;
[SerializeField]
private TMP_Text recognizedIntentTextMesh;
[SerializeField]
private TMP_Text failedIntentRecognitionTextMesh;
#endregion
#region Public Properties
#endregion
#region Private Properties
private ViaggioAIManager vaim { get { return ViaggioAIManager.Instance; } }
#endregion
#region Framework Functions
async void OnEnable()
{
this.clearTexts();
while (vaim == null)
{
await Task.Delay(10);
}
this.subscribeToEvents();
}
void OnDisable()
{
this.unsubscribeFromEvents();
}
#endregion
#region Events
private void subscribeToEvents()
{
// State
vaim.OnViaggioAIStateChangedEvent += this.onViaggioAIStateChanged;
// Speech Input
vaim.OnUserSpeechInputStartedEvent += this.onUserSpeechInputStarted;
vaim.OnPartialTranscriptionChangedEvent += this.onPartialTranscriptionChanged;
vaim.OnFullTranscriptionChangedEvent += this.onFullTranscriptionChanged;
vaim.OnUserSpeechInputEndedEvent += this.onUserSpeechInputEnded;
// Speech Output
vaim.OnSpeechOutputStartedEvent += this.onSpeechOutputStarted;
vaim.OnSpeechOutputEndedEvent += this.onSpeechOutputEnded;
// Intent Recognition
vaim.OnIntentRecognitionInitiatedEvent += this.onIntentRecognitionInitiated;
vaim.OnIntentRecognitionSucceededEvent += this.onIntentRecognitionSucceeded;
vaim.OnIntentRecognitionFailedEvent += this.onIntentRecognitionFailed;
}
private void unsubscribeFromEvents()
{
if (vaim != null)
{
// State
vaim.OnViaggioAIStateChangedEvent -= this.onViaggioAIStateChanged;
// Speech Input
vaim.OnUserSpeechInputStartedEvent -= this.onUserSpeechInputStarted;
vaim.OnPartialTranscriptionChangedEvent -= this.onPartialTranscriptionChanged;
vaim.OnFullTranscriptionChangedEvent -= this.onFullTranscriptionChanged;
vaim.OnUserSpeechInputEndedEvent -= this.onUserSpeechInputEnded;
// Speech Output
vaim.OnSpeechOutputStartedEvent -= this.onSpeechOutputStarted;
vaim.OnSpeechOutputEndedEvent -= this.onSpeechOutputEnded;
// Intent Recognition
vaim.OnIntentRecognitionInitiatedEvent -= this.onIntentRecognitionInitiated;
vaim.OnIntentRecognitionSucceededEvent -= this.onIntentRecognitionSucceeded;
vaim.OnIntentRecognitionFailedEvent -= this.onIntentRecognitionFailed;
}
}
private void onViaggioAIStateChanged(object sender, EViaggioAIState newState)
{
if (newState == EViaggioAIState.Listening)
{
this.clearTexts();
}
this.updatePossibleIntentsText();
this.stateTextMesh.text = newState.ToString();
}
private void onUserSpeechInputStarted(object sender, object e)
{
this.lastThrownEventTextMesh.text = "OnUserSpeechInputStarted";
}
private void onPartialTranscriptionChanged(object sender, string partialTranscription)
{
this.lastThrownEventTextMesh.text = "OnPartialTranscriptionChanged";
this.recognziedTextTextMesh.text = partialTranscription;
}
private void onFullTranscriptionChanged(object sender, string fullTranscription)
{
this.lastThrownEventTextMesh.text = "OnFullTranscriptionChanged";
this.recognziedTextTextMesh.text = fullTranscription;
}
private void onUserSpeechInputEnded(object sender, object e)
{
this.lastThrownEventTextMesh.text = "OnUserSpeechInputEnded";
}
private void onSpeechOutputStarted(object sender, bool e)
{
this.lastThrownEventTextMesh.text = "OnSpeechOutputStarted";
}
private void onSpeechOutputEnded(object sender, bool e)
{
this.lastThrownEventTextMesh.text = "OnSpeechOutputEnded";
}
private void onIntentRecognitionInitiated(object sender, bool e)
{
this.lastThrownEventTextMesh.text = "OnIntentRecognitionInitiated";
this.updatePossibleIntentsText();
}
private void onIntentRecognitionSucceeded(object sender, string intentID)
{
this.lastThrownEventTextMesh.text = "OnIntentRecognitionSucceeded";
this.recognizedIntentTextMesh.text = intentID;
}
private void onIntentRecognitionFailed(object sender, bool e)
{
this.lastThrownEventTextMesh.text = "OnIntentRecognitionFailed";
this.failedIntentRecognitionTextMesh.text = "No intent recognized";
}
#endregion
#region Public Functions
#endregion
#region Private Functions
private void clearTexts()
{
this.stateTextMesh.text = "";
this.lastThrownEventTextMesh.text = "";
this.recognziedTextTextMesh.text = "";
this.possibleIntentsTextMesh.text = "";
this.recognizedIntentTextMesh.text = "";
this.failedIntentRecognitionTextMesh.text = "";
}
private void updatePossibleIntentsText()
{
RequestDataModel intentRequestDataModel = vaim.PendingRequestDataModel;
if (intentRequestDataModel == null)
{
this.possibleIntentsTextMesh.text = "";
return;
}
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> keyValuePair in intentRequestDataModel.PossibleIntents)
{
sb.AppendLine($"{keyValuePair.Key}: \"{keyValuePair.Value}\"");
}
this.possibleIntentsTextMesh.text = sb.ToString();
}
#endregion
}