/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using FairyGUI;
using FL.Data;
using XGame;
using XGame.Database;
using XGame.Framework.UI;
namespace FL.FGUI
{
public class PartnerDetailsPanelParam
{
public int PlanId;
///
/// 槽位id,-1为非更换界面打开的详情
///
public int SlotId;
public int PartnerId;
}
///
/// UI逻辑处理类
///
///
public partial class PartnerDetailsPanelCtrl : UIController
{
private int _partnerId;
private int _slotId;
private int _planId;
private bool _initView = false;
private List _partnerIds = new List();
protected override void OnEnable(object intent)
{
var param = intent as PartnerDetailsPanelParam;
_partnerId = param.PartnerId;
_planId = param.PlanId;
_slotId = param.SlotId;
AddUIListenres();
AddEventListener();
InitPartnerIdList();
ShowUI();
}
protected override void OnDisable()
{
RemoveUIListenres();
RemoveEventListener();
}
#region 事件
private void AddUIListenres()
{
VM.PreviousBtn.onClick.Add(OnClickPreviousBtn);
VM.NextBtn.onClick.Add(OnClickNextBtn);
VM.ChangeBtn.onClick.Add(OnClickChangeBtn);
VM.UpLvBtn.onClick.Add(OnClickUpLvBtn);
VM.UpBtn.onClick.Add(OnClickUpBtn);
VM.DownBtn.onClick.Add(OnClickDownBtn);
VM.UpStarBtn.onClick.Add(OnClickUpStarBtn);
VM.BackBtn.onClick.Add(OnClickBackBtn);
VM.EpiItem.VM.PanelEvent.Add(OnClickEpiSet);
}
private void RemoveUIListenres()
{
VM.PreviousBtn.onClick.Remove(OnClickPreviousBtn);
VM.NextBtn.onClick.Remove(OnClickNextBtn);
VM.ChangeBtn.onClick.Remove(OnClickChangeBtn);
VM.UpLvBtn.onClick.Remove(OnClickUpLvBtn);
VM.UpBtn.onClick.Remove(OnClickUpBtn);
VM.DownBtn.onClick.Remove(OnClickDownBtn);
VM.UpStarBtn.onClick.Remove(OnClickUpStarBtn);
VM.BackBtn.onClick.Remove(OnClickBackBtn);
VM.EpiItem.VM.PanelEvent.Remove(OnClickEpiSet);
}
private void AddEventListener()
{
EventSingle.Instance.AddListener(EventDefine.PartnerUpLevel, OnUpdateLv);
EventSingle.Instance.AddListener(EventDefine.PartnerUpStar, OnUpdateStar);
EventSingle.Instance.AddListener(EventDefine.UpdataItemData, OnItemUpdate);
EventSingle.Instance.AddListener(EventDefine.PartnerUp, OnUpDownPartner);
EventSingle.Instance.AddListener(EventDefine.PartnerDown, OnUpDownPartner);
EventSingle.Instance.AddListener(EventDefine.PartnerBinding, OnBinding);
}
private void RemoveEventListener()
{
EventSingle.Instance.RemoveListener(EventDefine.PartnerUpLevel, OnUpdateLv);
EventSingle.Instance.RemoveListener(EventDefine.PartnerUpStar, OnUpdateStar);
EventSingle.Instance.RemoveListener(EventDefine.UpdataItemData, OnItemUpdate);
EventSingle.Instance.RemoveListener(EventDefine.PartnerUp, OnUpDownPartner);
EventSingle.Instance.RemoveListener(EventDefine.PartnerDown, OnUpDownPartner);
EventSingle.Instance.RemoveListener(EventDefine.PartnerBinding, OnBinding);
}
private void OnBinding(int eventId, object args)
{
ShowEpi();
}
private void OnUpdateStar(int eventId, object args)
{
ShowStarUI();
}
private void OnItemUpdate(int eventId, object args)
{
var info = PartnersData.Instance.GetPartnerAttrByTableId(_partnerId);
var starLv = info?.RisingStarLv ?? 1;
ShowStarRatingCostUI(starLv);
}
private void OnClickPreviousBtn(EventContext context)
{
OnSelectPartnerOffset(1);
}
private void OnClickNextBtn(EventContext context)
{
OnSelectPartnerOffset(-1);
}
private void OnClickEpiSet(EventContext context)
{
var info = PartnersData.Instance.GetPartnerAttrByTableId(_partnerId);
if (info == null)
{
Context.ShowTips(StringDefine.PartnerEmptyTips);
return;
}
Context.UI.OpenAsync(
UIKeys.PartnerEpiChangePanel,
new PartnerEpiChangePanelCtrlParam()
{
PlanId = PartnersData.Instance.UsePlanId,
SlotId = -1,
SbId = _partnerId,
}
);
}
private void OnClickChangeBtn(EventContext context)
{
var info = PartnersData.Instance.GetPartnerAttrByTableId(_partnerId);
if (info == null)
{
Context.ShowTips(StringDefine.PartnerEmptyTips);
return;
}
Context.UI.OpenAsync(
UIKeys.PartnerEpiChangePanel,
new PartnerEpiChangePanelCtrlParam()
{
PlanId = PartnersData.Instance.UsePlanId,
SlotId = -1,
SbId = _partnerId,
}
);
}
private void OnClickUpLvBtn(EventContext context)
{
var info = PartnersData.Instance.GetPartnerAttrByTableId(_partnerId);
if (info == null)
{
Context.ShowTips(StringDefine.PartnerEmptyTips);
return;
}
var lvTable = partnerLvUpTableRepo.GetPartnerLvUpTable(_partnerId, info.Level);
if (lvTable == null)
{
Context.ShowTips(StringDefine.PartnerUpLvEmptyTips);
return;
}
if (
!ItemService.Instance.IsEnough(lvTable.LevelUpCost[0], lvTable.LevelUpCost[1], true)
)
return;
PartnersService.Instance.RequestLvUp(_partnerId);
}
private void OnClickUpBtn(EventContext context)
{
if (!PartnersData.Instance.TryGetPartnerAttrByTableId(_partnerId, out var info))
{
Context.ShowTips(StringDefine.PartnerEmptyTips);
return;
}
if (PartnersData.Instance.UsePlanId == _planId)
{
if (info.Status > 0)
{
Context.ShowTips(StringDefine.PartnerAlreadyUpTips);
return;
}
if (_slotId < 0)
{
PartnersService.Instance.OpenPartnerMode(_partnerId);
}
else
{
PartnersService.Instance.RequestPartnerUp(_slotId, _partnerId);
}
}
else
{
PartnersService.Instance.RequestUpdatePlanSb(_planId, _slotId, _partnerId);
}
Context.ClosePanel();
}
///
/// 下阵按钮事件
///
///
private void OnClickDownBtn(EventContext context)
{
if (!PartnersData.Instance.TryGetPartnerAttrByTableId(_partnerId, out var info))
{
Context.ShowTips(StringDefine.PartnerEmptyTips);
return;
}
if (_planId == PartnersData.Instance.UsePlanId)
{
PartnersService.Instance.RequestPartnerDown(_partnerId);
}
else
{
if (!PartnersData.Instance.TryGetPlan(_planId, out var plan))
{
Context.ShowTips(StringDefine.PartnerPlanEmptyTips);
return;
}
if (!plan.TryGetUpPos(_partnerId, out var upPos))
{
Context.ShowTips(StringDefine.PartnerAlreadyUpTips);
return;
}
PartnersService.Instance.RequestUpdatePlanSb(_planId, upPos, 0);
}
Context.ClosePanel();
}
private void OnClickUpStarBtn(EventContext context)
{
var info = PartnersData.Instance.GetPartnerAttrByTableId(_partnerId);
if (info == null)
{
Context.ShowTips(StringDefine.PartnerEmptyTips);
return;
}
var starTable = partnerStarUpTableRepo.GetPartnerStarTable(
_partnerId,
info.RisingStarLv
);
if (starTable == null)
{
Context.ShowTips(StringDefine.PartnerUpStarEmptyTips);
return;
}
if (!ItemService.Instance.IsEnough(starTable.PetId, starTable.StarUpCost, true))
return;
PartnersService.Instance.RequestStarUp(_partnerId);
}
private void OnClickBackBtn(EventContext context)
{
Context.ClosePanel();
}
private void OnUpDownPartner(int eventId, object args)
{
ShowUpDownBtn();
}
///
/// 圣兵升级监听事件
///
///
///
private void OnUpdateLv(int eventId, object args)
{
ShowLevelUI();
}
#endregion
#region 界面
private void ShowUI()
{
ShowPartnerUI();
ShowEpi();
ShowSwitchState();
}
private void ShowEpi()
{
var info = PartnersData.Instance.GetPartnerAttrByTableId(_partnerId);
if (info == null)
{
VM.EpiItem.Disable();
}
else
{
VM.EpiItem.Enable(null);
VM.EpiItem.Ctrl.SetData(info.EpigraphId, _partnerId, true);
}
}
private void ShowPartnerUI()
{
var table = PartnerTableRepo.Get(_partnerId);
if (table == null)
{
Log.Error($"圣兵tableId {_partnerId} 不存在");
return;
}
VM.PartnerIcon.icon = table.Icon;
//LoadPartnerSpine(partnerInfo?.Model);
VM.QualityIcon.icon = AddressableDefine.ItemFrame(table.Quality);
VM.SacredVesselsIcon.icon = table.Icon;
VM.AttrTypeIcon.icon = AddressableDefine.PartnerElementType((int)table.Element);
VM.NameLabel.text = table.Name;
VM.TypeLabel.text = StringDefine.partnerTypeList[table.Type - 1];
ShowUpDownBtn();
var info = PartnersData.Instance.GetPartnerAttrByTableId(_partnerId);
VM.Have.selectedIndex = info != null ? 1 : 0;
ShowLevelUI();
ShowStarUI();
}
private void ShowUpDownBtn()
{
if (!PartnersData.Instance.TryGetPlan(_planId, out var plan))
{
Log.Error($"圣兵信息不存在,id:{_planId}");
return;
}
VM.ShowBtnCtrl.selectedIndex = plan.TryGetUpPos(_partnerId, out var upPos) ? 1 : 0;
}
private void ShowStarUI()
{
var info = PartnersData.Instance.GetPartnerAttrByTableId(_partnerId);
var starLv = info?.RisingStarLv ?? 1;
ShowPartnerStarUI(starLv);
ShowStarRatingCostUI(starLv);
ShowStarOwnAttr(starLv);
ShowBuff(starLv);
}
///
/// 星星数
///
///
private void ShowPartnerStarUI(int starLv)
{
int starNum = starLv == 0 ? 0 : starLv % PartnersData.MaxStarNum;
if (starLv > 0 && starNum == 0)
starNum = PartnersData.MaxStarNum;
int starStep = PartnersService.Instance.GetStarStep(starLv);
int[] array = Enumerable.Repeat(starStep, starNum).ToArray();
VM.StarRatingList.BindDatas(array);
}
///
/// 拥有的星级效果属性描述
///
private void ShowStarOwnAttr(int starLv)
{
partnerStarUpTable starTable = partnerStarUpTableRepo.GetPartnerStarTable(
_partnerId,
starLv
);
if (starTable == null)
{
Log.Error($"圣兵升星表tableId:{_partnerId}, starLv:{starLv} 不存在");
return;
}
var attrsArray = starTable.StarOwnAttr;
if (attrsArray?.Length > 0)
{
string desc = string.Empty;
for (int i = 0; i < attrsArray.Length; i += 2)
{
GetAttributeDesc(attrsArray[i], attrsArray[i + 1], ref desc);
if (i + 1 < attrsArray.Length - 1)
{
desc += ",";
}
}
VM.SkillEffectLabel.text = string.Format(StringDefine.partnerStarAttribute, desc);
VM.SkillEffectLabel.visible = true;
}
else
{
VM.SkillEffectLabel.visible = false;
}
}
private void GetAttributeDesc(int attrType, int attrValue, ref string desc)
{
var attributeInfo = AttrDescTableRepo.Get(attrType);
string valStr = attrValue > 0 ? TableUtils.ToRealDouble(attrValue).ToString("F2") : "0";
desc += $"{attributeInfo?.ShowName}{valStr}%";
}
///
/// 升星消耗
///
///
private void ShowStarRatingCostUI(int starLv)
{
partnerStarUpTable starTable = partnerStarUpTableRepo.GetPartnerStarTable(
_partnerId,
starLv
);
if (starTable == null)
{
Log.Error($"圣兵升星表tableId:{_partnerId}, starLv:{starLv} 不存在");
return;
}
bool isMaxStar =
partnerStarUpTableRepo.GetPartnerStarTable(_partnerId, starLv + 1) == null;
VM.MaxStarCtrl.selectedIndex = isMaxStar ? 1 : 0;
if (!isMaxStar && starTable?.StarUpCost > 0)
{
long num = ItemData.Instance.GetItemNum(starTable.PetId);
VM.StarCostBar.max = starTable.StarUpCost;
VM.StarCostBar.value = num;
}
}
private void InitPartnerIdList()
{
if (_initView)
return;
var attrs = PartnerTableRepo.GetAll();
foreach (var attr in attrs)
{
_partnerIds.Add(attr.Id);
}
_initView = true;
}
private void OnSelectPartnerOffset(int offset)
{
var index = _partnerIds.IndexOf(_partnerId);
index += offset;
if (index < 0)
{
index += _partnerIds.Count;
}
else if (index >= _partnerIds.Count)
{
index -= _partnerIds.Count;
}
_partnerId = _partnerIds[index];
ShowUI();
}
///
/// 上一个|下一个按钮显隐
///
private void ShowSwitchState()
{
VM.CanSwitch.selectedIndex = (_partnerIds.Count > 0 && _slotId < 0) ? 1 : 0;
}
///
/// 圣器升级基础属性
///
///
///
private void ShowLevelUI()
{
var info = PartnersData.Instance.GetPartnerAttrByTableId(_partnerId);
var lv = info?.Level ?? 1;
var lvTable = partnerLvUpTableRepo.GetPartnerLvUpTable(_partnerId, lv);
if (lvTable == null)
{
Log.Error($"圣兵等级提升表不存在sbId{_partnerId}, lv:{lv}");
return;
}
bool bMaxLv = partnerLvUpTableRepo.GetPartnerLvUpTable(_partnerId, lv + 1) == null;
if (info == null)
{
VM.UpgradeCostItem.Disable();
}
else if (bMaxLv)
{
VM.UpgradeCostItem.Disable();
}
else
{
VM.UpgradeCostItem.Enable(null);
}
VM.LvLabel.text = $"LV.{lv}";
if (lvTable.LvAttr.Length > 0)
{
VM.MaxLvCtrl.selectedIndex = bMaxLv ? 1 : 0;
if (!bMaxLv)
{
if (lvTable?.LevelUpCost?.Length > 0)
{
VM.UpgradeCostItem.Ctrl.ShowCostUI(
lvTable.LevelUpCost[0],
lvTable.LevelUpCost[1]
);
}
}
var atkVal = lvTable.LvAttr[1];
//var attributeInfo = AttrDescTableRepo.Get(attrType);
string desc = atkVal > 0 ? TableUtils.ToRealDouble(atkVal).ToString("F1") : "0";
//VM.AtkLabel.text = $"{attributeInfo?.ShowName}:{desc}";
VM.AtkLabel.text = string.Format(StringDefine.partnerAttack, desc);
if (lvTable.LvAttr.Length > 3)
{
//var attributeInfo = AttrDescTableRepo.Get(attrType);
var atkSpeed = lvTable.LvAttr[3];
string descSpeed =
atkSpeed > 0 ? TableUtils.ToRealDouble(atkSpeed).ToString("F1") : "0";
//VM.AtkSpeedLabel.text = $"{attributeInfo?.ShowName}:{desc}";
VM.AtkSpeedLabel.text = string.Format(StringDefine.partnerAtkSpeed, descSpeed);
}
}
}
///
/// 加载圣器的spine
///
///
private void LoadPartnerSpine(string spineName)
{
VM.PartnerSpine.LoadSpine(spineName, "idle", true);
}
private void ShowBuff(int starLv)
{
var table = PartnerTableRepo.Get(_partnerId);
if (table == null)
{
Log.Error($"圣兵tableId {_partnerId} 不存在");
return;
}
var starSkillIds = table?.Skill;
var dataList = new List>();
if (starSkillIds?.Length > 0)
{
int index = 0;
for (int i = 0; i < starSkillIds.Length; i += 2)
{
// 0星激活的技能效果
if (starSkillIds[i + 1] == 0)
{
ShowSkillBaseInfo(starSkillIds[i]);
}
else
{
var data = new List() { starSkillIds[i], starSkillIds[i + 1], starLv };
dataList.Add(data);
}
index++;
}
}
VM.StarBuffItemList.BindDatas(dataList);
}
///
/// 基础技能描述
///
///
private void ShowSkillBaseInfo(int skillId)
{
var skillInfo = SkillTableRepo.Get(skillId);
VM.SkillInfoLabel.text = skillInfo == null ? "" : skillInfo.Desc;
}
#endregion
}
}