| | 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 | |
|
| | 14 | | public event System.Action<string> OnInputChange; |
| | 15 | |
|
| | 16 | | [SerializeField] private int characterLimit; |
| | 17 | | [SerializeField] private bool isMandatory = false; |
| | 18 | | [SerializeField] private Color normalColor; |
| | 19 | | [SerializeField] private Color errorColor; |
| | 20 | |
|
| | 21 | | [SerializeField] private Sprite inputFieldNormalBackgroundSprite; |
| | 22 | | [SerializeField] private Sprite inputFieldErrorBackgroundSprite; |
| | 23 | | [SerializeField] private Sprite inputFieldFocusBackgroundSprite; |
| | 24 | |
|
| | 25 | | [SerializeField] private Image inputFieldbackgroundImg; |
| | 26 | | [SerializeField] private TMP_InputField inputField; |
| | 27 | | [SerializeField] private TextMeshProUGUI limitText; |
| | 28 | |
|
| | 29 | | internal bool hasPassedLimit = false; |
| | 30 | | internal bool hasBeenEmpty = false; |
| 52 | 31 | | private string currentValue = ""; |
| | 32 | |
|
| | 33 | | private void Start() |
| | 34 | | { |
| 8 | 35 | | inputField.onSelect.AddListener(InputFocused); |
| 8 | 36 | | inputField.onDeselect.AddListener(InputLostFocus); |
| 8 | 37 | | inputField.onValueChanged.AddListener(InputChanged); |
| 8 | 38 | | limitText?.SetText( currentValue.Length + "/" + characterLimit); |
| 8 | 39 | | if (isMandatory) |
| 4 | 40 | | hasBeenEmpty = true; |
| 8 | 41 | | } |
| | 42 | |
|
| 0 | 43 | | public void SetText(string value) { inputField.SetTextWithoutNotify(value); } |
| | 44 | |
|
| 0 | 45 | | public void SetError() { LimitReached(); } |
| | 46 | |
|
| | 47 | | private void InputLostFocus(string value) |
| | 48 | | { |
| 0 | 49 | | if (value.Length > characterLimit) |
| 0 | 50 | | inputFieldbackgroundImg.sprite = inputFieldErrorBackgroundSprite; |
| | 51 | | else |
| 0 | 52 | | inputFieldbackgroundImg.sprite = inputFieldNormalBackgroundSprite; |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | private void InputFocused(string value) |
| | 56 | | { |
| 0 | 57 | | if (value.Length > characterLimit) |
| 0 | 58 | | inputFieldbackgroundImg.sprite = inputFieldErrorBackgroundSprite; |
| | 59 | | else |
| 0 | 60 | | inputFieldbackgroundImg.sprite = inputFieldFocusBackgroundSprite; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | internal void InputChanged(string newValue) |
| | 64 | | { |
| 1 | 65 | | currentValue = newValue; |
| 1 | 66 | | limitText?.SetText(newValue.Length + "/" + characterLimit); |
| 1 | 67 | | if (newValue.Length > characterLimit) |
| 0 | 68 | | LimitReached(); |
| 1 | 69 | | else if (currentValue.Length == 0) |
| 0 | 70 | | Empty(); |
| | 71 | | else |
| 1 | 72 | | InputAvailable(); |
| | 73 | |
|
| 1 | 74 | | OnInputChange?.Invoke(newValue); |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | internal void InputAvailable() |
| | 78 | | { |
| 2 | 79 | | if (!hasPassedLimit && !hasBeenEmpty) |
| 1 | 80 | | return; |
| | 81 | |
|
| 1 | 82 | | if (limitText != null) |
| 1 | 83 | | limitText.color = normalColor; |
| 1 | 84 | | inputFieldbackgroundImg.sprite = inputFieldFocusBackgroundSprite; |
| 1 | 85 | | OnInputAvailable?.Invoke(); |
| 1 | 86 | | hasPassedLimit = false; |
| 1 | 87 | | hasBeenEmpty = false; |
| 1 | 88 | | } |
| | 89 | |
|
| | 90 | | internal void Empty() |
| | 91 | | { |
| 1 | 92 | | if (hasBeenEmpty) |
| 0 | 93 | | return; |
| | 94 | |
|
| 1 | 95 | | hasBeenEmpty = true; |
| 1 | 96 | | OnEmptyValue?.Invoke(); |
| 1 | 97 | | } |
| | 98 | |
|
| | 99 | | internal void LimitReached() |
| | 100 | | { |
| 1 | 101 | | if (hasPassedLimit) |
| 0 | 102 | | return; |
| | 103 | |
|
| 1 | 104 | | if (limitText != null) |
| 1 | 105 | | limitText.color = errorColor; |
| 1 | 106 | | inputFieldbackgroundImg.sprite = inputFieldErrorBackgroundSprite; |
| | 107 | |
|
| 1 | 108 | | OnLimitReached?.Invoke(); |
| 1 | 109 | | hasPassedLimit = true; |
| 1 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | public string GetValue() { return currentValue; } |
| | 113 | | } |