ItemIcon.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. namespace SoftKitty.InventoryEngine
  7. {
  8. public class ItemIcon : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
  9. {
  10. public enum IconType
  11. {
  12. Reference,
  13. Link,
  14. Item
  15. }
  16. [Header("(Hover the attributes to read the tooltips)")]
  17. [Tooltip("Reference: Only for showing information about an item.\n" +
  18. "| Link: Shortcut to an item; it updates its information when the stats of the linked item change.\n" +
  19. "| Item: Represents an item carried by an <InventoryHolder>.\n")]
  20. public IconType Type;
  21. [Tooltip("The hover information panel will align with this RectTransform")]
  22. public RectTransform HoverInfoAnchorPoint;
  23. [Tooltip("The hover information will promo player to right click with this hint text")]
  24. public string RightClickActionHint="Use";
  25. [Tooltip("The border with color of the item quality")]
  26. public Image Frame;
  27. [Tooltip("The background with color of the item category")]
  28. public Image Background;
  29. [Tooltip("A small mark on the top-left corner to indicate if this item is in player's favorite group.")]
  30. public GameObject Fav;
  31. [Tooltip("The glowing border will show up when item is selected")]
  32. public Image Outline;
  33. [Header("[Misc references]")]
  34. public RawImage Icon;
  35. public RawImage GlowIcon;
  36. public Image Hover;
  37. public Text NumberText;
  38. public Text UpgradeText;
  39. public Text NameText;
  40. public Text DescriptionText;
  41. public Text TypeText;
  42. public Text QualityText;
  43. public RectTransform Rect;
  44. public CanvasGroup Group;
  45. [Header("[Important Settings]")]
  46. [Tooltip("Determines if this item slot reacts to pointer hover events.")]
  47. public bool CanHover = true;
  48. [Tooltip("Indicates whether this item slot can be dragged to the shortcut slots.")]
  49. public bool CanBeLinked = true;
  50. #region Variables
  51. [HideInInspector]
  52. public bool Enabled = true;
  53. [HideInInspector]
  54. public float PriceMultiplier = 1F;
  55. public bool Visible
  56. {
  57. get
  58. {
  59. return isVisible;
  60. }
  61. }
  62. public delegate void OnItemClick(int _index, int _button);
  63. protected OnItemClick ClickCallback;
  64. protected int ItemClickId;
  65. protected bool OutlineVisible = false;
  66. protected float OutlineTimer = 0F;
  67. protected Texture emptyTexture
  68. {
  69. get
  70. {
  71. if(_emptyTexture==null) _emptyTexture= Icon.mainTexture;
  72. return _emptyTexture;
  73. }
  74. }
  75. private Texture _emptyTexture;
  76. protected bool isHover = false;
  77. protected bool isVisible = true;
  78. protected int number = 0;
  79. protected int upgrade = -1;
  80. protected int itemId = -2;
  81. protected bool inited = false;
  82. protected bool Empty = false;
  83. #endregion
  84. #region Internal Methods
  85. void Awake()
  86. {
  87. _emptyTexture = Icon.mainTexture;
  88. }
  89. void Update()
  90. {
  91. Icon.transform.localScale = Vector3.Lerp(Icon.transform.localScale, Vector3.one * (isHover ? 1F : 0.9F), Time.deltaTime * 8F);
  92. OutlineTimer = Mathf.MoveTowards(OutlineTimer,0F,Time.deltaTime);
  93. if (GlowIcon) GlowIcon.transform.localScale = new Vector3(Icon.transform.localScale.x*1.1F, Icon.transform.localScale.y,1F);
  94. if (Outline)
  95. {
  96. Outline.gameObject.SetActive(OutlineVisible || OutlineTimer > 0F);
  97. Outline.color = new Color(Outline.color.r, Outline.color.g, Outline.color.b, OutlineVisible?1F: Mathf.Clamp01(OutlineTimer*2F));
  98. }
  99. DoUpdate();
  100. }
  101. public void Click()
  102. {
  103. if (Empty || itemId < 0 || !ItemManager.itemDic[itemId].useable) return;
  104. Icon.transform.localScale = Vector3.one * 0.5F;
  105. if(GlowIcon) GlowIcon.transform.localScale = new Vector3(Icon.transform.localScale.x * 1.1F, Icon.transform.localScale.y, 1F);
  106. Instantiate(Resources.Load<GameObject>("InventoryEngine/ClickEffect"), transform.position, Quaternion.identity, WindowsManager.GetMainCanvas().transform);
  107. }
  108. public void OnPointerExit(PointerEventData eventData)
  109. {
  110. isHover = false;
  111. }
  112. public void OnPointerEnter(PointerEventData eventData)
  113. {
  114. if (CanHover) isHover = true;
  115. OnHover();
  116. SoundManager.Play2D("bt_hover");
  117. }
  118. public void OnPointerClick(PointerEventData eventData)
  119. {
  120. if (eventData.button == PointerEventData.InputButton.Left)
  121. {
  122. OnLeftClick();
  123. }
  124. else if (eventData.button == PointerEventData.InputButton.Right)
  125. {
  126. OnRightClick();
  127. }
  128. else if (eventData.button == PointerEventData.InputButton.Middle)
  129. {
  130. OnMiddleClick();
  131. }
  132. SoundManager.Play2D("bt_down");
  133. }
  134. public virtual void ResetState()
  135. {
  136. SetItemId(-2);
  137. number = 0;
  138. upgrade = -1;
  139. ToggleOutline(false);
  140. SetFavorate(false);
  141. }
  142. public virtual void OnHover()
  143. {
  144. if (HoverInfoAnchorPoint != null && itemId >= 0) HoverInformation.ShowHoverInfo(this, ItemManager.itemDic[itemId].Copy(), 0, HoverInfoAnchorPoint, PriceMultiplier, RightClickActionHint,false,false,false);
  145. }
  146. public virtual void OnLeftClick()
  147. {
  148. if (ClickCallback != null) ClickCallback(ItemClickId, 0);
  149. }
  150. public virtual void OnRightClick()
  151. {
  152. if (ClickCallback != null) ClickCallback(ItemClickId, 1);
  153. }
  154. public virtual void OnMiddleClick()
  155. {
  156. if (ClickCallback != null) ClickCallback(ItemClickId, 2);
  157. }
  158. public virtual void EndDrag(int _add)
  159. {
  160. }
  161. public virtual void DoUpdate()
  162. {
  163. }
  164. #endregion
  165. /// <summary>
  166. /// Retrieves the InventoryStack data of this item.
  167. /// </summary>
  168. /// <returns></returns>
  169. public virtual InventoryStack GetStackData()
  170. {
  171. return null;
  172. }
  173. /// <summary>
  174. /// Retrieves the InventoryHolder of this slot.
  175. /// </summary>
  176. /// <returns></returns>
  177. public virtual InventoryHolder GetStackHolder()
  178. {
  179. return null;
  180. }
  181. /// <summary>
  182. /// Retrieves the Item in this slot.
  183. /// </summary>
  184. /// <returns></returns>
  185. public virtual Item GetItem()
  186. {
  187. return null;
  188. }
  189. /// <summary>
  190. /// Registers a callback for when this icon is clicked.
  191. /// </summary>
  192. /// <param name="_id"></param>
  193. /// <param name="_callback"></param>
  194. public void RegisterClickCallback(int _id, OnItemClick _callback)
  195. {
  196. ItemClickId = _id;
  197. ClickCallback = _callback;
  198. }
  199. /// <summary>
  200. /// Returns if this slot is empty.
  201. /// </summary>
  202. /// <returns></returns>
  203. public bool isEmpty()
  204. {
  205. return Empty;
  206. }
  207. /// <summary>
  208. /// Returns the number of items in the slot.
  209. /// </summary>
  210. /// <returns></returns>
  211. public int GetNumber()
  212. {
  213. return number;
  214. }
  215. /// <summary>
  216. /// Returns the item ID. Returns -1 if the slot is empty.
  217. /// </summary>
  218. /// <returns></returns>
  219. public int GetItemId()
  220. {
  221. return itemId;
  222. }
  223. /// <summary>
  224. /// Returns the item category id. Returns -1 if the slot is empty.
  225. /// </summary>
  226. /// <returns></returns>
  227. public int GetCategory()
  228. {
  229. if (ItemManager.TryGetItem(itemId) != null)
  230. return ItemManager.TryGetItem(itemId).type;
  231. else
  232. return -1;
  233. }
  234. /// <summary>
  235. /// Returns whether this item slot is hovered over by the mouse.
  236. /// </summary>
  237. /// <returns></returns>
  238. public bool GetHover()
  239. {
  240. return isHover;
  241. }
  242. /// <summary>
  243. /// Overrides the item ID.
  244. /// </summary>
  245. /// <param name="_id"></param>
  246. public void SetItemId(int _id)
  247. {
  248. itemId = _id;
  249. }
  250. /// <summary>
  251. /// Sets the number of items in the slot.
  252. /// </summary>
  253. /// <param name="_num"></param>
  254. public void SetItemNumber(int _num)
  255. {
  256. if (NumberText) NumberText.text = _num.ToString();
  257. number = _num;
  258. }
  259. /// <summary>
  260. /// Sets the upgrade level of the item.
  261. /// </summary>
  262. /// <param name="_level"></param>
  263. public void SetUpgradeLevel(int _level)
  264. {
  265. if (UpgradeText) UpgradeText.text = _level > 0 ? "+" + _level.ToString() : "";
  266. if (GlowIcon )
  267. {
  268. if (ItemManager.instance.EnableEnhancingGlow)
  269. {
  270. GlowIcon.gameObject.SetActive(_level > 0);
  271. GlowIcon.color = Color.Lerp(Color.black, new Color(1F, 0.6F, 0.04F, 1F), ItemManager.instance.EnhancingGlowCurve.Evaluate(Mathf.Clamp01(_level * 1F / ItemManager.instance.MaxiumEnhancingLevel)));
  272. }
  273. else
  274. {
  275. GlowIcon.gameObject.SetActive(false);
  276. }
  277. }
  278. upgrade = _level;
  279. }
  280. /// <summary>
  281. /// Sets the icon and colors for the slot.
  282. /// </summary>
  283. /// <param name="_icon"></param>
  284. /// <param name="_backgroundColor"></param>
  285. /// <param name="_frameColor"></param>
  286. /// <param name="_numVisible"></param>
  287. /// <param name="_upgradeVisible"></param>
  288. public void SetAppearance(Texture _icon,Color _backgroundColor , Color _frameColor , bool _numVisible=false, bool _upgradeVisible=false)
  289. {
  290. Icon.texture = _icon;
  291. if (GlowIcon) GlowIcon.texture = _icon;
  292. if (Frame) Frame.color = _frameColor;
  293. Background.color = _backgroundColor;
  294. if (NumberText) NumberText.gameObject.SetActive(_numVisible);
  295. if (UpgradeText) UpgradeText.gameObject.SetActive(_upgradeVisible);
  296. }
  297. /// <summary>
  298. /// Sets the icon and colors for the slot.
  299. /// </summary>
  300. /// <param name="_item"></param>
  301. /// <param name="_numVisible"></param>
  302. /// <param name="_upgradeVisible"></param>
  303. public void SetAppearance(Item _item, bool _numVisible = false, bool _upgradeVisible = false)
  304. {
  305. Icon.texture = _item.icon;
  306. if (GlowIcon) GlowIcon.texture = _item.icon;
  307. if (Frame) Frame.color = _item.GetQualityColor();
  308. if (Background) Background.color = _item.GetTypeColor();
  309. if (NameText) NameText.text = _item.nameWithAffixing + (_item.upgradeLevel > 0 ? " +" + _item.upgradeLevel.ToString() : "");
  310. if(DescriptionText) DescriptionText.text = _item.description;
  311. if (TypeText) TypeText.text = "[ " + _item.GetTypeName() + " ]";
  312. if (TypeText) TypeText.color = _item.GetTypeColor();
  313. if (QualityText) QualityText.text = _item.GetQualityName();
  314. if (QualityText) QualityText.color = _item.GetQualityColor();
  315. if (NumberText) NumberText.gameObject.SetActive(_numVisible);
  316. if (UpgradeText) UpgradeText.gameObject.SetActive(_upgradeVisible);
  317. }
  318. /// <summary>
  319. /// Sets the slot to empty.
  320. /// </summary>
  321. public void SetEmpty()
  322. {
  323. ResetState();
  324. Empty = true;
  325. if (emptyTexture != null) Icon.texture = emptyTexture;
  326. if (Frame) Frame.color = InventorySkin.instance.EmptyItemBackColor;
  327. if (Background) Background.color = InventorySkin.instance.EmptyItemBackColor;
  328. if (NumberText) NumberText.text = "0";
  329. if (UpgradeText) UpgradeText.text = "";
  330. if (NumberText) NumberText.gameObject.SetActive(false);
  331. if (UpgradeText) UpgradeText.gameObject.SetActive(false);
  332. if (GlowIcon) GlowIcon.gameObject.SetActive(false);
  333. inited = true;
  334. }
  335. /// <summary>
  336. /// Toggles the outline effect of this slot.
  337. /// </summary>
  338. /// <param name="_visible"></param>
  339. public void ToggleOutline(bool _visible)
  340. {
  341. OutlineVisible = _visible;
  342. }
  343. /// <summary>
  344. /// Show the outline effect for x seconds.
  345. /// </summary>
  346. /// <param name="_time"></param>
  347. public void ShowOutline(float _time)
  348. {
  349. OutlineTimer = _time;
  350. }
  351. /// <summary>
  352. /// Toggles whether this item is marked as a favorite.
  353. /// </summary>
  354. /// <param name="_favorate"></param>
  355. public void SetFavorate(bool _favorate)
  356. {
  357. Fav.SetActive(_favorate);
  358. }
  359. /// <summary>
  360. /// Toggles this slot between fully visible or half-transparent.
  361. /// </summary>
  362. /// <param name="_visible"></param>
  363. public void SetVisible(bool _visible)
  364. {
  365. isVisible = _visible;
  366. Group.alpha = isVisible ? 1F : 0.1F;
  367. }
  368. /// <summary>
  369. /// //Returns whether the item matches the provided tag.
  370. /// </summary>
  371. /// <param name="_tag"></param>
  372. /// <returns></returns>
  373. public bool isTagMatchText(string _tag)
  374. {
  375. if (itemId <= 0) return false;
  376. return ItemManager.itemDic[itemId].isTagMatchText(_tag);
  377. }
  378. /// <summary>
  379. /// Returns whether the item's tags matches the provided list.
  380. /// </summary>
  381. /// <param name="_tags"></param>
  382. /// <param name="_allMatch"></param>
  383. /// <returns></returns>
  384. public bool isTagsMatchList(List<string> _tags, bool _allMatch = true)
  385. {
  386. if (itemId <= 0) return false;
  387. return ItemManager.itemDic[itemId].isTagsMatchList(_tags, _allMatch);
  388. }
  389. /// <summary>
  390. /// Returns whether the item has any tag contains the provided text.
  391. /// </summary>
  392. /// <param name="_text"></param>
  393. /// <param name="_caseSensitive"></param>
  394. /// <returns></returns>
  395. public bool isTagContainText(string _text, bool _caseSensitive = true)
  396. {
  397. if (itemId <= 0) return false;
  398. return ItemManager.itemDic[itemId].isTagContainText(_text, _caseSensitive);
  399. }
  400. /// <summary>
  401. /// Returns the tag of the item which contains the provided text.
  402. /// </summary>
  403. /// <param name="_text"></param>
  404. /// <param name="_caseSensitive"></param>
  405. /// <returns></returns>
  406. public string GetTagContainText(string _text, bool _caseSensitive = true)
  407. {
  408. if (itemId <= 0) return "";
  409. return ItemManager.itemDic[itemId].GetTagContainText(_text, _caseSensitive);
  410. }
  411. }
  412. }