ShopBuyItemPanel.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using Core.Language;
  3. using Core.Utility;
  4. using Excel2Json;
  5. using Fort23.Core;
  6. using Fort23.UTool;
  7. using GameLogic.Bag;
  8. namespace Fort23.Mono
  9. {
  10. /// <summary>
  11. /// 商店购买确认弹框
  12. /// </summary>
  13. [UIBinding(prefab = "ShopBuyItemPanel")]
  14. public partial class ShopBuyItemPanel : UIPanel
  15. {
  16. private string _poolName = "ShopBuyItemPanelItemWidget";
  17. private string _ShopPackBuyItemPanelPoolName = "ShopPackBuyItemPanelPoolName";
  18. public int currentSelectCount;
  19. public int maxCount;
  20. private AccountFileInfo.ShopItem _shopItem;
  21. private ShopItemConfig _shopItemConfig;
  22. private Action<bool, int> _callback;
  23. private bool _buyMark = false;
  24. private void Init()
  25. {
  26. }
  27. public async void CustomInit(AccountFileInfo.ShopItem shopItem, Action<bool, int> callback)
  28. {
  29. _buyMark = false;
  30. UIManager.Instance.DormancyAllGComponent<WidgetItem>(_poolName);
  31. _shopItem = shopItem;
  32. _shopItemConfig = ConfigComponent.Instance.Get<ShopItemConfig>(shopItem.id);
  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. ItemConfig itemConfig = ConfigComponent.Instance.Get<ItemConfig>(_shopItemConfig.itemId[0]);
  37. Text_name.text = LanguageManager.Instance.Text(itemConfig.itemName);
  38. Text_desc.text = LanguageManager.Instance.Text(itemWidget.itemInfo.config.itemName);
  39. Text_ItemCount.text = "当前持有:" + PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.itemId[0]);
  40. // UseSlider.value = 1;
  41. // UseSlider.minValue = 1;
  42. Text_BuyCount.text = "1";
  43. Refresh();
  44. UpdateShowData(1);
  45. ChangeBtnState();
  46. }
  47. private void Refresh()
  48. {
  49. //无限制 最多购买十个
  50. if (_shopItemConfig.buyCount == -1)
  51. {
  52. LimitObj.SetActive(false);
  53. Text_LimitCount.text = "无";
  54. if (_shopItemConfig.price != 0)
  55. {
  56. int total = (int)(PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.costItemId) / _shopItemConfig.price);
  57. maxCount = total >= 10 ? 10 : total;
  58. }
  59. if (maxCount <= 1)
  60. {
  61. maxCount = 1;
  62. }
  63. SliderObj.SetActive(maxCount > 1);
  64. // UseSlider.maxValue = maxCount;
  65. // UseSlider.value = 1;
  66. }
  67. else
  68. {
  69. LimitObj.SetActive(true);
  70. Text_LimitCount.text = $"{_shopItemConfig.buyCount - _shopItem.buyCount}/{_shopItemConfig.buyCount}";
  71. maxCount = _shopItemConfig.buyCount - _shopItem.buyCount;
  72. if (_shopItemConfig.price != 0)
  73. {
  74. int temp = (int)(PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.costItemId) / _shopItemConfig.price);
  75. if (temp < maxCount)
  76. {
  77. maxCount = temp;
  78. }
  79. }
  80. if (maxCount <= 1)
  81. {
  82. maxCount = 1;
  83. }
  84. SliderObj.SetActive(maxCount > 1);
  85. // UseSlider.maxValue = maxCount;
  86. // UseSlider.value = 1;
  87. }
  88. Text_Price.text = _shopItemConfig.price.ToString();
  89. if (_shopItemConfig.costItemId == GlobalParam.RMB_1031)
  90. {
  91. ImageCurrency.gameObject.SetActive(false);
  92. }
  93. else
  94. {
  95. ImageCurrency.gameObject.SetActive(true);
  96. }
  97. ImageCurrency.icon_name = ConfigComponent.Instance.Get<ItemConfig>(_shopItemConfig.costItemId).icon;
  98. }
  99. protected override void AddEvent()
  100. {
  101. }
  102. protected override void DelEvent()
  103. {
  104. }
  105. public override void AddButtonEvent()
  106. {
  107. // Btn_Close.onClick.AddListener((() => { UIManager.Instance.HideUIUIPanel(this); }));
  108. Btn_Close1.onClick.AddListener((() => { UIManager.Instance.HideUIUIPanel(this); }));
  109. // UseSlider.onValueChanged.AddListener(OnValueChanged);
  110. Btn_Add.onClick.AddListener(Add);
  111. Btn_Reduce.onClick.AddListener(Reduce);
  112. Btn_Function.onClick.AddListener(Function);
  113. Btn_Min.onClick.AddListener(ReduceMin);
  114. Btn_Max.onClick.AddListener(AddMax);
  115. }
  116. private async void Function()
  117. {
  118. _buyMark = true;
  119. UIManager.Instance.HideUIUIPanel(this);
  120. }
  121. private void Reduce()
  122. {
  123. currentSelectCount--;
  124. if (currentSelectCount < 1)
  125. {
  126. currentSelectCount = 1;
  127. }
  128. ChangeBtnState();
  129. OnValueChanged(currentSelectCount);
  130. // UseSlider.value = currentSelectCount;
  131. }
  132. private void Add()
  133. {
  134. currentSelectCount++;
  135. if (currentSelectCount > maxCount)
  136. {
  137. currentSelectCount = maxCount;
  138. }
  139. ChangeBtnState();
  140. OnValueChanged(currentSelectCount);
  141. // UseSlider.value = currentSelectCount;
  142. }
  143. private void ReduceMin()
  144. {
  145. currentSelectCount = 1;
  146. ChangeBtnState();
  147. OnValueChanged(currentSelectCount);
  148. }
  149. private void AddMax()
  150. {
  151. currentSelectCount = maxCount;
  152. ChangeBtnState();
  153. OnValueChanged(currentSelectCount);
  154. }
  155. private void ChangeBtnState()
  156. {
  157. if (currentSelectCount == 1)
  158. {
  159. Btn_Min.transform.Gray();
  160. Btn_Reduce.transform.Gray();
  161. }
  162. else
  163. {
  164. Btn_Min.transform.RecoverColor();
  165. Btn_Reduce.transform.RecoverColor();
  166. }
  167. if (currentSelectCount == maxCount)
  168. {
  169. Btn_Max.transform.Gray();
  170. Btn_Add.transform.Gray();
  171. }
  172. else
  173. {
  174. Btn_Max.transform.RecoverColor();
  175. Btn_Add.transform.RecoverColor();
  176. }
  177. }
  178. private void OnValueChanged(float arg0)
  179. {
  180. UpdateShowData(arg0);
  181. }
  182. private void UpdateShowData(float arg0)
  183. {
  184. int count = (int)arg0;
  185. currentSelectCount = count;
  186. Text_BuyCount.text = count.ToString();
  187. Text_Price.text = $"{(count * _shopItemConfig.price).ToString()}";
  188. }
  189. public async override CTask Close()
  190. {
  191. await base.Close();
  192. UIManager.Instance.DormancyAllGComponent<WidgetItem>(_poolName);
  193. _callback?.Invoke(_buyMark, currentSelectCount);
  194. }
  195. }
  196. }