BuyCountView.cs 12 KB

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