KLAppearPanel.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. ---@class KLAppearPanel:UIKmlLuaPanelBase
  2. ---@field view KLAppearPanelView
  3. local KLAppearPanel = class(UIKmlLuaPanelBase)
  4. local this = KLAppearPanel
  5. ---@type number[]
  6. local partData = {}
  7. ---@type table<E_AppearEquipType,CommonProtos.Item>
  8. local slot2Equips = {}
  9. ---@type table<EEquipSlotType,number> <EEquipSlotType,cfgId>
  10. local appearCfgIds = {}
  11. ---@type table<EEquipSlotType,number> <EEquipSlotType,cfgId>
  12. local serverCfgIds = {}
  13. ---创建时调用一次
  14. function this:Init()
  15. GUI:DataListInitData(self.view.loopscrollviewData,
  16. function()
  17. return self:ItemCountFunc()
  18. end,
  19. function(realIndex)
  20. return self:ItemGetFunc(realIndex)
  21. end,
  22. function(realIndex, kmlCtrl)
  23. return self:ItemInitFunc(realIndex, kmlCtrl)
  24. end,
  25. function(realIndex, kmlCtrl)
  26. return self:ItemUpdateFunc(realIndex, kmlCtrl)
  27. end,
  28. function(realIndex)
  29. return self:ItemSizeGetFunc(realIndex)
  30. end
  31. )
  32. self.Part_ITEM_H = 64
  33. self.Page_Width = GUI:GetWidth(self.view.loopscrollview)
  34. self.PAGE_EXT_H = 40
  35. ---@type table<KingML.KmlControl,KLAppearPartItem>
  36. self.partItems = {}
  37. end
  38. function this:ItemCountFunc()
  39. return #partData
  40. end
  41. function this:ItemGetFunc()
  42. ---@type KLAppearPartItem
  43. local item = GUI:UIPanel_Open("dev/outui/AppearGroup/Item/KLAppearPart/KLAppearPartItem", self.view.loopscrollviewData, self, nil, true)
  44. local kmlCtrl = item.view.root
  45. self.partItems[kmlCtrl] = item
  46. return kmlCtrl
  47. end
  48. function this:ItemInitFunc()
  49. end
  50. ---@param realIndex number
  51. ---@param kmlCtrl KingML.KmlControl
  52. function this:ItemUpdateFunc(realIndex, kmlCtrl)
  53. local item = self.partItems[kmlCtrl]
  54. local luaIndex = realIndex + 1
  55. local slot = partData[luaIndex]
  56. item:UpdateUI(appearCfgIds, slot2Equips[slot], slot, self.itemY, luaIndex)
  57. end
  58. ---@param realIndex number
  59. function this:ItemSizeGetFunc(realIndex)
  60. local luaIndex = realIndex + 1
  61. local slot = partData[luaIndex]
  62. local equipCount = #slot2Equips[slot]
  63. local line = math.ceil(equipCount / 5)
  64. self.itemY = line * self.Part_ITEM_H + (line - 1) * 5 + self.PAGE_EXT_H
  65. return Vector2(self.Page_Width, self.itemY)
  66. end
  67. ---创建或者刷新界面数据时调用
  68. function this:Refresh()
  69. SL:SendLuaNetMsg(LuaMessageIdToSever.GET_EQUIP_APPEAR)
  70. end
  71. ---注册UI事件和服务器消息
  72. function this:RegistEvents()
  73. GUI:AddOnClickEvent(self.view.BtnClose, self, self.BtnCloseOnClick)
  74. GUI:AddOnClickEvent(self.view.BtnCancel, self, self.BtnCancelOnClick)
  75. GUI:AddOnClickEvent(self.view.BtnSure, self, self.BtnSureOnClick)
  76. SL:RegisterLuaNetMsg(LuaMessageIdToClient.GET_EQUIP_APPEAR, self.GET_EQUIP_APPEAR, self)
  77. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_GUARD_APPEAR, self.RES_GUARD_APPEAR, self)
  78. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_SETTING_EQUIP_APPEAR, self.RES_SETTING_EQUIP_APPEAR, self)
  79. end
  80. --保存设置成功
  81. ---@param message {slot:EEquipSlotType,cfgId:number}[]
  82. function this:RES_SETTING_EQUIP_APPEAR(_, message)
  83. table.clear(appearCfgIds)
  84. table.clear(serverCfgIds)
  85. for slot, cfgId in pairs(message) do
  86. slot = tonumber(slot)
  87. appearCfgIds[slot] = cfgId
  88. serverCfgIds[slot] = cfgId
  89. end
  90. end
  91. ---@param message {itemId:number,getTime:number,time:number}[]
  92. function this:RES_GUARD_APPEAR(_, message)
  93. self:AnalysisEquipData()
  94. if table.count(message) > 0 then
  95. if not slot2Equips[E_AppearEquipType.Guard] then
  96. slot2Equips[E_AppearEquipType.Guard] = {}
  97. table.insert(partData, E_AppearEquipType.Guard)
  98. end
  99. for _, v in pairs(message) do
  100. table.insert(slot2Equips[E_AppearEquipType.Guard], v)
  101. end
  102. end
  103. GUI:DataListUpdateData(self.view.loopscrollviewData)
  104. SL:onLUAEvent(APPEAR_PREVIEW_SELECT_CHANGE, appearCfgIds)
  105. end
  106. function this:AnalysisEquipData()
  107. table.clear(partData)
  108. table.clear(slot2Equips)
  109. for tp, strParts in pairs(E_AppearType2StrPart) do
  110. ---@type CommonProtos.Item[]
  111. local equipList = {}
  112. for _, slot in pairs(strParts) do
  113. ---@type CommonProtos.Item[]
  114. local equips = SL:GetMetaValue("EQUIP_DATA_LIST", slot)
  115. table.concatTable(equipList, equips)
  116. end
  117. if #equipList > 0 then
  118. slot2Equips[tp] = equipList
  119. table.insert(partData, tp)
  120. end
  121. end
  122. end
  123. --返回外观
  124. ---@param message {slot:number,cfgId:number}[]
  125. function this:GET_EQUIP_APPEAR(_, message)
  126. if not message then
  127. return
  128. end
  129. table.clear(appearCfgIds)
  130. table.clear(serverCfgIds)
  131. for _, kp in pairs(message) do
  132. local slot = tonumber(kp.slot)
  133. appearCfgIds[slot] = kp.cfgId
  134. serverCfgIds[slot] = kp.cfgId
  135. end
  136. SL:SendLuaNetMsg(LuaMessageIdToSever.GET_GUARD_APPEAR)
  137. end
  138. function this:BtnSureOnClick()
  139. SL:CommonTipsMessage({
  140. stringTblID = 209, -- = "保存设置",
  141. title = "提示",
  142. callback = function()
  143. ---@type {slot:number,cfgId:number}[]
  144. local dt = {}
  145. for slot, cfgId in pairs(appearCfgIds) do
  146. table.insert(dt, { slot = slot, cfgId = cfgId })
  147. end
  148. SL:SendLuaNetMsg(LuaMessageIdToSever.SETTING_EQUIP_APPEAR, { equipindex = dt })
  149. self:BtnCloseOnClick()
  150. end
  151. })
  152. end
  153. function this:BtnCancelOnClick()
  154. return self:BtnCloseOnClick()
  155. end
  156. function this:BtnCloseOnClick()
  157. GUI:UIPanel_Close("dev/outui/AppearGroup/Panel/KLAppear/KLAppearPanel")
  158. GUI:UIPanel_Close("dev/outui/AppearGroup/Panel/KLAppearGroup/KLAppearGroupPanel")
  159. GUI:UIPanel_Close("dev/outui/AppearGroup/Panel/KLAppearPreview/KLAppearPreviewPanel")
  160. end
  161. function this:Close()
  162. table.clear(appearCfgIds)
  163. SL:onLUAEvent(APPEAR_PREVIEW_SELECT_CHANGE, serverCfgIds)
  164. end
  165. function this:OnEquipItemSelect(oldId, cfgId, slot)
  166. if oldId then
  167. for _, part in pairs(E_AppearType2StrPart[slot]) do
  168. if appearCfgIds[part] and appearCfgIds[part] == oldId then
  169. appearCfgIds[part] = nil
  170. break
  171. end
  172. end
  173. end
  174. if oldId ~= cfgId then
  175. for _, part in pairs(E_AppearType2StrPart[slot]) do
  176. ---@type cfg_item_column
  177. local itemTbl = SL:GetConfig("cfg_item", cfgId)
  178. for _, v in pairs(itemTbl.strPart) do
  179. if v == part then
  180. appearCfgIds[part] = cfgId
  181. break
  182. end
  183. end
  184. end
  185. end
  186. SL:onLUAEvent(APPEAR_PREVIEW_SELECT_CHANGE, appearCfgIds)
  187. end
  188. return this