// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; using UnityEngine.Events; namespace PixelCrushers.DialogueSystem { /// /// Unity UI text field UI implementation. /// [AddComponentMenu("")] // Use wrapper. public class UnityUITextFieldUI : MonoBehaviour, ITextFieldUI { /// /// The (optional) panel. If your text field UI contains more than a label and text field, you should /// assign the panel, too. /// [Tooltip("Optional panel containing the UI elements")] public UnityEngine.UI.Graphic panel; /// /// The label that will contain any label text prompting the user what to enter. /// [Tooltip("Optional text element for prompt")] public UnityEngine.UI.Text label; /// /// The text field. /// public UnityEngine.UI.InputField textField; /// /// The accept key. /// [Tooltip("Optional key code that accepts the input")] public KeyCode acceptKey = KeyCode.Return; /// /// The cancel key. /// [Tooltip("Optional key code that cancels the input")] public KeyCode cancelKey = KeyCode.Escape; [Tooltip("Automatically open touchscreen keyboard")] public bool showTouchScreenKeyboard = false; public UnityEvent onAccept = new UnityEvent(); public UnityEvent onCancel = new UnityEvent(); /// /// This delegate must be called when the player accepts the input in the text field. /// private AcceptedTextDelegate acceptedText = null; private bool isAwaitingInput = false; private TouchScreenKeyboard touchScreenKeyboard = null; private void Awake() { Tools.DeprecationWarning(this); } void Start() { if (DialogueDebug.logWarnings && (textField == null)) Debug.LogWarning(string.Format("{0}: No InputField is assigned to the text field UI {1}. TextInput() sequencer commands or [var?=] won't work.", new object[] { DialogueDebug.Prefix, name })); Hide(); } /// /// Starts the text input field. /// /// 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.characterLimit = maxLength; } this.acceptedText = acceptedText; Show(); isAwaitingInput = true; } public void Update() { if (isAwaitingInput && !DialogueManager.IsDialogueSystemInputDisabled()) { if (Input.GetKeyDown(acceptKey)) { AcceptTextInput(); } else if (Input.GetKeyDown(cancelKey)) { CancelTextInput(); } } } /// /// Cancels the text input field. /// public void CancelTextInput() { isAwaitingInput = false; Hide(); onCancel.Invoke(); } /// /// Accepts the text input and calls the accept handler delegate. /// public void AcceptTextInput() { isAwaitingInput = false; if (acceptedText != null) { if (textField != null) acceptedText(textField.text); acceptedText = null; } Hide(); onAccept.Invoke(); } private void Show() { SetActive(true); if (textField != null) { if (showTouchScreenKeyboard) touchScreenKeyboard = TouchScreenKeyboard.Open(textField.text); textField.ActivateInputField(); if (UnityEngine.EventSystems.EventSystem.current != null) { UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(textField.gameObject, null); } } } private void Hide() { SetActive(false); if (touchScreenKeyboard != null) { touchScreenKeyboard.active = false; touchScreenKeyboard = null; } } private void SetActive(bool value) { if (textField != null) textField.enabled = value; if (panel != null) { Tools.SetGameObjectActive(panel, value); } else { Tools.SetGameObjectActive(label, value); Tools.SetGameObjectActive(textField, value); } } } }