- using TMPro;
- using UnityEngine;
- using MyCollections.Interfaces;
- public class AmountInserter : MonoBehaviour
- {
- [SerializeField] private TMP_InputField inputField;
- [SerializeField] private InputType inputType;
- [SerializeField] private int amount;
- [SerializeField] private int index;
- private GameObject receiver;
-
- void Start()
- {
- inputField.lineType = TMP_InputField.LineType.SingleLine;
- inputField.onValueChanged.AddListener(delegate
- {
- Validate();
- });
- inputField.onSubmit.AddListener(delegate { Submit(); });
- }
-
- void Update()
- {
-
-
-
-
-
- }
- public void Init(GameObject obj, int limitAmount, int index)
- {
- amount = limitAmount;
- receiver = obj;
- this.index = index;
- }
- private void Submit()
- {
- if (receiver.TryGetComponent(out IReceiver<int, int> destination))
- {
- switch (inputType)
- {
- case InputType.Numbers:
- if (int.TryParse(inputField.text, out int result))
- {
- destination.Receive(result, index);
- Destroy(gameObject);
- }
- break;
- }
- }
- }
- private void Validate()
- {
- string text = inputField.text;
- if (int.TryParse(text, out int result))
- {
- if (result > amount)
- {
- inputField.text = amount.ToString();
- return;
- }
- inputField.text = result.ToString();
- return;
- }
- Debug.LogError("Couldn't Parse!");
- }
- public void ClosePanel() => Destroy(gameObject);
- }
- public enum InputType
- {
- Text,
- Numbers
- }