BagUsePanelCtrl.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /// #pkgName FGUI包名
  2. /// #panelName UIPanel名字
  3. /// #UIName = $"{#pkgName}{#panelName}" UIKey名字
  4. /// 该脚本由模板创建
  5. /// created by cb 2024
  6. using Codice.CM.Common;
  7. using FairyGUI;
  8. using FL.Data;
  9. using XGame.Database;
  10. using XGame.Framework.UI;
  11. using static Codice.CM.WorkspaceServer.WorkspaceTreeDataStore;
  12. namespace FL.FGUI
  13. {
  14. /// <summary>
  15. /// UI逻辑处理类
  16. /// </summary>
  17. /// <typeparam name=""></typeparam>
  18. public partial class BagUsePanelCtrl : UIController<BagUsePanelVM>
  19. {
  20. private int _useNum; // 要使用的数量
  21. private int _maxNum; // 拥有的数量
  22. protected override void OnEnable(object intent)
  23. {
  24. AddUIListenres();
  25. ShowUI(intent as ItemTable);
  26. }
  27. protected override void OnDisable()
  28. {
  29. RemoveUIListenres();
  30. }
  31. #region UI事件
  32. private void AddUIListenres()
  33. {
  34. VM.UseBtn.onClick.Add(OnClickUseBtn);
  35. VM.ReduceOneBtn.onClick.Add(OnClickReduceOneBtn);
  36. VM.ReduceTenBtn.onClick.Add(OnClickReduceTenBtn);
  37. VM.AddOneBtn.onClick.Add(OnClickAddOneBtn);
  38. VM.AddTenBtn.onClick.Add(OnClickAddTenBtn);
  39. VM.MaxBtn.onClick.Add(OnClickMaxBtn);
  40. }
  41. private void RemoveUIListenres()
  42. {
  43. VM.UseBtn.onClick.Remove(OnClickUseBtn);
  44. VM.ReduceOneBtn.onClick.Remove(OnClickReduceOneBtn);
  45. VM.ReduceTenBtn.onClick.Remove(OnClickReduceTenBtn);
  46. VM.AddOneBtn.onClick.Remove(OnClickAddOneBtn);
  47. VM.AddTenBtn.onClick.Remove(OnClickAddTenBtn);
  48. VM.MaxBtn.onClick.Remove(OnClickMaxBtn);
  49. }
  50. private void OnClickUseBtn(EventContext context)
  51. {
  52. }
  53. private void OnClickReduceOneBtn(EventContext context)
  54. {
  55. ShowUseNum(_useNum - 1);
  56. }
  57. private void OnClickReduceTenBtn(EventContext context)
  58. {
  59. ShowUseNum(_useNum - 10);
  60. }
  61. private void OnClickAddOneBtn(EventContext context)
  62. {
  63. ShowUseNum(_useNum + 1);
  64. }
  65. private void OnClickAddTenBtn(EventContext context)
  66. {
  67. ShowUseNum(_useNum + 10);
  68. }
  69. private void OnClickMaxBtn(EventContext context)
  70. {
  71. ShowUseNum(_maxNum);
  72. }
  73. #endregion
  74. private void ShowUI(ItemTable itemInfo)
  75. {
  76. if (itemInfo != null)
  77. {
  78. _maxNum = (int)ItemData.Instance.GetItemNum(itemInfo.Id);
  79. VM.CountLabel.text = _maxNum.ToString();
  80. VM.QualityIcon.icon = AddressableDefine.ItemFrame(itemInfo.Quality);
  81. VM.ItemIcon.icon = itemInfo.Icon;
  82. ShowUseNum(1);
  83. }
  84. }
  85. private void ShowUseNum(int count)
  86. {
  87. if (count < 0 || count > _maxNum) return;
  88. _useNum = count;
  89. VM.UseNumLabel.text = _useNum.ToString();
  90. ShowBtnState();
  91. }
  92. /// <summary>
  93. /// 增加|减少数量的按钮状态
  94. /// </summary>
  95. private void ShowBtnState()
  96. {
  97. VM.ReduceOneBtn.enabled = _useNum != 0;
  98. VM.ReduceTenBtn.enabled = _useNum != 0;
  99. VM.UseBtn.enabled = _useNum != 0;
  100. VM.AddOneBtn.enabled = _useNum != _maxNum;
  101. VM.AddTenBtn.enabled = _useNum != _maxNum;
  102. }
  103. }
  104. }