123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- using UI.CommonGame;
- using System;
- using FairyGUI;
- using System.Collections.Generic;
- using UnityEngine;
- namespace GFGGame
- {
- public class BuyCountView : BaseWindow
- {
- private UI_BuyCountUI _ui;
- // private ClothingShopCfg _cfg;
- private float _selectTimeCount = 0;//长按时间
- private int _consumeSelectIndex = 0;//0为减,1为加
- private float _delay = 300;
- private float longpress = 900;//大于900毫秒才算长按
- /// <summary>
- /// 购买物品编号
- /// </summary>
- private int _buyId;
- /// <summary>
- /// 最低购买次数
- /// </summary>
- private long _minBuyCount;
- /// <summary>
- /// 当前购买次数
- /// </summary>
- private long _count;
- /// <summary>
- /// 购买消耗Id
- /// </summary>
- private int _costId;//购买消耗Id
- /// <summary>
- /// 购买物品数量
- /// </summary>
- private int _buyNum;
- /// <summary>
- /// 购买物品消耗数量
- /// </summary>
- private int _costNum;
- /// <summary>
- /// 最大购买次数
- /// </summary>
- private int _maxCanBuy;
- // private int _perCount;
- // private int _perCostCount;
- // private int _maxCount;
- private bool _openSource;
- private bool _showTips;
- private int _buyType;
- private int _shopType;
- private int _itemId;
- private Action _onSuccess;
- public override void Dispose()
- {
- if (_ui != null)
- {
- _ui.Dispose();
- _ui = null;
- }
- BuyItemConteoller.Dispose();
- base.Dispose();
- }
- protected override void OnInit()
- {
- base.OnInit();
- _ui = UI_BuyCountUI.Create();
- this.viewCom = _ui.target;
- this.viewCom.Center();
- this.modal = true;
- viewAnimationType = EnumViewAnimationType.ZOOM_CENTER;
- _ui.m_btnPlus.onClick.Add(OnClickBtnPlus);
- _ui.m_btnMinus.onClick.Add(OnClickBtnMinus);
- _ui.m_btnAll.onClick.Add(OnClickBtnAll);
- _ui.m_btnPlus.onTouchBegin.Add(OnTouchPlusBegin);
- _ui.m_btnMinus.onTouchBegin.Add(OnTouchMinusBegin);
- _ui.m_btnPlus.onTouchEnd.Add(() => { Timers.inst.Remove(OnTimedEvent); });
- _ui.m_btnMinus.onTouchEnd.Add(() => { Timers.inst.Remove(OnTimedEvent); });
- _ui.m_btnSure.onClick.Add(OnClickBtnSure);
- _ui.m_btnCancel.onClick.Add(OnClickBtnCancel);
- _ui.m_txtCount.onFocusOut.Add(OnChangedTxtCount);
- }
- protected override void AddEventListener()
- {
- base.AddEventListener();
- EventAgent.AddEventListener(ConstMessage.CLOSE_SHOPPING_TIP, CloseShoppingTip);
- }
- protected override void RemoveEventListener()
- {
- base.RemoveEventListener();
- EventAgent.RemoveEventListener(ConstMessage.CLOSE_SHOPPING_TIP, CloseShoppingTip);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="buyId">购买物品对应的编号,非必须为物品Id</param>
- /// <param name="minBuyCount">最低兑换个数</param>
- /// <param name="buyType">购买类型,对应ConstBuyType</param>
- /// <param name="shopType">商店类型,仅buytype为TYPE_SHOP时有用</param>
- /// <param name="onSuccess"></param>
- /// <param name="showTips">是否弹购买成功飘字,默认是</param>
- /// <param name="openSource">是否打开来源界面。默认否</param>
- /// <param name="maxCount"></param>
- public void SetParams(int buyId, long minBuyCount, int buyType, Action onSuccess = null, bool showTips = true, bool openSource = false, int maxCount = 9990)
- {
- _buyId = buyId;
- _itemId = buyId;
- _minBuyCount = minBuyCount;
- _count = _minBuyCount;
- _onSuccess = onSuccess;
- _openSource = openSource;
- _showTips = showTips;
- _buyType = buyType;
- if (buyType == ConstBuyType.TYPE_SHOP)
- {
- ShopCfg shopCfg = ShopCfgArray.Instance.GetCfg(buyId);
- int maxBuyCount = (int)Math.Floor((decimal)(ItemDataManager.GetItemNum(shopCfg.CostIdReal) / shopCfg.Price));
- _maxCanBuy = Math.Min(maxCount, maxBuyCount);
- _itemId = shopCfg.itemId;
- }
- else if (buyType == ConstBuyType.TYPE_ITEM)
- {
- ItemExchangeCfg itemExchangeCfg = ItemExchangeCfgArray.Instance.GetCfg(buyId);
- _count = (int)Math.Ceiling(Convert.ToDecimal(minBuyCount) / Convert.ToDecimal(itemExchangeCfg.num));
- int maxBuyCount = (int)Math.Floor((decimal)(ItemDataManager.GetItemNum(itemExchangeCfg.costId) / itemExchangeCfg.costNumArr[0]));
- _maxCanBuy = Math.Min(Math.Min(maxCount, maxBuyCount), itemExchangeCfg.maxLimit == 0 ? maxCount : itemExchangeCfg.maxLimit - ItemDataManager.GetItemExchangeTimes(buyId));
- _itemId = buyId;
- ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemExchangeCfg.id);
- ItemCfg itemCostCfg = ItemCfgArray.Instance.GetCfg(itemExchangeCfg.costId);
- _ui.m_txtBuyTips.text = string.Format("消耗{0}{1},可兑换{2}{3}", itemExchangeCfg.costNumArr[0], itemCostCfg.name, itemExchangeCfg.num, itemCfg.name);
- }
- GetMoneyIdAndNum(minBuyCount, out _costId, out _costNum, out _buyNum);
- UpdateView();
- }
- protected override void OnShown()
- {
- base.OnShown();
- // UpdateView();
- Timers.inst.AddUpdate(CheckGuide);
- }
- private void UpdateView()
- {
- ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId);
- _ui.m_icon.url = ResPathUtil.GetIconPath(itemCfg);
- _ui.m_txtName.text = itemCfg.name;
- _ui.m_rarity.visible = false;
- if (ItemUtilCS.IsDressUpItem(_itemId))
- {
- _ui.m_rarity.visible = true;
- RarityIconController.UpdateRarityIcon(_ui.m_rarity, _itemId, false);
- }
- ItemCfg costItemCfg = ItemCfgArray.Instance.GetCfg(_costId);
- _ui.m_iconPrice.url = ResPathUtil.GetCommonGameResPath(costItemCfg.res);
- _ui.m_txtBuyTips.visible = false;
- UpdateCost();
- }
- private void UpdateCost()
- {
- GetMoneyIdAndNum(_count, out _costId, out _costNum, out _buyNum);
- _ui.m_txtCount.text = _buyNum.ToString();
- _ui.m_txtPrice.text = _costNum.ToString();
- SetBtnState();
- }
- private void OnClickBtnAll()
- {
- _count = _maxCanBuy;
- UpdateCost();
- }
- private void OnClickBtnPlus()
- {
- if (_count < _maxCanBuy)
- {
- _count += 1;
- }
- UpdateCost();
- }
- private void OnClickBtnMinus()
- {
- _count -= 1;
- _count = Math.Max(_minBuyCount, _count);
- UpdateCost();
- }
- private void SetBtnState()
- {
- string inputStr = _ui.m_txtCount.text.Trim();
- if (_count <= _minBuyCount)
- {
- _ui.m_btnMinus.enabled = false;
- }
- else
- {
- _ui.m_btnMinus.enabled = true;
- }
- if (_count >= _maxCanBuy)
- {
- _ui.m_btnPlus.enabled = false;
- _ui.m_btnAll.enabled = false;
- }
- else
- {
- _ui.m_btnPlus.enabled = true;
- _ui.m_btnAll.enabled = true;
- }
- }
- private async void OnClickBtnSure()
- {
- int count = int.Parse(_ui.m_txtCount.text.Trim());
- int price = int.Parse(_ui.m_txtPrice.text.Trim());
- if (_count > 0)
- {
- long hasCount = ItemDataManager.GetItemNum(_costId);
- if (price > hasCount)
- {
- if (!ItemUtil.BuyCurrency(_costId, price - hasCount))
- {
- ItemCfg costCfg = ItemCfgArray.Instance.GetCfg(_costId);
- PromptController.Instance.ShowFloatTextPrompt(string.Format("{0}不足", costCfg.name));
- }
- //else
- //{
- // this.Hide();
- //}
- return;
- }
- else
- {
- bool result = false;
- switch (_buyType)
- {
- case ConstBuyType.TYPE_ITEM:
- result = await ItemExchangeSProxy.ItemExchange(_buyId, _count);
- break;
- case ConstBuyType.TYPE_SHOP:
- result = await ShopSProxy.ReqShopBuy(_buyId, _count);
- break;
- }
- if (_onSuccess != null)
- {
- _onSuccess();
- }
- if (result && _showTips)
- {
- PromptController.Instance.ShowFloatTextPrompt("购买成功", MessageType.SUCCESS);
- }
- }
- }
- else
- {
- PromptController.Instance.ShowFloatTextPrompt("购买异常", MessageType.ERR);
- }
- this.Hide();
- }
- //监控输入
- private void OnChangedTxtCount(EventContext context)
- {
- GTextInput sender = context.sender as GTextInput;
- bool isNumeric = int.TryParse(sender.text, out var result);
- if (!isNumeric)
- {
- sender.text = _minBuyCount.ToString();
- }
- if (Convert.ToInt32(sender.text) <= 0)
- {
- sender.text = _minBuyCount.ToString();
- }
- _count = Math.Min(_maxCanBuy, Convert.ToInt32(sender.text));
- UpdateCost();
- }
- private void OnClickBtnCancel()
- {
- this.Hide();
- }
- private void GetMoneyIdAndNum(long count, out int _costId, out int _costNum, out int _buyNum)
- {
- _costId = 0;
- _costNum = 0;
- _buyNum = 0;
- if (_buyType == ConstBuyType.TYPE_SHOP)
- {
- ShopDataManager.Instance.GetMoneyIdAndNum(_buyId, (int)count, _shopType, out _costId, out _costNum, out _buyNum);
- }
- else if (_buyType == ConstBuyType.TYPE_ITEM)
- {
- ItemExchangeCfg itemExchangeCfg = ItemExchangeCfgArray.Instance.GetCfg(_itemId);
- int _exchangeCount = (int)Math.Ceiling(Convert.ToDecimal(count) / Convert.ToDecimal(itemExchangeCfg.num));
- ItemExchangeCfgArray.Instance.GetMoneyIdAndNum(_itemId, ItemDataManager.GetItemExchangeTimes(_itemId), (int)count, out _costId, out _costNum, out _buyNum);
- }
- }
- private void OnTouchPlusBegin()
- {
- _selectTimeCount = 0;
- _consumeSelectIndex = 1;
- Timers.inst.Add(_delay / 1000, 0, OnTimedEvent);
- }
- private void OnTouchMinusBegin()
- {
- _selectTimeCount = 0;
- _consumeSelectIndex = 0;
- Timers.inst.Add(_delay / 1000, 0, OnTimedEvent);
- }
- private void OnTimedEvent(object param)
- {
- _selectTimeCount += _delay;
- if (_selectTimeCount >= longpress)
- {
- if (_consumeSelectIndex == 0)
- {
- if (!CommonUtil.Instance.CheckPointIsOnComponent(_ui.m_btnMinus, CommonUtil.Instance.GetMouseV2Point()))
- {
- Timers.inst.Remove(OnTimedEvent);
- return;
- }
- this.OnClickBtnMinus();
- }
- else
- {
- if (!CommonUtil.Instance.CheckPointIsOnComponent(_ui.m_btnPlus, CommonUtil.Instance.GetMouseV2Point()))
- {
- Timers.inst.Remove(OnTimedEvent);
- return;
- }
- this.OnClickBtnPlus();
- }
- }
- }
- public void Reset()
- {
- _buyId = 0;
- _costId = 0;
- // _perCount = 0;
- // _perCostCount = 0;
- _minBuyCount = 0;
- // _maxCount = 0;
- }
- protected override void OnHide()
- {
- base.OnHide();
- // _cfg = null;
- Reset();
- Timers.inst.Remove(CheckGuide);
- }
- /// <summary>
- /// 是否显示购买提示
- /// </summary>
- /// <value></value>
- public bool showTxtBuyTips
- {
- set
- {
- _ui.m_txtBuyTips.visible = value;
- }
- }
- public void CloseShoppingTip()
- {
- this.Hide();
- }
- private void CheckGuide(object param)
- {
- if (GuideDataManager.IsGuideFinish(ConstGuideId.BUY_CLOTHING) <= 0)
- {
- UpdateToCheckGuide(null);
- }
- else
- {
- Timers.inst.Remove(CheckGuide);
- }
- }
- protected override void UpdateToCheckGuide(object param)
- {
- if (!ViewManager.CheckIsTopView(this.viewCom)) return;
- GuideController.TryGuide(_ui.m_btnSure, ConstGuideId.BUY_CLOTHING, 5, "找到需要的物品了,点击购买吧。");
- }
- }
- }
|