BagExchangeView.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. if (_ui != null)
  21. {
  22. _ui.Dispose();
  23. _ui = null;
  24. }
  25. base.Dispose();
  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. _ui.m_grpScore.visible = false;
  73. _ui.m_listTag.visible = false;
  74. UpdateThItemList(itemCfg);
  75. _ui.m_listItem.numItems = _itemList.Count; //itemCfg.itemsArr.Length;
  76. _ui.m_listItem.visible = itemCfg.itemType == ConstItemType.USEABLE &&
  77. itemCfg.subType != ConstItemSubType.USEABLE_AUTO;
  78. }
  79. private void UpdateThItemList(ItemCfg itemCfg)
  80. {
  81. _itemList.Clear();
  82. //普通物品
  83. foreach (var t in itemCfg.itemsArr)
  84. {
  85. var itemId = t[0];
  86. var itemNum = t.Length > 1 ? t[1] : 1;
  87. _itemList.Add(new[] { itemId, itemNum });
  88. }
  89. if (itemCfg.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM)
  90. {
  91. if (itemCfg.param2Arr.Length > 0)
  92. {
  93. //特殊物品 不存在的套装部件id
  94. List<int> noExistSuitItemIds = new List<int>();
  95. foreach (var suitId in itemCfg.param2Arr)
  96. {
  97. noExistSuitItemIds.Clear();
  98. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  99. foreach (var suitItemId in suitCfg.partsArr)
  100. {
  101. if (!ItemUtil.CheckItemEnough(suitItemId, 1))
  102. {
  103. noExistSuitItemIds.Add(suitItemId);
  104. }
  105. }
  106. foreach (var noExistSuitItemId in noExistSuitItemIds)
  107. {
  108. _itemList.Add(new[] { noExistSuitItemId, 1 });
  109. }
  110. }
  111. }
  112. }
  113. //保底物品
  114. if (itemCfg.param1Arr.Length > 0)
  115. {
  116. foreach (var t in itemCfg.param1Arr)
  117. {
  118. var itemId = t[0];
  119. var itemNum = t.Length > 1 ? t[1] : 1;
  120. _itemList.Add(new[] { itemId, itemNum });
  121. }
  122. }
  123. }
  124. private void UpdateUseView()
  125. {
  126. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  127. _ui.m_txtCostCount.text = _count.ToString();
  128. _ui.m_btnMinus.m_c1.selectedIndex = _count == 1 ? 1 : 0;
  129. _ui.m_btnMinus.target.touchable = _count == 1 ? false : true;
  130. long hasCount = ItemDataManager.GetItemNum(_itemId);
  131. _ui.m_btnAdd.GetController("c1").selectedIndex = _count == hasCount ? 1 : 0;
  132. _ui.m_btnAdd.touchable = _count == hasCount ? false : true;
  133. _ui.m_btnMax.m_c1.selectedIndex = _count == hasCount ? 1 : 0;
  134. _ui.m_btnMax.target.touchable = _count == hasCount ? false : true;
  135. _ui.m_txtExchangeCount.text = string.Format("{0}", hasCount);
  136. _ui.m_txtShow.text = "选择使用数量";
  137. _ui.m_txtTips.text = string.Format("是否使用{0}个{1}?", _count, itemCfg.name);
  138. }
  139. private void ListItemRenderer(int index, GObject obj)
  140. {
  141. //ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  142. ItemData itemData = ItemUtil.createItemData(_itemList[index]);
  143. if (obj.data == null)
  144. {
  145. obj.data = new ItemView(obj as GComponent);
  146. }
  147. (obj.data as ItemView).SetData(itemData);
  148. (obj.data as ItemView).ChangeTxtCountStyle();
  149. }
  150. private void OnBtnAddClick()
  151. {
  152. _count++;
  153. long hasCount = ItemDataManager.GetItemNum(_itemId);
  154. _count = Math.Min(hasCount, _count);
  155. UpdateUseView();
  156. }
  157. private void OnBtnMinusClick()
  158. {
  159. _count--;
  160. _count = Math.Max(1, _count);
  161. UpdateUseView();
  162. }
  163. private void OnBtnMaxClick()
  164. {
  165. _count = ItemDataManager.GetItemNum(_itemId);
  166. UpdateUseView();
  167. }
  168. private void OnBtnConfirmClick()
  169. {
  170. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  171. if (itemCfg.itemType == ConstItemType.USEABLE &&
  172. itemCfg.subType == ConstItemSubType.USEABLE_GIFT_BAG_RANDOM)
  173. {
  174. ItemProxy.ReqUseRandomItem(_itemId, _count).Coroutine();
  175. }
  176. else
  177. {
  178. ItemProxy.ReqUseItem(_itemId, _count).Coroutine();
  179. }
  180. this.Hide();
  181. }
  182. private void OnBtnCancleClick()
  183. {
  184. this.Hide();
  185. }
  186. }
  187. }