KLSelfSelectBoxPanel.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ---@class KLSelfSelectBoxPanel:UIKmlLuaPanelBase
  2. ---@field view KLSelfSelectBoxPanelView
  3. ---@field item cfg_item_column @道具宝箱
  4. ---@field box cfg_box_column @道具宝箱
  5. ---@field curCount number @已选个数
  6. ---@field totleCount number @总共可选个数
  7. ---@field useItems BagProtos.SelfChoiceBoxInfo[]
  8. local KLSelfSelectBoxPanel = class(UIKmlLuaPanelBase)
  9. local this = KLSelfSelectBoxPanel
  10. ---创建时调用一次
  11. function this:Init()
  12. self.item = nil
  13. self.curCount = 0
  14. self.curCountList = {}
  15. self.totleCount = 0
  16. self.BoxTemplateList = {}
  17. self.useItems = {}
  18. self.itemCount = {}
  19. GUI:DataListInitData(self.view.loopscrollviewData,
  20. function()
  21. return self:LoopDataListItemCountFunc()
  22. end,
  23. function(realIndex)
  24. return self:LoopDataListItemGetFunc(realIndex)
  25. end,
  26. nil,
  27. function(realIndex, kmlcontrol)
  28. return self:LoopDataListItemUpdateFunc(realIndex, kmlcontrol)
  29. end)
  30. self.BoxTemplateList = {}
  31. end
  32. function this:LoopDataListItemCountFunc()
  33. return #self.groupList
  34. end
  35. function this:LoopDataListItemGetFunc()
  36. local item = GUI:UIPanel_Open("dev/ui/ItemBase/Item/KLSelfSelectBox/KLSelfSelectBoxItem", self.view.loopscrollviewData, self, nil, true)
  37. local kmlControl = item.view.root
  38. self.BoxTemplateList[kmlControl] = item
  39. return kmlControl
  40. end
  41. function this:LoopDataListItemUpdateFunc(realIndex, kmlcontrol)
  42. ---@type KLSelfSelectBoxItem
  43. local item = self.BoxTemplateList[kmlcontrol]
  44. item:InitData(self.groupList[realIndex + 1], self, self.itemCount[self.groupList[realIndex + 1]])
  45. --local test = GUI:GetChildControl(self.view.equipzhiyeNeedData,realIndex,'test')
  46. --GUI:Text_setString(test,"dddddddd")
  47. end
  48. ---创建或者刷新界面数据时调用
  49. function this:Refresh()
  50. self.itemIndex = self.args.gridIndex
  51. self.item = SL:GetConfig("cfg_item", self.args.item.cfgId)
  52. if self.item == nil then
  53. return
  54. end
  55. local titleColor = SL:GetColorCfg(self.item.color)
  56. if titleColor then
  57. GUI:Text_setTextColor(self.view.text4, titleColor.color)
  58. end
  59. GUI:Text_setString(self.view.text4, self.item.name)
  60. self.groupList = self:GetAllGroupList()
  61. GUI:DataListUpdateData(self.view.loopscrollviewData)
  62. --SL:ScheduleOnce(0.15,function()
  63. -- local layoutW, _ = GUI:getSizeDelta(self.view.ItemLayout)
  64. -- local scrollW, _ = GUI:getSizeDelta(self.view.scrollview)
  65. -- GUI:setPositionX(self.view.ItemLayout, layoutW / 2 - scrollW)
  66. --end)
  67. end
  68. function this:GetAllGroupList()
  69. if string.isNullOrEmpty(self.item.useParam) then
  70. SL:LogError('self.item.useParam is nil')
  71. end
  72. self.curCountList = {}
  73. self.useItems = {}
  74. local boxgroupList = {}
  75. self:GetGroupList(boxgroupList, self.item.useParam)
  76. self.totleCount = SL:GetBagItemCount(self.item.id)
  77. self:SetNumText()
  78. --for _, v in ipairs(self.box.groupId) do
  79. -- ---@type cfg_box_group_column
  80. -- local boxgroup = SL:GetConfig("cfg_box_group",v)
  81. -- if boxgroup ~= nil then
  82. --
  83. -- self:GetGroupList(boxgroupList, boxgroup.item1)
  84. -- self:GetGroupList(boxgroupList, boxgroup.item2)
  85. -- self:GetGroupList(boxgroupList, boxgroup.item3)
  86. -- self:GetGroupList(boxgroupList, boxgroup.item4)
  87. -- self:GetGroupList(boxgroupList, boxgroup.item5)
  88. -- end
  89. --end
  90. return boxgroupList
  91. end
  92. ---@param originBoxGroupList table
  93. ---@param groupList IntListList
  94. function this:GetGroupList(originBoxGroupList, groupList)
  95. local tbl = SL:Split(groupList, "|")
  96. for _, v in pairs(tbl) do
  97. local item = SL:Split(v, "#")
  98. local id = tonumber(item[1])
  99. if not table.contains(originBoxGroupList, id) then
  100. table.insert(originBoxGroupList, id)
  101. if item[2] then
  102. self.itemCount[id] = tonumber(item[2])
  103. else
  104. self.itemCount[id] = 1
  105. end
  106. end
  107. end
  108. return originBoxGroupList
  109. end
  110. ---注册UI事件和服务器消息
  111. function this:RegistEvents()
  112. GUI:AddOnClickEvent(self.view.btnclose, self, self.CloseBtnClick)
  113. GUI:AddOnClickEvent(self.view.btnopen, self, self.OpenBtnClick)
  114. end
  115. function this:CloseBtnClick()
  116. GUI:UIPanel_Close("dev/ui/ItemBase/Panel/KLSelfSelectBox/KLSelfSelectBoxPanel")
  117. end
  118. function this:SetItemCount(itemId, num)
  119. if not self.curCountList[itemId] then
  120. self.curCountList[itemId] = 0
  121. end
  122. self.curCountList[itemId] = self.curCountList[itemId] + num
  123. self:SetNumText()
  124. self:SetItemInfo()
  125. end
  126. function this:InputItemCount(itemId, num)
  127. if not self.curCountList[itemId] then
  128. self.curCountList[itemId] = 0
  129. end
  130. self.curCountList[itemId] = num
  131. self:SetNumText()
  132. self:SetItemInfo()
  133. end
  134. function this:SetNumText()
  135. self.curCount = 0
  136. for i, v in pairs(self.curCountList) do
  137. self.curCount = self.curCount + v
  138. end
  139. GUI:Text_setString(self.view.NumText, "当前持有数量: <color=#1add1f>" .. tostring(self.curCount) .. '/' .. tostring(self.totleCount) .. "</color>")
  140. end
  141. function this:SetItemInfo()
  142. self.useItems = {}
  143. for i, v in pairs(self.curCountList) do
  144. for z = 1, v do
  145. table.insert(self.useItems, { itemId = i, count = self.itemCount[i] })
  146. end
  147. end
  148. end
  149. function this:OpenBtnClick()
  150. if table.count(self.useItems) == 0 then
  151. return
  152. end
  153. self:CloseBtnClick()
  154. --SL:ReqUseSelfChoiceBoxMessage(self.itemIndex, self.useItems)
  155. InfoManager.selfSelectBoxInfo.useItems = {}
  156. table.copyFrom(InfoManager.selfSelectBoxInfo.useItems, self.useItems)
  157. SL:SendLuaNetMsg(LuaMessageIdToSever.REQ_USE_SELF_CHOICE_BOX, { boxItemId = self.item.id, items = self.useItems })
  158. end
  159. function this:Close()
  160. end
  161. return this