BuyCountView.cs 12 KB

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