ShopBuyItemPanel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using Core.Language;
  3. using Core.Utility;
  4. using Excel2Json;
  5. using Fort23.UTool;
  6. using GameLogic.Bag;
  7. namespace Fort23.Mono
  8. {
  9. /// <summary>
  10. /// 商店购买确认弹框
  11. /// </summary>
  12. [UIBinding(prefab = "ShopBuyItemPanel")]
  13. public partial class ShopBuyItemPanel : UIPanel
  14. {
  15. private string _poolName = "ShopBuyItemPanelItemWidget";
  16. private string _ShopPackBuyItemPanelPoolName = "ShopPackBuyItemPanelPoolName";
  17. public int currentSelectCount;
  18. public int maxCount;
  19. private AccountFileInfo.ShopItem _shopItem;
  20. private ShopItemConfig _shopItemConfig;
  21. private Action<bool, int> _callback;
  22. private bool _buyMark = false;
  23. private void Init()
  24. {
  25. }
  26. public async void CustomInit(AccountFileInfo.ShopItem shopItem, Action<bool, int> callback)
  27. {
  28. _buyMark = false;
  29. UIManager.Instance.DormancyAllGComponent<WidgetItem>(_poolName);
  30. _shopItem = shopItem;
  31. ShopConfig shopConfig = ConfigComponent.Instance.Get<ShopConfig>(shopItem.id);
  32. _shopItemConfig = ConfigComponent.Instance.Get<ShopItemConfig>(shopConfig.shopItemID);
  33. _callback = callback;
  34. WidgetItem itemWidget = await UIManager.Instance.CreateGComponent<WidgetItem>(null, ItemRoot, poolName: _poolName);
  35. itemWidget.InitWidget(new ItemInfo(_shopItemConfig.itemId[0], _shopItemConfig.itemCount[0]));
  36. Text_name.text = LanguageManager.Instance.Text(shopConfig.shopName);
  37. Text_desc.text = LanguageManager.Instance.Text(itemWidget.itemInfo.config.itemName);
  38. UseSlider.value = 1;
  39. UseSlider.minValue = 1;
  40. Text_BuyCount.text = "1";
  41. Refresh();
  42. UpdateShowData(1);
  43. }
  44. private void Refresh()
  45. {
  46. //无限制 最多购买十个
  47. if (_shopItemConfig.buyCount == -1)
  48. {
  49. LimitObj.SetActive(false);
  50. Text_LimitCount.text = "无";
  51. int total = (int)(PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.costItemId) / _shopItemConfig.price);
  52. maxCount = total >= 10 ? 10 : total;
  53. if (maxCount <= 1)
  54. {
  55. maxCount = 1;
  56. }
  57. SliderObj.SetActive(maxCount > 1);
  58. UseSlider.maxValue = maxCount;
  59. UseSlider.value = 1;
  60. }
  61. else
  62. {
  63. LimitObj.SetActive(true);
  64. Text_LimitCount.text = $"{_shopItemConfig.buyCount - _shopItem.buyCount}/{_shopItemConfig.buyCount}";
  65. maxCount = _shopItemConfig.buyCount - _shopItem.buyCount;
  66. int temp = (int)(PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.costItemId) / _shopItemConfig.price);
  67. if (temp < maxCount)
  68. {
  69. maxCount = temp;
  70. }
  71. if (maxCount <= 1)
  72. {
  73. maxCount = 1;
  74. }
  75. SliderObj.SetActive(maxCount > 1);
  76. UseSlider.maxValue = maxCount;
  77. UseSlider.value = 1;
  78. }
  79. Text_Price.text = _shopItemConfig.price.ToString();
  80. if (_shopItemConfig.costItemId == GlobalParam.RMB_1031)
  81. {
  82. ImageCurrency.gameObject.SetActive(false);
  83. }
  84. else
  85. {
  86. ImageCurrency.gameObject.SetActive(true);
  87. }
  88. ImageCurrency.icon_name = ConfigComponent.Instance.Get<ItemConfig>(_shopItemConfig.costItemId).icon;
  89. }
  90. protected override void AddEvent()
  91. {
  92. }
  93. protected override void DelEvent()
  94. {
  95. }
  96. public override void AddButtonEvent()
  97. {
  98. Btn_Close.onClick.AddListener((() => { UIManager.Instance.HideUIUIPanel(this); }));
  99. Btn_Close1.onClick.AddListener((() => { UIManager.Instance.HideUIUIPanel(this); }));
  100. UseSlider.onValueChanged.AddListener(OnValueChanged);
  101. Btn_Add.onClick.AddListener(Add);
  102. Btn_Reduce.onClick.AddListener(Reduce);
  103. Btn_Function.onClick.AddListener(Function);
  104. }
  105. private async void Function()
  106. {
  107. _buyMark = true;
  108. UIManager.Instance.HideUIUIPanel(this);
  109. }
  110. private void Reduce()
  111. {
  112. currentSelectCount--;
  113. if (currentSelectCount < 1)
  114. {
  115. currentSelectCount = 1;
  116. }
  117. UseSlider.value = currentSelectCount;
  118. }
  119. private void Add()
  120. {
  121. currentSelectCount++;
  122. if (currentSelectCount > maxCount)
  123. {
  124. currentSelectCount = maxCount;
  125. }
  126. UseSlider.value = currentSelectCount;
  127. }
  128. private void OnValueChanged(float arg0)
  129. {
  130. UpdateShowData(arg0);
  131. }
  132. private void UpdateShowData(float arg0)
  133. {
  134. int count = (int)arg0;
  135. currentSelectCount = count;
  136. Text_BuyCount.text = count.ToString();
  137. Text_Price.text = $"{(count * _shopItemConfig.price).ToString()}";
  138. }
  139. public override void Close()
  140. {
  141. base.Close();
  142. UIManager.Instance.DormancyAllGComponent<WidgetItem>(_poolName);
  143. _callback?.Invoke(_buyMark, currentSelectCount);
  144. }
  145. }
  146. }