KLShopMallPanel.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. ---@class KLShopMallPanel:UIKmlLuaPanelBase
  2. ---@field view KLShopMallPanelView
  3. local KLShopMallPanel = class(UIKmlLuaPanelBase)
  4. local this =KLShopMallPanel
  5. local subTabItemPath = "dev/ui/ShopMall/Item/KLShopMallSubTab/KLShopMallSubTabItem"
  6. ---创建时调用一次
  7. function this:Init()
  8. ---@type MallProtos.MallGoods[]
  9. self.goodsList = {}
  10. ---@type table<KingML.KmlControl,KLShopMallGoodsItem>
  11. self.goodsItem = {}
  12. ---@type table<number,MallProtos.MallGoods>
  13. self.goodsListMap = {}
  14. ---@type table<number,KLShopMallGoodsItem>
  15. self.goodsItemMap = {}
  16. self.homeToggleMap = {}
  17. if self.args then
  18. ---@type cfg_menu_layer_column
  19. local jumpTbl = nil
  20. if type(self.args) == "table" then
  21. jumpTbl = SL:GetConfig("cfg_menu_layer",tonumber(self.args[1]))
  22. else
  23. jumpTbl = SL:GetConfig("cfg_menu_layer",tonumber(self.args))
  24. end
  25. if jumpTbl then
  26. self.jumpHomeTabId = jumpTbl.parentID
  27. self.jumpSubTabId = jumpTbl.id
  28. end
  29. end
  30. local level = SL:GetMetaValue(EMetaVarGetKey.LEVEL)
  31. ---@type cfg_menu_layer_column[]
  32. local homeTabConfigs = {}
  33. ---@type cfg_menu_layer_column[]
  34. local tblList = SL:GetConfigTable("cfg_menu_layer")
  35. for _, v in pairs(tblList) do
  36. if v.parentID == 1000 and v.level <= level then
  37. table.insert(homeTabConfigs,v)
  38. end
  39. end
  40. for k,homeTabConfig in pairs(homeTabConfigs) do
  41. ---@type KLShopMallHomeTabItem
  42. local homeTab = GUI:UIPanel_Open("dev/ui/ShopMall/Item/KLShopMallHomeTab/KLShopMallHomeTabItem",self.view.TopHomeToggleGroup,self,homeTabConfig,true, function(homeTab)
  43. local jump = (self.jumpHomeTabId and self.jumpHomeTabId == homeTabConfig.id) or (not self.jumpHomeTabId and k == 1)
  44. if jump then
  45. GUI:Toggle_setIsOn(homeTab.view.KmlShopMallHomeTab,true)
  46. self:RefreshSubTab(homeTabConfig.id)
  47. end
  48. GUI:SetToggleOnValueChange(homeTab.view.KmlShopMallHomeTab,self,self.HomeTabOnToggle,homeTabConfig.id)
  49. self.homeToggleMap[homeTabConfig.id] = homeTab.view.KmlShopMallHomeTab
  50. end)
  51. end
  52. self.jumpHomeTabId = nil
  53. GUI:DataListInitData(self.view.ShopMallGoodsList, function() return self:ShopMallCountGetFun() end,
  54. function(realIndex) return self:ShopMallItemGetFun(realIndex) end,
  55. nil,
  56. function(realIndex,item) self:ShopMallItemUpdateFun(realIndex,item) end,
  57. nil)
  58. GUI:OSAScrollView_Initialized(self.view.osa_money_list,nil,self.ItemUpdateFun,self)
  59. self.hasInit = true;
  60. end
  61. ---@param item UIKmlLuaControl
  62. function this:ItemUpdateFun(item,index)
  63. local moneyId = self.curSelectSubTabTbl.money[index + 1]
  64. local moneyIcon = GUI:GetChildById(item,"img_money_icon")
  65. GUI:Image_loadTexture(moneyIcon,moneyId,"Atlas/TS_Common.spriteatlas")
  66. local btnTip = GUI:GetChildById(item,"btn_money_tip")
  67. GUI:AddOnClickEvent(btnTip,self,self.BtnAddOnClick,moneyId)
  68. local textMoneyCount = GUI:GetChildById(item,"text_money_count")
  69. GUI:Text_setString(textMoneyCount,tostring(SL:GetTotalCoinBaseId(moneyId)))
  70. end
  71. function this:ShopMallCountGetFun()
  72. return #self.goodsList
  73. end
  74. function this:ShopMallItemGetFun(realIndex)
  75. ---@type KLShopMallGoodsItem
  76. local item = GUI:UIPanel_Open("dev/ui/ShopMall/Item/KLShopMallGoods/KLShopMallGoodsItem",self.view.ShopMallGoodsList,self,nil,true,function(item)
  77. local needRefresh = self.goodsItem[item.view.root] == false --经历过刷新失败的情况
  78. self.goodsItem[item.view.root] = item
  79. if needRefresh then
  80. self:ShopMallItemUpdateFun(realIndex,item.view.root)
  81. end
  82. end )
  83. return item.view.root
  84. end
  85. function this:ShopMallItemUpdateFun(realIndex,item)
  86. if not self.goodsItem[item] then
  87. self.goodsItem[item] = false
  88. return
  89. end
  90. local data = self.goodsList[realIndex + 1]
  91. local goodsItem = self.goodsItem[item]
  92. goodsItem:RefreshItem(data)
  93. GUI:SetID(goodsItem.view.ButtonBg, "ButtonBg"..tostring(data.goodsId))
  94. self.goodsListMap[data.goodsId] = data
  95. self.goodsItemMap[data.goodsId] = goodsItem
  96. end
  97. function this:HomeTabOnToggle(_,customData,eventData)
  98. if eventData[1] then
  99. self:RefreshSubTab(customData)
  100. end
  101. end
  102. function this:RefreshSubTab(homeTabId)
  103. if self.subWins[subTabItemPath] then
  104. for _, win in pairs(self.subWins[subTabItemPath]) do
  105. GUI:UIPanel_Close(subTabItemPath,win)
  106. end
  107. table.clear(self.subWins[subTabItemPath])
  108. end
  109. local level = SL:GetMetaValue(EMetaVarGetKey.LEVEL)
  110. ---@type cfg_menu_layer_column[]
  111. local subTabConfigs = {}
  112. ---@type cfg_menu_layer_column[]
  113. local tblList = SL:GetConfigTable("cfg_menu_layer")
  114. for _, v in pairs(tblList) do
  115. if v.parentID == homeTabId and v.level <= level then
  116. table.insert(subTabConfigs,v)
  117. end
  118. end
  119. for k,subTabConfig in pairs(subTabConfigs) do
  120. ---@type KLShopMallSubTabItem
  121. GUI:UIPanel_Open(subTabItemPath,self.view.LeftSubToggleGroup,self,subTabConfig,true, function(subTab)
  122. if (self.jumpSubTabId and self.jumpSubTabId == subTabConfig.id) or (not self.jumpSubTabId and k == 1) then
  123. GUI:Toggle_setIsOn(subTab.view.ToggleLevel,true)
  124. self:SubTabOnToggle(_,subTabConfig,{true})
  125. self.jumpSubTabId = nil
  126. end
  127. GUI:SetToggleOnValueChange(subTab.view.ToggleLevel,self,self.SubTabOnToggle,subTabConfig)
  128. end)
  129. end
  130. end
  131. ---@param customData cfg_menu_layer_column
  132. function this:SubTabOnToggle(_,customData,eventData)
  133. if eventData[1] then
  134. SL:RequestShopMallGoodsInfo(customData.id)
  135. self.curSelectSubTabTbl = customData
  136. self:RefreshMoneyInfo()
  137. end
  138. end
  139. ---创建或者刷新界面数据时调用
  140. function this:Refresh()
  141. SL.HideMainPanel()
  142. self:RefreshMoneyInfo()
  143. end
  144. function this:RefreshMoneyInfo()
  145. if self.curSelectSubTabTbl then
  146. GUI:OSAScrollView_RefreshList(self.view.osa_money_list,#self.curSelectSubTabTbl.money)
  147. end
  148. --[[ GUI:Text_setString(self.view.GoldCount,tostring(SL:GetBagItemCount(10010001)))
  149. GUI:Text_setString(self.view.QjCount,tostring(SL:GetBagItemCount(10020001)))
  150. GUI:Text_setString(self.view.QjBindCount,tostring(SL:GetBagItemCount(10030001)))
  151. GUI:Text_setString(self.view.DiamondCount,tostring(SL:GetBagItemCount(10040001)))]]
  152. end
  153. ---注册UI事件和服务器消息
  154. function this:RegistEvents()
  155. GUI:AddOnClickEvent(self.view.CloseBtn,self,self.CloseBtnOnClick)
  156. SL:RegisterLuaNetMsg(MessageDef.ResMallGoodsInfoMessage,self.ResMallGoodsInfoMessage,self)
  157. SL:RegisterLuaNetMsg(MessageDef.ResBuyMallGoodsMessage,self.ResBuyMallGoodsMessage,self)
  158. SL:RegisterLUAEvent(LUA_EVENT_MONEYCHANGE,self.RefreshMoneyInfo,self)
  159. --[[ GUI:AddOnClickEvent(self.view.BtnGoldGetWayTip,self,self.BtnAddOnClick,10010001)
  160. GUI:AddOnClickEvent(self.view.BtnQjGetWayTip,self,self.BtnAddOnClick,10020001)
  161. GUI:AddOnClickEvent(self.view.BtnQjBindGetWayTip,self,self.BtnAddOnClick,10030001)
  162. GUI:AddOnClickEvent(self.view.BtnDiamondGetWayTip,self,self.BtnAddOnClick,10040001)]]
  163. end
  164. function this:BtnAddOnClick(_,cfgId)
  165. SL:CommonItemGetPath(nil,cfgId,nil,nil,self)
  166. end
  167. function this:CoinOnClick(_,cfgId)
  168. SL:OpenTips(nil,cfgId)
  169. end
  170. ---@param message MallProtos.MallGoodsInfoRes
  171. function this:ResMallGoodsInfoMessage(_,message)
  172. self.goodsList = message.goodsList
  173. GUI:DataListUpdateData(self.view.ShopMallGoodsList)
  174. if self.guideId then
  175. KLGuideManager.HidePointControlGuide(self.guideId)
  176. end
  177. end
  178. ---@param message MallProtos.BuyMallGoodsRes
  179. function this:ResBuyMallGoodsMessage(_,message)
  180. self:Refresh()
  181. local goodsItem = self.goodsItemMap[message.goodsId]
  182. local goodsData = self.goodsListMap[message.goodsId]
  183. goodsData.limitCount = goodsData.limitCount - message.count
  184. goodsData.limitCount = goodsData.limitCount < 0 and 0 or goodsData.limitCount
  185. goodsItem:RefreshItem(goodsData)
  186. ---@type cfg_shopMall_column
  187. local shopTbl = SL:GetConfig("cfg_shopMall",message.goodsId)
  188. SL:OpenRewardTips({[goodsItem.config.itemId] = (message.count * shopTbl.count)},0)
  189. end
  190. function this:CloseBtnOnClick()
  191. GUI:UIPanel_Close("dev/ui/ShopMall/Panel/KLShopMall/KLShopMallPanel")
  192. GUI:UIPanel_Close("dev/outui/ShopMain/Panel/KLShopMain/KLShopMainPanel")
  193. SL.ShowMainPanel()
  194. end
  195. function this:Close()
  196. SL:UnRegisterLuaNetMsg(MessageDef.ResMallGoodsInfoMessage,self.ResMallGoodsInfoMessage,self)
  197. end
  198. function this:GotoItemSubPage(itemId, guideId)
  199. local shopMallItem
  200. ---@type cfg_shopMall_column[]
  201. local tblList = SL:GetConfigTable("cfg_shopMall")
  202. for _, v in pairs(tblList) do
  203. if v.itemId == itemId then
  204. shopMallItem = v
  205. break
  206. end
  207. end
  208. if not shopMallItem then
  209. return
  210. end
  211. local subTabId = shopMallItem.subTab[1]
  212. self.jumpSubTabId = subTabId
  213. ---@type cfg_menu_layer_column
  214. local menuLayer = SL:GetConfig("cfg_menu_layer",subTabId)
  215. local homeTabId = menuLayer.parentID
  216. if self.homeToggleMap[homeTabId] then
  217. GUI:Toggle_setIsOn(self.homeToggleMap[homeTabId],true)
  218. --self:RefreshSubTab(homeTabId)
  219. end
  220. if guideId then
  221. self.guideId = guideId
  222. Coroutine.Start(function()
  223. Coroutine.WaitForEndOfFrame()
  224. KLGuideManager.AddPointControlGuide("KLShopMallPanel", "ButtonBg"..tostring(shopMallItem.id), guideId)
  225. end)
  226. end
  227. end
  228. return this