// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; namespace PixelCrushers.DialogueSystem { /// /// StandardDialogueUI input field implementation. /// [AddComponentMenu("")] // Use wrapper. public class StandardUIInputField : UIPanel, ITextFieldUI { [Tooltip("(Optional) Text field panel.")] public UnityEngine.UI.Graphic panel; [Tooltip("(Optional) Text element for prompt.")] public UITextField label; [Tooltip("Input field.")] public UIInputField inputField; [Tooltip("(Optional) Key code that accepts input.")] public KeyCode acceptKey = KeyCode.Return; [Tooltip("(Optional) key code that cancels 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(); /// /// Call this delegate when the player accepts the input in the text field. /// private AcceptedTextDelegate m_acceptedText = null; private bool m_isAwaitingInput = false; private TouchScreenKeyboard m_touchScreenKeyboard = null; protected override void Start() { if (DialogueDebug.logWarnings && (inputField == null)) Debug.LogWarning("Dialogue System: No InputField is assigned to the text field UI " + name + ". TextInput() sequencer commands or [var?=] won't work."); SetActive(false); } /// /// 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) { label.text = labelText; inputField.text = text; inputField.characterLimit = maxLength; m_acceptedText = acceptedText; m_isAwaitingInput = true; Show(); } protected override void Update() { if (m_isAwaitingInput && !DialogueManager.IsDialogueSystemInputDisabled()) { if (InputDeviceManager.IsKeyDown(acceptKey)) { AcceptTextInput(); } else if (InputDeviceManager.IsKeyDown(cancelKey)) { CancelTextInput(); } } } /// /// Cancels the text input field. /// public void CancelTextInput() { m_isAwaitingInput = false; Hide(); onCancel.Invoke(); } /// /// Accepts the text input and calls the accept handler delegate. /// public void AcceptTextInput() { m_isAwaitingInput = false; if (m_acceptedText != null) { if (inputField != null) m_acceptedText(inputField.text); m_acceptedText = null; } Hide(); onAccept.Invoke(); } private void Show() { SetActive(true); Open(); if (showTouchScreenKeyboard) m_touchScreenKeyboard = TouchScreenKeyboard.Open(inputField.text); inputField.ActivateInputField(); EventSystem.current.SetSelectedGameObject(inputField.gameObject); } private void Hide() { Close(); SetActive(false); if (m_touchScreenKeyboard != null) { m_touchScreenKeyboard.active = false; m_touchScreenKeyboard = null; } } private void SetActive(bool value) { if (panel != null) panel.gameObject.SetActive(value); if (panel == null || value == true) { label.SetActive(value); inputField.SetActive(value); } } } }