BuyCountView.cs 11 KB

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