ShopBuyItemPanel.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. _shopItemConfig = ConfigComponent.Instance.Get<ShopItemConfig>(shopItem.id);
  32. _callback = callback;
  33. WidgetItem itemWidget = await UIManager.Instance.CreateGComponent<WidgetItem>(null, ItemRoot, poolName: _poolName);
  34. itemWidget.InitWidget(new ItemInfo(_shopItemConfig.itemId[0], _shopItemConfig.itemCount[0]));
  35. ItemConfig itemConfig = ConfigComponent.Instance.Get<ItemConfig>(_shopItemConfig.itemId[0]);
  36. Text_name.text = LanguageManager.Instance.Text(itemConfig.itemName);
  37. Text_desc.text = LanguageManager.Instance.Text(itemWidget.itemInfo.config.itemName);
  38. Text_ItemCount.text = "当前持有:" + PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.itemId[0]);
  39. // UseSlider.value = 1;
  40. // UseSlider.minValue = 1;
  41. Text_BuyCount.text = "1";
  42. Refresh();
  43. UpdateShowData(1);
  44. ChangeBtnState();
  45. }
  46. private void Refresh()
  47. {
  48. //无限制 最多购买十个
  49. if (_shopItemConfig.buyCount == -1)
  50. {
  51. LimitObj.SetActive(false);
  52. Text_LimitCount.text = "无";
  53. if (_shopItemConfig.price != 0)
  54. {
  55. int total = (int)(PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.costItemId) / _shopItemConfig.price);
  56. maxCount = total >= 10 ? 10 : total;
  57. }
  58. if (maxCount <= 1)
  59. {
  60. maxCount = 1;
  61. }
  62. SliderObj.SetActive(maxCount > 1);
  63. // UseSlider.maxValue = maxCount;
  64. // UseSlider.value = 1;
  65. }
  66. else
  67. {
  68. LimitObj.SetActive(true);
  69. Text_LimitCount.text = $"{_shopItemConfig.buyCount - _shopItem.buyCount}/{_shopItemConfig.buyCount}";
  70. maxCount = _shopItemConfig.buyCount - _shopItem.buyCount;
  71. if (_shopItemConfig.price != 0)
  72. {
  73. int temp = (int)(PlayerManager.Instance.BagController.GetItemCount(_shopItemConfig.costItemId) / _shopItemConfig.price);
  74. if (temp < maxCount)
  75. {
  76. maxCount = temp;
  77. }
  78. }
  79. if (maxCount <= 1)
  80. {
  81. maxCount = 1;
  82. }
  83. SliderObj.SetActive(maxCount > 1);
  84. // UseSlider.maxValue = maxCount;
  85. // UseSlider.value = 1;
  86. }
  87. Text_Price.text = _shopItemConfig.price.ToString();
  88. if (_shopItemConfig.costItemId == GlobalParam.RMB_1031)
  89. {
  90. ImageCurrency.gameObject.SetActive(false);
  91. }
  92. else
  93. {
  94. ImageCurrency.gameObject.SetActive(true);
  95. }
  96. ImageCurrency.icon_name = ConfigComponent.Instance.Get<ItemConfig>(_shopItemConfig.costItemId).icon;
  97. }
  98. protected override void AddEvent()
  99. {
  100. }
  101. protected override void DelEvent()
  102. {
  103. }
  104. public override void AddButtonEvent()
  105. {
  106. // Btn_Close.onClick.AddListener((() => { UIManager.Instance.HideUIUIPanel(this); }));
  107. Btn_Close1.onClick.AddListener((() => { UIManager.Instance.HideUIUIPanel(this); }));
  108. // UseSlider.onValueChanged.AddListener(OnValueChanged);
  109. Btn_Add.onClick.AddListener(Add);
  110. Btn_Reduce.onClick.AddListener(Reduce);
  111. Btn_Function.onClick.AddListener(Function);
  112. Btn_Min.onClick.AddListener(ReduceMin);
  113. Btn_Max.onClick.AddListener(AddMax);
  114. }
  115. private async void Function()
  116. {
  117. _buyMark = true;
  118. UIManager.Instance.HideUIUIPanel(this);
  119. }
  120. private void Reduce()
  121. {
  122. currentSelectCount--;
  123. if (currentSelectCount < 1)
  124. {
  125. currentSelectCount = 1;
  126. }
  127. ChangeBtnState();
  128. OnValueChanged(currentSelectCount);
  129. // UseSlider.value = currentSelectCount;
  130. }
  131. private void Add()
  132. {
  133. currentSelectCount++;
  134. if (currentSelectCount > maxCount)
  135. {
  136. currentSelectCount = maxCount;
  137. }
  138. ChangeBtnState();
  139. OnValueChanged(currentSelectCount);
  140. // UseSlider.value = currentSelectCount;
  141. }
  142. private void ReduceMin()
  143. {
  144. currentSelectCount = 1;
  145. ChangeBtnState();
  146. OnValueChanged(currentSelectCount);
  147. }
  148. private void AddMax()
  149. {
  150. currentSelectCount = maxCount;
  151. ChangeBtnState();
  152. OnValueChanged(currentSelectCount);
  153. }
  154. private void ChangeBtnState()
  155. {
  156. if (currentSelectCount == 1)
  157. {
  158. Btn_Min.transform.Gray();
  159. Btn_Reduce.transform.Gray();
  160. }
  161. else
  162. {
  163. Btn_Min.transform.RecoverColor();
  164. Btn_Reduce.transform.RecoverColor();
  165. }
  166. if (currentSelectCount == maxCount)
  167. {
  168. Btn_Max.transform.Gray();
  169. Btn_Add.transform.Gray();
  170. }
  171. else
  172. {
  173. Btn_Max.transform.RecoverColor();
  174. Btn_Add.transform.RecoverColor();
  175. }
  176. }
  177. private void OnValueChanged(float arg0)
  178. {
  179. UpdateShowData(arg0);
  180. }
  181. private void UpdateShowData(float arg0)
  182. {
  183. int count = (int)arg0;
  184. currentSelectCount = count;
  185. Text_BuyCount.text = count.ToString();
  186. Text_Price.text = $"{(count * _shopItemConfig.price).ToString()}";
  187. }
  188. public override void Close()
  189. {
  190. base.Close();
  191. UIManager.Instance.DormancyAllGComponent<WidgetItem>(_poolName);
  192. _callback?.Invoke(_buyMark, currentSelectCount);
  193. }
  194. }
  195. }