UiWindow.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. /// <summary>
  8. /// Inherit from this class to make a window managed by WindowManager.cs.
  9. /// </summary>
  10. public class UiWindow : MonoBehaviour,IPointerDownHandler
  11. {
  12. #region Variables
  13. public KeyCode CloseKey = KeyCode.Escape;
  14. public bool Draggable = true;
  15. public bool FixedDefaultPosition = false;
  16. public Vector2 DefaultPosition;
  17. [HideInInspector]
  18. public bool ChildWindow = false;
  19. public delegate void OnWindowClose(InventoryHolder _key);
  20. private OnWindowClose closeCallback;
  21. private InventoryHolder windowKey;
  22. #endregion
  23. #region Internal Methods
  24. public void RegisterCloseCallback(OnWindowClose _callback, InventoryHolder _key)
  25. {
  26. windowKey = _key;
  27. closeCallback = _callback;
  28. }
  29. public void OnPointerDown(PointerEventData eventData)
  30. {
  31. ActiveWindow();
  32. }
  33. public void ActiveWindow()
  34. {
  35. if (!ChildWindow)
  36. {
  37. transform.SetAsLastSibling();
  38. WindowsManager.ActiveWindow = this;
  39. }
  40. }
  41. #endregion
  42. #region MonoBehaviour
  43. private void OnEnable()
  44. {
  45. SoundManager.Play2D("MenuOn");
  46. if (GetComponentInChildren<DragableUi>()) GetComponentInChildren<DragableUi>().enabled = Draggable;
  47. if(FixedDefaultPosition) GetComponent<RectTransform>().anchoredPosition = DefaultPosition;
  48. }
  49. public virtual void Update()
  50. {
  51. if (!NumberInput.isVisible() && !ItemDragManager.isVisible() && InputProxy.GetKeyDown(CloseKey) && WindowsManager.ActiveWindow==this)
  52. {
  53. Close();
  54. }
  55. }
  56. #endregion
  57. public virtual void Close()//Close this window.
  58. {
  59. SoundManager.Play2D("MenuOff");
  60. if (closeCallback != null) closeCallback(windowKey);
  61. Destroy(gameObject);
  62. }
  63. public virtual void Initialize(InventoryHolder _inventoryHolder, InventoryHolder _equipHolder, string _name = "")//Initialize this interface
  64. {
  65. }
  66. }
  67. }