namespace UIWidgets { using UIWidgets.Styles; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; /// /// InputField with exposed functions to validate input. /// public class InputFieldExtended : InputField, IInputFieldExtended { #if !(UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER) /// /// Current InputField caret position (also selection tail). /// /// The caret position. int IInputFieldProxy.caretPosition { get { return base.caretPosition; } set { base.caretPosition = value; } } #endif /// /// Function to validate input. /// public OnValidateInput Validation { get; set; } /// /// Function to process changed value. /// public UnityAction ValueChanged { get; set; } /// /// The Unity Event to call when edit. /// /// The OnValueChange. UnityEvent IInputFieldProxy.onValueChanged { get { #if UNITY_5_3 || UNITY_5_3_OR_NEWER return onValueChanged; #else return onValueChange; #endif } } /// /// Function to process end edit. /// public UnityAction ValueEndEdit { get; set; } /// /// The Unity Event to call when editing has ended. /// /// The OnEndEdit. UnityEvent IInputFieldProxy.onEndEdit { get { return onEndEdit; } } /// /// Current value. /// public string Value { get { return text; } set { #if UNITY_5_3_4 || UNITY_5_3_OR_NEWER if (text != value) { text = value; UpdateLabel(); } #else text = value; #endif } } /// /// Start selection position. /// public int SelectionStart { get { #if UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER return Mathf.Min(selectionAnchorPosition, selectionFocusPosition); #else return Mathf.Min(caretSelectPos, caretPosition); #endif } } /// /// End selection position. /// public int SelectionEnd { get { #if UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER return Mathf.Max(selectionAnchorPosition, selectionFocusPosition); #else return Mathf.Max(caretSelectPos, caretPosition); #endif } } /// /// Text component. /// Graphic IInputFieldProxy.textComponent { get { return textComponent; } set { textComponent = value as Text; } } static char NoValidation(string text, int charIndex, char addedChar) { return addedChar; } static void DoNothing(string value) { } /// /// Start this instance. /// protected override void Start() { base.Start(); if (Validation == null) { Validation = NoValidation; } if (ValueChanged == null) { ValueChanged = DoNothing; } if (ValueEndEdit == null) { ValueEndEdit = DoNothing; } onValidateInput = ProcessValidation; #if UNITY_5_3 || UNITY_5_3_OR_NEWER onValueChanged.AddListener(ProcessValueChanged); #else onValueChange.AddListener(ProcessValueChanged); #endif onEndEdit.AddListener(ProcessValueEndEdit); } char ProcessValidation(string validateText, int charIndex, char addedChar) { return Validation(validateText, charIndex, addedChar); } void ProcessValueChanged(string text) { ValueChanged(text); } void ProcessValueEndEdit(string text) { ValueEndEdit(text); } /// /// Determines whether InputField instance is null. /// /// true if InputField instance is null; otherwise, false. public bool IsNull() { return this != null; } /// /// Determines whether this lineType is LineType.MultiLineNewline. /// /// true if lineType is LineType.MultiLineNewline; otherwise, false. public bool IsMultiLineNewline() { return lineType == LineType.MultiLineNewline; } #if UNITY_4_6 || UNITY_4_7 || UNITY_5_0 /// /// Move caret to end. /// public void MoveToEnd() { MoveTextStart(false); MoveTextEnd(false); } #endif /// /// Set focus to InputField. /// public void Focus() { ActivateInputField(); Select(); } /// /// Remove callbacks. /// protected override void OnDestroy() { base.OnDestroy(); #if UNITY_5_3 || UNITY_5_3_OR_NEWER onValueChanged.RemoveListener(ProcessValueChanged); #else onValueChange.RemoveListener(ProcessValueChanged); #endif onEndEdit.RemoveListener(ProcessValueEndEdit); } /// public virtual void SetStyle(StyleSpinner styleSpinner, Style style) { styleSpinner.InputText.ApplyTo(textComponent, true); if (placeholder != null) { styleSpinner.InputPlaceholder.ApplyTo(placeholder.gameObject, true); } } /// public virtual void GetStyle(StyleSpinner styleSpinner, Style style) { styleSpinner.InputText.GetFrom(textComponent, true); if (placeholder != null) { styleSpinner.InputPlaceholder.GetFrom(placeholder.gameObject, true); } } } }