LinkIcon.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 LinkIcon : ItemIcon
  9. {
  10. [Tooltip("Determines if this item slot is draggable.")]
  11. public bool Draggable = true;
  12. [Tooltip("When set to true, the shortcut will automatically re-link to the highest upgrade level of the same item from the player's inventory.")]
  13. public bool AutoLinkToHighestUpgradeLevel=true;
  14. [Tooltip("If this item slot only accepts items with a specific tag, specify that tag here.")]
  15. public string LimitedByTag = "";
  16. [Tooltip("If this item slot only accepts items from a specific owner, specify that owner here.")]
  17. public InventoryHolder LimitedByOwner;
  18. [Tooltip("Whether this item slot only accepts useable items.")]
  19. public bool OnlyRecieveUseableItem = true;
  20. #region Variables
  21. private InventoryHolder Holder;
  22. private float mouseDownTime = 0F;
  23. private Vector3 mouseDownPos;
  24. private bool isDragging = false;
  25. private bool waittingForDrop = false;
  26. private Item LinkedItem;
  27. private int ItemNumber = 0;
  28. private int ItemLinkId = 0;
  29. public delegate void OnItemLinked(int _index, Item _item);
  30. public Image CoolDownMask;
  31. public Text CoolDownText;
  32. protected OnItemLinked LinkedCallback;
  33. #endregion
  34. #region Internal Methods
  35. public void OnHolderItemChanged(Dictionary<Item, int> _changedItems)
  36. {
  37. if (itemId >= 0)
  38. {
  39. ItemNumber = Holder.GetItemNumberWithHighestUpgradeLevel(itemId);
  40. if (AutoLinkToHighestUpgradeLevel) LinkedItem.upgradeLevel = Holder.GetHighestUpgradeLevel(itemId);
  41. }
  42. }
  43. public override void OnHover()
  44. {
  45. if (HoverInfoAnchorPoint != null && !Empty) HoverInformation.ShowHoverInfo(this, LinkedItem, ItemNumber, HoverInfoAnchorPoint, PriceMultiplier, RightClickActionHint,false,false,false);
  46. }
  47. public override void ResetState()
  48. {
  49. SetItemId(-2);
  50. number = 0;
  51. upgrade = -1;
  52. isDragging = false;
  53. mouseDownTime = 0F;
  54. mouseDownPos = InputProxy.mousePosition;
  55. ToggleOutline(false);
  56. SetFavorate(false);
  57. }
  58. public override void DoUpdate()
  59. {
  60. if (!inited) return;
  61. StateUpdate();
  62. if (!Enabled) return;
  63. CheckDropping();
  64. if (itemId < 0) return;
  65. CheckDragging();
  66. CheckCoolDown();
  67. }
  68. private void CheckCoolDown()
  69. {
  70. if (!Empty && itemId >= 0 && number > 0 && CoolDownMask && CoolDownText)
  71. {
  72. float _cd = ItemManager.itemDic[itemId].GetCoolDownTime();
  73. if (_cd > 0F )
  74. {
  75. if (CoolDownMask.gameObject.activeSelf != ItemManager.itemDic[itemId].isCoolDown()) CoolDownMask.gameObject.SetActive(ItemManager.itemDic[itemId].isCoolDown());
  76. if (ItemManager.itemDic[itemId].isCoolDown())
  77. {
  78. CoolDownMask.fillAmount = ItemManager.itemDic[itemId].GetRemainCoolDownTime() / _cd;
  79. if (ItemManager.itemDic[itemId].GetRemainCoolDownTime() >= 1F)
  80. {
  81. CoolDownText.text = Mathf.CeilToInt(ItemManager.itemDic[itemId].GetRemainCoolDownTime()).ToString();
  82. }
  83. else
  84. {
  85. CoolDownText.text = ItemManager.itemDic[itemId].GetRemainCoolDownTime().ToString("0.0");
  86. }
  87. }
  88. }
  89. }
  90. }
  91. private void OnDestroy()
  92. {
  93. UnLinkHolder();
  94. }
  95. private void StateUpdate()
  96. {
  97. if (!Empty)
  98. {
  99. if (itemId != LinkedItem.uid)
  100. {
  101. SetAppearance(LinkedItem.icon, LinkedItem.GetTypeColor(), LinkedItem.GetQualityColor(), true, true);
  102. }
  103. if (number != ItemNumber) SetItemNumber(ItemNumber);
  104. if (upgrade != LinkedItem.upgradeLevel) SetUpgradeLevel(LinkedItem.upgradeLevel);
  105. if (Fav.activeSelf != LinkedItem.favorite) SetFavorate(LinkedItem.favorite);
  106. SetItemId(LinkedItem.uid);
  107. }
  108. else
  109. {
  110. if (itemId >= 0)
  111. {
  112. SetEmpty();
  113. }
  114. }
  115. }
  116. private void CheckDropping()
  117. {
  118. if (ItemDragManager.isDragging)
  119. {
  120. waittingForDrop = true;
  121. }
  122. else
  123. {
  124. if (waittingForDrop && CanBeLinked && (ItemDragManager.DraggingSource.Type == IconType.Item || ItemDragManager.DraggingSource.Type == IconType.Link))
  125. {
  126. if (Vector3.Distance(ItemDragManager.DropPos, transform.position) < Mathf.Min(Rect.sizeDelta.x * 0.48F, Rect.sizeDelta.y * 0.48F) * Rect.lossyScale.x)
  127. {
  128. ItemDragManager.DropReceived = true;
  129. if (ItemDragManager.DraggingSource.GetStackHolder() != null)
  130. {
  131. ItemIcon _source = (ItemIcon)ItemDragManager.DraggingSource;
  132. InventoryItem _itemSource = ItemDragManager.DraggingSource.GetComponent<InventoryItem>();
  133. if (LimitedByTag.Length > 0 && !_source.isTagMatchText(LimitedByTag))
  134. {
  135. DynamicMsg.PopMsg("Sorry you can not assign this item here.");
  136. }
  137. else if (LimitedByOwner != null && _itemSource!=null && _itemSource.Holder != LimitedByOwner)
  138. {
  139. DynamicMsg.PopMsg("Sorry you can not assign this item here.");
  140. }
  141. else if (ItemDragManager.DraggingSource.Type == IconType.Link)
  142. {
  143. SwapLink((LinkIcon)ItemDragManager.DraggingSource);
  144. Icon.transform.localScale = Vector3.one * 0.5F;
  145. ItemDragManager.DraggingSource.Icon.transform.localScale = Vector3.one * 0.5F;
  146. }
  147. else if (ItemDragManager.DraggingSource.Type == IconType.Item)
  148. {
  149. if (ItemDragManager.DraggingSource.GetItem().useable || !OnlyRecieveUseableItem)
  150. LinkItem(ItemDragManager.DraggingSource.GetStackHolder(), ItemDragManager.DraggingSource.GetItem(), ItemDragManager.DraggingSource.GetNumber(), ItemDragManager.DraggingSource.isEmpty());
  151. }
  152. }
  153. }
  154. }
  155. waittingForDrop = false;
  156. }
  157. }
  158. public override void EndDrag(int _add)
  159. {
  160. StartCoroutine(EndDragCo(_add));
  161. }
  162. IEnumerator EndDragCo(int _add)
  163. {
  164. yield return 2;
  165. if (!ItemDragManager.DropReceived)
  166. {
  167. if (!EventSystem.current.IsPointerOverGameObject())
  168. {
  169. SetHolder(null);
  170. Empty = true;
  171. }
  172. }
  173. ItemDragManager.DropReceived = true;
  174. }
  175. private void CheckDragging()
  176. {
  177. if (ItemDragManager.isDragging || !Draggable) return;
  178. if (isHover)
  179. {
  180. if (InputProxy.GetMouseButtonDown(0))
  181. {
  182. mouseDownPos = InputProxy.mousePosition;
  183. isDragging = true;
  184. }
  185. if (InputProxy.GetMouseButton(0))
  186. {
  187. mouseDownTime += Time.deltaTime;
  188. }
  189. else
  190. {
  191. mouseDownTime = 0F;
  192. }
  193. }
  194. else
  195. {
  196. mouseDownTime = 0F;
  197. }
  198. if (isDragging)
  199. {
  200. if (InputProxy.GetMouseButton(0))
  201. {
  202. if (mouseDownTime > 0.5F || Vector3.Distance(InputProxy.mousePosition, mouseDownPos) > 40F)
  203. {
  204. isDragging = false;
  205. ItemDragManager.StartDragging(this, GetComponent<RectTransform>());
  206. }
  207. }
  208. else
  209. {
  210. isDragging = false;
  211. }
  212. }
  213. }
  214. #endregion
  215. /// <summary>
  216. /// Retrieves the InventoryHolder of this slot.
  217. /// </summary>
  218. /// <returns></returns>
  219. public override InventoryHolder GetStackHolder()
  220. {
  221. return Holder;
  222. }
  223. /// <summary>
  224. /// Retrieves the Item in this slot.
  225. /// </summary>
  226. /// <returns></returns>
  227. public override Item GetItem()
  228. {
  229. return Empty?null: LinkedItem;
  230. }
  231. /// <summary>
  232. /// Initialize by an item
  233. /// </summary>
  234. /// <param name="_item"></param>
  235. /// <param name="_empty"></param>
  236. public void Initialize(Item _item,bool _empty)
  237. {
  238. ResetState();
  239. LinkedItem = _item;
  240. Empty = _empty;
  241. inited = true;
  242. }
  243. /// <summary>
  244. /// Sets the InventoryHolder for this slot.
  245. /// </summary>
  246. /// <param name="_holder"></param>
  247. public void SetHolder(InventoryHolder _holder)
  248. {
  249. UnLinkHolder();
  250. Holder = _holder;
  251. if (Holder != null) Holder.RegisterItemChangeCallback(OnHolderItemChanged);
  252. }
  253. /// <summary>
  254. /// Disconnect from the linked InventoryHolder.
  255. /// </summary>
  256. public void UnLinkHolder()
  257. {
  258. if (Holder != null) Holder.UnRegisterItemChangeCallback(OnHolderItemChanged);
  259. }
  260. /// <summary>
  261. /// Registers a callback for when this icon is linked with an item.
  262. /// </summary>
  263. /// <param name="_callback"></param>
  264. public void RegisterLinkedCallback(int _index, OnItemLinked _callback)
  265. {
  266. ItemLinkId = _index;
  267. LinkedCallback = _callback;
  268. }
  269. /// <summary>
  270. /// Swaps the linked item with another LinkIcon.
  271. /// </summary>
  272. /// <param name="_source"></param>
  273. public void SwapLink(LinkIcon _source)
  274. {
  275. Item _item = _source.LinkedItem.Copy();
  276. int _num = _source.ItemNumber;
  277. InventoryHolder _holder = _source.Holder;
  278. bool _empty = _source.Empty;
  279. if (LinkedItem != null && itemId>=0)
  280. {
  281. _source.LinkItem(Holder, LinkedItem.Copy(), ItemNumber, Empty);
  282. }
  283. else
  284. {
  285. _source.SetEmpty();
  286. }
  287. LinkItem(_holder, _item, _num, _empty);
  288. }
  289. /// <summary>
  290. /// Links this icon with the provided InventoryHolder and item.
  291. /// </summary>
  292. /// <param name="_holder"></param>
  293. /// <param name="_item"></param>
  294. /// <param name="_num"></param>
  295. /// <param name="_empty"></param>
  296. public void LinkItem(InventoryHolder _holder, Item _item, int _num, bool _empty)
  297. {
  298. inited = false;
  299. if (_item != null)
  300. {
  301. ResetState();
  302. LinkedItem = _item.Copy();
  303. SetHolder(_holder);
  304. Empty = _empty;
  305. }
  306. inited = true;
  307. StateUpdate();
  308. OnHolderItemChanged(new Dictionary<Item, int>());
  309. if (LinkedCallback!=null) LinkedCallback(ItemLinkId, _item);
  310. }
  311. }
  312. }