UIChatView.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. local UIChatView = require("UIChat/UIChatView_Generate")
  2. local UIChatRoot = require("UIChat/UIChatRoot")
  3. local pageToggleData = {}
  4. local regexPattern = "%[e%d+%]"
  5. local emojiFormat = "[e%s]"
  6. local uIChatRootWorld
  7. function UIChatView:OnAwake(data)
  8. self.controller = require("UIChat/UIChatCtr")
  9. self.controller:Init(self)
  10. self.controller:SetData(data)
  11. end
  12. function UIChatView:AddEventListener()
  13. --ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name, UIEventNames.REFRESH_WOLRD_CHAT, function()
  14. -- self:OnChannelChange(Enum.ChatChannel.World)
  15. --end)
  16. end
  17. function UIChatView:FillContent(data, uiBase)
  18. self.uiBase = uiBase
  19. local gameObject = self.uiBase:GetRoot()
  20. if gameObject ~= nil then
  21. self.gameObject = gameObject
  22. self.transform = gameObject.transform
  23. end
  24. self:InitGenerate(self.transform, data)
  25. self:OnceInit()
  26. self:Init()
  27. end
  28. function UIChatView:OnceInit()
  29. self:InitEmojiPanel()
  30. end
  31. function UIChatView:Init()
  32. local channel = ManagerContainer.DataMgr.ChatData:GetChatChannelType()
  33. if uIChatRootWorld == nil then
  34. uIChatRootWorld = UIChatRoot:new()
  35. uIChatRootWorld:Init(self,channel, self.chatWindow, self.inputField)
  36. end
  37. self:SetToggleGroupStatus(channel)
  38. self:OnChannelChange(nil, channel, true)
  39. end
  40. function UIChatView:InitEmojiPanel()
  41. local emojis = ManagerContainer.CfgMgr:GetEmojiCfg()
  42. CommonUtil.LoopGridViewEleCreateNoItem(self,
  43. self.emojiWindow.loopVerticalScrollRect,
  44. emojis,
  45. 0,
  46. self, function (owner, go, idx, logicData)
  47. local textTrans = go.transform:Find("Text")
  48. local text = textTrans:GetComponent(Enum.TypeInfo.SymbolText)
  49. local index = idx
  50. if idx < 10 then
  51. index = "0"..idx
  52. end
  53. text.text = string.format(emojiFormat, index)
  54. local uiTrigger = textTrans:GetComponent(Enum.TypeInfo.UIEventTriggerListener)
  55. uiTrigger.onClick = function()
  56. self:OnEmojiClick(idx)
  57. end
  58. uiTrigger.onBeginDrag = function()
  59. self.emojiWindow.loopVerticalScrollRect:OnBeginDrag(UIEventTriggerListener.currentEventData)
  60. end
  61. uiTrigger.onDrag = function()
  62. self.emojiWindow.loopVerticalScrollRect:OnDrag(UIEventTriggerListener.currentEventData)
  63. end
  64. uiTrigger.onEndDrag = function()
  65. self.emojiWindow.loopVerticalScrollRect:OnEndDrag(UIEventTriggerListener.currentEventData)
  66. end
  67. end)
  68. end
  69. function UIChatView:OnEmojiClick(idx)
  70. local count = 0
  71. for k, v in string.gmatch(self.inputField.inputField.text, regexPattern) do
  72. count = count + 1
  73. end
  74. if count >= 5 then
  75. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ChatError2")
  76. return
  77. end
  78. if idx < 10 then
  79. idx = "0"..idx
  80. end
  81. self.inputField.inputField.text = self.inputField.inputField.text..string.format(emojiFormat, idx)
  82. end
  83. function UIChatView:OnChannelChange(toggle, chatType, isOn)
  84. if not isOn then return end
  85. if self:IsSomeFrameCount('ChatChannelToggle') then
  86. return
  87. end
  88. if chatType == Enum.ChatChannel.World then
  89. uIChatRootWorld:SetChannelType(Enum.ChatChannel.World)
  90. elseif chatType == Enum.ChatChannel.System then
  91. uIChatRootWorld:SetChannelType(Enum.ChatChannel.System)
  92. elseif chatType == Enum.ChatChannel.Guild then
  93. local errorCode = ManagerContainer.DataMgr.GuildDataMgr:GetGuildDataStateErrorCode()
  94. if errorCode then
  95. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay(errorCode)
  96. self:SetToggleGroupStatus(uIChatRootWorld:GetChannelType())
  97. return
  98. end
  99. uIChatRootWorld:SetChannelType(Enum.ChatChannel.Guild)
  100. end
  101. ManagerContainer.DataMgr.ChatData:SetChatChannelType(chatType)
  102. end
  103. function UIChatView:RemoveEventListener()
  104. ManagerContainer.LuaEventMgr:Unregister(self.uiData.name)
  105. end
  106. function UIChatView:OnBtnClose(button, params)
  107. if self.emojiWindow.activeSelf then
  108. self:OnEmojiBgClick()
  109. return
  110. end
  111. self:UIClose()
  112. end
  113. function UIChatView:OnEmojiBgClick(button, params)
  114. self.emojiWindow:SetActive(not self.emojiWindow.activeSelf)
  115. end
  116. function UIChatView:AddUIEventListener()
  117. ManagerContainer.LuaEventMgr:RegisterUIEvent(self.uiData.name)
  118. -- CommonUtil.CreateToggleMouduleOnlyBtns(self, pageToggleData, self.toggle, Enum.ChatChannel.World, self.OnChannelChange)
  119. self.uiBase:AddToggleEventListener(self.chWorld.toggle, self, self.OnChannelChange, Enum.ChatChannel.World)
  120. self.uiBase:AddToggleEventListener(self.chGuild.toggle, self, self.OnChannelChange, Enum.ChatChannel.Guild)
  121. self.uiBase:AddToggleEventListener(self.chSystem.toggle, self, self.OnChannelChange, Enum.ChatChannel.System)
  122. self.uiBase:AddButtonEventListener(self.AnyBtn.button, self, self.OnBtnClose)
  123. uIChatRootWorld:AddUIEventListener()
  124. self.uiBase:AddButtonEventListener(self.btnEmoji.button, self, self.OnEmojiBgClick)
  125. self.uiBase:AddButtonEventListener(self.BtnClose.button, self, self.OnBtnClose)
  126. end
  127. function UIChatView:OnHide()
  128. end
  129. function UIChatView:OnShow(data)
  130. self:OnChannelChange(nil, ManagerContainer.DataMgr.ChatData:GetChatChannelType(), true)
  131. self:Init()
  132. end
  133. function UIChatView:OnClose()
  134. if not self.closed then
  135. self.closed = true
  136. end
  137. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.WORLD_CHAT_UI_CLOSE)
  138. if uIChatRootWorld then
  139. uIChatRootWorld:Dispose()
  140. uIChatRootWorld = nil
  141. end
  142. end
  143. function UIChatView:OnDispose()
  144. self.emojiWindow:SetActive(false)
  145. self.inputField.inputField:DeactivateInputField()
  146. self.emojiWindow.loopVerticalScrollRect:ClearCells()
  147. end
  148. function UIChatView:IsSomeFrameCount(keyName)
  149. local value = self[keyName]
  150. if value and value >= Time.frameCount then
  151. return true
  152. end
  153. self[keyName] = Time.frameCount
  154. return false
  155. end
  156. function UIChatView:SetToggleGroupStatus(chatChannelType)
  157. local toggleGroup = self.toggle.toggleGroup
  158. local oldAllowSwitchOff = toggleGroup.allowSwitchOff
  159. toggleGroup.allowSwitchOff = false
  160. self.chWorld.toggle.isOn = (chatChannelType == Enum.ChatChannel.World)
  161. self.chGuild.toggle.isOn = (chatChannelType == Enum.ChatChannel.Guild)
  162. self.chSystem.toggle.isOn = (chatChannelType == Enum.ChatChannel.System)
  163. toggleGroup.allowSwitchOff = oldAllowSwitchOff
  164. end
  165. return UIChatView