using TMPro; using UnityEngine; namespace Convai.Scripts.Utils { /// /// 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 TextMeshProUGUI _subtitleText; private GameObject _feedbackButtons; /// /// 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); _subtitleText = UIInstance.GetComponentInChildren(); // Start with the UI inactive UIInstance.SetActive(false); _feedbackButtons = _subtitleText.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) { // Check if the subtitle text component is available before updating. if (_subtitleText != null) _subtitleText.text = FormatText(speakerName, text, color); else Debug.LogError("Subtitle text component not found."); } /// /// Formats the 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. /// The formatted text string. private string FormatText(string speakerName, string text, 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}: {text}"; } } }