using UnityEngine; namespace PixelCrushers.DialogueSystem.UnityGUI { /// /// This is a Unity GUI implementation of ITextFieldUI. It uses GUITextField for text input. It also supports a /// panel that can contain the text field and other controls such as Ok and Cancel buttons. The Ok button should /// send an OnAccept message to this object. The Cancel button should send OnCancel. /// [AddComponentMenu("")] // Deprecated public class UnityTextFieldUI : MonoBehaviour, ITextFieldUI { /// /// The (optional) panel. If your text field UI contains more than a label and text field, you should /// assign the panel, too. /// public GUIControl panel; /// /// The label that will contain any label text prompting the user what to enter. /// public GUILabel label; /// /// The text field. /// public GUITextField textField; /// /// The accept key. /// public KeyCode acceptKey = KeyCode.Return; /// /// The cancel key. /// public KeyCode cancelKey = KeyCode.None; private AcceptedTextDelegate acceptedText = null; private GUIControl control; /// /// If the text field starts with the accept key in the down position, /// we need to ignore the first accept key event. Otherwise it will /// immediately accept the input. /// private bool ignoreFirstAccept = false; /// /// If the text field starts with the cancel key in the down position, /// we need to ignore the first cancel key event. Otherwise it will /// immediately cancel the input. /// private bool ignoreFirstCancel = false; public void Awake() { control = GetComponent(); if (control == null) control = gameObject.AddComponent(); control.visible = false; } /// /// Sets up the text input controls. /// /// The label text. /// The current value to use for the input field. /// Max length, or 0 for unlimited. /// The delegate to call when accepting text. public void StartTextInput(string labelText, string text, int maxLength, AcceptedTextDelegate acceptedText) { if (label != null) label.text = labelText; if (textField != null) { textField.text = text; textField.maxLength = maxLength; textField.TakeFocus(); ignoreFirstAccept = (acceptKey != KeyCode.None) && Input.GetKeyDown(acceptKey); ignoreFirstCancel = (cancelKey != KeyCode.None) && Input.GetKeyDown(cancelKey); } this.acceptedText = acceptedText; Show(); } /// /// If the text field is active, this method handles accept/cancel keypresses. /// void OnGUI() { if (control.visible) { if (textField != null) textField.TakeFocus(); if (IsAcceptKey()) { if (ignoreFirstAccept) { ignoreFirstAccept = false; } else { Event.current.Use(); AcceptTextInput(); } } else if (Event.current.isKey) { if ((cancelKey != KeyCode.None) && (Event.current.keyCode == cancelKey)) { if (ignoreFirstCancel) { ignoreFirstCancel = false; } else { Event.current.Use(); CancelTextInput(); } } } } } /// /// This method includes special handling for Enter/Return to handle Mac webplayers. /// /// true if the current event is the accept key; otherwise, false. private bool IsAcceptKey() { if (IsKeyCodeReturn(acceptKey)) { return Event.current.Equals(Event.KeyboardEvent("[enter]")) || Event.current.Equals(Event.KeyboardEvent("return")) || (Event.current.isKey && (Event.current.keyCode == KeyCode.KeypadEnter)) || (Event.current.isKey && (Event.current.keyCode == KeyCode.Return)) || ((Event.current.type == EventType.KeyDown) && (Event.current.character == '\n')); } else { return (acceptKey != KeyCode.None) && (Event.current.keyCode == acceptKey); } } private bool IsKeyCodeReturn(KeyCode keyCode) { return (keyCode == KeyCode.KeypadEnter) || (keyCode == KeyCode.Return); } /// /// Cancels the text input by hiding the controls. /// /// true if this instance cancel text input; otherwise, false. public void CancelTextInput() { Hide(); } /// /// Accepts the text input and calls the accept handler delegate. /// private void AcceptTextInput() { Hide(); if (acceptedText != null) { if (IsKeyCodeReturn(acceptKey)) textField.text = textField.text.Replace("\n", ""); if (textField != null) acceptedText(textField.text); acceptedText = null; } } /// /// This is received from the Ok/Accept button if it exists. It simply accepts the text. /// public void OnAccept(object data) { AcceptTextInput(); } /// /// This is received from the Cancel button if it exists. It cancels the input. /// public void OnCancel(object data) { CancelTextInput(); } private void Show() { SetControlsActive(true); } private void Hide() { SetControlsActive(false); } private void SetControlsActive(bool value) { control.visible = value; UnityDialogueUIControls.SetControlActive(label, value); UnityDialogueUIControls.SetControlActive(textField, value); UnityDialogueUIControls.SetControlActive(panel, value); } } }