ContainerInterface.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. namespace HQFPSWeapons.UserInterface
  4. {
  5. public abstract class ContainerInterface<T> : UserInterfaceBehaviour where T: Slot
  6. {
  7. public T[] SlotInterfaces { get { return m_SlotInterfaces; } }
  8. public readonly Message<T> SlotInterfaceRefresh = new Message<T>();
  9. //public readonly Message AllSlotsDeselected = new Message();
  10. public readonly Message<T, PointerEventData> SlotPointerDown = new Message<T, PointerEventData>();
  11. public readonly Message<T, PointerEventData> SlotPointerUp = new Message<T, PointerEventData>();
  12. public readonly Message<PointerEventData, T> SlotBeginDrag = new Message<PointerEventData, T>();
  13. public readonly Message<PointerEventData, T> SlotDrag = new Message<PointerEventData, T>();
  14. public readonly Message<PointerEventData, T> SlotEndDrag = new Message<PointerEventData, T> ();
  15. [SerializeField]
  16. protected ItemSlotInterface m_SlotTemplate = null;
  17. [SerializeField]
  18. private RectTransform m_SlotsParent = null;
  19. [SerializeField]
  20. private int m_DefaultSlotCount = 1;
  21. [SerializeField]
  22. [Tooltip("If enabled, the template will be disabled after the slots are generated.")]
  23. private bool m_DisableTemplateAfterGen = false;
  24. protected T[] m_SlotInterfaces;
  25. protected T m_Selected;
  26. public virtual bool GenerateSlots()
  27. {
  28. return GenerateSlots(m_DefaultSlotCount);
  29. }
  30. public virtual bool GenerateSlots(int count)
  31. {
  32. if(m_SlotTemplate == null)
  33. {
  34. Debug.LogError("No slot template is provided, can't generate any slots.", gameObject);
  35. return false;
  36. }
  37. if(m_SlotsParent == null)
  38. Debug.LogWarning("The slots parent is not assigned. Will parent them under this object.", gameObject);
  39. // Remove listeners from the old slots
  40. RemoveListeners(m_SlotInterfaces);
  41. var parent = m_SlotsParent == null ? transform : m_SlotsParent;
  42. int childCount = parent.childCount;
  43. // Destroy the old slots
  44. for(int i = 0;i < childCount;i ++)
  45. {
  46. var child = parent.GetChild(parent.childCount - 1);
  47. if(child != m_SlotTemplate.transform && child.GetComponent<T>())
  48. DestroyImmediate(child.gameObject);
  49. }
  50. // Make sure the slot template is active, so we don't spawn disabled slots
  51. bool slotTemplateActive = m_SlotTemplate.gameObject.activeSelf;
  52. m_SlotTemplate.gameObject.SetActive(true);
  53. // Create the new slots
  54. m_SlotInterfaces = new T[count];
  55. for(int i = 0;i < count;i ++)
  56. m_SlotInterfaces[i] = Instantiate(m_SlotTemplate, parent) as T;
  57. // Add listeners to the new slots
  58. AddListeners(m_SlotInterfaces);
  59. m_SlotTemplate.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
  60. m_SlotTemplate.gameObject.SetActive(m_DisableTemplateAfterGen ? false : slotTemplateActive);
  61. return true;
  62. }
  63. public void Select(int index)
  64. {
  65. if(index >= m_SlotInterfaces.Length || (m_Selected != null && m_SlotInterfaces[index] == m_Selected))
  66. return;
  67. if(m_Selected != null)
  68. m_Selected.Deselect();
  69. m_Selected = m_SlotInterfaces[index];
  70. m_Selected.Select();
  71. //SelectedSlotChanged.Send(m_Selected);
  72. }
  73. public void Select(Slot slot)
  74. {
  75. if((m_Selected != null && slot == m_Selected))
  76. return;
  77. for(int i = 0;i < m_SlotInterfaces.Length;i++)
  78. if(m_SlotInterfaces[i] == slot)
  79. {
  80. Select(i);
  81. return;
  82. }
  83. }
  84. public void DeselectAll()
  85. {
  86. for(int i = 0;i < m_SlotInterfaces.Length;i++)
  87. m_SlotInterfaces[i].Deselect();
  88. if(m_Selected != null)
  89. {
  90. m_Selected = null;
  91. //AllSlotsDeselected.Send();
  92. }
  93. }
  94. private void On_PointerDown()
  95. {
  96. // if(m_Selected != null && m_PanelBody != null && !Manager.RectangleContainsScreenPoint(m_PanelBody))
  97. // {
  98. // m_Selected.Deselect();
  99. // m_Selected = null;
  100. //
  101. // //AllSlotsDeselected.Send();
  102. // }
  103. }
  104. private void RemoveListeners(Slot[] slotInterfaces)
  105. {
  106. if(!Application.isPlaying || slotInterfaces == null)
  107. return;
  108. for(int i = 0;i < slotInterfaces.Length;i ++)
  109. {
  110. slotInterfaces[i].Refresh.RemoveListener(OnSlotInterfaceRefresh);
  111. slotInterfaces[i].PointerDown.RemoveListener(OnPointerDownOnSlot);
  112. slotInterfaces[i].PointerUp.RemoveListener(OnPointerUpOnSlot);
  113. }
  114. }
  115. private void AddListeners(Slot[] slotInterfaces)
  116. {
  117. if(!Application.isPlaying || slotInterfaces == null)
  118. return;
  119. for(int i = 0;i < slotInterfaces.Length;i ++)
  120. {
  121. slotInterfaces[i].Refresh.AddListener(OnSlotInterfaceRefresh);
  122. slotInterfaces[i].PointerDown.AddListener(OnPointerDownOnSlot);
  123. slotInterfaces[i].PointerUp.AddListener(OnPointerUpOnSlot);
  124. slotInterfaces[i].BeginDrag.AddListener(OnSlotBeginDrag);
  125. slotInterfaces[i].Drag.AddListener(OnSlotDrag);
  126. slotInterfaces[i].EndDrag.AddListener(OnSlotEndDrag);
  127. }
  128. }
  129. private void OnSlotBeginDrag(PointerEventData data, Slot slotInterface)
  130. {
  131. SlotBeginDrag.Send(data, slotInterface as T);
  132. }
  133. private void OnSlotDrag(PointerEventData data, Slot slotInterface)
  134. {
  135. SlotDrag.Send(data, slotInterface as T);
  136. }
  137. private void OnSlotEndDrag(PointerEventData data, Slot slotInterface)
  138. {
  139. SlotEndDrag.Send(data, slotInterface as T);
  140. }
  141. private void OnSlotInterfaceRefresh(Slot slotInterface)
  142. {
  143. SlotInterfaceRefresh.Send(slotInterface as T);
  144. }
  145. private void OnPointerDownOnSlot(Slot slotInterface, PointerEventData data)
  146. {
  147. SlotPointerDown.Send(slotInterface as T, data);
  148. }
  149. private void OnPointerUpOnSlot(Slot slotInterface, PointerEventData data)
  150. {
  151. // if(data.pointerCurrentRaycast.gameObject != null && data.pointerCurrentRaycast.gameObject == slotInterface.gameObject)
  152. // Inventory.CurrentSelectedSlot.Set(slotInterface);
  153. SlotPointerUp.Send(slotInterface as T, data);
  154. }
  155. }
  156. }