ItemExchangeView.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Threading.Tasks;
  3. using ET;
  4. using FairyGUI;
  5. using UI.CommonGame;
  6. namespace GFGGame
  7. {
  8. public class ItemExchangeView : BaseWindow
  9. {
  10. private UI_ItemExchangeUI _ui;
  11. private ShopCfg _shopCfg;
  12. private int _goodIds;
  13. private long _buyCount = 0;
  14. private long _maxCanBuy = 0;
  15. public override void Dispose()
  16. {
  17. if (_ui != null)
  18. {
  19. _ui.Dispose();
  20. }
  21. _ui = null;
  22. base.Dispose();
  23. }
  24. protected override void OnHide()
  25. {
  26. base.OnHide();
  27. }
  28. protected override void OnInit()
  29. {
  30. base.OnInit();
  31. packageName = UI_ItemExchangeUI.PACKAGE_NAME;
  32. _ui = UI_ItemExchangeUI.Create();
  33. this.viewCom = _ui.target;
  34. this.viewCom.Center();
  35. this.modal = true;
  36. viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
  37. _ui.m_btnAdd.target.onClick.Add(OnBtnPlusClick);
  38. _ui.m_btnMinus.target.onClick.Add(OnBtnMinusClick);
  39. _ui.m_btnMax.target.onClick.Add(OnBtnAllClick);
  40. _ui.m_btnConfirm.onClick.Add(OnBtnExchangeClick);
  41. _ui.m_btnCancle.onClick.Add(this.Hide);
  42. _ui.m_listItem.itemRenderer = ListItemRenderer;
  43. }
  44. protected override void OnShown()
  45. {
  46. base.OnShown();
  47. _goodIds = (int)this.viewData;
  48. _shopCfg = ShopCfgArray.Instance.GetCfg(_goodIds);
  49. _buyCount = 1;
  50. if (_shopCfg.costType == CostType.RMB)
  51. {
  52. _maxCanBuy = 1;
  53. }
  54. else
  55. {
  56. long curMoneyCanBuy = ItemDataManager.GetItemNum(_shopCfg.costId) / _shopCfg.price;
  57. if (_shopCfg.maxBuyNum == 0)
  58. {
  59. _maxCanBuy = Math.Min(curMoneyCanBuy, GameConst.MAX_COUNT_TO_BUY_ITEMS);
  60. }
  61. else
  62. {
  63. int lastBuyCount = _shopCfg.maxBuyNum - ShopDataManager.Instance.GetGoodsBuyNumById(_shopCfg.id);//剩余购买次数
  64. _maxCanBuy = Math.Min(curMoneyCanBuy, lastBuyCount);
  65. }
  66. }
  67. _maxCanBuy = Math.Max(1, _maxCanBuy);
  68. _ui.m_txtExchangeCount.visible = true;
  69. _ui.m_comCost.target.visible = true;
  70. UpdateStaticView();
  71. UpdateView();
  72. }
  73. private void UpdateStaticView()
  74. {
  75. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_shopCfg.itemId);
  76. _ui.m_txtName.text = itemCfg.name;
  77. _ui.m_txtDesc.text = string.IsNullOrEmpty(itemCfg.desc) ? "暂无描述" : itemCfg.desc;
  78. _ui.m_txtOwned.SetVar("count", ItemDataManager.GetItemNum(_shopCfg.itemId).ToString()).FlushVars();
  79. _ui.m_loaIcon.url = ResPathUtil.GetIconPath(itemCfg);
  80. _ui.m_loaRarity.visible = false;
  81. if (ItemUtilCS.IsDressUpItem(_shopCfg.itemId))
  82. {
  83. _ui.m_loaRarity.visible = true;
  84. RarityIconController.UpdateRarityIcon(_ui.m_loaRarity, _shopCfg.itemId, false);
  85. }
  86. _ui.m_listItem.numItems = itemCfg.itemsArr.Length;
  87. _ui.m_listItem.visible = itemCfg.itemsArr.Length > 0;
  88. }
  89. private void ListItemRenderer(int index, GObject obj)
  90. {
  91. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_shopCfg.itemId);
  92. ItemData itemData = ItemUtil.createItemData(itemCfg.itemsArr[index]);
  93. if (obj.data == null)
  94. {
  95. obj.data = new ItemView(obj as GComponent);
  96. }
  97. (obj.data as ItemView).SetData(itemData);
  98. (obj.data as ItemView).ChangeTxtCountStyle();
  99. }
  100. private void UpdateView()
  101. {
  102. _ui.m_txtExchangeCount.text = string.Format("x{0}", _shopCfg.itemNum * _buyCount);
  103. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_shopCfg.itemId);
  104. _ui.m_txtCostCount.text = _buyCount.ToString();
  105. _ui.m_btnMinus.m_c1.selectedIndex = _buyCount == 1 ? 1 : 0;
  106. _ui.m_btnMinus.target.touchable = _buyCount == 1 ? false : true;
  107. // int hasCount = ItemDataManager.GetItemNum(cfg.itemId);
  108. _ui.m_btnAdd.m_c1.selectedIndex = _shopCfg.costType == CostType.RMB || _buyCount == _maxCanBuy ? 1 : 0;
  109. _ui.m_btnAdd.target.touchable = _shopCfg.costType == CostType.RMB || _buyCount == _maxCanBuy ? false : true;
  110. _ui.m_btnMax.m_c1.selectedIndex = _shopCfg.costType == CostType.RMB || _buyCount == _maxCanBuy ? 1 : 0;
  111. _ui.m_btnMax.target.touchable = _shopCfg.costType == CostType.RMB || _buyCount == _maxCanBuy ? false : true;
  112. _ui.m_txtShow.text = "选择购买数量";
  113. _ui.m_txtTips.text = string.Format("确定购买{0}个{1}?", _buyCount, itemCfg.name);
  114. if (_shopCfg.costType == CostType.RMB)
  115. {
  116. _ui.m_comCost.target.visible = false;
  117. _ui.m_txtRmbCost.visible = true;
  118. _ui.m_txtRmbCost.text = string.Format("¥ {0}", _shopCfg.price);
  119. // _ui.m_txtRmbCost.SetVar("value", _shopCfg.price.ToString()).FlushVars();
  120. }
  121. else
  122. {
  123. _ui.m_comCost.target.visible = true;
  124. _ui.m_txtRmbCost.visible = false;
  125. ItemUtil.UpdateItemNeedNum(_ui.m_comCost.target, _shopCfg.costId, (int)_buyCount * _shopCfg.price);
  126. }
  127. }
  128. private void OnBtnPlusClick()
  129. {
  130. _buyCount++;
  131. _buyCount = Math.Min(_buyCount, _maxCanBuy);
  132. UpdateView();
  133. }
  134. private void OnBtnMinusClick()
  135. {
  136. // if (_buyCount == 1)
  137. // {
  138. // PromptController.Instance.ShowFloatTextPrompt("已经是最小数量了");
  139. // return;
  140. // }
  141. _buyCount--;
  142. _buyCount = Math.Max(1, _buyCount);
  143. UpdateView();
  144. }
  145. private void OnBtnAllClick()
  146. {
  147. _buyCount = Math.Max(1, _maxCanBuy);
  148. UpdateView();
  149. }
  150. private void OnBtnExchangeClick()
  151. {
  152. ShopCfg cfg = ShopCfgArray.Instance.GetCfg(_goodIds);
  153. if (cfg.costType != CostType.RMB)
  154. {
  155. int costId = cfg.costId;
  156. if (cfg.menu1 == ConstStoreTabId.STORE_ARENA && cfg.menu2 != ArenaDataManager.Instance.SeasonId && cfg.menu2 != ConstStoreSubId.STORE_ARENA_ITEM)
  157. {
  158. costId = cfg.oldSeasonCostId;
  159. }
  160. if (costId == ConstItemID.ARENA_PAST_COST && ItemDataManager.GetItemNum(costId) < cfg.price
  161. && ItemDataManager.GetItemNum(costId) + ItemDataManager.GetItemNum(ConstItemID.ARENA_CUR_COST) >= cfg.price)
  162. {
  163. string oldSeasonName = ItemCfgArray.Instance.GetCfg(ConstItemID.ARENA_PAST_COST).name;
  164. string curSeasonName = ItemCfgArray.Instance.GetCfg(ConstItemID.ARENA_CUR_COST).name;
  165. string strTips = string.Format("{0}不足,是否确定将{1}1:1转化为花签够买该商品?", oldSeasonName, curSeasonName);
  166. AlertUI.Show(strTips, "")
  167. .SetLeftButton(true, "否").SetRightButton(true, "是", (object data) =>
  168. {
  169. ReqBuyGoodsAsync(_goodIds);
  170. });
  171. return;
  172. }
  173. else if (ItemDataManager.GetItemNum(costId) < cfg.price)
  174. {
  175. PromptController.Instance.ShowFloatTextPrompt("道具不足");
  176. return;
  177. }
  178. }
  179. ReqBuyGoodsAsync(_goodIds);
  180. }
  181. private async void ReqBuyGoodsAsync(int _goodIds)
  182. {
  183. bool result = await ShopSProxy.ReqShopBuy(_goodIds, _buyCount);
  184. if (result)
  185. {
  186. LogServerHelper.SendPlayParticipationLog((int)PlayParticipationEnum.SHANG_CHENG, 2);
  187. this.Hide();
  188. }
  189. }
  190. }
  191. }