/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024
using System.Collections.Generic;
using FairyGUI;
using FL.Data;
using XGame;
using XGame.Framework.UI;
namespace FL.FGUI
{
public class PartnerReplacePartnerPanelCtrlParam
{
public int PlanId;
public int SlotId;
}
///
/// UI逻辑处理类
///
///
public partial class PartnerReplacePartnerPanelCtrl
: UIController,
IPartnerReplacePanelCtrl
{
private int _planId;
private int _slotId;
private int _curSelectId;
public int PlanId => _planId;
public int SlotId => _slotId;
public int CurSelectId => _curSelectId;
protected override void OnEnable(object intent)
{
AddUIListenres();
AddEventListener();
ShowUI(intent as PartnerReplacePartnerPanelCtrlParam);
}
protected override void OnDisable()
{
RemoveUIListenres();
RemoveEventListener();
}
#region UI事件
private void AddUIListenres()
{
VM.UpBtn.onClick.Add(OnClickUpPartner);
}
private void RemoveUIListenres()
{
VM.UpBtn.onClick.Remove(OnClickUpPartner);
}
private void OnClickUpPartner(EventContext context)
{
if (_curSelectId <= 0)
{
Log.Error("当前选择圣兵id为0");
return;
}
var plan = PartnersData.Instance.GetPlanById(_planId);
if (plan == null)
{
Log.Error($"方案信息不存在id: {_planId}");
return;
}
foreach (var slot in plan.Slots)
{
if (slot.SbId == CurSelectId)
{
Context.ShowTips(StringDefine.PartnerAlreadyUpTips);
return;
}
}
Context.ClosePanel();
if (_planId == PartnersData.Instance.UsePlanId)
{
PartnersService.Instance.RequestPartnerUp(_slotId, _curSelectId);
}
else
{
PartnersService.Instance.RequestUpdatePlanSb(_planId, _slotId, _curSelectId);
}
}
#endregion
private void AddEventListener()
{
EventSingle.Instance.AddListener(
EventDefine.PartnerSelectReplace,
SelectReplacePartner
);
EventSingle.Instance.AddListener(EventDefine.PartnerBinding, OnInfoUpdate);
EventSingle.Instance.AddListener(EventDefine.PartnerUp, OnInfoUpdate);
EventSingle.Instance.AddListener(EventDefine.PartnerDown, OnInfoUpdate);
EventSingle.Instance.AddListener(EventDefine.PartnerUpdatePlan, OnInfoUpdate);
}
private void RemoveEventListener()
{
EventSingle.Instance.RemoveListener(
EventDefine.PartnerSelectReplace,
SelectReplacePartner
);
EventSingle.Instance.RemoveListener(EventDefine.PartnerBinding, OnInfoUpdate);
EventSingle.Instance.RemoveListener(EventDefine.PartnerUp, OnInfoUpdate);
EventSingle.Instance.RemoveListener(EventDefine.PartnerDown, OnInfoUpdate);
EventSingle.Instance.RemoveListener(EventDefine.PartnerUpdatePlan, OnInfoUpdate);
}
private void OnInfoUpdate(int eventId, object args)
{
UpdateList();
}
private void SelectReplacePartner(int eventId, object args)
{
var id = (int)args;
if (id == _curSelectId)
{
Context.UI.OpenAsync(
UIKeys.PartnerDetailsPanel,
new PartnerDetailsPanelParam()
{
PlanId = _planId,
SlotId = _slotId,
PartnerId = _curSelectId,
}
);
Context.ClosePanel();
return;
}
_curSelectId = id;
UpdateList();
}
private void ShowUI(PartnerReplacePartnerPanelCtrlParam param)
{
_slotId = param.SlotId;
_planId = param.PlanId;
var slot = PartnersData.Instance.GetPlanSlot(_planId, _slotId);
var list = PartnersData.Instance.PartnerList;
if (list.Count <= 0)
{
Log.Error($"圣兵库内不存在圣兵");
return;
}
var sbId = slot?.SbId ?? 0;
_curSelectId = sbId > 0 ? sbId : list[0].TableId;
UpdateList();
}
private void UpdateList()
{
var list = PartnersData.Instance.PartnerList;
var datas = new List();
foreach (var partner in list)
{
datas.Add(
new PartnerReplacePartnerListItemCtrlParam()
{
PartnerId = partner.TableId,
Ctrl = this,
}
);
}
VM.PartnerList.BindDatas(datas);
}
}
}