using System;
using UnityEngine;
using UnityEngine.UI;
///
/// 武将皮肤
///
public class HeroSkinWin : UIBase
{
[SerializeField] RawImage bgTexture;
[SerializeField] Button showFuncBtn; //只显示立绘时点击,显示功能
[SerializeField] Transform funcForm;
[SerializeField] UIHeroController roleLhModel; //展示英雄立绘
[SerializeField] UIHeroController roleXsModel; //像素
[SerializeField] Button seeLhBtn; //查看立绘
[SerializeField] Button previewFightBtn; //预览战斗
[SerializeField] Button shopBtn; //商店
[SerializeField] Button changeClothBtn; //只更换服装外观
[SerializeField] Text nameText;
[SerializeField] Text skinNameText;
// [SerializeField] Text skinLVText;
//属性区
[SerializeField] Text[] onAttrText; //穿戴属性
[SerializeField] Text[] allAttrText; //全体加成
[SerializeField] Transform attrObj;
[SerializeField] ScrollerController skinScroller;
[SerializeField] Button unlockBtn;
[SerializeField] Image itemIcon;
[SerializeField] Text itemCountText;
[SerializeField] Button putonBtn;
[SerializeField] GameObject putonYetObj;
[SerializeField] Button showGetObj;
[SerializeField] GameObject showNormalObj;
HeroConfig heroConfig;
int heroID;
int skinID;
public HeroInfo hero;
protected override void InitComponent()
{
showFuncBtn.AddListener(() =>
{
funcForm.SetActive(true);
ChangeParentWinAlpha(1);
});
seeLhBtn.AddListener(() =>
{
funcForm.SetActive(false);
ChangeParentWinAlpha(0);
});
previewFightBtn.AddListener(() =>
{
var config = HeroConfig.Get(heroID);
int index = Array.IndexOf(config.SkinIDList, skinID);
BattleManager.Instance.SendTurnFight(30000, valueList: new uint[] { (uint)heroID, (uint)index });
});
shopBtn.AddListener(() =>
{
UIManager.Instance.OpenWindow(1);
});
unlockBtn.AddListener(() =>
{
UnLockSkin();
});
putonBtn.AddListener(() =>
{
if (hero == null)
{
return;
}
HeroUIManager.Instance.SendSkinOP(heroID, skinID, 4, hero.itemHero.gridIndex);
});
showGetObj.AddListener(() =>
{
showGetObj.SetActive(false);
showNormalObj.SetActive(true);
ChangeParentWinAlpha(1);
});
changeClothBtn.AddListener(() =>
{
UIManager.Instance.OpenWindow();
});
}
int activeSkinID;
protected override void OnPreOpen()
{
activeSkinID = 0;
skinScroller.OnRefreshCell += OnRefreshCell;
if (functionOrder == 0)
{
//代表从培养界面打开
hero = HeroManager.Instance.GetHero(HeroUIManager.Instance.selectHeroGuid);
heroConfig = hero.heroConfig;
if (HeroUIManager.Instance.selectSkinIndex == -1)
{
HeroUIManager.Instance.selectSkinIndex = hero.SkinIndex;
}
}
else
{
//从图鉴界面打开
heroConfig = HeroConfig.Get(HeroUIManager.Instance.selectForPreviewHeroID);
if (HeroUIManager.Instance.selectSkinIndex == -1)
{
HeroUIManager.Instance.selectSkinIndex = 0;
}
}
heroID = heroConfig.HeroID;
HeroUIManager.Instance.OnSkinIndexChanged += OnSkinIndexChanged;
HeroUIManager.Instance.OnHeroCollectEvent += OnHeroCollectEvent;
PackManager.Instance.RefreshItemEvent += OnRefreshItemEvent;
showGetObj.SetActive(false);
showNormalObj.SetActive(true);
Display();
CreateScroller();
}
protected override void OnPreClose()
{
skinScroller.OnRefreshCell -= OnRefreshCell;
HeroUIManager.Instance.OnSkinIndexChanged -= OnSkinIndexChanged;
HeroUIManager.Instance.OnHeroCollectEvent -= OnHeroCollectEvent;
PackManager.Instance.RefreshItemEvent -= OnRefreshItemEvent;
hero = null;
heroConfig = null;
HeroUIManager.Instance.selectSkinIndex = -1;
}
public void Display()
{
skinID = heroConfig.SkinIDList[HeroUIManager.Instance.selectSkinIndex];
bgTexture.SetTexture2D(HeroUIManager.Instance.GetBGName(skinID, heroConfig.Country));
roleLhModel.Create(skinID, 1, motionName: "", isLh: true);
roleXsModel.Create(skinID, 1);
HeroUIManager.Instance.PlayerLHSound(skinID);
nameText.text = heroConfig.Name;
skinNameText.text = HeroSkinConfig.Get(skinID).SkinName;
RefreshAttr();
ShowBtns();
HeroUIManager.Instance.skinRedpoint.state = HeroUIManager.Instance.HeroAllSkinStateForRedpoint(heroID, hero == null) > 0 ? RedPointState.Simple : RedPointState.None;
}
void RefreshAttr()
{
var cfg = HeroSkinAttrConfig.Get(skinID);
if (cfg == null)
{
attrObj.SetActive(false);
return;
}
attrObj.SetActive(true);
string format = "{0}" + UIHelper.AppendColor(TextColType.Green, "+{1}", false);
for (int i = 0; i < onAttrText.Length; i++)
{
if (i < cfg.WearAttrIDList.Length)
{
onAttrText[i].text = PlayerPropertyConfig.GetFullDescription(cfg.WearAttrIDList[i], cfg.WearAttrValueList[i], format);
}
else
{
onAttrText[i].text = "";
}
}
for (int i = 0; i < allAttrText.Length; i++)
{
if (i < cfg.RoleAttrIDList.Length)
{
allAttrText[i].text = PlayerPropertyConfig.GetFullDescription(cfg.RoleAttrIDList[i], cfg.RoleAttrValueList[i], format);
}
else
{
allAttrText[i].text = "";
}
}
}
void CreateScroller()
{
skinScroller.Refresh();
for (int i = 0; i < heroConfig.SkinIDList.Length; i++)
{
skinScroller.AddCell(ScrollerDataType.Header, i);
}
skinScroller.Restart();
if (HeroUIManager.Instance.selectSkinIndex > 2)
{
skinScroller.JumpIndex(HeroUIManager.Instance.selectSkinIndex - 1);
}
}
void OnRefreshCell(ScrollerDataType type, CellView cell)
{
var _cell = cell as HeroSkinCell;
_cell.Display(heroID, cell.index, hero != null);
}
void OnSkinIndexChanged()
{
Display();
skinScroller.m_Scorller.RefreshActiveCellViews();
}
void OnHeroCollectEvent()
{
if (activeSkinID == skinID && HeroUIManager.Instance.IsHeroSkinActive(heroID, skinID))
{
showGetObj.SetActive(true);
ChangeParentWinAlpha(0);
showNormalObj.SetActive(false);
Display();
skinScroller.m_Scorller.RefreshActiveCellViews();
activeSkinID = 0;
return;
}
}
void OnRefreshItemEvent(PackType type, int index, int itemID)
{
if (type == PackType.Hero)
{
ShowBtns();
skinScroller.m_Scorller.RefreshActiveCellViews();
}
}
void UnLockSkin()
{
var cfg = HeroSkinAttrConfig.Get(skinID);
if (cfg == null)
{
return;
}
if (!ItemLogicUtility.CheckItemCount(PackType.Item, cfg.NeedItemID, 1, 2))
{
return;
}
activeSkinID = skinID;
HeroUIManager.Instance.SendSkinOP(heroID, skinID, 1);
}
void ShowBtns()
{
if (hero == null)
{
putonBtn.SetActive(false);
putonYetObj.SetActive(false);
shopBtn.SetActive(false);
changeClothBtn.SetActive(false);
}
else
{
shopBtn.SetActive(true);
changeClothBtn.SetActive(true);
}
if (!HeroUIManager.Instance.IsHeroSkinActive(heroID, skinID))
{
var cfg = HeroSkinAttrConfig.Get(skinID);
if (cfg == null)
{
unlockBtn.SetActive(false);
}
else
{
unlockBtn.SetActive(true);
int itemID = cfg.NeedItemID;
itemIcon.SetItemSprite(itemID);
itemCountText.text = UIHelper.ShowUseItem(PackType.Item, itemID, 1, bright: false);
}
putonBtn.SetActive(false);
putonYetObj.SetActive(false);
return;
}
unlockBtn.SetActive(false);
if (hero == null) return;
putonBtn.SetActive(hero.SkinAttrID != skinID);
putonYetObj.SetActive(hero.SkinAttrID == skinID);
}
void ChangeParentWinAlpha(float alpha)
{
if (hero != null)
{
UIManager.Instance.GetUI().GetCanvasGroup().alpha = alpha;
}
else
{
UIManager.Instance.GetUI().GetCanvasGroup().alpha = alpha;
}
}
}