using UI.CommonGame; using System; using FairyGUI; using System.Collections.Generic; 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毫秒才算长按 private int _buyId; private int _costId; private int _perCount; private int _perCostCount; private int _count; 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() { 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); } /// /// /// /// 购买物品对应的编号,非必须为物品Id /// 消耗品id /// 单位兑换数量 /// 单位消耗数量 /// 购买完成回调 /// 是否弹购买成功飘字,默认是 /// 是否打开来源界面。默认否 /// 兑换总量 public void SetParams(int buyId, int costId, int perCount, int perCostCount, int count, int buyType, int shopType = 0, Action onSuccess = null, bool showTips = true, bool openSource = false, int maxCount = 9990) { if (buyType == ConstBuyType.TYPE_SHOP) { _itemId = ClothingShopCfgManager.Instance.GetShopCfg(buyId, shopType).itemID; } else { _itemId = buyId; } _buyId = buyId; _costId = costId; _perCount = perCount; _perCostCount = perCostCount; _count = count; _onSuccess = onSuccess; _maxCount = maxCount; _openSource = openSource; _showTips = showTips; _buyType = buyType; _shopType = shopType; } protected override void OnShown() { base.OnShown(); _ui.m_txtCount.text = "" + _count; UpdateView(); } private void UpdateView() { ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(_itemId); _ui.m_icon.url = ResPathUtil.GetIconPath(itemCfg); string itemName = itemCfg.name; _ui.m_txtName.text = itemName; if (ItemUtilCS.IsDressUpItem(_itemId)) { _ui.m_rarity.visible = true; RarityIconController.UpdateRarityIcon(_ui.m_rarity, _itemId, false); } else { _ui.m_rarity.visible = false; } ItemCfg costItemCfg = ItemCfgArray.Instance.GetCfg(_costId); _ui.m_iconPrice.url = "ui://CommonGame/" + costItemCfg.res; _ui.m_txtBuyTips.text = string.Format("消耗{0}{1},可兑换{2}{3}", _perCostCount, costItemCfg.name, _perCount, itemCfg.name); _ui.m_txtBuyTips.visible = false; UpdateCost(); } private void UpdateCost() { int count = int.Parse(_ui.m_txtCount.text.Trim()); int price = (int)Math.Ceiling((decimal)count / _perCount) * _perCostCount; _ui.m_txtPrice.text = "" + price; SetBtnState(); } 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; //_timer.Interval; 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(); } } } private void OnClickBtnAll() { int costHasNum = ItemDataManager.GetItemNum(_costId); int value = (int)Math.Floor((decimal)(costHasNum / _perCostCount * _perCount)); // int value = ItemUtil.ItemExChangeCount(_itemId, costHasNum); value = Math.Min(_maxCount, value); _ui.m_txtCount.text = value.ToString(); UpdateCost(); } private void OnClickBtnPlus() { string inputStr = _ui.m_txtCount.text.Trim(); int value = _count; if (inputStr.Length > 0) { value = int.Parse(inputStr); } if (value < _maxCount) { value += _perCount; } _ui.m_txtCount.text = "" + value; UpdateCost(); } private void OnClickBtnMinus() { string inputStr = _ui.m_txtCount.text.Trim(); int value = _count; if (inputStr.Length > 0) { value = int.Parse(inputStr); } value -= _perCount; value = Math.Max(_count, value); _ui.m_txtCount.text = "" + value; UpdateCost(); } private void SetBtnState() { string inputStr = _ui.m_txtCount.text.Trim(); if (inputStr == null || int.Parse(inputStr) <= _count) { _ui.m_btnMinus.enabled = false; } else { _ui.m_btnMinus.enabled = true; } int costHasNum = ItemDataManager.GetItemNum(_costId); int maxCanBuy = (int)Math.Floor((decimal)(costHasNum / _perCostCount * _perCount)); // int maxCanBuy = ItemUtil.ItemExChangeCount(_itemId, costHasNum); if (inputStr != null && (int.Parse(inputStr) >= 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) { if (price > ItemDataManager.GetItemNum(_costId)) { if (_openSource) { if (_buyId == ConstItemID.DIAMOND_RED) { ItemUtil.AddDiamondPurple(); } } else { ItemCfg costCfg = ItemCfgArray.Instance.GetCfg(_costId); PromptController.Instance.ShowFloatTextPrompt(string.Format("{0}不足!", costCfg.name)); } } else { int buyCount = price / _perCostCount * _perCount; // ItemUtil.AddItemUseCost(_itemId, buyCount, _costId, price); bool result = false; // result switch (_buyType) { case ConstBuyType.TYPE_NORMAL: break; case ConstBuyType.TYPE_ITEM: result = await ItemExchangeSProxy.ItemExchange(_buyId, count); break; case ConstBuyType.TYPE_SHOP: result = await ShopSProxy.ShopBuy(_shopType, _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 OnClickBtnCancel() { this.Hide(); } public void Reset() { _buyId = 0; _costId = 0; _perCount = 0; _perCostCount = 0; _count = 0; _maxCount = 0; } protected override void OnHide() { base.OnHide(); // _cfg = null; Reset(); } /// /// 是否显示购买提示 /// /// public bool showTxtBuyTips { set { _ui.m_txtBuyTips.visible = value; } } protected override void UpdateToCheckGuide(object param) { if (!ViewManager.CheckIsTopView(this.viewCom)) return; GuideController.TryGuide(_ui.m_btnSure, ConstGuideId.BUY_CLOTHING, 3, "找到需要的物品了,点击购买吧"); } } }