BagExchangeView.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using ET;
  4. using FairyGUI;
  5. using UI.Bag;
  6. using UI.CommonGame;
  7. using UnityEngine;
  8. namespace GFGGame
  9. {
  10. public class BagExchangeView : BaseWindow
  11. {
  12. private UI_ItemExchangeUI _ui;
  13. private int _itemId;
  14. private long _count;
  15. private List<int[]> _itemList = new List<int[]>();
  16. public override void Dispose()
  17. {
  18. base.Dispose();
  19. if (_ui != null)
  20. {
  21. _ui.Dispose();
  22. _ui = null;
  23. }
  24. }
  25. protected override void OnInit()
  26. {
  27. base.OnInit();
  28. packageName = UI_ItemExchangeUI.PACKAGE_NAME;
  29. _ui = UI_ItemExchangeUI.Create();
  30. this.viewCom = _ui.target;
  31. this.viewCom.Center();
  32. this.modal = true;
  33. _ui.m_btnAdd.target.onClick.Add(OnBtnAddClick);
  34. _ui.m_btnMinus.target.onClick.Add(OnBtnMinusClick);
  35. _ui.m_btnMax.target.onClick.Add(OnBtnMaxClick);
  36. _ui.m_btnConfirm.onClick.Add(OnBtnConfirmClick);
  37. _ui.m_btnCancle.onClick.Add(OnBtnCancleClick);
  38. _ui.m_listItem.itemRenderer = ListItemRenderer;
  39. }
  40. protected override void AddEventListener()
  41. {
  42. base.AddEventListener();
  43. }
  44. protected override void OnShown()
  45. {
  46. base.OnShown();
  47. _itemId = (int)this.viewData;
  48. _count = 1;
  49. UpdateView();
  50. UpdateUseView();
  51. }
  52. protected override void OnHide()
  53. {
  54. base.OnHide();
  55. }
  56. protected override void RemoveEventListener()
  57. {
  58. base.RemoveEventListener();
  59. // EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, UpdateList);
  60. }
  61. private void UpdateView()
  62. {
  63. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  64. _ui.m_txtName.text = itemCfg.name;
  65. _ui.m_txtOwned.SetVar("count", "" + ItemDataManager.GetItemNum(itemCfg.id)).FlushVars();
  66. _ui.m_txtDesc.text = string.IsNullOrEmpty(itemCfg.desc) ? "暂无描述" : itemCfg.desc;
  67. _ui.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg);
  68. RarityIconController.UpdateRarityIcon(_ui.m_loaRarity, itemCfg.id, false);
  69. _ui.m_loaRarity.visible = itemCfg.itemType == ConstItemType.DRESS_UP;
  70. _ui.m_txtExchangeCount.visible = false;
  71. _ui.m_comCost.target.visible = false;
  72. UpdateThItemList(itemCfg);
  73. _ui.m_listItem.numItems = _itemList.Count; //itemCfg.itemsArr.Length;
  74. _ui.m_listItem.visible = itemCfg.itemType == ConstItemType.USEABLE &&
  75. itemCfg.subType != ConstItemSubType.USEABLE_AUTO;
  76. }
  77. private void UpdateThItemList(ItemCfg itemCfg)
  78. {
  79. _itemList.Clear();
  80. //普通物品
  81. foreach (var t in itemCfg.itemsArr)
  82. {
  83. var itemId = t[0];
  84. var itemNum = t.Length > 1 ? t[1] : 1;
  85. _itemList.Add(new[] { itemId, itemNum });
  86. }
  87. if (itemCfg.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM)
  88. {
  89. if (itemCfg.param2Arr.Length > 0)
  90. {
  91. //特殊物品 不存在的套装部件id
  92. List<int> noExistSuitItemIds = new List<int>();
  93. foreach (var suitId in itemCfg.param2Arr)
  94. {
  95. noExistSuitItemIds.Clear();
  96. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  97. foreach (var suitItemId in suitCfg.partsArr)
  98. {
  99. if (!ItemUtil.CheckItemEnough(suitItemId, 1))
  100. {
  101. noExistSuitItemIds.Add(suitItemId);
  102. }
  103. }
  104. foreach (var noExistSuitItemId in noExistSuitItemIds)
  105. {
  106. _itemList.Add(new[] { noExistSuitItemId, 1 });
  107. }
  108. }
  109. }
  110. }
  111. //保底物品
  112. if (itemCfg.param1Arr.Length > 0)
  113. {
  114. foreach (var t in itemCfg.param1Arr)
  115. {
  116. var itemId = t[0];
  117. var itemNum = t.Length > 1 ? t[1] : 1;
  118. _itemList.Add(new[] { itemId, itemNum });
  119. }
  120. }
  121. }
  122. private void UpdateUseView()
  123. {
  124. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  125. _ui.m_txtCostCount.text = _count.ToString();
  126. _ui.m_btnMinus.m_c1.selectedIndex = _count == 1 ? 1 : 0;
  127. _ui.m_btnMinus.target.touchable = _count == 1 ? false : true;
  128. long hasCount = ItemDataManager.GetItemNum(_itemId);
  129. _ui.m_btnAdd.m_c1.selectedIndex = _count == hasCount ? 1 : 0;
  130. _ui.m_btnAdd.target.touchable = _count == hasCount ? false : true;
  131. _ui.m_btnMax.m_c1.selectedIndex = _count == hasCount ? 1 : 0;
  132. _ui.m_btnMax.target.touchable = _count == hasCount ? false : true;
  133. _ui.m_txtShow.text = "选择使用数量";
  134. _ui.m_txtTips.text = string.Format("是否使用{0}个{1}?", _count, itemCfg.name);
  135. }
  136. private void ListItemRenderer(int index, GObject obj)
  137. {
  138. //ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  139. ItemData itemData = ItemUtil.createItemData(_itemList[index]);
  140. if (obj.data == null)
  141. {
  142. obj.data = new ItemView(obj as GComponent);
  143. }
  144. (obj.data as ItemView).SetData(itemData);
  145. (obj.data as ItemView).ChangeTxtCountStyle();
  146. }
  147. private void OnBtnAddClick()
  148. {
  149. _count++;
  150. long hasCount = ItemDataManager.GetItemNum(_itemId);
  151. _count = Math.Min(hasCount, _count);
  152. UpdateUseView();
  153. }
  154. private void OnBtnMinusClick()
  155. {
  156. _count--;
  157. _count = Math.Max(1, _count);
  158. UpdateUseView();
  159. }
  160. private void OnBtnMaxClick()
  161. {
  162. _count = ItemDataManager.GetItemNum(_itemId);
  163. UpdateUseView();
  164. }
  165. private void OnBtnConfirmClick()
  166. {
  167. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  168. if (itemCfg.itemType == ConstItemType.USEABLE &&
  169. itemCfg.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM)
  170. {
  171. ItemProxy.ReqUseRandomItem(_itemId, _count).Coroutine();
  172. }
  173. else
  174. {
  175. ItemProxy.ReqUseItem(_itemId, _count).Coroutine();
  176. }
  177. this.Hide();
  178. }
  179. private void OnBtnCancleClick()
  180. {
  181. this.Hide();
  182. }
  183. }
  184. }