ItemSlotInterface.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. namespace HQFPSWeapons.UserInterface
  5. {
  6. public class ItemSlotInterface : Slot
  7. {
  8. public ItemSlot ItemSlot
  9. {
  10. get
  11. {
  12. if(m_ItemSlot != null)
  13. return m_ItemSlot;
  14. else
  15. {
  16. Debug.LogError("No item slot is linked to this interface.");
  17. return null;
  18. }
  19. }
  20. }
  21. public bool HasItem { get { return m_ItemSlot == null ? false : m_ItemSlot.HasItem; } }
  22. public SaveableItem Item { get { return m_ItemSlot == null ? null : m_ItemSlot.Item; } }
  23. public ItemContainerInterface Parent { get; private set; }
  24. [BHeader("Item Slot")]
  25. [SerializeField]
  26. private Image m_Icon = null;
  27. [SerializeField]
  28. private Text m_Stack = null;
  29. [SerializeField]
  30. private Color m_NormalStackColor = Color.grey;
  31. [SerializeField]
  32. private Color m_HighlightStackColor = Color.black;
  33. private ItemSlot m_ItemSlot;
  34. public void LinkToSlot(ItemSlot itemSlot)
  35. {
  36. m_ItemSlot = itemSlot;
  37. if(m_ItemSlot != null)
  38. m_ItemSlot.Changed.RemoveListener(OnSlotChanged);
  39. m_ItemSlot.Changed.AddListener(OnSlotChanged);
  40. DoRefresh();
  41. }
  42. public void UnlinkFromSlot()
  43. {
  44. if(m_ItemSlot == null)
  45. return;
  46. m_ItemSlot.Changed.RemoveListener(OnSlotChanged);
  47. }
  48. public void DoRefresh()
  49. {
  50. m_Icon.enabled = HasItem;
  51. if(m_Stack != null)
  52. m_Stack.enabled = HasItem && Item.CurrentStackSize > 1;
  53. if(m_Icon.enabled)
  54. m_Icon.sprite = Item.Data.Icon;
  55. if(m_Stack != null && m_Stack.enabled)
  56. m_Stack.text = "x" + Item.CurrentStackSize.ToString();
  57. //if(m_DurabilityBar.Active)
  58. // m_DurabilityBar.SetFillAmount(Item.GetProperty("Durability").Float.Ratio);
  59. Refresh.Send(this);
  60. }
  61. // <summary>
  62. /// Will return a clone of this slot, without the background.
  63. /// </summary>
  64. public RectTransform GetItemUI(SaveableItem item, float alpha)
  65. {
  66. var itemUI = Instantiate<ItemSlotInterface>(this);
  67. // Disable the slot UI
  68. itemUI.enabled = false;
  69. itemUI._Graphic.enabled = false;
  70. // Set up the icon
  71. itemUI.m_Icon.enabled = true;
  72. itemUI.m_Icon.sprite = item.Data.Icon;
  73. // Set up the stack text
  74. if(m_Stack != null)
  75. {
  76. itemUI.m_Stack.enabled = item.CurrentStackSize > 1;
  77. itemUI.m_Stack.text = string.Format("x{0}", item.CurrentStackSize);
  78. }
  79. // Set up the durability bar
  80. //itemUI.m_DurabilityBar.SetActive(item.HasProperty("Durability"));
  81. //if(itemUI.m_DurabilityBar.Active)
  82. // itemUI.m_DurabilityBar.SetFillAmount(item.GetProperty("Durability").Float.Ratio);
  83. // Add a CanvasGroup so we can set a global alpha value
  84. var group = itemUI.gameObject.AddComponent<CanvasGroup>();
  85. group.alpha = alpha;
  86. group.interactable = false;
  87. return itemUI.GetComponent<RectTransform>();
  88. }
  89. public override void OnPointerDown(PointerEventData data)
  90. {
  91. base.OnPointerDown(data);
  92. }
  93. protected override void Awake()
  94. {
  95. base.Awake();
  96. Parent = GetComponentInParent<ItemContainerInterface>();
  97. StateChanged.AddListener(OnStateChanged);
  98. }
  99. protected override void OnDestroy()
  100. {
  101. base.Awake();
  102. if(m_ItemSlot != null)
  103. m_ItemSlot.Changed.RemoveListener(OnSlotChanged);
  104. }
  105. private void OnSlotChanged(ItemSlot itemSlot)
  106. {
  107. DoRefresh();
  108. }
  109. private void OnStateChanged(State state)
  110. {
  111. if(m_Stack != null)
  112. m_Stack.color = state == State.Normal ? m_NormalStackColor : m_HighlightStackColor;
  113. }
  114. }
  115. }