BagUsePanelCtrl.cs 3.3 KB

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