NumberInput.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. /// <summary>
  8. /// This module prompts the player to input numbers. For example, when a player tries to split an item stack, this module can be used to ask how many items they want to split.
  9. /// </summary>
  10. public class NumberInput : MonoBehaviour
  11. {
  12. #region Variables
  13. public static NumberInput instance;
  14. public delegate void NumberCallback(int _result);
  15. private NumberCallback Callback;
  16. public RectTransform Root;
  17. public NumberPanel NumberPanel;
  18. #endregion
  19. #region Internal Methods
  20. public void ShowInput(int _startValue, int _maxiumValue, RectTransform _rect, NumberCallback _callback)
  21. {
  22. Callback = _callback;
  23. Root.position = _rect.position;
  24. NumberPanel.Initialize(_startValue, _maxiumValue);
  25. Root.gameObject.SetActive(true);
  26. }
  27. public void Confirm()
  28. {
  29. Callback(NumberPanel.GetNumber());
  30. Destroy(gameObject);
  31. }
  32. public void Cancel()
  33. {
  34. Callback(-1);
  35. Destroy(gameObject);
  36. }
  37. private void Update()
  38. {
  39. if (InputProxy.GetKeyDown(KeyCode.Escape))
  40. {
  41. Cancel();
  42. }
  43. }
  44. #endregion
  45. /// <summary>
  46. /// Return if this interface is visible
  47. /// </summary>
  48. /// <returns></returns>
  49. public static bool isVisible()
  50. {
  51. return instance != null;
  52. }
  53. /// <summary>
  54. /// Displays a panel prompting the player to input a number. After the player confirms, the _callback is called with the resulting number.
  55. /// </summary>
  56. /// <param name="_startValue"></param>
  57. /// <param name="_maxiumValue"></param>
  58. /// <param name="_rect"></param>
  59. /// <param name="_callback"></param>
  60. public static void GetNumber(int _startValue,int _maxiumValue,RectTransform _rect ,NumberCallback _callback)
  61. {
  62. if (instance == null)
  63. {
  64. GameObject newObj = Instantiate(Resources.Load<GameObject>("InventoryEngine/NumberInput"), _rect.GetComponentInParent<UiWindow>().transform);
  65. newObj.transform.SetAsLastSibling();
  66. newObj.transform.localPosition = Vector3.zero;
  67. newObj.transform.localScale = Vector3.one;
  68. instance = newObj.GetComponent<NumberInput>();
  69. }
  70. instance.ShowInput(_startValue, _maxiumValue, _rect, _callback);
  71. }
  72. }
  73. }