miduo_client/Assets/ManagedResources/~Lua/Modules/Chat/ChatPanel.lua

520 lines
20 KiB
Lua
Raw Normal View History

2020-08-25 15:46:38 +08:00
require("Base/BasePanel")
2020-05-09 13:31:21 +08:00
local ChatPanel = Inherit(BasePanel)
local this = ChatPanel
local itemConfig = ConfigManager.GetConfig(ConfigName.ItemConfig)
local TabBox = require("Modules/Common/TabBox")
local _TabFontColor = { default = Color.New(130 / 255, 128 / 255, 120 / 255, 1),
lock = Color.New(130 / 255, 128 / 255, 120 / 255, 1),
select = Color.New(243 / 255, 235 / 255, 202 / 255, 1) }
local _TabImage = { default = "r_tongyong_xiaanniu_02", lock = "r_tongyong_xiaanniu_02", select = "r_tongyong_xiaanniu_01"}
local _TabData = {
2021-04-09 12:06:17 +08:00
{text = "好友", channel = CHAT_CHANNEL.FRIEND, rpType = RedPointType.Chat_Friend},
{text = "公会", channel = CHAT_CHANNEL.FAMILY},
{text = "世界", channel = CHAT_CHANNEL.GLOBAL},
{text = "系统", channel = CHAT_CHANNEL.SYSTEM},
2020-05-09 13:31:21 +08:00
}
-- 当前所在的聊天通道
this._CurChannel = 1
--初始化组件(用于子类重写)
function ChatPanel:InitComponent()
this.tabbox = Util.GetGameObject(self.gameObject, "tabbox")
this.btnBack = Util.GetGameObject(self.gameObject, "btnBack")
this.scrollRoot = Util.GetGameObject(self.gameObject, "content/scrollroot")
this.friendItem = Util.GetGameObject(self.gameObject, "content/friend")
this.systemItem = Util.GetGameObject(self.gameObject, "content/system")
this.chatItem = Util.GetGameObject(self.gameObject, "content/chat")
this.input = Util.GetGameObject(self.gameObject, "bottom/input")
this.inputText = Util.GetGameObject(self.gameObject, "bottom/input/InputField"):GetComponent("InputField")
this.btnSend = Util.GetGameObject(self.gameObject, "bottom/input/send")
this.noInputTip = Util.GetGameObject(self.gameObject, "bottom/tip")
this.playerInfo = Util.GetGameObject(self.gameObject, "playerInfo")
this.playerBg = Util.GetGameObject(this.playerInfo, "bg")
this.playerHead = Util.GetGameObject(this.playerBg, "head"):GetComponent("Image")
this.playerHeadKuang = Util.GetGameObject(this.playerBg, "headkuang"):GetComponent("Image")
this.playerLv = Util.GetGameObject(this.playerBg, "lv"):GetComponent("Text")
this.playerName = Util.GetGameObject(this.playerBg, "name"):GetComponent("Text")
this.playerPower = Util.GetGameObject(this.playerBg, "power"):GetComponent("Text")
this.playerInvite = Util.GetGameObject(this.playerBg, "btnInvite")
this.playerFriend = Util.GetGameObject(this.playerBg, "btnFriend")
-- 上部货币显示
this.UpView = SubUIManager.Open(SubUIConfig.UpView, self.transform, { showType = UpViewOpenType.ShowLeft})
end
--绑定事件(用于子类重写)
function ChatPanel:BindEvent()
-- 关闭界面
Util.AddClick(this.btnBack, function()
PlaySoundWithoutClick(SoundConfig.Sound_UICancel)
-- todo:添加公会界面打开聊天,关闭时跳转至主界面的处理
if this._JumpType == 1 then
this:ClosePanel()
UIManager.OpenPanel(UIName.MainPanel)
else
this:ClosePanel()
end
end)
-- 发送消息
Util.AddClick(this.btnSend, function()
local content = this.inputText.text
ChatManager.RequestSendChatMsg(this._CurChannel, content, function()
-- 清空聊天显示
this.inputText.text = ""
end)
end)
-- 关闭玩家信息界面
Util.AddClick(this.playerInfo, function()
this.playerInfo:SetActive(false)
end)
-- 创建一个tab管理
this.TabCtrl = TabBox.New()
this.TabCtrl:SetTabAdapter(this.TabAdapter)
this.TabCtrl:SetTabIsLockCheck(this.TabIsLockCheck)
this.TabCtrl:SetChangeTabCallBack(this.OnTabChange)
this.TabCtrl:Init(this.tabbox, _TabData)
end
--添加事件监听(用于子类重写)
function ChatPanel:AddListener()
Game.GlobalEvent:AddEvent(GameEvent.Chat.OnChatDataChanged, this.OnChatDataChanged)
end
--移除事件监听(用于子类重写)
function ChatPanel:RemoveListener()
Game.GlobalEvent:RemoveEvent(GameEvent.Chat.OnChatDataChanged, this.OnChatDataChanged)
end
--界面打开时调用(用于子类重写)
function ChatPanel:OnOpen(...)
-- 参数保存
local args = {...}
this._CurTabIndex = args[1] or 3
this._JumpType = args[2] or 1
this._CurChannel = _TabData[this._CurTabIndex].channel
end
--界面打开或者重新打开后,界面刷新时调用(用于子类重写)
function ChatPanel:OnShow()
if this.TabCtrl then
this.TabCtrl:ChangeTab(this._CurTabIndex or 3)
end
-- 货币界面
this.UpView:OnOpen({ showType = UpViewOpenType.ShowLeft, panelType = PanelType.Main })
-- 开始定时刷新数据
ChatManager.StartChatDataUpdate()
end
--界面关闭时调用(用于子类重写)
function ChatPanel:OnClose()
-- 关闭定时刷新数据
ChatManager.StopChatDataUpdate()
end
--界面销毁时调用(用于子类重写)
function ChatPanel:OnDestroy()
--
SubUIManager.Close(this.UpView)
this.FriendScrollView = nil
this.ChatScrollView = nil
this.SystemScrollView = nil
-- 资源回收
-- if this.ChatScrollView then
-- this.ChatScrollView:RecycleNode()
-- this.ChatScrollView = nil
-- end
-- -- 资源回收
-- if this.SystemScrollView then
-- this.SystemScrollView:RecycleNode()
-- this.SystemScrollView = nil
-- end
-- 清除红点绑定
for _, v in ipairs(_TabData) do
if v.rpType then
ClearRedPointObject(v.rpType)
end
end
end
-- tab节点自定义设置
function this.TabAdapter(tab, index, status)
local tabLab = Util.GetGameObject(tab, "Text")
tabLab:GetComponent("Text").text = _TabData[index].text
tabLab:GetComponent("Text").color = _TabFontColor[status]
local tabImg = Util.GetGameObject(tab, "Image")
tabImg:GetComponent("Image").sprite = Util.LoadSprite(_TabImage[status])
local redpot = Util.GetGameObject(tab, "redpot")
if _TabData[index].rpType then
BindRedPointObject(_TabData[index].rpType, redpot)
else
redpot:SetActive(false)
end
end
-- tab锁定检测
function this.TabIsLockCheck(index)
local channel = _TabData[index].channel
if channel == CHAT_CHANNEL.FAMILY then
local isOpen = ActTimeCtrlManager.SingleFuncState(FUNCTION_OPEN_TYPE.GUILD)
if not isOpen then
2021-04-09 12:06:17 +08:00
return true, "功能尚未开启,敬请期待"
2020-05-09 13:31:21 +08:00
end
if isOpen and PlayerManager.familyId == 0 then
2021-04-09 12:06:17 +08:00
return true, "尚未加入公会"
2020-05-09 13:31:21 +08:00
end
end
return false
end
-- 节点状态改变回调事件
function this.OnTabChange(index, lastIndex)
-- 关闭上个界面
if lastIndex then
local lastSV = this.GetScrollView(lastIndex)
-- if lastIndex ~= 1 then
-- lastSV:RecycleNode()
-- end
lastSV.gameObject:SetActive(false)
end
-- 打开当前界面
this._CurTabIndex = index
local curSV = this.GetScrollView(index)
curSV.gameObject:SetActive(true)
-- 是否能够发言
this._CurChannel = _TabData[index].channel
if this._CurChannel == 1 or this._CurChannel == 2 then
if ActTimeCtrlManager.SingleFuncState(FUNCTION_OPEN_TYPE.CHAT) then
this.input:SetActive(true)
this.noInputTip:SetActive(false)
else
this.input:SetActive(false)
this.noInputTip:SetActive(true)
2021-04-09 12:06:17 +08:00
this.noInputTip:GetComponent("Text").text = "玩家等级13解锁发言功能"
2020-05-09 13:31:21 +08:00
end
elseif this._CurChannel == 0 or this._CurChannel == 3 then
this.input:SetActive(false)
this.noInputTip:SetActive(true)
2021-04-09 12:06:17 +08:00
this.noInputTip:GetComponent("Text").text = "该频道下无法发言"
2020-05-09 13:31:21 +08:00
end
-- 刷新显示
this.RefreshShow(true)
end
-- 数据变化回调
function this.OnChatDataChanged(channel)
if channel == this._CurChannel then
this.RefreshShow(false)
end
end
-- 刷新当前显示
function this.RefreshShow(isForceBottom)
-- 刷新数据
local curSV = this.GetScrollView(this._CurTabIndex)
local datalist = ChatManager.GetChatList(this._CurChannel)
local _GoToIndex = nil
if this._CurChannel ~= CHAT_CHANNEL.FRIEND then
_GoToIndex = #datalist
end
curSV:SetData(datalist, function(dataIndex, go)
-- 判断是否要显示聊天的时间
local isShowTime = false
if dataIndex == 1 then
isShowTime = true
elseif dataIndex > 1 then
local deltaTime = datalist[dataIndex].times - datalist[dataIndex - 1].times
isShowTime = deltaTime >= ChatManager.ShowTimeDT
end
-- 设置数据
local data = datalist[dataIndex]
if this._CurChannel == CHAT_CHANNEL.FRIEND then
this.FriendItemAdapter(go, data)
elseif this._CurChannel == CHAT_CHANNEL.SYSTEM then
this.SystemItemAdapter(go, data, isShowTime)
else
this.ChatItemAdapter(go, data, isShowTime)
end
end, _GoToIndex)
-- 好友数据置顶
if this._CurChannel == CHAT_CHANNEL.FRIEND then
curSV:SetIndex(1)
end
end
-- 创建滚动列表
function this.GetScrollView(index)
-- 不存在则创建
if index == 1 then
-- 判断是否存在
if not this.FriendScrollView then
local rootHight = this.scrollRoot.transform.rect.height
local sv = SubUIManager.Open(SubUIConfig.ScrollCycleView, this.scrollRoot.transform,
this.friendItem, nil, Vector2.New(1080, rootHight - 10), 1, 1, Vector2.New(0, 10))
sv.gameObject:GetComponent("RectTransform").anchoredPosition = Vector2.New(0, -5)
sv.moveTween.Strength = 2
-- 保存
this.FriendScrollView = sv
end
return this.FriendScrollView
elseif index == 2 or index == 3 then
if not this.ChatScrollView then
local rootHight = this.scrollRoot.transform.rect.height
local sv = SubUIManager.Open(SubUIConfig.ScrollFitterView, this.scrollRoot.transform,
this.chatItem, Vector2.New(1080, rootHight - 10), 1, 10)
sv.gameObject:GetComponent("RectTransform").anchoredPosition = Vector2.New(0, -5)
sv.moveTween.Strength = 2
-- 保存
this.ChatScrollView = sv
end
return this.ChatScrollView
elseif index == 4 then
if not this.SystemScrollView then
local rootHight = this.scrollRoot.transform.rect.height
local sv = SubUIManager.Open(SubUIConfig.ScrollFitterView, this.scrollRoot.transform,
this.systemItem, Vector2.New(1080, rootHight - 10), 1, 10)
sv.gameObject:GetComponent("RectTransform").anchoredPosition = Vector2.New(0, -5)
sv.moveTween.Strength = 2
-- 保存
this.SystemScrollView = sv
end
return this.SystemScrollView
end
end
-- 好友节点数据匹配
function this.FriendItemAdapter(node, data)
-- 获取节点
local bg = Util.GetGameObject(node, "bg")
local content = Util.GetGameObject(node, "content"):GetComponent("Text")
local name = Util.GetGameObject(node, "name"):GetComponent("Text")
local lv = Util.GetGameObject(node, "lv"):GetComponent("Text")
local head = Util.GetGameObject(node, "head"):GetComponent("Image")
local frame = Util.GetGameObject(node, "headkuang"):GetComponent("Image")
local redpot = Util.GetGameObject(node, "redpot")
-- 数据匹配
local fdata = GoodFriendManager.GetFriendInfo(data.friendId)
content.text = data.content
name.text = fdata.name
lv.text = fdata.lv
head.sprite = GetPlayerHeadSprite(fdata.head)
frame.sprite = GetPlayerHeadFrameSprite(fdata.frame)
redpot:SetActive(FriendChatManager.IsFriendNewChat(data.friendId))
-- 头像添加点击事件
Util.AddOnceClick(bg, function()
UIManager.OpenPanel(UIName.FriendChatPanel, data.friendId)
end)
end
-- 系统消息节点数据匹配
function this.SystemItemAdapter(node, data, isShowTime)
local time = Util.GetGameObject(node, "time")
local bg = Util.GetGameObject(node, "bg")
local content = Util.GetGameObject(node, "bg/content"):GetComponent("Text")
local sType = Util.GetGameObject(node, "bg/content/type")
local sTypeStr = Util.GetGameObject(node, "bg/content/type/Text"):GetComponent("Text")
time:SetActive(isShowTime)
if isShowTime then
time:GetComponent("Text").text = TimeStampToDateStr(tonumber(data.times)/1000)
end
local sTypeInfo = ChatManager.SYS_MSG_TYPE[data.messageType]
if sTypeInfo then
sType:SetActive(true)
sTypeStr.text = sTypeInfo.name
content.text = "   " .. data.msg
else
sType:SetActive(false)
content.text = data.msg
end
Util.AddOnceClick(bg, function()
-- 内容包含异妖的不显示
2021-04-09 12:06:17 +08:00
local pos = string.find(data.msg, "异妖")
2020-05-09 13:31:21 +08:00
if pos then return end
-- 其他的按物品显示
local itemDataConFig = itemConfig[data.itemId]
if not itemDataConFig then return end
if itemDataConFig.ItemType == ItemType.Hero then
local heroConfigData=ConfigManager.GetConfigData(ConfigName.HeroConfig, itemConfig[data.itemId].HeroStar[1])
2021-04-09 12:06:17 +08:00
local showStarNum = string.find(data.msg, "觉醒") and heroConfigData.MaxRank or itemConfig[data.itemId].HeroStar[2]
if showStarNum == HeroManager.awakeStarIndex then--只有觉醒有点击事件
UIManager.OpenPanel(UIName.RoleGetInfoPopup,false,heroConfigData.Id,showStarNum)
end
2020-05-09 13:31:21 +08:00
elseif itemDataConFig.ItemType == ItemType.Equip then
UIManager.OpenPanel(UIName.HandBookEquipInfoPanel, data.itemId)
elseif itemDataConFig.ItemType == ItemType.Talisman then
-- UIManager.OpenPanel(UIName.TalismanInfoPopup, data.itemId, 2, 1)
2020-05-09 13:31:21 +08:00
else
UIManager.OpenPanel(UIName.RewardItemSingleShowPopup, data.itemId)
end
end)
end
-- 聊天节点数据匹配
function this.ChatItemAdapter(node, data, isShowTime)
local right = Util.GetGameObject(node, "rightchat")
local left = Util.GetGameObject(node, "leftchat")
local isMyChat = data.senderId == PlayerManager.uid
right:SetActive(isMyChat)
left:SetActive(not isMyChat)
node = isMyChat and right or left
local time = Util.GetGameObject(node, "time")
local timeLab = Util.GetGameObject(node, "time/Text"):GetComponent("Text")
local content = Util.GetGameObject(node, "contentroot/bg/content"):GetComponent("Text")
local head = Util.GetGameObject(node, "base/head"):GetComponent("Image")
local headKuang = Util.GetGameObject(node, "base/headkuang"):GetComponent("Image")
local lv = Util.GetGameObject(node, "base/lv"):GetComponent("Text")
local name = Util.GetGameObject(node, "base/name"):GetComponent("Text")
local zw = Util.GetGameObject(node, "base/zhiwei")
local zwName = Util.GetGameObject(node, "base/zhiwei/Text"):GetComponent("Text")
-- 判断是否显示时间
time:SetActive(isShowTime)
if isShowTime then
timeLab.text = TimeStampToDateStr(tonumber(data.times)/1000)
end
local contentStr = ""
if this._CurChannel == CHAT_CHANNEL.GLOBAL then
local chat = ChatManager.AnalysisGlobalChat(data.msg)
contentStr = chat.content
if chat.type == GLOBAL_CHAT_TYPE.COMMON then
Util.AddOnceClick(content.gameObject, function() end)
elseif chat.type == GLOBAL_CHAT_TYPE.GUILD_INVITE then
Util.AddOnceClick(content.gameObject, function()
if PlayerManager.familyId ~= 0 then
2021-04-09 12:06:17 +08:00
PopupTipPanel.ShowTip("您已加入公会")
2020-05-09 13:31:21 +08:00
return
end
--local _IsInFight = GuildFightManager.IsInGuildFight()
--if _IsInFight then
-- PopupTipPanel.ShowTip("公会战期间无法加入公会")
-- return
--end
2021-04-09 12:06:17 +08:00
MsgPanel.ShowTwo(string.format("确认要加入 %s 公会吗", chat.gName),nil, function()
2020-05-09 13:31:21 +08:00
if PlayerManager.level < chat.gLimitLv then
2021-04-09 12:06:17 +08:00
PopupTipPanel.ShowTip("不符合该公会限制条件")
2020-05-09 13:31:21 +08:00
return
end
if chat.joinType == GUILD_JOIN_TYPE.NO_LIMIT then
GuildManager.RequestJoinGuild(chat.gId, function ()
-- 关闭当前界面
if this._JumpType == 1 then
this:ClosePanel()
UIManager.OpenPanel(UIName.MainPanel)
else
this:ClosePanel()
end
-- 打开公会主城
UIManager.OpenPanel(UIName.GuildMainCityPanel)
2021-04-09 12:06:17 +08:00
PopupTipPanel.ShowTip("成功加入公会")
2020-05-09 13:31:21 +08:00
end)
elseif chat.joinType == GUILD_JOIN_TYPE.LIMIT then
GuildManager.RequestApplyManyGuilds({chat.gId}, function()
2021-04-09 12:06:17 +08:00
PopupTipPanel.ShowTip("申请发送成功")
2020-05-09 13:31:21 +08:00
end)
elseif chat.joinType == GUILD_JOIN_TYPE.FORBID then
2021-04-09 12:06:17 +08:00
PopupTipPanel.ShowTip("公会禁止加入")
2020-05-09 13:31:21 +08:00
end
end)
end)
end
elseif this._CurChannel == CHAT_CHANNEL.FAMILY then
local chat = ChatManager.AnalysisGlobalChat(data.msg)
contentStr = chat.content
if chat.type == GLOBAL_CHAT_TYPE.COMMON then
Util.AddOnceClick(content.gameObject, function() end)
elseif chat.type == GLOBAL_CHAT_TYPE.GUILD_REDPACKET then
contentStr=chat.content
2020-05-15 17:03:13 +08:00
elseif chat.type == GLOBAL_CHAT_TYPE.GUILD_AID then
Util.AddOnceClick(content.gameObject, function()
JumpManager.GoJump(72001)
end)
2020-05-09 13:31:21 +08:00
end
else
contentStr = data.msg
end
-- 基础信息
content.text = contentStr
if data.senderId == PlayerManager.uid then
lv.text = PlayerManager.level
name.text = PlayerManager.nickName
head.sprite = GetPlayerHeadSprite(PlayerManager.head)
2021-01-18 15:07:23 +08:00
headKuang.sprite = GetPlayerHeadFrameSprite(HeadManager.GetCurFrameId())
2020-05-09 13:31:21 +08:00
else
lv.text = data.senderlevel
name.text = data.senderName
head.sprite = GetPlayerHeadSprite(data.head)
headKuang.sprite = GetPlayerHeadFrameSprite(data.frame)
end
-- 头像添加点击事件
Util.AddOnceClick(head.gameObject, function()
-- 自己不做任何操作
if data.senderId ~= PlayerManager.uid then
this.playerFriend:SetActive(true)
for i,v in pairs(GoodFriendManager.friendAllData) do
if(i==data.senderId and v~=nil) then
this.playerFriend:SetActive(false)
end
end
--this.ShowPlayerInfo(node, data)
UIManager.OpenPanel(UIName.PlayerInfoPopup,data.senderId)
end
end)
zw:SetActive(false)
end
--- 显示玩家信息
function this.ShowPlayerInfo(node, data)
-- 显示
this.playerInfo:SetActive(true)
-- 设置位置
local v2 = RectTransformUtility.WorldToScreenPoint(UIManager.camera, node.transform.position)
this.playerBg.transform.localPosition = Vector3(v2.x + 200, v2.y, 0)
-- 设置内容
this.playerLv.text = data.senderlevel
this.playerName.text = data.senderName
this.playerPower.text = data.soulVal
this.playerHead.sprite = GetPlayerHeadSprite(data.head)
this.playerHeadKuang.sprite = GetPlayerHeadFrameSprite(data.frame)
-- 邀请加入公会
Util.AddOnceClick(this.playerInvite, function()
2021-04-09 12:06:17 +08:00
PopupTipPanel.ShowTip("功能尚未开启,敬请期待!")
2020-05-09 13:31:21 +08:00
end)
-- 添加好友
Util.AddOnceClick(this.playerFriend, function()
GoodFriendManager.InviteFriendRequest(data.senderId, function()
2021-04-09 12:06:17 +08:00
PopupTipPanel.ShowTip("请求发送成功!")
2020-05-09 13:31:21 +08:00
this.playerInfo:SetActive(false)
end)
end)
end
2020-06-23 18:36:24 +08:00
return ChatPanel