using TMPro;
using UnityEngine;
namespace Convai.Scripts.Runtime.UI
{
///
/// The QuestionAnswerUI class is responsible for managing the UI elements
/// that display questions and answers in a conversational interface.
///
public class QuestionAnswerUI : ChatUIBase
{
private Message _answer;
private GameObject _feedbackButtons;
private Message _question;
///
/// Initializes the UI with the provided prefab.
///
/// The UI prefab to instantiate.
public override void Initialize(GameObject uiPrefab)
{
UIInstance = Instantiate(uiPrefab);
_question = new Message
{
SenderTextObject = UIInstance.transform.Find("Background").Find("Question").Find("Sender").GetComponent(),
MessageTextObject = UIInstance.transform.Find("Background").Find("Question").Find("Text").GetComponent()
};
_answer = new Message
{
SenderTextObject = UIInstance.transform.Find("Background").Find("Answer").Find("Sender").GetComponent(),
MessageTextObject = UIInstance.transform.Find("Background").Find("Answer").Find("AnswerText").Find("Text").GetComponent()
};
UIInstance.SetActive(false);
_feedbackButtons = _answer.MessageTextObject.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 (string.IsNullOrEmpty(text)) return;
if (_answer != null)
{
_feedbackButtons.SetActive(false);
_answer.SenderTextObject.text = FormatSpeakerName(charName, characterTextColor);
_answer.MessageTextObject.text = text;
_answer.RTLUpdate();
_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 (_question != null)
{
_question.SenderTextObject.text = FormatSpeakerName(playerName, playerTextColor);
_question.MessageTextObject.text = text;
_answer.RTLUpdate();
_feedbackButtons.SetActive(false);
}
}
///
/// Formats the speaker's name with the color tag
///
/// The name of the speaker.
/// The color associated with the speaker.
/// Formatted speaker name.
private string FormatSpeakerName(string speakerName, Color speakerColor)
{
string colorHex = ColorUtility.ToHtmlStringRGBA(speakerColor);
return $"{speakerName}: ";
}
}
}