using Convai.Scripts.Runtime.LoggerSystem; using TMPro; using UnityEngine; namespace Convai.Scripts.Runtime.UI { /// /// SubtitleChatUI is responsible for displaying subtitles on the screen. /// It inherits from ChatUIBase and overrides methods to provide specific functionality for subtitle UI. /// public class SubtitleChatUI : ChatUIBase { private GameObject _feedbackButtons; private Message _subtitle; /// /// Initializes the subtitle UI with the provided prefab. /// /// The UI prefab to instantiate. public override void Initialize(GameObject uiPrefab) { // Instantiate the UI prefab and get the subtitle text component UIInstance = Instantiate(uiPrefab); _subtitle = new Message { SenderTextObject = UIInstance.transform.Find("Background").Find("ChatBox").Find("Subtitle").Find("Sender").GetComponent(), MessageTextObject = UIInstance.transform.Find("Background").Find("ChatBox").Find("Subtitle").Find("Text").GetComponent() }; // Start with the UI inactive UIInstance.SetActive(false); _feedbackButtons = _subtitle.MessageTextObject.transform.GetChild(0).gameObject; } /// /// Sends the character's text to the subtitle UI. /// /// 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) { // Update the subtitle text with formatted character dialogue. _feedbackButtons.SetActive(false); UpdateSubtitleText(charName, text, characterTextColor); _feedbackButtons.SetActive(true); } /// /// Sends the player's text to the subtitle UI. /// /// 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) { // Update the subtitle text with formatted player dialogue. UpdateSubtitleText(playerName, text, playerTextColor); _feedbackButtons.SetActive(false); } /// /// Updates the subtitle text with the provided speaker's name, text, and color. /// /// The name of the speaker. /// The text spoken by the speaker. /// The color associated with the speaker. private void UpdateSubtitleText(string speakerName, string text, Color color) { if (string.IsNullOrEmpty(text)) return; // Check if the subtitle text component is available before updating. if (_subtitle != null) { _subtitle.SenderTextObject.text = FormatSpeakerName(speakerName, color); _subtitle.MessageTextObject.text = text; _subtitle.RTLUpdate(); } else { ConvaiLogger.Warn("Subtitle text component is not available.", ConvaiLogger.LogCategory.UI); } } /// /// Formats the speaker's name with the color tag. /// /// The name of the speaker. /// The color associated with the speaker. /// The formatted speaker name private string FormatSpeakerName(string speakerName, Color color) { // Convert the color to a hex string for HTML color formatting. string colorHex = ColorUtility.ToHtmlStringRGB(color); // Return the formatted text with the speaker's name and color. return $"{speakerName}: "; } } }