// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; namespace PixelCrushers.DialogueSystem { /// /// A Unity UI response button for use with UnityUIDialogueControls. Add this component to every /// response button in the dialogue UI. /// [AddComponentMenu("")] // Use wrapper. public class UnityUIResponseButton : MonoBehaviour { /// /// The UnityUI button. /// public UnityEngine.UI.Button button; /// /// The UnityUI label that will display the response text. /// public UnityEngine.UI.Text label; /// /// The default color for response text. /// [Tooltip("Set the button's text to this color by default")] public Color defaultColor = Color.white; /// /// Set true to set the button color when applying emphasis tags. /// [Tooltip("Apply emphasis tag colors to the button background")] public bool setButtonColor = false; /// /// Set true to set the label color when applying emphasis tags. /// [Tooltip("Apply emphasis tag colors to the button text")] public bool setLabelColor = true; /// /// Gets or sets the response text. /// /// /// The text. /// public string Text { get { return (label != null) ? label.text : string.Empty; } set { if (label != null) { label.text = UITools.StripRPGMakerCodes(value); UITools.SendTextChangeMessage(label); } else { if (DialogueDebug.logErrors) Debug.LogError(string.Format("{0}: No Text UI element is unassigned on {1}", new object[] { DialogueDebug.Prefix, name })); } } } /// /// Indicates whether the button is an allowable response. /// /// /// true if clickable; otherwise, false. /// public bool clickable { get { return (button != null) && button.interactable; } set { if (button != null) button.interactable = value; } } /// /// Indicates whether the button is shown or not. /// /// /// true if visible; otherwise, false. /// public bool visible { get; set; } /// /// Gets or sets the response associated with this button. If the player clicks this /// button, this response is sent back to the dialogue system. /// /// /// The response. /// public Response response { get; set; } /// /// Gets or sets the target that will receive click notifications. /// /// /// The target. /// public Transform target { get; set; } /// /// Clears the button. /// public void Reset() { Text = string.Empty; clickable = false; visible = false; response = null; SetColor(defaultColor); } public void Awake() { if (button == null) button = GetComponent(); if (button == null) Debug.LogWarning("Dialogue System: Response button '" + name + "' is missing a Unity UI Button component!", this); Tools.DeprecationWarning(this); } /// /// Sets the button's text using the specified formatted text. /// /// /// The formatted text for the button label. /// public void SetFormattedText(FormattedText formattedText) { if (formattedText != null) { Text = UITools.GetUIFormattedText(formattedText); SetColor((formattedText.emphases.Length > 0) ? formattedText.emphases[0].color : defaultColor); } } /// /// Sets the button's text using plain text. /// /// /// Unformatted text for the button label. /// public void SetUnformattedText(string unformattedText) { Text = unformattedText; SetColor(defaultColor); } protected virtual void SetColor(Color currentColor) { if (button != null) { //if (setButtonColor) button.defaultColor = currentColor; } else { if (DialogueDebug.logWarnings) Debug.LogWarning(string.Format("{0}: No Button is assigned to {1}", new object[] { DialogueDebug.Prefix, name })); } if (label != null) { if (setLabelColor) label.color = currentColor; } else { if (DialogueDebug.logWarnings) Debug.LogWarning(string.Format("{0}: No Text is assigned to {1}", new object[] { DialogueDebug.Prefix, name })); } } /// /// Handles a button click by calling the response handler. /// public void OnClick() { if (target != null) target.SendMessage("OnClick", response, SendMessageOptions.RequireReceiver); } } }