BuyCountView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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.GetCfgs(buyId)[0];
  106. int maxBuyCount = (int)Math.Floor((decimal)(ItemDataManager.GetItemNum(itemExchangeCfg.costId) / itemExchangeCfg.costNum));
  107. _maxCanBuy = Math.Min(Math.Min(maxCount, maxBuyCount), 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. _ui.m_txtCount.text = "" + _minBuyCount;
  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 = _count.ToString();
  138. _ui.m_txtPrice.text = _costNum.ToString();
  139. SetBtnState();
  140. }
  141. private void OnTouchPlusBegin()
  142. {
  143. _selectTimeCount = 0;
  144. _consumeSelectIndex = 1;
  145. Timers.inst.Add(_delay / 1000, 0, OnTimedEvent);
  146. }
  147. private void OnTouchMinusBegin()
  148. {
  149. _selectTimeCount = 0;
  150. _consumeSelectIndex = 0;
  151. Timers.inst.Add(_delay / 1000, 0, OnTimedEvent);
  152. }
  153. private void OnTimedEvent(object param)
  154. {
  155. _selectTimeCount += _delay;
  156. if (_selectTimeCount >= longpress)
  157. {
  158. if (_consumeSelectIndex == 0)
  159. {
  160. if (!CommonUtil.Instance.CheckPointIsOnComponent(_ui.m_btnMinus, CommonUtil.Instance.GetMouseV2Point()))
  161. {
  162. Timers.inst.Remove(OnTimedEvent);
  163. return;
  164. }
  165. this.OnClickBtnMinus();
  166. }
  167. else
  168. {
  169. if (!CommonUtil.Instance.CheckPointIsOnComponent(_ui.m_btnPlus, CommonUtil.Instance.GetMouseV2Point()))
  170. {
  171. Timers.inst.Remove(OnTimedEvent);
  172. return;
  173. }
  174. this.OnClickBtnPlus();
  175. }
  176. }
  177. }
  178. private void OnClickBtnAll()
  179. {
  180. _count = _maxCanBuy;
  181. UpdateCost();
  182. }
  183. private void OnClickBtnPlus()
  184. {
  185. if (_count < _maxCanBuy)
  186. {
  187. _count += 1;
  188. }
  189. UpdateCost();
  190. }
  191. private void OnClickBtnMinus()
  192. {
  193. _count -= 1;
  194. _count = Math.Max(_minBuyCount, _count);
  195. UpdateCost();
  196. }
  197. private void SetBtnState()
  198. {
  199. string inputStr = _ui.m_txtCount.text.Trim();
  200. if (inputStr == null || int.Parse(inputStr) <= _minBuyCount)
  201. {
  202. _ui.m_btnMinus.enabled = false;
  203. }
  204. else
  205. {
  206. _ui.m_btnMinus.enabled = true;
  207. }
  208. if (inputStr != null && (int.Parse(inputStr) >= _maxCanBuy))
  209. {
  210. _ui.m_btnPlus.enabled = false;
  211. _ui.m_btnAll.enabled = false;
  212. }
  213. else
  214. {
  215. _ui.m_btnPlus.enabled = true;
  216. _ui.m_btnAll.enabled = true;
  217. }
  218. }
  219. private async void OnClickBtnSure()
  220. {
  221. int count = int.Parse(_ui.m_txtCount.text.Trim());
  222. int price = int.Parse(_ui.m_txtPrice.text.Trim());
  223. if (count > 0)
  224. {
  225. if (price > ItemDataManager.GetItemNum(_costId))
  226. {
  227. if (_openSource)
  228. {
  229. if (_buyId == ConstItemID.DIAMOND_RED)
  230. {
  231. ItemUtil.AddDiamondPurple();
  232. }
  233. }
  234. else
  235. {
  236. ItemCfg costCfg = ItemCfgArray.Instance.GetCfg(_costId);
  237. PromptController.Instance.ShowFloatTextPrompt(string.Format("{0}不足!", costCfg.name));
  238. }
  239. }
  240. else
  241. {
  242. bool result = false;
  243. switch (_buyType)
  244. {
  245. case ConstBuyType.TYPE_NORMAL:
  246. break;
  247. case ConstBuyType.TYPE_ITEM:
  248. result = await ItemExchangeSProxy.ItemExchange(_buyId, count);
  249. break;
  250. case ConstBuyType.TYPE_SHOP:
  251. result = await ShopSProxy.ShopBuy(_shopType, _buyId, count);
  252. break;
  253. }
  254. if (_onSuccess != null)
  255. {
  256. _onSuccess();
  257. }
  258. if (result && _showTips)
  259. {
  260. PromptController.Instance.ShowFloatTextPrompt("购买成功", MessageType.SUCCESS);
  261. }
  262. }
  263. }
  264. else
  265. {
  266. PromptController.Instance.ShowFloatTextPrompt("购买异常", MessageType.ERR);
  267. }
  268. this.Hide();
  269. }
  270. private void OnClickBtnCancel()
  271. {
  272. this.Hide();
  273. }
  274. private void GetMoneyIdAndNum(int count, out int _costId, out int _costNum, out int _buyNum)
  275. {
  276. _costId = 0;
  277. _costNum = 0;
  278. _buyNum = 0;
  279. if (_buyType == ConstBuyType.TYPE_SHOP)
  280. {
  281. ClothingShopCfgManager.Instance.GetMoneyIdAndNum(_buyId, count, _shopType, out _costId, out _costNum, out _buyNum);
  282. }
  283. else if (_buyType == ConstBuyType.TYPE_ITEM)
  284. {
  285. ItemExchangeCfgArray.Instance.GetMoneyIdAndNum(_itemId, ItemDataManager.GetItemExchangeTimes(_itemId), count, out _costId, out _costNum, out _buyNum);
  286. }
  287. // else
  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. }