local EventSystem = require('Event/EventSystem') local UIBattleChatDlgWatch = UIViewClass() -- context = { -- onlyChat = false, -- 是否仅仅显示聊天(隐藏好友、语音等功能) -- } function UIBattleChatDlgWatch:OnOpened(context) self.QuickMsgCellTable = nil self.QuickMsgCellTableIndex = -1 --注册Update self.LastMsgChannelId = nil self:RegistSystemEvent(EventID.UI_ChannelChat_OnChatViewClose,self.OnChatViewClose) self:RegistSystemEvent(EventID.UI_ChannelChat_OnReceiveNewMsg,self.OnReceiveNewMsg) self:RegistSystemEvent(EventID.UI_UpdateBlack, self.RefreshLoopScroll) self:RegistSystemEvent(EventID.UI_UpdateChatRed, self.OnRefreshFriendRed) self:RegistSystemEvent(EventID.FunctionUnlockUpdate, self.UpdateInfoReminderEntry) self:RegistSystemEvent(EventID.UI_Open_Chat_By_External, self.OnClickOpenChatByExternal) self:RegistSystemEvent(EventID.UIBattle_OnDeadChatDlgViewTop, self.OnDeadChatViewTop) self:RegistSystemEvent(EventID.UIBattle_OnFriendBaseClose, self.OnFriendBaseClose) self.isOpen = true self.TextPreferredHeight = 0 UIUtils.SetActive(self.Condition_Msg, false) MDL.ChannelChat.SetGlobalChatDlgIsOpen(true) self:RefreshLoopScroll() UIUtils.SetActive(self.btn_QuickMsg, MDL.ChannelChat.IsDisplayCurrentChatChannel()) local isFriendUnlock = MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.Friend) if isFriendUnlock then UIUtils.SetActive(self.btn_Friend, not self:OnlyShowChat()) else --有isFriendUnlock 为nil的情况 UIUtils.SetActive(self.btn_Friend, false) end self:OnRefreshFriendRed() -- 战斗内 聊天界面的快捷短语按钮隐藏 UIUtils.SetActive(self.btn_QuickMsg, false) if not self:OnlyShowChat() then self.infoReminderCanvas:OpenView(GetViewClass(Const_UI_Lua.UIEnum.UIInfoReminderTipsView)) end self:UpdateInfoReminderEntry() self:InitCurVoiceChannel() end function UIBattleChatDlgWatch:OnClickOpenChatByExternal() if UIManager.IsFocus(Const_UI_Lua.UIEnum.UIBattleChatDlgWatch) or CTL.ChannelChat.SelfIsDead() then if not UIManager.IsViewOpened(Const_UI_Lua.UIEnum.UIChannelChatMain) then self:OnClickOpenChat() end end end function UIBattleChatDlgWatch:OnClosed() CTL.ChannelChat.CancelRecordVoice() end function UIBattleChatDlgWatch:OnRefreshFriendRed() local num = MDL.Friend.GetUnreadMsgNumByType(MDL.Friend.ListType.Recent) + CTL.SpecialTeam.GetSumGroupChatRedCount() + CTL.SpecialTeam.GetNoticeGroupChatRed() if num > 0 then UIUtils.SetActive(self.img_NumBg, true) self.txt_MessageNum.text = num else UIUtils.SetActive(self.img_NumBg, false) end end function UIBattleChatDlgWatch:UpdateInfoReminderEntry() UIUtils.SetActive(self.btn_Msg, MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.InfoReminder)) end function UIBattleChatDlgWatch:OnClickOpenChat() self.gameObject:SetActive(false) self.isOpen = false UIUtils.SetActive(self.Condition_Msg, false) EventSystem.Dispatch(EventID.UI_Auto_Fight, false) -- 通知显隐自动战斗提示 UIManager.OpenView(Const_UI_Lua.UIEnum.UIChannelChatMain, {LastMsgChannelId = nil}) end function UIBattleChatDlgWatch:RefreshLoopScroll() self.obj_ScrollPanel:ClearCells() for i,v in ipairs(MDL.ChannelChat.GetDlgMsgList()) do if not MDL.ChannelChat.IsMsgFromBlack(v) then self.obj_ScrollPanel:AddData(v,0) end end if self.lab_Empty then local inRegion = GameAppInfo.IsJapanesePackage() or GameAppInfo.IsOverseaPackage() if inRegion and self.obj_ScrollPanel.Count <= 0 then UIUtils.SetActive(self.lab_Empty, true) UIUtils.SetLabelText(self.lab_Empty, Const_IDS["IDS_Chat_Blank"]) else UIUtils.SetActive(self.lab_Empty, false) end end UnityEngine.Canvas.ForceUpdateCanvases() -- self.obj_ScrollPanel:MoveIndex(self.obj_ScrollPanel.Count-1) self.obj_ScrollPanel:MoveEnd() UnityEngine.Canvas.ForceUpdateCanvases() self.obj_ScrollPanel:RefreshScrollHeight(self.TextPreferredHeight) end function UIBattleChatDlgWatch:OnChatViewClose() self.gameObject:SetActive(true) self.isOpen = true self.obj_ScrollPanel:ClearCells() self:RefreshLoopScroll() EventSystem.Dispatch(EventID.UI_Auto_Fight, true) -- 通知显隐自动战斗提示 end function UIBattleChatDlgWatch:OnReceiveNewMsg(msg) if MDL.ChannelChat.IsCurrentChannelByChannelId(msg.ChannelId) then if not MDL.ChannelChat.IsDisplayCurrentChatChannel() then return end end --黑名单屏蔽 if MDL.ChannelChat.IsMsgFromBlack(msg) then return end if self.isOpen then UIUtils.SetActive(self.lab_Empty, false) local scrollCellCount = self.obj_ScrollPanel.Count if scrollCellCount <= 0 then self.obj_ScrollPanel:AddData(msg, 0) else self.obj_ScrollPanel:AddDynamicData(msg, 0) end self.obj_ScrollPanel:MoveEnd() self.obj_ScrollPanel:RefreshScrollHeight(self.TextPreferredHeight) end end function UIBattleChatDlgWatch:OnCellInit(scrollRect, index, cellTable, data) if scrollRect == self.QuickMsgScroll then cellTable.objQuickMsg_InputField.characterLimit = DataSheetManager.ConstData:GetDataByID("ChatConvenientLimit").IntValue local tmpValue = MDL.ChannelChat.GetQuickMsgByIndex(index + 1) cellTable.txt_01.text = tmpValue if cellTable.txt_01.preferredWidth >= 500 then local length = string.len(tmpValue) while (length > 0) do length = length - 1 local subValue = string.sub(tmpValue, 1, length) cellTable.txt_01.text = subValue if cellTable.txt_01.preferredWidth <= 490 then cellTable.txt_01.text = cellTable.txt_01.text .. "..." return end end end elseif scrollRect == self.loop_Channel then UIUtils.SetLabelText(cellTable.txt_Channel,Const_IDS[data.AudioChatName]) else local content = "" if MDL.ChannelChat.IsZhaoMuChannelByCfgId(data.ChatCfgId) or MDL.ChannelChat.IsSystemChannelByCfgId(data.ChatCfgId) then content = data.content else if not string.isNilOrEmpty(data.senderName) then if data.isVoice then content = data.senderName .. "[j]" .. data.content else content = data.senderName .. ":" .. data.content end else content = data.content end end -- 设置频道(需要优先设置,以便首行空格) -- chatCfgId 从1 开始 state从0开始所以减1 CTL.ChannelChat.SetChannelBgColor(data.ChatCfgId, cellTable.groupBG, cellTable.txt_Channel, data.serId, not string.isNilOrEmpty(data.senderName)) -- 设置频道文字空格(需要在设置text之前设置) cellTable.msg_Content.text = CTL.ChannelChat.GetChannelBlankSpace(data.ChatCfgId, data.serId, not string.isNilOrEmpty(data.senderName)) .. content self.LastMsgChannelId = data.ChannelId self.TextPreferredHeight = cellTable.msg_Content.preferredHeight -- 设置频道位置 if UIUtils.GetFirstLineHaveEmojiByEmojiText(cellTable.msg_Content) then UIUtils.SetRectPosition(cellTable.groupBG, 0, -37) else UIUtils.SetRectPosition(cellTable.groupBG, 0, 0) end end end --以下为按钮点击事件 function UIBattleChatDlgWatch:OnButtonClicked(obj) if(obj==self.event_OpenChat) then self:OnClickOpenChat() elseif obj == self.btn_QuickMsg then self:LoadQuickMsg() elseif obj == self.btn_Friend then CTL.Friend.OpenFriendPanel(CTL.Friend.MainIndex.Main) self:OnClickOpenFriend() elseif obj == self.mask_Talk then self:CloseChannelSetting() end end function UIBattleChatDlgWatch:LoadQuickMsg() --频道是否开启,现在的版本快捷频道只是发送到当前频道,后面有其他功能根据相应功能选择频道是否开启 local isOpen, unlockMsg = MDL.ChannelChat.IsChannelOpen(MDL.ChannelChat.ChannelCfgId.Current) if not isOpen then CTL.Tips.AddTipsMsg(unlockMsg) return end if self.Condition_Msg.gameObject.activeSelf then UIUtils.SetActive(self.Condition_Msg, false) return else UIUtils.SetActive(self.Condition_Msg, true) end local quickDatas = MDL.ChannelChat.GetQuickMsgData() self.QuickMsgScroll:ReloadAll(quickDatas) end function UIBattleChatDlgWatch:OnCellClicked(scrollRect, index, cellTable, target, data) if scrollRect == self.QuickMsgScroll then if self.QuickMsgCellTable ~= nil and self.QuickMsgCellTable ~= cellTable then self:OnDeselect(nil, nil) end if target.transform.name == "event_01" then UIUtils.SetActive(self.Condition_Msg, false) self:SendMsg(MDL.ChannelChat.GetQuickMsgByIndex(index + 1)) elseif target.transform.name == "btn_Change01" then if self.QuickMsgCellTable == cellTable then self:OnDeselect(nil, nil) return end self.QuickMsgCellTableIndex = index self.QuickMsgCellTable = cellTable UIUtils.SetActive(self.QuickMsgCellTable.event_Mask, true) UIUtils.SetActive(cellTable.objQuickMsg_InputField, true) cellTable.objQuickMsg_InputField.text = MDL.ChannelChat.GetQuickMsgByIndex(index + 1) cellTable.objQuickMsg_InputField:ActivateInputField() self:RegistTriggerEvent(self.QuickMsgCellTable.objQuickMsg_InputField, UIEventTriggerType.Deselect) UIUtils.SetActive(cellTable.event_01, false) elseif target.transform.name == "event_Mask" then self:OnDeselect(nil, nil) end elseif scrollRect == self.loop_Channel then self:SetCurVoiceChannel(data.ID) self:CloseChannelSetting() else self:OnClickOpenChat() end end function UIBattleChatDlgWatch:OnDeselect(comp, eventData) if self.QuickMsgCellTable ~= nil then local newContent = self.QuickMsgCellTable.objQuickMsg_InputField.text self.QuickMsgCellTable.txt_01.text = newContent UIUtils.SetActive(self.QuickMsgCellTable.objQuickMsg_InputField, false) self:UnregistTriggerEvent(self.QuickMsgCellTable.objQuickMsg_InputField) UIUtils.SetActive(self.QuickMsgCellTable.event_01, true) UIUtils.SetActive(self.QuickMsgCellTable.event_Mask, false) MDL.ChannelChat.SaveQuickMsg(self.QuickMsgCellTableIndex + 1, newContent) local tmpValue = newContent self.QuickMsgCellTable.txt_01.text = tmpValue if self.QuickMsgCellTable.txt_01.preferredWidth >= 500 then local length = string.len(tmpValue) while (length > 0) do length = length - 1 local subValue = string.sub(tmpValue, 1, length) self.QuickMsgCellTable.txt_01.text = subValue if self.QuickMsgCellTable.txt_01.preferredWidth <= 490 then self.QuickMsgCellTable.txt_01.text = self.QuickMsgCellTable.txt_01.text .. "..." self.QuickMsgCellTableIndex = -1 self.QuickMsgCellTable = nil return end end else self.QuickMsgCellTableIndex = -1 self.QuickMsgCellTable = nil end end end function UIBattleChatDlgWatch:SendMsg(content) if MDL.ChannelChat.CanSendMsg() then local msg = content local CurChannelID = MDL.ChannelChat.GetChannelIdByCfgID(MDL.ChannelChat.ChannelCfgId.Current) if not CTL.ChannelChat.IsOutOfLength(msg, CurChannelID) then CTL.ChannelChat.SendTextMsg(msg, CurChannelID) end else CTL.Tips.AddTipsMsg(MDL.ChannelChat.GetForbiddenMsg()) end end function UIBattleChatDlgWatch:RefreshInviteButtonRedPoint(isAdd) local length = #MDL.Team.GetInviteList() length = isAdd and length or length - 1 return length > 0 end function UIBattleChatDlgWatch:RefreshApplyButtonRedPoint(isAdd) local length = #MDL.Team.GetApplyTeamList() length = isAdd and length or length - 1 return length > 0 end function UIBattleChatDlgWatch:OnClickTeamBubble() if CTL.Team.IsSelfLeader() then UIManager.OpenView(Const_UI_Lua.UIEnum.UITeamApplyPanel) else UIManager.OpenView(Const_UI_Lua.UIEnum.UITeamInvitedPanel) end end function UIBattleChatDlgWatch:InitCurVoiceChannel() self.channelSettingActive = false UIUtils.SetActive(self.mask_Talk, false) local voiceChannelList = CTL.ChannelChat.GetEnableVoiceChannelList() if #voiceChannelList > 0 and CTL.ChannelChat.IsEnableVoiceChat() then UIUtils.SetActive(self.btn_Mic, not self:OnlyShowChat()) local curVoiceChannelCfgID = MDL.ChannelChat.GetCurAudioChannelCfgId() or CTL.ChannelChat.GetDefaultSelectChannel() --当前频道可能是默认频道,不一定解锁,需要判断是否允许语音 local isAllow = false for _, v in ipairs(voiceChannelList) do if v.ID == curVoiceChannelCfgID then isAllow = true break end end if isAllow == false then curVoiceChannelCfgID = voiceChannelList[1].ID end self:SetCurVoiceChannel(curVoiceChannelCfgID) else UIUtils.SetActive(self.btn_Mic, false) end self:RegistTriggerEvent(self.btn_Mic, UIEventTriggerType.PointerExit) self:RegistTriggerEvent(self.btn_Mic, UIEventTriggerType.PointerDown) self:RegistTriggerEvent(self.btn_Mic, UIEventTriggerType.PointerUp) self:RegistTriggerEvent(self.btn_Mic, UIEventTriggerType.PointerEnter) self:RegistSystemEvent(EventID.UI_ChannelChat_OnRecordStartResult, self.OnStartRecordVoiceResult) self:RegistSystemEvent(EventID.UI_ChannelChat_OnRecordTimeEnd, self.OnRecordTimeEnd) self:RegistSystemEvent(EventID.UI_ChannelChat_OnSelectChannelChanged, self.SetCurVoiceChannel) end function UIBattleChatDlgWatch:SetCurVoiceChannel(channelCfgID) if MDL.ChannelChat.IsChannelOpen(channelCfgID) == false then return end if CTL.ChannelChat.IsChannelEnableAudio(channelCfgID) == false then return end self.curVoiceChannelCfgID = channelCfgID local channelCfgData = DataSheetManager.Chat:GetDataByID(channelCfgID) UIUtils.SetLabelText(self.txt_Channel,Const_IDS[channelCfgData.AudioChatName]) MDL.ChannelChat.SetCurAudioChannelCfgId(channelCfgID) end function UIBattleChatDlgWatch:OnRecordTimeEnd() UIUtils.SetActive(self.mask_Talk, false) self.isRecordTimeEnd = true end function UIBattleChatDlgWatch:OnPointerDown(comp, eventData) if comp == self.btn_Mic then --按住时间小于1s,弹出设置scroll;大于1s,录制声音 self:StartRepeatFunc(self.OnPointerPress, 0.5, 1) self.pressTimestamp = UTL.Time.GetServerTimestamp() self.isRecordTimeEnd = false end end function UIBattleChatDlgWatch:OnPointerExit(comp, eventData) if comp == self.btn_Mic then self.isExitAudioPointer = true if self.isRecordTimeEnd == false then EventSystem.Dispatch(EventID.UI_ChannelChat_OnExitPress) end end end function UIBattleChatDlgWatch:OnPointerEnter(comp, eventData) if comp == self.btn_Mic then self.isExitAudioPointer = false if self.isRecordTimeEnd == false then EventSystem.Dispatch(EventID.UI_ChannelChat_OnEnterPress) end end end function UIBattleChatDlgWatch:OnPointerUp(comp, eventData) if comp == self.btn_Mic then self:StopRepeatFunc(self.OnPointerPress) if UTL.Time.GetServerTimestamp() - self.pressTimestamp < 0.5 then if self.channelSettingActive == false then self:OpenChannelSetting() else self:CloseChannelSetting() end else UIUtils.SetActive(self.mask_Talk, false) if self.isRecordTimeEnd == false then if self.isExitAudioPointer then CTL.ChannelChat.CancelRecordVoice() else CTL.ChannelChat.StopRecordVoice() end end end end end function UIBattleChatDlgWatch:OnPointerPress() CTL.ChannelChat.StartRecordVoice(MDL.ChannelChat.GetChannelIdByCfgID(self.curVoiceChannelCfgID), false) end function UIBattleChatDlgWatch:OnStartRecordVoiceResult(result) if result == "true" then UIUtils.SetActive(self.mask_Talk, true) end end function UIBattleChatDlgWatch:OpenChannelSetting() local channelList = CTL.ChannelChat.GetEnableVoiceChannelList(self.curVoiceChannelCfgID) if #channelList <= 0 then return end self.channelSettingActive = true UIUtils.SetActive(self.group_Channel, true) UIUtils.SetActive(self.mask_Talk, true) self.loop_Channel:ReloadAll(channelList) end function UIBattleChatDlgWatch:CloseChannelSetting() self.channelSettingActive = false UIUtils.SetActive(self.group_Channel, false) UIUtils.SetActive(self.mask_Talk, false) end function UIBattleChatDlgWatch:OnDeadChatViewTop(isDead) self:SetSortingLayer(isDead, "UITopmost", 0) self.changeSortingLayer = isDead end function UIBattleChatDlgWatch:OnClickOpenFriend() if self.changeSortingLayer then self.gameObject:SetActive(false) self.isOpen = false UIUtils.SetActive(self.Condition_Msg, false) end end function UIBattleChatDlgWatch:OnFriendBaseClose() if self.isOpen == false then self.gameObject:SetActive(true) self.isOpen = true UIUtils.SetActive(self.Condition_Msg, true) end end function UIBattleChatDlgWatch:OnlyShowChat() return self.context and self.context.onlyChat end return UIBattleChatDlgWatch