using TMPro;
using UnityEngine;
namespace Convai.Scripts.Utils
{
///
/// The QuestionAnswerUI class is responsible for managing the UI elements
/// that display questions and answers in a conversational interface.
///
public class QuestionAnswerUI : ChatUIBase
{
private TextMeshProUGUI _answerText;
private TextMeshProUGUI _questionText;
private GameObject _feedbackButtons;
///
/// Initializes the UI with the provided prefab.
///
/// The UI prefab to instantiate.
public override void Initialize(GameObject uiPrefab)
{
UIInstance = Instantiate(uiPrefab);
_questionText = UIInstance.transform.Find("Background").Find("QuestionText")
.GetComponent();
_answerText = UIInstance.transform.Find("Background").Find("AnswerBox").Find("AnswerText").GetComponent();
UIInstance.SetActive(false);
_feedbackButtons = _answerText.transform.GetChild(0).gameObject;
}
///
/// Sends the character's text to the UI, formatted with the character's color.
///
/// The name of the character speaking.
/// The text spoken by the character.
/// The color associated with the character.
public override void SendCharacterText(string charName, string text, Color characterTextColor)
{
if (_answerText != null)
{
_feedbackButtons.SetActive(false);
_answerText.text = FormatDialogueText(charName, text, characterTextColor);
_feedbackButtons.SetActive(true);
}
}
///
/// Sends the player's text to the UI, formatted with the player's color.
///
/// The name of the player speaking.
/// The text spoken by the player.
/// The color associated with the player.
public override void SendPlayerText(string playerName, string text, Color playerTextColor)
{
if (_questionText != null)
{
_questionText.text = FormatDialogueText(playerName, text, playerTextColor);
_feedbackButtons.SetActive(false);
}
}
///
/// Formats the dialogue text with the speaker's name and color.
///
/// The name of the speaker.
/// The text spoken by the speaker.
/// The color associated with the speaker.
/// Formatted dialogue text.
private string FormatDialogueText(string speakerName, string text, Color speakerColor)
{
string colorHex = ColorUtility.ToHtmlStringRGBA(speakerColor);
return $"{speakerName}: {text}";
}
}
}