ShopBuyItemPanel.cs 8.0 KB

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