UIChatRoot.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. local UIChatRoot = class("UIChatRoot")
  2. local NickNameCtr = require("Common/NickNameCtr")
  3. local regexPattern = "%[e%d+%]"
  4. local sendCd,sendCdList
  5. function UIChatRoot:ctor()
  6. local sendCdCfg = GlobalConfig.Instance:GetConfigStrValue(90)
  7. local list = CommonUtil.DeserializeGlobalStrToNumberTable(sendCdCfg)
  8. sendCdList = list[1]
  9. if ManagerContainer.PayMgr:GetTotalRecharge() > 0 then
  10. sendCd = sendCdList[2]
  11. else
  12. sendCd = sendCdList[1]
  13. end
  14. end
  15. function UIChatRoot:OnPayTotalRechargeChanged()
  16. if ManagerContainer.PayMgr:GetTotalRecharge() > 0 then
  17. sendCd = sendCdList[2]
  18. else
  19. sendCd = sendCdList[1]
  20. end
  21. end
  22. function UIChatRoot:Init(root, channelType, window, inputField, bgOpeCB,isNewChat)
  23. self.root = root
  24. if isNewChat then
  25. self.tempViewLua = root.viewLua
  26. else
  27. self.tempViewLua = root
  28. end
  29. self.channelType = channelType
  30. self.window = window
  31. self.inputField = inputField
  32. self.bgOpeCB = bgOpeCB
  33. self.canSend = true
  34. self:InitPanel()
  35. self.tempViewLua.funcbg:SetActive(false)
  36. self:AddEventListener()
  37. end
  38. function UIChatRoot:InitPanel()
  39. local loopGridViewInitParam = SuperScrollView.LoopGridViewInitParam.CopyDefaultInitParam()
  40. loopGridViewInitParam.mSnapVecThreshold = 9999
  41. local datas = self:GetDatas()
  42. local max = datas and #datas or 0
  43. self.window.loopListView:InitListView(max, function(gridView, itemIndex)
  44. return self:RefreshChats(gridView, itemIndex)
  45. end)
  46. self:RefreshInputStatus()
  47. self.window.loopListView:MovePanelToItemIndex(max, 0)
  48. end
  49. function UIChatRoot:AddEventListener()
  50. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.root.uiData.name, UIEventNames.REFRESH_WOLRD_CHAT, self, self.OnRefreshChat)
  51. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.root.uiData.name, UIEventNames.PAY_TOTAL_RECHARGE_CHANGED, self, self.OnPayTotalRechargeChanged)
  52. end
  53. function UIChatRoot:OnRefreshChat(channelType)
  54. if self.channelType ~= channelType then return end
  55. self:RefreshWindow(true)
  56. end
  57. function UIChatRoot:GetChannelType()
  58. return self.channelType
  59. end
  60. function UIChatRoot:SetChannelType(channelType, forceRefresh)
  61. if self.channelType == channelType and not forceRefresh then return end
  62. self.channelType = channelType
  63. self.window.loopListView:SetListItemCount(0, false)
  64. self:RefreshWindow(true)
  65. end
  66. function UIChatRoot:RefreshWindow(resetPos)
  67. if not self.root.uiBase.IsActive then return end
  68. local datas = self:GetDatas()
  69. local max = datas and #datas or 0
  70. self.window.loopListView:SetListItemCount(max, false)
  71. if self.beginDrag then return end
  72. self:RefreshInputStatus()
  73. if resetPos then
  74. self.window.loopListView:MovePanelToItemIndex(max, 0)
  75. else
  76. --self.window.loopListView:RefreshAllShownItem()
  77. end
  78. end
  79. function UIChatRoot:RefreshInputStatus()
  80. if self.inputField then
  81. if self.channelType ~= Enum.ChatChannel.System then
  82. self.window.loopListView.ScrollRect.viewport.sizeDelta = Vector2(0, -150)
  83. self.root.input:SetActive(true)
  84. self.window.loopListView:ResetListView(false)
  85. else
  86. self.window.loopListView.ScrollRect.viewport.sizeDelta = Vector2.zero
  87. self.root.input:SetActive(false)
  88. end
  89. end
  90. end
  91. function UIChatRoot:GetDatas()
  92. if self.channelType == Enum.ChatChannel.World then
  93. return ManagerContainer.DataMgr.ChatData:GetWorldChatDatas()
  94. elseif self.channelType == Enum.ChatChannel.System then
  95. return ManagerContainer.DataMgr.ChatData:GetSystemDatas()
  96. elseif self.channelType == Enum.ChatChannel.Guild then
  97. return ManagerContainer.DataMgr.ChatData:GetGuildChatDatas()
  98. end
  99. return nil
  100. end
  101. function UIChatRoot:RefreshChats(gridView, itemIndex)
  102. local datas = self:GetDatas()
  103. if not datas then return nil end
  104. local logicData = datas[itemIndex + 1]
  105. if not logicData then return nil end
  106. local item = gridView:NewListViewItem(Enum.PrefabNames.ChatTextItem)
  107. item.gameObject.name = itemIndex
  108. local itemLua = CommonUtil.BindGridViewItem2Lua(self.root, Enum.PrefabNames.ChatTextItem, item.gameObject)
  109. if logicData.isSystem then
  110. CommonUtil.UpdateItemPrefab(self.root, itemLua, logicData, Enum.ItemIEnterType.System)
  111. else
  112. CommonUtil.UpdateItemPrefab(self.root, itemLua, logicData, Enum.ItemIEnterType.WorldChat, self, self.OnTargetSelected)
  113. end
  114. return item
  115. end
  116. function UIChatRoot:OnTargetSelected(button, params)
  117. local playerHead = params[0]
  118. self.curSelectedData = params[1]
  119. local userUid = ManagerContainer.DataMgr.UserData:GetUserId()
  120. if self.curSelectedData.uid == tostring(userUid) then
  121. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ChatError1")
  122. self.curSelectedData = nil
  123. end
  124. if self.curSelectedData == nil then return end
  125. local position = CommonUtil.LocalUIPos2ScreenPos(playerHead.transform.position)
  126. local isLeft = position.x < UnityEngine.Screen.width/2
  127. local isBottom = position.y < UnityEngine.Screen.height/2
  128. position.x = position.x + self.tempViewLua.headOrder.rectTransform.sizeDelta.x/2 - UnityEngine.Screen.width/2
  129. local sizeY = self.tempViewLua.headOrder.rectTransform.sizeDelta.y/2
  130. if isBottom then
  131. position.y = position.y + (sizeY > 0 and sizeY or 246) - UnityEngine.Screen.height/2
  132. else
  133. position.y = position.y - (sizeY > 0 and sizeY or 246) - UnityEngine.Screen.height/2
  134. end
  135. self.tempViewLua.headOrder.rectTransform.anchoredPosition3D = position
  136. self:RefreshFuncBg()
  137. self.tempViewLua.funcbg:SetActive(true)
  138. if self.root.uiview and self.root.uiview.dragBg then
  139. self.root.uiview.dragBg:SetActive(false)
  140. end
  141. end
  142. function UIChatRoot:RefreshFuncBg()
  143. local friendState = ManagerContainer.DataMgr.FriendDataMgr:HasInterestPlayer(int64.new(self.curSelectedData.uid))
  144. self.tempViewLua.btnAddFriend.text.text.text = friendState and I18N.T("RemoveFriends") or I18N.T("AddFriends")
  145. local blockState = ManagerContainer.DataMgr.FriendDataMgr:HasBlackPlayer(int64.new(self.curSelectedData.uid))
  146. self.tempViewLua.btnBlock.text.text.text = blockState and I18N.T("RemoveBlackList") or I18N.T("AddBlackList")
  147. end
  148. function UIChatRoot:AddUIEventListener()
  149. if self.window.uIEventTriggerListener then
  150. self.window.uIEventTriggerListener.onClick = function()
  151. if self.tempViewLua.OnBtnClose then
  152. self.tempViewLua:OnBtnClose()
  153. end
  154. if self.bgOpeCB then
  155. self.bgOpeCB(self.root)
  156. end
  157. end
  158. self.window.uIEventTriggerListener.onBeginDrag = function()
  159. self.beginDrag = true
  160. end
  161. self.window.uIEventTriggerListener.onEndDrag = function()
  162. if self.bgOpeCB then
  163. self.bgOpeCB(self.root)
  164. end
  165. end
  166. self.window.uIEventTriggerListener.onPointerUp = function()
  167. self.beginDrag = false
  168. end
  169. self.window.uIEventTriggerListener.onPointerExit = function()
  170. self.beginDrag = false
  171. end
  172. end
  173. if self.tempViewLua.btnPrivateChat then
  174. self.root.uiBase:AddButtonUniqueEventListener(self.tempViewLua.btnPrivateChat.button,self, self.OnPrivateChat)
  175. end
  176. if self.tempViewLua.funcbg then
  177. self.root.uiBase:AddButtonUniqueEventListener(self.tempViewLua.funcbg.button, self, self.OnCloseFuncBg)
  178. end
  179. if self.tempViewLua.btnInfor then
  180. self.root.uiBase:AddButtonUniqueEventListener(self.tempViewLua.btnInfor.button, self,self.OnPlayerQuery)
  181. end
  182. if self.tempViewLua.btnAddFriend then
  183. self.root.uiBase:AddButtonUniqueEventListener(self.tempViewLua.btnAddFriend.button, self, self.AddFriend)
  184. end
  185. if self.tempViewLua.btnBlock then
  186. self.root.uiBase:AddButtonUniqueEventListener(self.tempViewLua.btnBlock.button, self, self.AddBlack)
  187. end
  188. if self.inputField then
  189. self.inputField.inputField.characterLimit = Constant.CHAT_BYTE_LIMIT
  190. self.inputField.inputField.onValueChanged:RemoveAllListeners()
  191. self.root.uiBase:AddInputFileEventListener(self.inputField.inputField, function(input, content)
  192. self:OnInputChanged(content)
  193. end)
  194. end
  195. if self.tempViewLua.btnSend then
  196. self.root.uiBase:AddButtonUniqueEventListener(self.tempViewLua.btnSend.button,self, self.OnSendClick)
  197. end
  198. end
  199. function UIChatRoot:OnInputChanged(content)
  200. local temp = content
  201. --LogWarning("==========1==="..temp)
  202. --local length = StringUtil.GetTextLeng(self.inputField.inputField.textComponent, temp)
  203. --if string.len(temp) > Constant.CHAT_BYTE_LIMIT then
  204. --temp = string.sub(temp, 0, Constant.CHAT_BYTE_LIMIT)
  205. --LogWarning("==========2==="..temp)
  206. --end
  207. temp = string.gsub(temp, "#", "")
  208. --LogWarning("==========3==="..temp)
  209. --temp = string.gsub(temp, "#h", "")
  210. --temp = string.gsub(temp, "#t", "")
  211. --temp = string.gsub(temp, "#c", "")
  212. temp = StringUtil.FilterEmoji(temp)
  213. --LogWarning("==========4==="..temp)
  214. self.inputField.inputField.text = temp
  215. end
  216. function UIChatRoot:OnPrivateChat(button, params)
  217. self.tempViewLua.funcbg:SetActive(false)
  218. if self.curSelectedData == nil then return end
  219. ManagerContainer.LuaUIMgr:PrivateChatOtherPlayer(self.curSelectedData.uid, self.curSelectedData.nickname, self.curSelectedData.imgId, self.curSelectedData.jobId, self.curSelectedData.level, self.curSelectedData.sex)
  220. end
  221. function UIChatRoot:OnCloseFuncBg(button, params)
  222. self.tempViewLua.funcbg:SetActive(false)
  223. end
  224. function UIChatRoot:OnPlayerQuery()
  225. ManagerContainer.LuaUIMgr:OpenRoleMessagePanel(self.curSelectedData.uid);
  226. self.tempViewLua.funcbg:SetActive(false)
  227. end
  228. function UIChatRoot:AddFriend()
  229. if self.curSelectedData and self.curSelectedData.uid then
  230. local friendState = ManagerContainer.DataMgr.FriendDataMgr:HasInterestPlayer(int64.new(self.curSelectedData.uid))
  231. if friendState then
  232. ManagerContainer.DataMgr.FriendDataMgr:DeleteFriendReq(int64.new(self.curSelectedData.uid))
  233. else
  234. ManagerContainer.DataMgr.FriendDataMgr:AddFriendReq(int64.new(self.curSelectedData.uid))
  235. end
  236. end
  237. self:OnCloseFuncBg()
  238. end
  239. function UIChatRoot:AddBlack()
  240. if self.curSelectedData and self.curSelectedData.uid then
  241. local blockState = ManagerContainer.DataMgr.FriendDataMgr:HasBlackPlayer(int64.new(self.curSelectedData.uid))
  242. if blockState then
  243. ManagerContainer.DataMgr.FriendDataMgr:RemoveBlackReq(int64.new(self.curSelectedData.uid))
  244. else
  245. ManagerContainer.DataMgr.FriendDataMgr:AddBlackReq(int64.new(self.curSelectedData.uid))
  246. end
  247. end
  248. self:OnCloseFuncBg()
  249. end
  250. function UIChatRoot:OnSendClick(button, params)
  251. if self.tempViewLua.emojiWindow and self.tempViewLua.emojiWindow.activeSelf then
  252. self.root:OnEmojiBgClick()
  253. end
  254. local _openCreateName = NickNameCtr:NeedOpenCreate()
  255. if _openCreateName then
  256. NickNameCtr:OpenNickNamePage(true);
  257. return;
  258. end
  259. if not self.canSend then
  260. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ChatFast")
  261. return
  262. end
  263. local emojis = ManagerContainer.CfgMgr:GetEmojiCfg()
  264. for k, v in string.gmatch(self.inputField.inputField.text, regexPattern) do
  265. local num = tonumber(string.match(k, '%d+'))
  266. if num >= #emojis then
  267. self.inputField.inputField.text = string.gsub(self.inputField.inputField.text, k, "")
  268. end
  269. end
  270. if self.inputField.inputField.text == "[]" then
  271. self.inputField.inputField.text = ""
  272. end
  273. if self.inputField.inputField.text == "" then
  274. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ChatContentEmpty")
  275. return
  276. end
  277. local chatDataType, sdkChatType = self:GetChatDataType()
  278. if not chatDataType then
  279. self.inputField.inputField.text = ""
  280. return
  281. end
  282. local content = self.inputField.inputField.text
  283. local level = ManagerContainer.DataMgr.UserData:GetRoleLv()
  284. if SDKMgr.Instance:CheckSpeech(level, sdkChatType, content) then
  285. self:StartSendCD()
  286. self.root.controller:SendChatReq(content, chatDataType)
  287. self.inputField.inputField.text = ""
  288. self:ReportChat(true)
  289. else
  290. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ShieldTips01")
  291. end
  292. end
  293. function UIChatRoot:ReportChat(success)
  294. if not SDKMgr.Instance:IsReportAction() then
  295. return
  296. end
  297. local datas = System.Collections.Generic.Dictionary_object_object()
  298. datas:Add('event', 'mj_speak')
  299. datas:Add('is_achieve', (success and 1 or 0))
  300. SDKMgr.Instance:ReportAction(datas)
  301. end
  302. function UIChatRoot:GetChatDataType()
  303. if self.channelType == Enum.ChatChannel.World then
  304. return Enum.ChatDataType.World, ChannelType.WORLD
  305. elseif self.channelType == Enum.ChatChannel.Guild then
  306. return Enum.ChatDataType.Guild, ChannelType.GUILD
  307. end
  308. return nil
  309. end
  310. function UIChatRoot:StartSendCD()
  311. self.canSend = false
  312. if self.sendCDTimer then
  313. self.sendCDTimer.time = sendCd
  314. else
  315. self.sendCDTimer = Timer.New(function()
  316. self.canSend = true
  317. end, sendCd)
  318. end
  319. if not self.sendCDTimer.running then
  320. self.sendCDTimer:Start()
  321. end
  322. end
  323. function UIChatRoot:Dispose()
  324. if self.inputField then
  325. self.inputField.inputField.onValueChanged:RemoveAllListeners()
  326. end
  327. self.beginDrag = false
  328. self.canSend = true
  329. self.curSelectedData = nil
  330. if self.sendCDTimer then
  331. self.sendCDTimer:Stop()
  332. end
  333. if self.window.uIEventTriggerListener then
  334. self.window.uIEventTriggerListener.onClick = nil
  335. self.window.uIEventTriggerListener.onBeginDrag = nil
  336. self.window.uIEventTriggerListener.onPointerUp = nil
  337. self.window.uIEventTriggerListener.onPointerExit = nil
  338. self.window.uIEventTriggerListener.onEndDrag = nil
  339. end
  340. self.window.loopListView:Dispose()
  341. self.bgOpeCB = nil
  342. self.root = nil
  343. self.type = nil
  344. self.window = nil
  345. self.inputField = nil
  346. end
  347. return UIChatRoot