local UIFriendMail = UIViewClass() --常量 local AtlasCommon = "UI_Common" local MailBGOfficial = "yj_icon_gf" local MailBGUnion = "yj_icon_sl" local MailBGCountry = "yj_icon_gj" local MailBGRead = "Mail_Bg_Read" local MailBGUnRead = "Mail_Bg_UnRead" local MailIconRead = "Mail_Icon_Read" local MailIconUnRead = "Mail_Icon_UnRead" local ConstContentHeightWithReward = 400 local ConstContentHeightWithoutReward = 574 local COUNT_IN_RANGE = 6 local OFFSET_IN_END = 4 UIFriendMail.mailCellList = {} UIFriendMail.rewardCellList = {} -- **************** 通用部分 ******************* -- function UIFriendMail:OnCreated() self.tempContext = {} self.tempContext.scroll = self.scroll_Reward end function UIFriendMail:OnOpened(context) self:RegistSystemEvent(EventID.UI_Mail_SetCellData, self.SetCellData) self:RegistSystemEvent(EventID.UI_Mail_RefreshMail, self.RefreshMail) self.index = context.MailIndex self:RefreshMail(true) end function UIFriendMail:OnClosed() CTL.Mail.OnMailPanelClosed() self.rewardCellList = {} self.mailCellList = {} end -- **************** 点击事件 ******************* -- function UIFriendMail:OnClickReceive() CTL.Mail.ReceiveMail() end function UIFriendMail:OnClickReceiveAll() CTL.Mail.ReceiveAllMail() end function UIFriendMail:OnClickDelete() self.isDeleteMail = true CTL.Mail.DeleteMail() end function UIFriendMail:OnClickDeleteAll() self.isDeleteMail = true CTL.Mail.DeleteAllMail() end function UIFriendMail:OnClickClose() self:CloseView() end -- **************** 界面刷新 ******************* -- function UIFriendMail:RefreshMail(refill) self:RefreshMailList(refill) self:RefreshMailDetail() self.isDeleteMail = false end function UIFriendMail:RefreshMailList(refill) local mailCount = MDL.Mail.GetMailClientCount() UIUtils.SetLabel(self.labLocal_MailNum, mailCount .. "/" .. MDL.Mail.GetTotalCount()) UIUtils.SetActive(self.group_Empty_NoMail, mailCount == 0) if refill then for i = 1, mailCount do self.loopScroll_Panel:AddData(i, 0) end if mailCount > 0 then self.loopScroll_Panel:SelectIndex(self.index or 0, true) end else local arrayData = {} for i = 1, mailCount do table.insert(arrayData, i) end self.loopScroll_Panel:ReloadAll(arrayData) if self.isDeleteMail then --删除之后,选中上一个相同的位置 local lastSelectCount = self.context.MailIndex + 1 if mailCount >= lastSelectCount then self.loopScroll_Panel:SelectIndex(self.context.MailIndex) else self.loopScroll_Panel:SelectIndex(mailCount - 1) end if mailCount < COUNT_IN_RANGE then --邮件数量小于范围内cell数目时,置顶 self.loopScroll_Panel:MoveBegin() else --选中在上中部分,会一直在视野内,可以不处理 --选中在下部分,由于选中项一直向上移动,位置需要同步向下移 --通过距离终点的offset个定义为下部分 if mailCount - lastSelectCount < OFFSET_IN_END then --由于底部可能会留出部分空间,需要修改下content坐标 local oriX, oriY = self.loopScroll_Panel.content:GetAnchoredPosition() self.loopScroll_Panel.content:SetAnchoredPosition(oriX, oriY - 100) self.loopScroll_Panel:MoveEnd() end end else local index = math.min(self.context.MailIndex, self.loopScroll_Panel.Count - 1) if index >= 0 then self.loopScroll_Panel:SelectIndex(index, true) end end end end function UIFriendMail:SaveSelectedMailIndex(index) self.context.MailIndex = index end function UIFriendMail:OnCellSelect(scrollRect, index, cellTable, data) if scrollRect == self.loopScroll_Panel then local _currentIndex = data + MDL.Mail.GetMailOffset() CTL.Mail.ShowMail(_currentIndex) local mail = MDL.Mail.GetMail(_currentIndex) self.curShowMailId = mail.UID self:RefreshMailDetail() self:SaveSelectedMailIndex(index) end end function UIFriendMail:OnCellInit(scrollRect, index, cellTable, data) if scrollRect == self.loopScroll_Panel then self:SetCellData(data, cellTable, index) elseif scrollRect == self.scroll_Reward then UTL.Item.SetItemCell(cellTable.item, data.item.itemId, data.item.itemCount) if data.received then UIUtils.SetActive(cellTable.item.obj_Stamp, true) else UIUtils.SetActive(cellTable.item.obj_Stamp, false) end end end function UIFriendMail:OnCellClicked(scrollRect, index, cellTable, target, data) if scrollRect == self.scroll_Reward then UTL.ItemTips.ShowCommonTipsById(data.item.itemId) end end function UIFriendMail:RefreshMailDetail() if MDL.Mail.GetMailClientCount() == 0 then UIUtils.SetActive(self.obj_NoMail.gameObject, true) UIUtils.SetActive(self.obj_WithMail.gameObject, false) else UIUtils.SetActive(self.obj_NoMail.gameObject, false) UIUtils.SetActive(self.obj_WithMail.gameObject, true) local index = CTL.Mail.GetCurrentIndex() if index == 0 then index = 1 end local mail = MDL.Mail.GetMail(index) MDL.Mail.SelectMailTime = mail.SendTime UIUtils.SetLabel(self.labLocal_Title, mail.Title) UIUtils.SetLabel(self.labLocal_Content, mail.Content) local leftTime = UTL.Time.GetLeftTime_SimpleFormat(mail.ExpireTime, true) if not leftTime then UIUtils.SetActive(self.labLocal_LeftTime.gameObject, false) else UIUtils.SetActive(self.labLocal_LeftTime.gameObject, true) local text = UTL.Time.CheckLeftTimeLessOneMinute(mail.ExpireTime) and UIUtils.GetLocalizedTxt(Const_IDS.IDS_Mail_InsufficientTime) or leftTime UIUtils.SetLabel(self.labLocal_LeftTime, text) end if CTL.Mail.CheckHasReward(mail) then --有奖励的情况 UIUtils.ChangeHeight(self.scroll_Content.gameObject, ConstContentHeightWithReward) UIUtils.SetActive(self.obj_Reward.gameObject, true) UIUtils.SetActive(self.event_Receive.gameObject, not mail.IsReceived) self:RefreshMailReward(mail.ItemList, mail.IsReceived) else --没有奖励的情况 UIUtils.ChangeHeight(self.scroll_Content.gameObject, ConstContentHeightWithoutReward) UIUtils.SetActive(self.obj_Reward.gameObject, false) end self.labLocal_Content.transform:SetAnchoredPosition(0, 0) end end function UIFriendMail:RefreshMailReward(itemList, received) self.scroll_Reward:ClearCells() for i, v in ipairs(itemList) do local tempContext = {} tempContext.item = v tempContext.received = received self.scroll_Reward:AddData(tempContext, 0) end if #self.rewardCellList > #itemList then for i = #itemList + 1, #self.rewardCellList do self.rewardCellList[i]:OnClosed() end end --UIUtils.ResetScrollRect(self.scroll_Reward) end -- **************** 邮件单元 ******************* -- function UIFriendMail:SetCellData(index, cellTable, cellIndex) index = MDL.Mail.GetMailOffset() + index local mail = MDL.Mail.GetMail(index) if not mail then logging.error(LTAG.ERROR, "No Mail With Index:", index) return end local mailCell = cellTable UIUtils.SetLabel(mailCell.img_UnSelect.labLocal_Title, mail.Title) UIUtils.SetLabel(mailCell.img_Select.labLocal_Title, mail.Title) local leftTime = UTL.Time.GetLeftTime_SimpleFormat(mail.ExpireTime, false) if not leftTime then UIUtils.SetActive(mailCell.img_UnSelect.labLocal_LeftTime.gameObject, false) UIUtils.SetActive(mailCell.img_Select.labLocal_LeftTime.gameObject, false) else UIUtils.SetActive(mailCell.img_UnSelect.labLocal_LeftTime.gameObject, true) UIUtils.SetActive(mailCell.img_Select.labLocal_LeftTime.gameObject, true) local text = UTL.Time.CheckLeftTimeLessOneMinute(mail.ExpireTime) and UIUtils.GetLocalizedTxt(Const_IDS.IDS_Mail_InsufficientTime) or leftTime UIUtils.SetLabel(mailCell.img_UnSelect.labLocal_LeftTime, text) UIUtils.SetLabel(mailCell.img_Select.labLocal_LeftTime, text) end if mail.IsOpened then mailCell.obj_RedPoint.gameObject:SetActive(false) mailCell.read_State:SetState(0) else mailCell.obj_RedPoint.gameObject:SetActive(true) mailCell.read_State:SetState(1) end if mail.Type == MDL.Mail.Enum_MailType.Official then UTL.Item.SetAtlasSprite(mailCell.sprite_MailType, AtlasCommon, MailBGOfficial) elseif mail.Type == MDL.Mail.Enum_MailType.Union then UTL.Item.SetAtlasSprite(mailCell.sprite_MailType, AtlasCommon, MailBGUnion) elseif mail.Type == MDL.Mail.Enum_MailType.Country then UTL.Item.SetAtlasSprite(mailCell.sprite_MailType, AtlasCommon, MailBGCountry) end if mail.ItemList and #mail.ItemList > 0 then if mail.IsReceived then mailCell.received_State:SetState(0) else mailCell.received_State:SetState(1) end else mailCell.received_State:SetState(2) end if self.curShowMailId == mail.UID then self.loopScroll_Panel:SelectIndex(cellIndex) end end function UIFriendMail:OnButtonClicked(obj) if (obj == self.event_Receive) then self:OnClickReceive() elseif (obj == self.event_Delete) then self:OnClickDelete() elseif (obj == self.event_ReceiveAll) then self:OnClickReceiveAll() elseif (obj == self.event_DeleteAll) then self:OnClickDeleteAll() elseif obj == self.btn_Close then self:OnClickClose() end end return UIFriendMail