---@class KLMailPanel:UIKmlLuaPanelBase ---@field view KLMailPanelView ---@field selectBtn KLMailItemItem[] local KLMailPanel = class(UIKmlLuaPanelBase) local this = KLMailPanel ---创建时调用一次 function this:Init() SL:HideMainPanel() self.selectBtn = {} end ---创建或者刷新界面数据时调用 function this:Refresh() self.info = self:LoadData() if table.count(self.info) == 0 then GUI:SetActive(self.view.null_bg, true) GUI:SetActive(self.view.left_panel, false) GUI:SetActive(self.view.mail_content_rt, false) else self.selectid = 0 self.mailId = 0 self:RefreshUI() self.all = false end end function this:RefreshUI() GUI:SetActive(self.view.null_bg, false) GUI:SetActive(self.view.left_panel, true) GUI:SetActive(self.view.mail_content_rt, true) GUI:DataListInitData(self.view.mail_data_list, function() return self:DataListMailCountFunc() end, function(realIndex) return self:DataListMailGetFunc(realIndex) end, function(realIndex, kmlcontrol) return self:DataListMailInitFunc(realIndex, kmlcontrol) end, function(realIndex, kmlcontrol) return self:DataListMailUpdateFunc(realIndex, kmlcontrol) end) GUI:DataListUpdateData(self.view.mail_data_list) self:SelectMail(1, self.info[1].mailId) end ---注册UI事件和服务器消息 function this:RegistEvents() SL:RegisterLuaNetMsg(MessageDef.ResGetMailItemMessage, self.ResGetMailItemMessage, self) SL:RegisterLUAEvent(LUA_EVENT_MAIL_STATE_CHANGE, self.CheckState, self) SL:RegisterLUAEvent(LUA_EVENT_RECEIVE_EMAIL, self.Refresh, self) GUI:AddOnClickEvent(self.view.get_all_btn, self, self.AllReceive) GUI:AddOnClickEvent(self.view.del_all_btn, self, self.AllDelMail) GUI:AddOnClickEvent(self.view.del_btn, self, self.DelMail) GUI:AddOnClickEvent(self.view.detail_btn, self, function() SL:GetMailItems(self.mailId) end) GUI:AddOnClickEvent(self.view.des_btn, self, function() local helpCfg = SL:GetConfig("cfg_rule_text", 15,"id") if helpCfg then SL:CommonStrTipsMessage({ title = helpCfg.menutxt, str = helpCfg.location }) end end) GUI:AddOnClickEvent(self.view.closeBtn, self, function() GUI:UIPanel_Close("dev/ui/Mail/Panel/KLMail/KLMailPanel") end) end ---@param message MailProto.MailIdMsg function this:ResGetMailItemMessage(_, message) local items = {} if table.count(message.mailIds) == 0 then return end local mails = SL.Mail:GetAllMail() for i, v in pairs(message.mailIds) do local info = mails[v] for _, k in pairs(info.items) do if items[k.cfgId] then items[k.cfgId] = items[k.cfgId] + k.count else items[k.cfgId] = k.count end end end local reward = {} for i, v in pairs(items) do reward[#reward + 1] = { cfgId = i, count = v } end SL:OpenRewardTips(reward, 0) GUI:SetActive(self.view.del_btn, true) GUI:SetActive(self.view.detail_btn, false) self:SelectMail(nil, message.mailIds[1]) if self.all then self:Refresh() self.all = false end end function this:DataListMailCountFunc() return table.count(SL.Mail:GetAllMail()) end function this:DataListMailGetFunc(realIndex) local item = GUI:UIPanel_Open("dev/ui/Mail/Item/KLMailItem/KLMailItemItem", self.view.mail_data_list, self, nil, true) self.selectBtn[realIndex + 1] = item return item.view.root end function this:DataListMailInitFunc(realIndex, kmlcontrol) end function this:DataListMailUpdateFunc(realIndex, kmlcontrol) local luaind = realIndex + 1 local data = self.info[luaind] local title = self.selectBtn[realIndex + 1].view.mail_title_ GUI:Text_setString(title, data.title) local icon = self.selectBtn[realIndex + 1].view.mail_icon_ local atlas = "Atlas/UIEmailPanel.spriteatlas" if data.state == 0 then if data.item then GUI:Image_loadTexture(icon, "icon_haveitem", atlas) else GUI:Image_loadTexture(icon, "icon_unread", atlas) end elseif data.state == 1 then GUI:Image_loadTexture(icon, "icon_unclaimed", atlas) else if data.item then GUI:Image_loadTexture(icon, "icon_received", atlas) else GUI:Image_loadTexture(icon, "icon_read", atlas) end end local timestr = Time.FormatTimeYMDHMS(data.time) local time = self.selectBtn[realIndex + 1].view .mail_time_ GUI:Text_setString(time, timestr) self.selectBtn[realIndex + 1].index = luaind self.selectBtn[realIndex + 1].callback = self.SelectMail self.selectBtn[realIndex + 1].ui = self self.selectBtn[realIndex + 1].mailId = data.mailId GUI:setVisible(self.selectBtn[realIndex + 1].view.redpoint,data.state == 0 or (data.item and data.state ~= 2)) end function this:LoadData() local data = {} for i, v in pairs(SL.Mail:GetAllMail()) do local time = v.overdueTime - Time.GetServerTime() / 1000 if time > 0 then local info = { mailId = v.mailId, title = v.title, time = v.sendTime, state = v.state, item = table.count(v.items) ~= 0,hasItems = v.state ~= 2 and v.items and #v.items > 0 } table.insert(data, info) end end table.sort(data, function(a, b) local timeCompare = a.time > b.time local wa = timeCompare and 1 or 0 local wb = timeCompare and 0 or 1 if a.state == 0 then wa = wa + 10 end if b.state == 0 then wb = wb + 10 end if a.hasItems then wa = wa + 100 end if b.hasItems then wb = wb + 100 end return wa > wb end) return data end function this:SelectMail(index, mailId) if index == nil then index = self.selectid else self.selectid = index end local mails = SL.Mail:GetAllMail() for i, v in pairs(self.selectBtn) do --v.SelectMail(v,) if i == index then v.Selected(v, true) local icon = v.view.mail_icon_ if table.count(mails[mailId].items) > 0 and mails[mailId].state ~= 2 then GUI:Image_loadTexture(icon, "icon_unclaimed", "Atlas/UIEmailPanel.spriteatlas") elseif table.count(mails[mailId].items) > 0 then GUI:Image_loadTexture(icon, "icon_received", "Atlas/UIEmailPanel.spriteatlas") else GUI:Image_loadTexture(icon, "icon_read", "Atlas/UIEmailPanel.spriteatlas") end GUI:setVisible(v.view.redpoint,mails[mailId].state == 0 or (#mails[mailId].items>0 and mails[mailId].state ~= 2)) else --local select = v.view.selected --GUI:SetActive(select, false) v.Selected(v, false) end end if mails[mailId].state == 0 then SL:RequestReadMail({ mailId }) end self:LoadPanel(mailId) end --- 页面加载 function this:LoadPanel(mailId) self.mailId = mailId local data = SL.Mail:GetAllMail()[mailId] --标题 GUI:Text_setString(self.view.content_title, data.title) -- 时间 local time = data.overdueTime - Time.GetServerTime() / 1000 local timeText = "" if time <= 0 then timeText = "已过期" else timeText = "有效期:" .. Time.FormatTimeDH(time) end GUI:Text_setString(self.view.content_time, timeText) -- 内容 GUI:Text_setString(self.view.content_text, data.content) -- 状态 if table.count(data.items) > 0 and data.state ~= 2 and time > 0 then GUI:SetActive(self.view.del_btn, false) GUI:SetActive(self.view.detail_btn, true) --GUI:AddOnClickEvent(self.view.detail_btn, self, function() -- SL:GetMailItems(data.mailId) --end) else GUI:SetActive(self.view.del_btn, true) GUI:SetActive(self.view.detail_btn, false) end -- 附件 if table.count(data.items) == 0 then GUI:SetActive(self.view.received_attachment, false) else GUI:SetActive(self.view.received_attachment, true) end GUI:DataListInitData(self.view.items_list, function() return table.count(data.items) end, function(realIndex) return self:DataListItemGetFunc(realIndex) end, function(realIndex, kmlcontrol) return self:DataListItemInitFunc(realIndex, kmlcontrol) end, function(realIndex, kmlcontrol) return self:DataListItemUpdateFunc(realIndex, kmlcontrol, data) end) GUI:DataListUpdateData(self.view.items_list) end function this:DataListItemGetFunc(realIndex) end function this:DataListItemInitFunc(realIndex, kmlcontrol) end function this:DataListItemUpdateFunc(realIndex, kmlcontrol, data) local itemTbl = data.items[realIndex + 1] --local item2 = self.view.items_list:GetChildControl(realIndex, "item2") local item2 = GUI:GetChildControl(self.view.items_list,realIndex, "item2") GUI:Item_UpdataData(item2, { itemid = itemTbl.cfgId, --itemcount = itemTbl.count, }) --local itemcount = self.view.items_list:GetChildControl(realIndex, "itemcount") local itemcount = GUI:GetChildControl(self.view.items_list,realIndex,"itemcount") local count = SL:GetSimpleNumber(itemTbl.count, 1) GUI:Text_setString(itemcount, tostring(count)) --local click_item = self.view.items_list:GetChildControl(realIndex, "click_item") local click_item = GUI:GetChildControl(self.view.items_list,realIndex,"click_item") --local item_received = self.view.items_list:GetChildControl(realIndex, "item_received") local item_received = GUI:GetChildControl(self.view.items_list,realIndex,"item_received") GUI:AddOnClickEvent(click_item, self, function() SL:OpenTips("mail", itemTbl.cfgId, itemTbl.id, itemTbl.count) end) if data.state ~= 2 then GUI:SetActive(item_received, false) else --GUI:AddOnClickEvent(click_item, self, function() -- SL:OpenTips("mail", itemTbl.cfgId, itemTbl.id,itemTbl.count) --end) GUI:SetActive(item_received, true) end end function this:DelMail() SL.Mail:ReqDeleteMail({ self.mailId }) SL.Mail:GetAllMail()[self.mailId] = nil self:Refresh() end function this:AllDelMail() local data = {} local mails = SL.Mail:GetAllMail() for i, v in pairs(mails) do if v.state == 2 then table.insert(data, i) end end SL.Mail:ReqDeleteMail(data) self.all = true --self:RefreshUI() end function this:AllReceive() SL:GetAllMailItems() self.all = true end function this:Close() SL:UnRegisterLuaNetMsg(MessageDef.ResGetMailItemMessage, self.ResGetMailItemMessage, self) SL:UnRegisterLUAEvent(LUA_EVENT_MAIL_STATE_CHANGE, self.CheckState, self) SL:UnRegisterLUAEvent(LUA_EVENT_RECEIVE_EMAIL, self.Refresh, self) SL:ShowMainPanel() end function this:CheckState() if self.all == false then return end local data = SL.Mail:GetAllMail() self:Refresh() end return this