| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public class LimitInputField : MonoBehaviour |
| | 9 | | { |
| | 10 | | public event System.Action OnLimitReached; |
| | 11 | | public event System.Action OnEmptyValue; |
| | 12 | | public event System.Action OnInputAvailable; |
| | 13 | | public event System.Action OnInputFocused; |
| | 14 | | public event System.Action OnInputLostFocus; |
| | 15 | |
|
| | 16 | | public event System.Action<string> OnInputChange; |
| | 17 | |
|
| | 18 | | [SerializeField] private int characterLimit; |
| | 19 | | [SerializeField] private bool isMandatory = false; |
| | 20 | | [SerializeField] private bool stopInputFromLimit = false; |
| 72 | 21 | | [SerializeField] private string placeHolderText = ""; |
| | 22 | | [SerializeField] private Color normalColor; |
| | 23 | | [SerializeField] private Color errorColor; |
| | 24 | | [SerializeField] private Color focusColor; |
| | 25 | |
|
| | 26 | | [SerializeField] private Image inputFieldbackgroundImg; |
| | 27 | | [SerializeField] private TMP_InputField inputField; |
| | 28 | | [SerializeField] private TextMeshProUGUI inputFieldPlaceHolderText; |
| | 29 | | [SerializeField] private TextMeshProUGUI limitText; |
| | 30 | |
|
| | 31 | | internal bool hasPassedLimit = false; |
| | 32 | | internal bool hasBeenEmpty = false; |
| | 33 | | internal bool hasFocus = false; |
| 72 | 34 | | private string currentValue = ""; |
| | 35 | |
|
| | 36 | | private void Start() |
| | 37 | | { |
| 38 | 38 | | inputField.onSelect.AddListener(InputFocused); |
| 38 | 39 | | inputField.onDeselect.AddListener(InputLostFocus); |
| 38 | 40 | | inputField.onValueChanged.AddListener(InputChanged); |
| 38 | 41 | | limitText?.SetText( currentValue.Length + "/" + characterLimit); |
| 38 | 42 | | if (isMandatory) |
| 29 | 43 | | hasBeenEmpty = true; |
| 38 | 44 | | } |
| | 45 | |
|
| 0 | 46 | | public bool HasFocus() => hasFocus; |
| | 47 | |
|
| | 48 | | public void SetCharacterLimit(int limit) |
| | 49 | | { |
| 0 | 50 | | characterLimit = limit; |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void SetText(string value) |
| | 54 | | { |
| 2 | 55 | | if(string.IsNullOrEmpty(value) && inputFieldPlaceHolderText != null) |
| 0 | 56 | | inputFieldPlaceHolderText.text = placeHolderText; |
| | 57 | |
|
| 2 | 58 | | currentValue = value; |
| 2 | 59 | | inputField.SetTextWithoutNotify(value); |
| 2 | 60 | | InputChanged(value, false); |
| 2 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | public void SetError() { LimitReached(); } |
| | 64 | |
|
| | 65 | | private void InputLostFocus(string value) |
| | 66 | | { |
| 0 | 67 | | hasFocus = false; |
| 0 | 68 | | if(string.IsNullOrEmpty(currentValue) && inputFieldPlaceHolderText != null) |
| 0 | 69 | | inputFieldPlaceHolderText.text = placeHolderText; |
| | 70 | |
|
| 0 | 71 | | if (hasPassedLimit) |
| | 72 | | { |
| 0 | 73 | | inputFieldbackgroundImg.enabled = true; |
| 0 | 74 | | inputFieldbackgroundImg.color = errorColor; |
| 0 | 75 | | } |
| | 76 | | else |
| | 77 | | { |
| 0 | 78 | | inputFieldbackgroundImg.enabled = false; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | OnInputLostFocus?.Invoke(); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | private void InputFocused(string value) |
| | 85 | | { |
| 0 | 86 | | if(inputFieldPlaceHolderText != null) |
| 0 | 87 | | inputFieldPlaceHolderText.text = ""; |
| 0 | 88 | | hasFocus = true; |
| | 89 | |
|
| 0 | 90 | | inputFieldbackgroundImg.enabled = true; |
| | 91 | |
|
| 0 | 92 | | if (hasPassedLimit) |
| 0 | 93 | | inputFieldbackgroundImg.color = errorColor; |
| | 94 | | else |
| 0 | 95 | | inputFieldbackgroundImg.color = focusColor; |
| | 96 | |
|
| 0 | 97 | | OnInputFocused?.Invoke(); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | internal void InputChanged(string newValue) |
| | 101 | | { |
| 1 | 102 | | InputChanged(newValue, true); |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | internal void InputChanged(string newValue, bool invokeCallback) |
| | 106 | | { |
| 3 | 107 | | if (hasPassedLimit && newValue.Length > currentValue.Length && stopInputFromLimit) |
| | 108 | | { |
| 0 | 109 | | inputField.SetTextWithoutNotify(currentValue); |
| 0 | 110 | | return; |
| | 111 | | } |
| 3 | 112 | | currentValue = newValue; |
| 3 | 113 | | limitText?.SetText(newValue.Length + "/" + characterLimit); |
| 3 | 114 | | if (newValue.Length > characterLimit) |
| | 115 | | { |
| 0 | 116 | | LimitReached(); |
| 0 | 117 | | } |
| 3 | 118 | | else if (currentValue.Length == 0) |
| | 119 | | { |
| 0 | 120 | | Empty(); |
| 0 | 121 | | } |
| | 122 | | else |
| | 123 | | { |
| 3 | 124 | | InputAvailable(); |
| | 125 | | } |
| | 126 | |
|
| 3 | 127 | | if(invokeCallback) |
| 1 | 128 | | OnInputChange?.Invoke(newValue); |
| 3 | 129 | | } |
| | 130 | |
|
| 0 | 131 | | public bool IsInputAvailable() => !hasPassedLimit; |
| | 132 | |
|
| | 133 | | public void InputAvailable() |
| | 134 | | { |
| 8 | 135 | | if (!hasPassedLimit && !hasBeenEmpty) |
| 7 | 136 | | return; |
| | 137 | |
|
| 1 | 138 | | if (limitText != null) |
| 1 | 139 | | limitText.color = normalColor; |
| | 140 | |
|
| 1 | 141 | | if (hasFocus) |
| | 142 | | { |
| 0 | 143 | | inputFieldbackgroundImg.enabled = true; |
| 0 | 144 | | inputFieldbackgroundImg.color = focusColor; |
| 0 | 145 | | } |
| | 146 | | else |
| | 147 | | { |
| 1 | 148 | | inputFieldbackgroundImg.enabled = false; |
| | 149 | | } |
| | 150 | |
|
| 1 | 151 | | hasPassedLimit = false; |
| 1 | 152 | | hasBeenEmpty = false; |
| | 153 | |
|
| 1 | 154 | | OnInputAvailable?.Invoke(); |
| 1 | 155 | | } |
| | 156 | |
|
| | 157 | | internal void Empty() |
| | 158 | | { |
| 1 | 159 | | if (hasBeenEmpty) |
| 0 | 160 | | return; |
| | 161 | |
|
| 1 | 162 | | hasBeenEmpty = true; |
| 1 | 163 | | OnEmptyValue?.Invoke(); |
| 1 | 164 | | } |
| | 165 | |
|
| | 166 | | public void LimitReached() |
| | 167 | | { |
| 1 | 168 | | if (hasPassedLimit) |
| 0 | 169 | | return; |
| | 170 | |
|
| 1 | 171 | | if (limitText != null) |
| 1 | 172 | | limitText.color = errorColor; |
| 1 | 173 | | inputFieldbackgroundImg.enabled = true; |
| 1 | 174 | | inputFieldbackgroundImg.color = errorColor; |
| | 175 | |
|
| 1 | 176 | | OnLimitReached?.Invoke(); |
| 1 | 177 | | hasPassedLimit = true; |
| 1 | 178 | | } |
| | 179 | |
|
| 0 | 180 | | public string GetValue() { return currentValue; } |
| | 181 | | } |