InventoryItem.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections;
  3. using Assets.HeroEditor4D.Common.Scripts.Common;
  4. using Assets.HeroEditor4D.InventorySystem.Scripts.Data;
  5. using Assets.HeroEditor4D.InventorySystem.Scripts.Enums;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. using UnityEngine.UI;
  9. namespace Assets.HeroEditor4D.InventorySystem.Scripts.Elements
  10. {
  11. /// <summary>
  12. /// Represents inventory item and handles drag & drop operations.
  13. /// </summary>
  14. public class InventoryItem : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
  15. {
  16. [Header("Main")]
  17. public Image Icon;
  18. public Image Background;
  19. public Image Frame;
  20. public Text Count;
  21. public Toggle Toggle;
  22. [Header("Extra")]
  23. public Sprite IconEmpty;
  24. public Sprite IconMissed;
  25. public Image Comparer;
  26. public Image Fragment;
  27. public GameObject Modificator;
  28. public Text ModificatorText;
  29. public Item Item { get; private set; }
  30. private Action _scheduled;
  31. private float _clickTime;
  32. /// <summary>
  33. /// These actions should be set when inventory UI is opened.
  34. /// </summary>
  35. public static Action<Item> OnLeftClick;
  36. public static Action<Item> OnRightClick;
  37. public static Action<Item> OnDoubleClick;
  38. public static Action<Item> OnMouseEnter;
  39. public static Action<Item> OnMouseExit;
  40. public void OnEnable()
  41. {
  42. if (_scheduled != null)
  43. {
  44. StartCoroutine(ExecuteScheduled());
  45. }
  46. }
  47. public void Initialize(Item item, ToggleGroup toggleGroup = null)
  48. {
  49. Item = item;
  50. if (Item == null)
  51. {
  52. Reset();
  53. return;
  54. }
  55. Icon.enabled = true;
  56. Icon.sprite = item.Id == null ? IconEmpty : ItemCollection.Active.GetItemIcon(Item)?.Sprite ?? IconMissed;
  57. Background.sprite = ItemCollection.Active.GetBackground(Item) ?? ItemCollection.Active.BackgroundBrown;
  58. Background.color = Color.white;
  59. Frame.raycastTarget = true;
  60. if (Count)
  61. {
  62. Count.SetActive(true);
  63. Count.text = item.Count.ToString();
  64. }
  65. if (Fragment)
  66. {
  67. Fragment.SetActive(true);
  68. Fragment.SetActive(Item.Params.Type == ItemType.Fragment);
  69. }
  70. if (Toggle)
  71. {
  72. Toggle.group = toggleGroup ?? GetComponentInParent<ToggleGroup>();
  73. }
  74. if (Modificator)
  75. {
  76. var mod = Item.Modifier != null && Item.Modifier.Id != ItemModifier.None;
  77. Modificator.SetActive(mod);
  78. if (mod)
  79. {
  80. string text;
  81. switch (Item.Modifier.Id)
  82. {
  83. case ItemModifier.LevelDown: text = "G-"; break;
  84. case ItemModifier.LevelUp: text = "G+"; break;
  85. default: text = Item.Modifier.Id.ToString().ToUpper()[0].ToString(); break;
  86. }
  87. ModificatorText.text = text;
  88. }
  89. }
  90. }
  91. public void Reset()
  92. {
  93. Icon.enabled = false;
  94. Icon.sprite = null;
  95. Background.sprite = ItemCollection.Active.BackgroundBrown ?? Background.sprite;
  96. Background.color = new Color32(150, 150, 150, 255);
  97. Frame.raycastTarget = false;
  98. if (Count) Count.SetActive(false);
  99. if (Toggle) { Toggle.group = null; Toggle.SetIsOnWithoutNotify(false); }
  100. if (Modificator) Modificator.SetActive(false);
  101. if (Comparer) Comparer.SetActive(false);
  102. if (Fragment) Fragment.SetActive(false);
  103. }
  104. public void OnPointerClick(PointerEventData eventData)
  105. {
  106. OnPointerClick(eventData.button);
  107. }
  108. public void OnPointerEnter(PointerEventData eventData)
  109. {
  110. OnMouseEnter?.Invoke(Item);
  111. }
  112. public void OnPointerExit(PointerEventData eventData)
  113. {
  114. OnMouseExit?.Invoke(Item);
  115. }
  116. public void OnPointerClick(PointerEventData.InputButton button)
  117. {
  118. if (button == PointerEventData.InputButton.Left)
  119. {
  120. OnLeftClick?.Invoke(Item);
  121. var delta = Mathf.Abs(Time.time - _clickTime);
  122. if (delta < 0.5f) // If double click.
  123. {
  124. _clickTime = 0;
  125. if (OnDoubleClick != null)
  126. {
  127. StartCoroutine(ExecuteInNextUpdate(() => OnDoubleClick(Item)));
  128. }
  129. }
  130. else
  131. {
  132. _clickTime = Time.time;
  133. }
  134. }
  135. else if (button == PointerEventData.InputButton.Right)
  136. {
  137. OnRightClick?.Invoke(Item);
  138. }
  139. }
  140. private static IEnumerator ExecuteInNextUpdate(Action action)
  141. {
  142. yield return null;
  143. action();
  144. }
  145. public void Select(bool selected)
  146. {
  147. if (Toggle == null) return;
  148. if (gameObject.activeInHierarchy || !selected)
  149. {
  150. Toggle.isOn = selected;
  151. }
  152. else
  153. {
  154. _scheduled = () => Toggle.isOn = true;
  155. }
  156. if (selected)
  157. {
  158. OnLeftClick?.Invoke(Item);
  159. }
  160. }
  161. private IEnumerator ExecuteScheduled()
  162. {
  163. yield return null;
  164. _scheduled();
  165. _scheduled = null;
  166. }
  167. }
  168. }