TemporaryEquipBagPanelCtrl.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using FairyGUI;
  7. using FL.Data;
  8. using FL.Data.Items;
  9. using System;
  10. using System.Collections.Generic;
  11. using XGame.Database;
  12. using XGame.Framework.UI;
  13. namespace FL.FGUI
  14. {
  15. /// <summary>
  16. /// UI逻辑处理类
  17. /// </summary>
  18. /// <typeparam name=""></typeparam>
  19. public partial class TemporaryEquipBagPanelCtrl : UIController<TemporaryEquipBagPanelVM>
  20. {
  21. private const int MaxRow = 6;// 最大行数
  22. private List<EquipAttr> _equipDataList; //装备背包的数据
  23. private Dictionary<EEquipType, long> _wearEquipPowerMap; // 身上部位穿戴的装备战力数据
  24. protected override void OnEnable(object intent)
  25. {
  26. AddUIListenres();
  27. EventSingle.Instance.AddListener(EventDefine.RefreshTemporaryEquip, RefreshTemporaryEquip);
  28. EventSingle.Instance.AddListener(EventDefine.OnChangeEquip, OnChangeEquip);
  29. ShowUI();
  30. }
  31. protected override void OnDisable()
  32. {
  33. RemoveUIListenres();
  34. EventSingle.Instance.RemoveListener(EventDefine.RefreshTemporaryEquip, RefreshTemporaryEquip);
  35. EventSingle.Instance.RemoveListener(EventDefine.OnChangeEquip, OnChangeEquip);
  36. _wearEquipPowerMap?.Clear();
  37. _wearEquipPowerMap = null;
  38. _equipDataList?.Clear();
  39. _equipDataList = null;
  40. }
  41. #region UI事件
  42. private void AddUIListenres()
  43. {
  44. VM.SellBtn.onClick.Add(OnClickSellBtn);
  45. }
  46. private void RemoveUIListenres()
  47. {
  48. VM.SellBtn.onClick.Remove(OnClickSellBtn);
  49. }
  50. /// <summary>
  51. /// 一键出售按钮
  52. /// </summary>
  53. /// <param name="context"></param>
  54. private void OnClickSellBtn(EventContext context)
  55. {
  56. Context.ClosePanel();
  57. DragonEggService.Instance.SendToSellAllBagEquips();
  58. }
  59. #endregion
  60. private void Init()
  61. {
  62. if (_equipDataList == null) _equipDataList = new List<EquipAttr>();
  63. else _equipDataList.Clear();
  64. if (_wearEquipPowerMap == null)
  65. {
  66. _wearEquipPowerMap = new Dictionary<EEquipType, long>();
  67. for (int i = 1; i < 7; i++)
  68. {
  69. _wearEquipPowerMap.Add((EEquipType)i, 0);
  70. }
  71. }
  72. EquipData.Instance.GetWearEquipPower(ref _wearEquipPowerMap);
  73. }
  74. private void ShowUI()
  75. {
  76. bool bEmpty = EquipData.Instance.IsEquipbagEmpty();
  77. VM.EmptyCtrl.selectedIndex = bEmpty ? 1 : 0;
  78. if (bEmpty)
  79. {
  80. VM.ListBG.height = 425;
  81. VM.UIGroup.height = 462;
  82. return;
  83. }
  84. Init();
  85. EquipData.Instance.GetBagEquipList(ref _equipDataList);
  86. OnSortEquipDataList();
  87. VM.EquipList.BindDatas(_equipDataList);
  88. int num = (int)Math.Ceiling(_equipDataList.Count / 5.0f);
  89. int addHeight = (Math.Min(num, MaxRow)-1) * 140 -32;
  90. VM.EquipList.height = 140 + addHeight;
  91. VM.ListBG.height = 425 + addHeight;
  92. VM.UIGroup.height = 462 + addHeight;
  93. }
  94. private void OnSortEquipDataList()
  95. {
  96. _equipDataList.Sort((a, b) =>
  97. {
  98. var aHighPower = GetCompareValue(a);
  99. var bHighPower = GetCompareValue(b);
  100. if (aHighPower == bHighPower)
  101. {
  102. if (a.FightingPower == b.FightingPower)
  103. {
  104. return a.Id.CompareTo(b.Id); // 升序
  105. }
  106. return b.FightingPower.CompareTo(a.FightingPower); //降序
  107. }
  108. else
  109. return bHighPower - aHighPower;
  110. });
  111. }
  112. /// <summary>
  113. /// 比身上穿戴的装备的战力高标识
  114. /// </summary>
  115. /// <param name="equipData"></param>
  116. /// <returns></returns>
  117. private int GetCompareValue(EquipAttr equipData)
  118. {
  119. int compareFlag = 0;
  120. if (equipData?.Table.Part > 0)
  121. {
  122. if (equipData.FightingPower > _wearEquipPowerMap[equipData.Table.Part])
  123. compareFlag = 1;
  124. }
  125. return compareFlag;
  126. }
  127. /// <summary>
  128. /// 更换装备
  129. /// </summary>
  130. /// <param name="type"></param>
  131. /// <param name="id"></param>
  132. private void OnChangeEquip(int eventId, object args)
  133. {
  134. if (args == null)
  135. {
  136. return;
  137. }
  138. var changeEquipPartList = args as List<EEquipType>;
  139. if (changeEquipPartList?.Count > 0)
  140. {
  141. foreach (var equipPart in changeEquipPartList)
  142. {
  143. _wearEquipPowerMap[equipPart] = EquipData.Instance.GetWearPartPower(equipPart);
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// 刷新临时装备背包
  149. /// </summary>
  150. /// <param name="eventId"></param>
  151. /// <param name="args"></param>
  152. private void RefreshTemporaryEquip(int eventId, object args)
  153. {
  154. _equipDataList.Clear();
  155. EquipData.Instance.GetBagEquipList(ref _equipDataList);
  156. OnSortEquipDataList();
  157. VM.EquipList.BindDatas(_equipDataList);
  158. VM.EquipList.scrollPane.SetPercY(0,false);
  159. }
  160. }
  161. }