BuyCountView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using UI.CommonGame;
  2. using System;
  3. using FairyGUI;
  4. using System.Collections.Generic;
  5. namespace GFGGame
  6. {
  7. public class BuyCountView : BaseWindow
  8. {
  9. private UI_BuyCountUI _ui;
  10. // private ClothingShopCfg _cfg;
  11. private float _selectTimeCount = 0;//长按时间
  12. private int _consumeSelectIndex = 0;//0为减,1为加
  13. private float _delay = 300;
  14. private float longpress = 900;//大于900毫秒才算长按
  15. private int _itemId;
  16. private int _costId;
  17. private int _perCount;
  18. private int _perCostCount;
  19. private int _count;
  20. private int _maxCount;
  21. private bool _openSource;
  22. private bool _showTips;
  23. private int _buyType;
  24. private int _shopType;
  25. private Action _onSuccess;
  26. public override void Dispose()
  27. {
  28. base.Dispose();
  29. }
  30. protected override void OnInit()
  31. {
  32. base.OnInit();
  33. _ui = UI_BuyCountUI.Create();
  34. this.viewCom = _ui.target;
  35. this.viewCom.Center();
  36. this.modal = true;
  37. viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
  38. _ui.m_btnPlus.onClick.Add(OnClickBtnPlus);
  39. _ui.m_btnMinus.onClick.Add(OnClickBtnMinus);
  40. _ui.m_btnAll.onClick.Add(OnClickBtnAll);
  41. _ui.m_btnPlus.onTouchBegin.Add(OnTouchPlusBegin);
  42. _ui.m_btnMinus.onTouchBegin.Add(OnTouchMinusBegin);
  43. _ui.m_btnPlus.onTouchEnd.Add(() => { Timers.inst.Remove(OnTimedEvent); });
  44. _ui.m_btnMinus.onTouchEnd.Add(() => { Timers.inst.Remove(OnTimedEvent); });
  45. _ui.m_btnSure.onClick.Add(OnClickBtnSure);
  46. _ui.m_btnCancel.onClick.Add(OnClickBtnCancel);
  47. }
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. /// <param name="itemId">物品id</param>
  52. /// <param name="costId">消耗品id</param>
  53. /// <param name="perCount">单位兑换数量</param>
  54. /// <param name="perCostCount">单位消耗数量</param>
  55. /// <param name="onSuccess">购买完成回调</param>
  56. /// <param name="showTips">是否弹购买成功飘字,默认是</param>
  57. /// <param name="openSource">是否打开来源界面。默认否</param>
  58. /// <param name="count">兑换总量</param>
  59. public void SetParams(int itemId, int costId, int perCount, int perCostCount, int count, int buyType, int shopType = 0, Action onSuccess = null, bool showTips = true, bool openSource = false, int maxCount = 9990)
  60. {
  61. _itemId = itemId;
  62. _costId = costId;
  63. _perCount = perCount;
  64. _perCostCount = perCostCount;
  65. _count = count;
  66. _onSuccess = onSuccess;
  67. _maxCount = maxCount;
  68. _openSource = openSource;
  69. _showTips = showTips;
  70. _buyType = buyType;
  71. _shopType = shopType;
  72. }
  73. protected override void OnShown()
  74. {
  75. base.OnShown();
  76. _ui.m_txtCount.text = "" + _count;
  77. UpdateView();
  78. }
  79. private void UpdateView()
  80. {
  81. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  82. _ui.m_icon.url = ResPathUtil.GetIconPath(itemCfg);
  83. string itemName = itemCfg.name;
  84. _ui.m_txtName.text = itemName;
  85. if (ItemUtilCS.IsDressUpItem(_itemId))
  86. {
  87. _ui.m_rarity.visible = true;
  88. RarityIconController.UpdateRarityIcon(_ui.m_rarity, _itemId, false);
  89. }
  90. else
  91. {
  92. _ui.m_rarity.visible = false;
  93. }
  94. ItemCfg costItemCfg = ItemCfgArray.Instance.GetCfg(_costId);
  95. _ui.m_iconPrice.url = "ui://CommonGame/" + costItemCfg.res;
  96. _ui.m_txtBuyTips.text = string.Format("消耗{0}{1},可兑换{2}{3}", _perCostCount, costItemCfg.name, _perCount, itemCfg.name);
  97. _ui.m_txtBuyTips.visible = false;
  98. UpdateCost();
  99. }
  100. private void UpdateCost()
  101. {
  102. int count = int.Parse(_ui.m_txtCount.text.Trim());
  103. int price = (int)Math.Ceiling((decimal)count / _perCount) * _perCostCount;
  104. _ui.m_txtPrice.text = "" + price;
  105. SetBtnState();
  106. }
  107. private void OnTouchPlusBegin()
  108. {
  109. _selectTimeCount = 0;
  110. _consumeSelectIndex = 1;
  111. Timers.inst.Add(_delay / 1000, 0, OnTimedEvent);
  112. }
  113. private void OnTouchMinusBegin()
  114. {
  115. _selectTimeCount = 0;
  116. _consumeSelectIndex = 0;
  117. Timers.inst.Add(_delay / 1000, 0, OnTimedEvent);
  118. }
  119. private void OnTimedEvent(object param)
  120. {
  121. _selectTimeCount += _delay; //_timer.Interval;
  122. if (_selectTimeCount >= longpress)
  123. {
  124. if (_consumeSelectIndex == 0)
  125. {
  126. if (!CommonUtil.Instance.CheckPointIsOnComponent(_ui.m_btnMinus, CommonUtil.Instance.GetMouseV2Point()))
  127. {
  128. Timers.inst.Remove(OnTimedEvent);
  129. return;
  130. }
  131. this.OnClickBtnMinus();
  132. }
  133. else
  134. {
  135. if (!CommonUtil.Instance.CheckPointIsOnComponent(_ui.m_btnPlus, CommonUtil.Instance.GetMouseV2Point()))
  136. {
  137. Timers.inst.Remove(OnTimedEvent);
  138. return;
  139. }
  140. this.OnClickBtnPlus();
  141. }
  142. }
  143. }
  144. private void OnClickBtnAll()
  145. {
  146. int costHasNum = ItemDataManager.GetItemNum(_costId);
  147. int value = (int)Math.Floor((decimal)(costHasNum / _perCostCount * _perCount));
  148. // int value = ItemUtil.ItemExChangeCount(_itemId, costHasNum);
  149. value = Math.Min(_maxCount, value);
  150. _ui.m_txtCount.text = value.ToString();
  151. UpdateCost();
  152. }
  153. private void OnClickBtnPlus()
  154. {
  155. string inputStr = _ui.m_txtCount.text.Trim();
  156. int value = _count;
  157. if (inputStr.Length > 0)
  158. {
  159. value = int.Parse(inputStr);
  160. }
  161. if (value < _maxCount)
  162. {
  163. value += _perCount;
  164. }
  165. _ui.m_txtCount.text = "" + value;
  166. UpdateCost();
  167. }
  168. private void OnClickBtnMinus()
  169. {
  170. string inputStr = _ui.m_txtCount.text.Trim();
  171. int value = _count;
  172. if (inputStr.Length > 0)
  173. {
  174. value = int.Parse(inputStr);
  175. }
  176. value -= _perCount;
  177. value = Math.Max(_count, value);
  178. _ui.m_txtCount.text = "" + value;
  179. UpdateCost();
  180. }
  181. private void SetBtnState()
  182. {
  183. string inputStr = _ui.m_txtCount.text.Trim();
  184. if (inputStr == null || int.Parse(inputStr) <= _count)
  185. {
  186. _ui.m_btnMinus.enabled = false;
  187. }
  188. else
  189. {
  190. _ui.m_btnMinus.enabled = true;
  191. }
  192. int costHasNum = ItemDataManager.GetItemNum(_costId);
  193. int maxCanBuy = (int)Math.Floor((decimal)(costHasNum / _perCostCount * _perCount));
  194. // int maxCanBuy = ItemUtil.ItemExChangeCount(_itemId, costHasNum);
  195. if (inputStr != null && (int.Parse(inputStr) >= maxCanBuy))
  196. {
  197. _ui.m_btnPlus.enabled = false;
  198. _ui.m_btnAll.enabled = false;
  199. }
  200. else
  201. {
  202. _ui.m_btnPlus.enabled = true;
  203. _ui.m_btnAll.enabled = true;
  204. }
  205. }
  206. private async void OnClickBtnSure()
  207. {
  208. int count = int.Parse(_ui.m_txtCount.text.Trim());
  209. int price = int.Parse(_ui.m_txtPrice.text.Trim());
  210. if (count > 0)
  211. {
  212. if (price > ItemDataManager.GetItemNum(_costId))
  213. {
  214. if (_openSource)
  215. {
  216. if (_itemId == ConstItemID.DIAMOND_RED)
  217. {
  218. ItemUtil.AddDiamondPurple();
  219. }
  220. }
  221. else
  222. {
  223. ItemCfg costCfg = ItemCfgArray.Instance.GetCfg(_costId);
  224. PromptController.Instance.ShowFloatTextPrompt(string.Format("{0}不足!", costCfg.name));
  225. }
  226. }
  227. else
  228. {
  229. int buyCount = price / _perCostCount * _perCount;
  230. // ItemUtil.AddItemUseCost(_itemId, buyCount, _costId, price);
  231. bool result = false;
  232. // result
  233. switch (_buyType)
  234. {
  235. case ConstBuyType.TYPE_NORMAL:
  236. break;
  237. case ConstBuyType.TYPE_ITEM:
  238. result = await ItemExchangeSProxy.ItemExchange(_itemId, count);
  239. break;
  240. case ConstBuyType.TYPE_SHOP:
  241. result = await ShopSProxy.ShopBuy(_shopType, _itemId, count);
  242. break;
  243. }
  244. if (_onSuccess != null)
  245. {
  246. _onSuccess();
  247. }
  248. if (result && _showTips)
  249. {
  250. PromptController.Instance.ShowFloatTextPrompt("购买成功", MessageType.SUCCESS);
  251. }
  252. }
  253. }
  254. else
  255. {
  256. PromptController.Instance.ShowFloatTextPrompt("购买异常", MessageType.ERR);
  257. }
  258. this.Hide();
  259. }
  260. private void OnClickBtnCancel()
  261. {
  262. this.Hide();
  263. }
  264. public void Reset()
  265. {
  266. _itemId = 0;
  267. _costId = 0;
  268. _perCount = 0;
  269. _perCostCount = 0;
  270. _count = 0;
  271. _maxCount = 0;
  272. }
  273. protected override void OnHide()
  274. {
  275. base.OnHide();
  276. // _cfg = null;
  277. Reset();
  278. }
  279. /// <summary>
  280. /// 是否显示购买提示
  281. /// </summary>
  282. /// <value></value>
  283. public bool showTxtBuyTips
  284. {
  285. set
  286. {
  287. _ui.m_txtBuyTips.visible = value;
  288. }
  289. }
  290. protected override void UpdateToCheckGuide(object param)
  291. {
  292. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  293. GuideController.TryGuide(_ui.m_btnSure, ConstGuideId.BUY_CLOTHING, 3, "找到需要的物品了,点击购买吧");
  294. }
  295. }
  296. }