UIItemSlot.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public abstract class UIItemSlot : MonoBehaviour
  5. {
  6. protected abstract InvGameItem observedItem { get; }
  7. protected abstract InvGameItem Replace(InvGameItem item);
  8. private void OnTooltip(bool show)
  9. {
  10. InvGameItem invGameItem = (!show) ? null : this.mItem;
  11. if (invGameItem != null)
  12. {
  13. InvBaseItem baseItem = invGameItem.baseItem;
  14. if (baseItem != null)
  15. {
  16. string text = string.Concat(new string[]
  17. {
  18. "[",
  19. NGUIText.EncodeColor(invGameItem.color),
  20. "]",
  21. invGameItem.name,
  22. "[-]\n"
  23. });
  24. string text2 = text;
  25. text = string.Concat(new object[]
  26. {
  27. text2,
  28. "[AFAFAF]Level ",
  29. invGameItem.itemLevel,
  30. " ",
  31. baseItem.slot
  32. });
  33. List<InvStat> list = invGameItem.CalculateStats();
  34. int i = 0;
  35. int count = list.Count;
  36. while (i < count)
  37. {
  38. InvStat invStat = list[i];
  39. if (invStat.amount != 0)
  40. {
  41. if (invStat.amount < 0)
  42. {
  43. text = text + "\n[FF0000]" + invStat.amount;
  44. }
  45. else
  46. {
  47. text = text + "\n[00FF00]+" + invStat.amount;
  48. }
  49. if (invStat.modifier == InvStat.Modifier.Percent)
  50. {
  51. text += "%";
  52. }
  53. text = text + " " + invStat.id;
  54. text += "[-]";
  55. }
  56. i++;
  57. }
  58. if (!string.IsNullOrEmpty(baseItem.description))
  59. {
  60. text = text + "\n[FF9900]" + baseItem.description;
  61. }
  62. UITooltip.Show(text);
  63. return;
  64. }
  65. }
  66. UITooltip.Hide();
  67. }
  68. private void OnClick()
  69. {
  70. if (UIItemSlot.mDraggedItem != null)
  71. {
  72. this.OnDrop(null);
  73. }
  74. else if (this.mItem != null)
  75. {
  76. UIItemSlot.mDraggedItem = this.Replace(null);
  77. if (UIItemSlot.mDraggedItem != null)
  78. {
  79. NGUITools.PlaySound(this.grabSound);
  80. }
  81. this.UpdateCursor();
  82. }
  83. }
  84. private void OnDrag(Vector2 delta)
  85. {
  86. if (UIItemSlot.mDraggedItem == null && this.mItem != null)
  87. {
  88. UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
  89. UIItemSlot.mDraggedItem = this.Replace(null);
  90. NGUITools.PlaySound(this.grabSound);
  91. this.UpdateCursor();
  92. }
  93. }
  94. private void OnDrop(GameObject go)
  95. {
  96. InvGameItem invGameItem = this.Replace(UIItemSlot.mDraggedItem);
  97. if (UIItemSlot.mDraggedItem == invGameItem)
  98. {
  99. NGUITools.PlaySound(this.errorSound);
  100. }
  101. else if (invGameItem != null)
  102. {
  103. NGUITools.PlaySound(this.grabSound);
  104. }
  105. else
  106. {
  107. NGUITools.PlaySound(this.placeSound);
  108. }
  109. UIItemSlot.mDraggedItem = invGameItem;
  110. this.UpdateCursor();
  111. }
  112. private void UpdateCursor()
  113. {
  114. if (UIItemSlot.mDraggedItem != null && UIItemSlot.mDraggedItem.baseItem != null)
  115. {
  116. UICursor.Set(UIItemSlot.mDraggedItem.baseItem.iconAtlas, UIItemSlot.mDraggedItem.baseItem.iconName);
  117. }
  118. else
  119. {
  120. UICursor.Clear();
  121. }
  122. }
  123. private void Update()
  124. {
  125. InvGameItem observedItem = this.observedItem;
  126. if (this.mItem != observedItem)
  127. {
  128. this.mItem = observedItem;
  129. InvBaseItem invBaseItem = (observedItem == null) ? null : observedItem.baseItem;
  130. if (this.label != null)
  131. {
  132. string text = (observedItem == null) ? null : observedItem.name;
  133. if (string.IsNullOrEmpty(this.mText))
  134. {
  135. this.mText = this.label.text;
  136. }
  137. this.label.text = ((text == null) ? this.mText : text);
  138. }
  139. if (this.icon != null)
  140. {
  141. if (invBaseItem == null || invBaseItem.iconAtlas == null)
  142. {
  143. this.icon.enabled = false;
  144. }
  145. else
  146. {
  147. this.icon.atlas = invBaseItem.iconAtlas;
  148. this.icon.spriteName = invBaseItem.iconName;
  149. this.icon.enabled = true;
  150. this.icon.MakePixelPerfect();
  151. }
  152. }
  153. if (this.background != null)
  154. {
  155. this.background.color = ((observedItem == null) ? Color.white : observedItem.color);
  156. }
  157. }
  158. }
  159. public UISprite icon;
  160. public UIWidget background;
  161. public UILabel label;
  162. public AudioClip grabSound;
  163. public AudioClip placeSound;
  164. public AudioClip errorSound;
  165. private InvGameItem mItem;
  166. private string mText = string.Empty;
  167. private static InvGameItem mDraggedItem;
  168. }