BuyCountView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using UI.CommonGame;
  2. using System;
  3. using FairyGUI;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace GFGGame
  7. {
  8. public class BuyCountView : BaseWindow
  9. {
  10. private UI_BuyCountUI _ui;
  11. // private ClothingShopCfg _cfg;
  12. private float _selectTimeCount = 0;//长按时间
  13. private int _consumeSelectIndex = 0;//0为减,1为加
  14. private float _delay = 300;
  15. private float longpress = 900;//大于900毫秒才算长按
  16. /// <summary>
  17. /// 购买物品编号
  18. /// </summary>
  19. private int _buyId;
  20. /// <summary>
  21. /// 最低购买次数
  22. /// </summary>
  23. private long _minBuyCount;
  24. /// <summary>
  25. /// 当前购买次数
  26. /// </summary>
  27. private long _count;
  28. /// <summary>
  29. /// 购买消耗Id
  30. /// </summary>
  31. private int _costId;//购买消耗Id
  32. /// <summary>
  33. /// 购买物品数量
  34. /// </summary>
  35. private int _buyNum;
  36. /// <summary>
  37. /// 购买物品消耗数量
  38. /// </summary>
  39. private int _costNum;
  40. /// <summary>
  41. /// 最大购买次数
  42. /// </summary>
  43. private int _maxCanBuy;
  44. // private int _perCount;
  45. // private int _perCostCount;
  46. // private int _maxCount;
  47. private bool _openSource;
  48. private bool _showTips;
  49. private int _buyType;
  50. private int _shopType;
  51. private int _itemId;
  52. private Action _onSuccess;
  53. public override void Dispose()
  54. {
  55. if (_ui != null)
  56. {
  57. _ui.Dispose();
  58. _ui = null;
  59. }
  60. BuyItemConteoller.Dispose();
  61. base.Dispose();
  62. }
  63. protected override void OnInit()
  64. {
  65. base.OnInit();
  66. _ui = UI_BuyCountUI.Create();
  67. this.viewCom = _ui.target;
  68. this.viewCom.Center();
  69. this.modal = true;
  70. viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
  71. _ui.m_btnPlus.onClick.Add(OnClickBtnPlus);
  72. _ui.m_btnMinus.onClick.Add(OnClickBtnMinus);
  73. _ui.m_btnAll.onClick.Add(OnClickBtnAll);
  74. _ui.m_btnPlus.onTouchBegin.Add(OnTouchPlusBegin);
  75. _ui.m_btnMinus.onTouchBegin.Add(OnTouchMinusBegin);
  76. _ui.m_btnPlus.onTouchEnd.Add(() => { Timers.inst.Remove(OnTimedEvent); });
  77. _ui.m_btnMinus.onTouchEnd.Add(() => { Timers.inst.Remove(OnTimedEvent); });
  78. _ui.m_btnSure.onClick.Add(OnClickBtnSure);
  79. _ui.m_btnCancel.onClick.Add(OnClickBtnCancel);
  80. _ui.m_txtCount.onFocusOut.Add(OnChangedTxtCount);
  81. }
  82. protected override void AddEventListener()
  83. {
  84. base.AddEventListener();
  85. EventAgent.AddEventListener(ConstMessage.CLOSE_SHOPPING_TIP, CloseShoppingTip);
  86. }
  87. protected override void RemoveEventListener()
  88. {
  89. base.RemoveEventListener();
  90. EventAgent.RemoveEventListener(ConstMessage.CLOSE_SHOPPING_TIP, CloseShoppingTip);
  91. }
  92. /// <summary>
  93. ///
  94. /// </summary>
  95. /// <param name="buyId">购买物品对应的编号,非必须为物品Id</param>
  96. /// <param name="minBuyCount">最低兑换个数</param>
  97. /// <param name="buyType">购买类型,对应ConstBuyType</param>
  98. /// <param name="shopType">商店类型,仅buytype为TYPE_SHOP时有用</param>
  99. /// <param name="onSuccess"></param>
  100. /// <param name="showTips">是否弹购买成功飘字,默认是</param>
  101. /// <param name="openSource">是否打开来源界面。默认否</param>
  102. /// <param name="maxCount"></param>
  103. public void SetParams(int buyId, long minBuyCount, int buyType, Action onSuccess = null, bool showTips = true, bool openSource = false, int maxCount = 9990)
  104. {
  105. _buyId = buyId;
  106. _itemId = buyId;
  107. _minBuyCount = minBuyCount;
  108. _count = _minBuyCount;
  109. _onSuccess = onSuccess;
  110. _openSource = openSource;
  111. _showTips = showTips;
  112. _buyType = buyType;
  113. if (buyType == ConstBuyType.TYPE_SHOP)
  114. {
  115. ShopCfg shopCfg = ShopCfgArray.Instance.GetCfg(buyId);
  116. int maxBuyCount = (int)Math.Floor((decimal)(ItemDataManager.GetItemNum(shopCfg.CostIdReal) / shopCfg.Price));
  117. _maxCanBuy = Math.Min(maxCount, maxBuyCount);
  118. _itemId = shopCfg.itemId;
  119. }
  120. else if (buyType == ConstBuyType.TYPE_ITEM)
  121. {
  122. ItemExchangeCfg itemExchangeCfg = ItemExchangeCfgArray.Instance.GetCfg(buyId);
  123. _count = (int)Math.Ceiling(Convert.ToDecimal(minBuyCount) / Convert.ToDecimal(itemExchangeCfg.num));
  124. int maxBuyCount = (int)Math.Floor((decimal)(ItemDataManager.GetItemNum(itemExchangeCfg.costId) / itemExchangeCfg.costNumArr[0]));
  125. _maxCanBuy = Math.Min(Math.Min(maxCount, maxBuyCount), itemExchangeCfg.maxLimit == 0 ? maxCount : itemExchangeCfg.maxLimit - ItemDataManager.GetItemExchangeTimes(buyId));
  126. _itemId = buyId;
  127. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemExchangeCfg.id);
  128. ItemCfg itemCostCfg = ItemCfgArray.Instance.GetCfg(itemExchangeCfg.costId);
  129. _ui.m_txtBuyTips.text = string.Format("消耗{0}{1},可兑换{2}{3}", itemExchangeCfg.costNumArr[0], itemCostCfg.name, itemExchangeCfg.num, itemCfg.name);
  130. }
  131. GetMoneyIdAndNum(minBuyCount, out _costId, out _costNum, out _buyNum);
  132. UpdateView();
  133. }
  134. protected override void OnShown()
  135. {
  136. base.OnShown();
  137. // UpdateView();
  138. Timers.inst.AddUpdate(CheckGuide);
  139. }
  140. private void UpdateView()
  141. {
  142. ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
  143. _ui.m_icon.url = ResPathUtil.GetIconPath(itemCfg);
  144. _ui.m_txtName.text = itemCfg.name;
  145. _ui.m_rarity.visible = false;
  146. if (ItemUtilCS.IsDressUpItem(_itemId))
  147. {
  148. _ui.m_rarity.visible = true;
  149. RarityIconController.UpdateRarityIcon(_ui.m_rarity, _itemId, false);
  150. }
  151. ItemCfg costItemCfg = ItemCfgArray.Instance.GetCfg(_costId);
  152. _ui.m_iconPrice.url = ResPathUtil.GetCommonGameResPath(costItemCfg.res);
  153. _ui.m_txtBuyTips.visible = false;
  154. UpdateCost();
  155. }
  156. private void UpdateCost()
  157. {
  158. GetMoneyIdAndNum(_count, out _costId, out _costNum, out _buyNum);
  159. _ui.m_txtCount.text = _buyNum.ToString();
  160. _ui.m_txtPrice.text = _costNum.ToString();
  161. SetBtnState();
  162. }
  163. private void OnClickBtnAll()
  164. {
  165. _count = _maxCanBuy;
  166. UpdateCost();
  167. }
  168. private void OnClickBtnPlus()
  169. {
  170. if (_count < _maxCanBuy)
  171. {
  172. _count += 1;
  173. }
  174. UpdateCost();
  175. }
  176. private void OnClickBtnMinus()
  177. {
  178. _count -= 1;
  179. _count = Math.Max(_minBuyCount, _count);
  180. UpdateCost();
  181. }
  182. private void SetBtnState()
  183. {
  184. string inputStr = _ui.m_txtCount.text.Trim();
  185. if (_count <= _minBuyCount)
  186. {
  187. _ui.m_btnMinus.enabled = false;
  188. }
  189. else
  190. {
  191. _ui.m_btnMinus.enabled = true;
  192. }
  193. if (_count >= _maxCanBuy)
  194. {
  195. _ui.m_btnPlus.enabled = false;
  196. _ui.m_btnAll.enabled = false;
  197. }
  198. else
  199. {
  200. _ui.m_btnPlus.enabled = true;
  201. _ui.m_btnAll.enabled = true;
  202. }
  203. }
  204. private async void OnClickBtnSure()
  205. {
  206. int count = int.Parse(_ui.m_txtCount.text.Trim());
  207. int price = int.Parse(_ui.m_txtPrice.text.Trim());
  208. if (_count > 0)
  209. {
  210. long hasCount = ItemDataManager.GetItemNum(_costId);
  211. if (price > hasCount)
  212. {
  213. if (!ItemUtil.BuyCurrency(_costId, price - hasCount))
  214. {
  215. ItemCfg costCfg = ItemCfgArray.Instance.GetCfg(_costId);
  216. PromptController.Instance.ShowFloatTextPrompt(string.Format("{0}不足", costCfg.name));
  217. }
  218. //else
  219. //{
  220. // this.Hide();
  221. //}
  222. return;
  223. }
  224. else
  225. {
  226. bool result = false;
  227. switch (_buyType)
  228. {
  229. case ConstBuyType.TYPE_ITEM:
  230. result = await ItemExchangeSProxy.ItemExchange(_buyId, _count);
  231. break;
  232. case ConstBuyType.TYPE_SHOP:
  233. result = await ShopSProxy.ReqShopBuy(_buyId, _count);
  234. break;
  235. }
  236. if (_onSuccess != null)
  237. {
  238. _onSuccess();
  239. }
  240. if (result && _showTips)
  241. {
  242. PromptController.Instance.ShowFloatTextPrompt("购买成功", MessageType.SUCCESS);
  243. }
  244. }
  245. }
  246. else
  247. {
  248. PromptController.Instance.ShowFloatTextPrompt("购买异常", MessageType.ERR);
  249. }
  250. this.Hide();
  251. }
  252. //监控输入
  253. private void OnChangedTxtCount(EventContext context)
  254. {
  255. GTextInput sender = context.sender as GTextInput;
  256. bool isNumeric = int.TryParse(sender.text, out var result);
  257. if (!isNumeric)
  258. {
  259. sender.text = _minBuyCount.ToString();
  260. }
  261. if (Convert.ToInt32(sender.text) <= 0)
  262. {
  263. sender.text = _minBuyCount.ToString();
  264. }
  265. _count = Math.Min(_maxCanBuy, Convert.ToInt32(sender.text));
  266. UpdateCost();
  267. }
  268. private void OnClickBtnCancel()
  269. {
  270. this.Hide();
  271. }
  272. private void GetMoneyIdAndNum(long count, out int _costId, out int _costNum, out int _buyNum)
  273. {
  274. _costId = 0;
  275. _costNum = 0;
  276. _buyNum = 0;
  277. if (_buyType == ConstBuyType.TYPE_SHOP)
  278. {
  279. ShopDataManager.Instance.GetMoneyIdAndNum(_buyId, (int)count, _shopType, out _costId, out _costNum, out _buyNum);
  280. }
  281. else if (_buyType == ConstBuyType.TYPE_ITEM)
  282. {
  283. ItemExchangeCfg itemExchangeCfg = ItemExchangeCfgArray.Instance.GetCfg(_itemId);
  284. int _exchangeCount = (int)Math.Ceiling(Convert.ToDecimal(count) / Convert.ToDecimal(itemExchangeCfg.num));
  285. ItemExchangeCfgArray.Instance.GetMoneyIdAndNum(_itemId, ItemDataManager.GetItemExchangeTimes(_itemId), (int)count, out _costId, out _costNum, out _buyNum);
  286. }
  287. }
  288. private void OnTouchPlusBegin()
  289. {
  290. _selectTimeCount = 0;
  291. _consumeSelectIndex = 1;
  292. Timers.inst.Add(_delay / 1000, 0, OnTimedEvent);
  293. }
  294. private void OnTouchMinusBegin()
  295. {
  296. _selectTimeCount = 0;
  297. _consumeSelectIndex = 0;
  298. Timers.inst.Add(_delay / 1000, 0, OnTimedEvent);
  299. }
  300. private void OnTimedEvent(object param)
  301. {
  302. _selectTimeCount += _delay;
  303. if (_selectTimeCount >= longpress)
  304. {
  305. if (_consumeSelectIndex == 0)
  306. {
  307. if (!CommonUtil.Instance.CheckPointIsOnComponent(_ui.m_btnMinus, CommonUtil.Instance.GetMouseV2Point()))
  308. {
  309. Timers.inst.Remove(OnTimedEvent);
  310. return;
  311. }
  312. this.OnClickBtnMinus();
  313. }
  314. else
  315. {
  316. if (!CommonUtil.Instance.CheckPointIsOnComponent(_ui.m_btnPlus, CommonUtil.Instance.GetMouseV2Point()))
  317. {
  318. Timers.inst.Remove(OnTimedEvent);
  319. return;
  320. }
  321. this.OnClickBtnPlus();
  322. }
  323. }
  324. }
  325. public void Reset()
  326. {
  327. _buyId = 0;
  328. _costId = 0;
  329. // _perCount = 0;
  330. // _perCostCount = 0;
  331. _minBuyCount = 0;
  332. // _maxCount = 0;
  333. }
  334. protected override void OnHide()
  335. {
  336. base.OnHide();
  337. // _cfg = null;
  338. Reset();
  339. Timers.inst.Remove(CheckGuide);
  340. }
  341. /// <summary>
  342. /// 是否显示购买提示
  343. /// </summary>
  344. /// <value></value>
  345. public bool showTxtBuyTips
  346. {
  347. set
  348. {
  349. _ui.m_txtBuyTips.visible = value;
  350. }
  351. }
  352. public void CloseShoppingTip()
  353. {
  354. this.Hide();
  355. }
  356. private void CheckGuide(object param)
  357. {
  358. if (GuideDataManager.IsGuideFinish(ConstGuideId.BUY_CLOTHING) <= 0)
  359. {
  360. UpdateToCheckGuide(null);
  361. }
  362. else
  363. {
  364. Timers.inst.Remove(CheckGuide);
  365. }
  366. }
  367. protected override void UpdateToCheckGuide(object param)
  368. {
  369. if (!ViewManager.CheckIsTopView(this.viewCom)) return;
  370. GuideController.TryGuide(_ui.m_btnSure, ConstGuideId.BUY_CLOTHING, 5, "找到需要的物品了,点击购买吧。");
  371. }
  372. }
  373. }