BuyCountView.cs 12 KB

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