using UnityEngine;
|
using UnityEngine.UI;
|
|
using System;
|
|
namespace vnxbqy.UI
|
{
|
public class ComposeMatCell : MonoBehaviour
|
{
|
[SerializeField] Button selectBtn;
|
[SerializeField] public ItemCell itemCell;
|
[SerializeField] GameObject lockObj;
|
[SerializeField] Text desText;
|
|
NeedMatType matType = NeedMatType.Nothing;
|
int itemId = 0;
|
string matDes = string.Empty;
|
public ItemModel itemModel { get; private set;}
|
ItemCompoundConfig itemCompound;
|
PackModel playerPack { get { return ModelCenter.Instance.GetModel<PackModel>(); } }
|
|
SelectEquipModel selectModel {get { return ModelCenter.Instance.GetModel<SelectEquipModel>(); }}
|
ComposeWinModel composeModel { get { return ModelCenter.Instance.GetModel<ComposeWinModel>(); } }
|
|
private void OnEnable()
|
{
|
playerPack.itemCntReduceEvent += UpdateItemCount;
|
}
|
|
private void OnDisable()
|
{
|
playerPack.itemCntReduceEvent -= UpdateItemCount;
|
}
|
|
public void SetDisplay(ItemCompoundConfig _itemCompound,NeedMatType _matType,bool isLock,string des = "", int _itemId = 0,int _itemIndex = -1)
|
{
|
this.itemCompound = _itemCompound;
|
var packType = composeModel.GetPackTypeByMakerId(itemCompound.makeID);
|
this.itemModel = playerPack.GetItemByIndex(packType, _itemIndex);
|
this.matType = _matType;
|
this.itemId = _itemId;
|
matDes = des;
|
desText.SetActive(des != "" && !string.IsNullOrEmpty(des));
|
desText.text = des;
|
lockObj.SetActive(isLock);
|
UpdateItemCell();
|
UpdateMatCount();
|
selectBtn.RemoveAllListeners();
|
if(!isLock)
|
{
|
switch (matType)
|
{
|
case NeedMatType.unfixedItem:
|
case NeedMatType.addItem:
|
selectBtn.AddListener(ClickSelect);
|
break;
|
}
|
}
|
}
|
|
private void UpdateItemCell()
|
{
|
bool isShowMat = itemId != 0 || itemModel != null ? true : false;
|
itemCell.SetActive(isShowMat);
|
if (isShowMat)
|
{
|
if (itemModel != null)
|
{
|
itemCell.Init(itemModel);
|
itemCell.button.SetListener(ClickItemCell);
|
}
|
else
|
{
|
|
ItemCellModel cellModel = new ItemCellModel(itemId);
|
itemCell.Init(cellModel);
|
itemCell.button.SetListener(ClickItemCell);
|
}
|
|
switch(matType)
|
{
|
case NeedMatType.unfixedItem:
|
case NeedMatType.addItem:
|
itemCell.reducebtn.SetActive(true);
|
itemCell.reducebtn.SetListener(ClickReduce);
|
break;
|
}
|
}
|
}
|
|
private void UpdateMatCount()
|
{
|
switch (matType)
|
{
|
case NeedMatType.IncreaseItem:
|
case NeedMatType.fixedItem:
|
int needCount = 0;
|
TryGetCountById(out needCount);
|
var packType = composeModel.GetPackTypeByMakerId(itemCompound.makeID);
|
int haveCount = playerPack.GetItemCountByID(packType, itemId);
|
itemCell.countText.SetActive(true);
|
if (haveCount >= needCount)
|
{
|
itemCell.countText.text = StringUtility.Contact(UIHelper.AppendColor(TextColType.Green, haveCount.ToString()),
|
"/", needCount);
|
}
|
else
|
{
|
itemCell.countText.text = StringUtility.Contact(UIHelper.AppendColor(TextColType.Red, haveCount.ToString()),
|
"/", needCount);
|
}
|
break;
|
case NeedMatType.unfixedItem:
|
case NeedMatType.Nothing:
|
case NeedMatType.MakeItem:
|
case NeedMatType.addItem:
|
itemCell.countText.SetActive(false);
|
break;
|
}
|
}
|
|
private bool TryGetCountById(out int itemCount)
|
{
|
itemCount = 0;
|
if (itemCompound == null) return false;
|
|
switch (matType)
|
{
|
case NeedMatType.IncreaseItem:
|
int[] increases = itemCompound.successRateIncrease;
|
if(increases != null && increases.Length >= 3)
|
{
|
itemCount = increases[1];
|
return true;
|
}
|
break;
|
case NeedMatType.fixedItem:
|
int[] fixedIds = itemCompound.itemID;
|
int[] fixedCounts = itemCompound.itemCount;
|
if (fixedIds != null)
|
{
|
for(int i = 0; i < fixedIds.Length; i++)
|
{
|
if(fixedIds[i] == itemId)
|
{
|
itemCount = fixedCounts[i];
|
return true;
|
}
|
}
|
}
|
break;
|
}
|
return false;
|
}
|
|
private void ClickItemCell()
|
{
|
ItemConfig itemConfig = ItemConfig.Get(itemId);
|
switch (matType)
|
{
|
case NeedMatType.IncreaseItem:
|
case NeedMatType.fixedItem:
|
if (itemConfig.GetWay != null && itemConfig.GetWay.Length > 0)
|
{
|
ItemTipUtility.Show(itemId);
|
return;
|
}
|
break;
|
|
}
|
|
if (itemModel == null)
|
{
|
ItemTipUtility.Show(itemId);
|
}
|
else
|
{
|
ItemTipUtility.Show(itemModel.guid);
|
}
|
}
|
|
private void ClickSelect()
|
{
|
selectModel.SetSelectMatCell(this);
|
switch (matType)
|
{
|
case NeedMatType.unfixedItem:
|
selectModel.selectItem = SelectItemType.unfixed;
|
break;
|
case NeedMatType.addItem:
|
selectModel.selectItem = SelectItemType.addons;
|
break;
|
}
|
WindowCenter.Instance.Open<SelectItemWin>();
|
}
|
|
private void UpdateItemCount(PackType type, int index, int id)
|
{
|
if (itemModel == null) return;
|
|
if(itemModel.packType == type
|
&& itemModel.gridIndex == index
|
&& itemModel.itemId == id)
|
{
|
ClickReduce();
|
}
|
}
|
|
private void ClickReduce()
|
{
|
if (itemModel == null) return;
|
|
switch (matType)
|
{
|
case NeedMatType.unfixedItem:
|
selectModel.RemoveHaveUnfixedSelectItem(itemModel.gridIndex);
|
break;
|
case NeedMatType.addItem:
|
selectModel.RemoveHaveAddSelectItem(itemModel.gridIndex);
|
break;
|
}
|
composeModel.UpdateReduce(this,matType);
|
}
|
}
|
}
|