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. // 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. _ui.m_txtRmbCost.visible = false;
  71. UpdateThItemList(itemCfg);
  72. _ui.m_listItem.numItems = _itemList.Count; //itemCfg.itemsArr.Length;
  73. _ui.m_listItem.visible = itemCfg.itemType == ConstItemType.USEABLE &&
  74. itemCfg.subType != ConstItemSubType.USEABLE_AUTO;
  75. }
  76. private void UpdateThItemList(ItemCfg itemCfg)
  77. {
  78. _itemList.Clear();
  79. //普通物品
  80. foreach (var t in itemCfg.itemsArr)
  81. {
  82. var itemId = t[0];
  83. var itemNum = t.Length > 1 ? t[1] : 1;
  84. _itemList.Add(new[] { itemId, itemNum });
  85. }
  86. if (itemCfg.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM)
  87. {
  88. if (itemCfg.param2Arr.Length > 0)
  89. {
  90. //特殊物品 不存在的套装部件id
  91. List<int> noExistSuitItemIds = new List<int>();
  92. foreach (var suitId in itemCfg.param2Arr)
  93. {
  94. noExistSuitItemIds.Clear();
  95. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  96. foreach (var suitItemId in suitCfg.partsArr)
  97. {
  98. if (!ItemUtil.CheckItemEnough(suitItemId, 1))
  99. {
  100. noExistSuitItemIds.Add(suitItemId);
  101. }
  102. }
  103. foreach (var noExistSuitItemId in noExistSuitItemIds)
  104. {
  105. _itemList.Add(new[] { noExistSuitItemId, 1 });
  106. }
  107. }
  108. }
  109. }
  110. //保底物品
  111. if (itemCfg.param1Arr.Length > 0)
  112. {
  113. foreach (var t in itemCfg.param1Arr)
  114. {
  115. var itemId = t[0];
  116. var itemNum = t.Length > 1 ? t[1] : 1;
  117. _itemList.Add(new[] { itemId, itemNum });
  118. }
  119. }
  120. }
  121. private void UpdateUseView()
  122. {
  123. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  124. _ui.m_txtCostCount.text = _count.ToString();
  125. _ui.m_btnMinus.m_c1.selectedIndex = _count == 1 ? 1 : 0;
  126. _ui.m_btnMinus.target.touchable = _count == 1 ? false : true;
  127. long hasCount = ItemDataManager.GetItemNum(_itemId);
  128. _ui.m_btnAdd.m_c1.selectedIndex = _count == hasCount ? 1 : 0;
  129. _ui.m_btnAdd.target.touchable = _count == hasCount ? false : true;
  130. _ui.m_btnMax.m_c1.selectedIndex = _count == hasCount ? 1 : 0;
  131. _ui.m_btnMax.target.touchable = _count == hasCount ? false : true;
  132. _ui.m_txtExchangeCount.text = string.Format("x{0}", hasCount);
  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. }