BagExchangeView.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. using ET;
  4. using FairyGUI;
  5. using UI.Bag;
  6. using UnityEngine;
  7. using UI.CommonGame;
  8. namespace GFGGame
  9. {
  10. public class BagExchangeView : BaseWindow
  11. {
  12. private UI.Bag.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.Bag.UI_ItemExchangeUI.PACKAGE_NAME;
  31. _ui = UI.Bag.UI_ItemExchangeUI.Create();
  32. this.viewCom = _ui.target;
  33. this.viewCom.Center();
  34. this.modal = true;
  35. _ui.m_btnAdd.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. _ui.m_comBg.GetChild("btnClose").asCom.onClick.Add(Hide);
  42. }
  43. protected override void AddEventListener()
  44. {
  45. base.AddEventListener();
  46. }
  47. protected override void OnShown()
  48. {
  49. base.OnShown();
  50. _itemId = (int)this.viewData;
  51. _count = 1;
  52. UpdateView();
  53. UpdateUseView();
  54. }
  55. protected override void OnHide()
  56. {
  57. base.OnHide();
  58. }
  59. protected override void RemoveEventListener()
  60. {
  61. base.RemoveEventListener();
  62. // EventAgent.RemoveEventListener(ConstMessage.ITEM_CHANGED, UpdateList);
  63. }
  64. private void UpdateView()
  65. {
  66. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  67. _ui.m_comBg.GetChild("txtTitle").asTextField.text = itemCfg.name;
  68. _ui.m_txtOwned.SetVar("count", "" + ItemDataManager.GetItemNum(itemCfg.id)).FlushVars();
  69. _ui.m_txtDesc.text = string.IsNullOrEmpty(itemCfg.desc) ? "暂无描述" : itemCfg.desc;
  70. _ui.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg);
  71. _ui.m_txtRmbCost.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.GetController("c1").selectedIndex = _count == hasCount ? 1 : 0;
  130. _ui.m_btnAdd.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_txtExchangeCount.text = string.Format("{0}", hasCount);
  134. _ui.m_txtShow.text = "选择使用数量";
  135. _ui.m_txtTips.text = string.Format("是否使用{0}个{1}?", _count, itemCfg.name);
  136. }
  137. private void ListItemRenderer(int index, GObject obj)
  138. {
  139. //ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  140. ItemData itemData = ItemUtil.createItemData(_itemList[index]);
  141. if (obj.data == null)
  142. {
  143. obj.data = new ItemView(obj as GComponent);
  144. }
  145. (obj.data as ItemView).SetData(itemData);
  146. (obj.data as ItemView).ChangeTxtCountStyle();
  147. }
  148. private void OnBtnAddClick()
  149. {
  150. _count++;
  151. long hasCount = ItemDataManager.GetItemNum(_itemId);
  152. _count = Math.Min(hasCount, _count);
  153. UpdateUseView();
  154. }
  155. private void OnBtnMinusClick()
  156. {
  157. _count--;
  158. _count = Math.Max(1, _count);
  159. UpdateUseView();
  160. }
  161. private void OnBtnMaxClick()
  162. {
  163. _count = ItemDataManager.GetItemNum(_itemId);
  164. UpdateUseView();
  165. }
  166. private void OnBtnConfirmClick()
  167. {
  168. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  169. if (itemCfg.itemType == ConstItemType.USEABLE &&
  170. itemCfg.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM)
  171. {
  172. ItemProxy.ReqUseRandomItem(_itemId, _count).Coroutine();
  173. }
  174. else
  175. {
  176. ItemProxy.ReqUseItem(_itemId, _count).Coroutine();
  177. }
  178. this.Hide();
  179. }
  180. private void OnBtnCancleClick()
  181. {
  182. this.Hide();
  183. }
  184. }
  185. }