UIOptionalGiftCtr.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. local UIOptionalGiftCtr = class("UIOptionalGiftCtr", require("UICtrBase"))
  2. function UIOptionalGiftCtr:Init(view)
  3. self.view = view
  4. end
  5. function UIOptionalGiftCtr:SetData(data)
  6. self.asyncIdx = 0
  7. if data == nil then return end
  8. self.data = data
  9. end
  10. function UIOptionalGiftCtr:GetAsyncIdx()
  11. self.asyncIdx = self.asyncIdx + 1
  12. return self.asyncIdx
  13. end
  14. function UIOptionalGiftCtr:GetData()
  15. return self.data
  16. end
  17. function UIOptionalGiftCtr:OnDispose()
  18. self.itemId = nil
  19. self.cfgId = nil
  20. self.num = nil
  21. self.cfgData = nil
  22. self.selectIdx = nil
  23. self.selectCfgIds = nil
  24. self.selectCfgIdNum = nil
  25. self.data = nil
  26. self.view = nil
  27. end
  28. function UIOptionalGiftCtr:RefreshData()
  29. if self.data then
  30. self.itemId = self.data[1]
  31. self.cfgId = self.data[2]
  32. self.num = self.data[3]
  33. else
  34. self.itemId = nil
  35. self.cfgId = nil
  36. self.num = nil
  37. end
  38. self.cfgData = nil
  39. self.selectIdx = nil
  40. self.selectCfgIds = nil
  41. self.selectCfgIdNum = 0
  42. if self.cfgId then
  43. self.cfgData = ManagerContainer.CfgMgr:GetItemById(self.cfgId)
  44. if self.cfgData then
  45. local composeItems = self.cfgData.ComposeItem
  46. if composeItems then
  47. self.selectCfgIdNum = #composeItems
  48. self.selectCfgIds = {}
  49. for i = 1, self.selectCfgIdNum do
  50. local composeItem = composeItems[i]
  51. self.selectCfgIds[i] = {cfgId = composeItem[1], num = composeItem[2]}
  52. end
  53. end
  54. end
  55. end
  56. end
  57. function UIOptionalGiftCtr:GetCfgData()
  58. return self.cfgData
  59. end
  60. function UIOptionalGiftCtr:GetSelectCfgIdNum()
  61. return self.selectCfgIdNum
  62. end
  63. function UIOptionalGiftCtr:GetSelectItemData(idx)
  64. if self.selectCfgIds then
  65. return self.selectCfgIds[idx]
  66. end
  67. return nil
  68. end
  69. function UIOptionalGiftCtr:GetSelectIdx()
  70. return self.selectIdx
  71. end
  72. function UIOptionalGiftCtr:SetSelectIdx(idx)
  73. if self.selectIdx == idx then
  74. return false
  75. end
  76. self.selectIdx = idx
  77. return true
  78. end
  79. function UIOptionalGiftCtr:CanCost()
  80. if self.num then
  81. if self:GetOwnResCountByItemId() >= self.num then
  82. return true
  83. end
  84. end
  85. return false
  86. end
  87. function UIOptionalGiftCtr:GetOwnResCountByItemId()
  88. if self.itemId then
  89. local item = ManagerContainer.DataMgr.BagData:GetItemById(self.itemId)
  90. if item then
  91. return item.num
  92. end
  93. end
  94. return 0
  95. end
  96. function UIOptionalGiftCtr:SendOpenGiftReq()
  97. if not self.selectIdx then
  98. return 'ZixuanBag2'
  99. end
  100. if not self.selectCfgIds then
  101. return 'ZixuanBag1'
  102. end
  103. local itemData = self.selectCfgIds[self.selectIdx]
  104. if not itemData and not itemData.cfgId then
  105. return 'ZixuanBag1'
  106. end
  107. if not self.itemId then
  108. return 'ZixuanBag1'
  109. end
  110. if not self.cfgId or not self.num or self.num <= 0 then
  111. return 'ZixuanBag1'
  112. end
  113. if CommonUtil.GetOwnResCountByItemId(self.cfgId) < self.num then
  114. return 'ZixuanBag3'
  115. end
  116. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_USE_ITEM_REQ, {
  117. item_id = self.itemId,
  118. item_num = self.num,
  119. item_idx_list = {self.selectIdx - 1},
  120. force_item_id = true,
  121. })
  122. self.selectIdx = nil
  123. return 0
  124. end
  125. return UIOptionalGiftCtr