BagExchangeView.cs 6.8 KB

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