You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
9.9 KiB

  1. local ChatDefine = require("luaScript.GameChatDefine")
  2. local RoomChatView = class("RoomChatView", cc.UIView)
  3. --[[
  4. -- 使用方法
  5. function XXRoomView:initChatView()
  6. ...
  7. local messageList = {}
  8. local headInfos = {}
  9. self.chatView = import("luaScript.Views.Room.RoomChatView"):new(messageList,faceList)
  10. self:addChild(self.chatView)
  11. end
  12. --
  13. messageList =
  14. {
  15. chatList = {
  16. [1] = {oggStandard = "", oggLocal = "", txt = "各位前辈,我要开车了!"},
  17. [2] = {oggStandard = "", oggLocal = "", txt = "今天牌真是太好了!"},
  18. [3] = {oggStandard = "", oggLocal = "", txt = "快点啦!准备开局!"},
  19. [4] = {oggStandard = "", oggLocal = "", txt = "你们打的好,但是我要自摸了"},
  20. [5] = {oggStandard = "", oggLocal = "", txt = "牌神来了,让座让座!"},
  21. [6] = {oggStandard = "", oggLocal = "", txt = "稍等一下,我拉个朋友"},
  22. [7] = {oggStandard = "", oggLocal = "", txt = "时间很宝贵的,快点出牌吧"},
  23. [8] = {oggStandard = "", oggLocal = "", txt = "辛苦十几年,一把回到解放前!!"},
  24. [9] = {oggStandard = "", oggLocal = "", txt = "等下再来一把!"},
  25. },
  26. --采用plist
  27. faceList = {
  28. [1] = {oggStandard = "", oggLocal = "", btnPng = "face_j_1001.png", aniFile = "res/ui/zy_tongyong/zy_face/face1_anims_i6p.plist", aniString = "face1_%d.png", frameNum = 10},
  29. [2] = {oggStandard = "", oggLocal = "", btnPng = "face_j_1002.png", aniFile = "res/ui/zy_tongyong/zy_face/face2_anims_i6p.plist", aniString = "face2_%d.png", frameNum = 10},
  30. [3] = {oggStandard = "", oggLocal = "", btnPng = "face_j_1003.png", aniFile = "res/ui/zy_tongyong/zy_face/face3_anims_i6p.plist", aniString = "face3_%d.png", frameNum = 10},
  31. [4] = {oggStandard = "", oggLocal = "", btnPng = "face_j_1004.png", aniFile = "res/ui/zy_tongyong/zy_face/face4_anims_i6p.plist", aniString = "face4_%d.png", frameNum = 10},
  32. [5] = {oggStandard = "", oggLocal = "", btnPng = "face_j_1005.png", aniFile = "res/ui/zy_tongyong/zy_face/face5_anims_i6p.plist", aniString = "face5_%d.png", frameNum = 10},
  33. },
  34. }
  35. -- 玩家头像所在的节点 voiceDir不定义将会根据屏幕的中心判断是左边还是右边
  36. headInfos =
  37. {
  38. [1] = {headPos = cc.p(0,0), voiceDir = 1},
  39. [2] = {headPos = cc.p(0,0), voiceDir = 1},
  40. [3] = {headPos = cc.p(0,0), voiceDir = 1},
  41. [4] = {headPos = cc.p(0,0), voiceDir = 1},
  42. [5] = {headPos = cc.p(0,0), voiceDir = 1},
  43. [6] = {headPos = cc.p(0,0), voiceDir = 1},
  44. [7] = {headPos = cc.p(0,0), voiceDir = 1},
  45. [8] = {headPos = cc.p(0,0), voiceDir = 1},
  46. }
  47. --]]
  48. local DEFAULT_INDEX = ChatDefine.MessageType.Message
  49. function RoomChatView:ctor(messageList,faceList)
  50. RoomChatView.super.ctor(self)
  51. self:loadUI()
  52. self.messageList = messageList or {
  53. [1] = {oggStandard = "", oggLocal = "", txt = "各位前辈,我要开车了!"},
  54. [2] = {oggStandard = "", oggLocal = "", txt = "今天牌真是太好了!"},
  55. [3] = {oggStandard = "", oggLocal = "", txt = "快点啦!准备开局!"},
  56. [4] = {oggStandard = "", oggLocal = "", txt = "你们打的好,但是我要自摸了"},
  57. [5] = {oggStandard = "", oggLocal = "", txt = "牌神来了,让座让座!"},
  58. [6] = {oggStandard = "", oggLocal = "", txt = "稍等一下,我拉个朋友"},
  59. [7] = {oggStandard = "", oggLocal = "", txt = "时间很宝贵的,快点出牌吧"},
  60. [8] = {oggStandard = "", oggLocal = "", txt = "辛苦十几年,一把回到解放前!!"},
  61. [9] = {oggStandard = "", oggLocal = "", txt = "等下再来一把!"},
  62. }
  63. self.faceList = faceList or ChatDefine.FaceConfigs
  64. end
  65. function RoomChatView:loadUI()
  66. local ui = loadUI("res/ui/ui_fangjian/ui_fangjian_chat_new.ui")
  67. autoAdapt(ui)
  68. self.ui = ui
  69. self:addChild(ui)
  70. end
  71. function RoomChatView:onEnter()
  72. self.ui.Items.Layout_Face:setVisible(false)
  73. self.ui.Items.Layout_Message:setVisible(false)
  74. self.ui.Items.Layout_Chat_Other:setVisible(false)
  75. self.ui.Items.Layout_Chat_My:setVisible(false)
  76. self:initFace()
  77. self:initMessage()
  78. -- 初始化checkBox的切换
  79. local radio = import("luaScript.Tools.RadioManager"):new()
  80. radio:addItem(self.ui.Items.CheckBox_Message, ChatDefine.MessageType.Message)
  81. radio:addItem(self.ui.Items.CheckBox_Face, ChatDefine.MessageType.Face)
  82. radio:addItem(self.ui.Items.CheckBox_Chat, ChatDefine.MessageType.Chat)
  83. radio:setCallback(handler(self, self.onClickCheckBox))
  84. radio:setDefault(DEFAULT_INDEX)
  85. self.messageRadio = radio
  86. -- 发送按钮
  87. self.ui.Items.Button_Send:registerClick(handler(self , self.onClickSend))
  88. self:bindEvent(app.user, GAME_EVENT.CHAT_PRSPONSE, handler(self, self.onChatResposeEvent))
  89. self:bindEvent(app.user, GAME_EVENT.CHAT_GET_RECORED_RESP, handler(self, self.onGetRecordRespEvent))
  90. app.user:dispatchEvent({name = GAME_EVENT.CHAT_GET_RECORED})
  91. --屏蔽聊天 ljx
  92. self.ui.Items.CheckBox_Chat:setVisible(false)
  93. self.ui.Items.Button_Send:setVisible(false)
  94. self.ui.Items.ImageView_InputBG:setVisible(false)
  95. end
  96. function RoomChatView:onClickCheckBox(idx)
  97. playBtnEffect()
  98. self.ui.Items.ScrollView_Message:setVisible(idx == ChatDefine.MessageType.Message)
  99. self.ui.Items.ScrollView_Face:setVisible(idx == ChatDefine.MessageType.Face)
  100. self.ui.Items.ScrollView_Chat:setVisible(idx == ChatDefine.MessageType.Chat)
  101. DEFAULT_INDEX = idx
  102. end
  103. -- 创建一个表情按钮的节点
  104. function RoomChatView:createFaceItem(idx)
  105. local ui = self.ui.Items.Layout_Face:getCopied()
  106. ui.Items = getUIItems(ui)
  107. local faceConfig = self.faceList[idx]--ChatDefine.FaceConfigs[idx]
  108. if faceConfig then
  109. local btnPng = faceConfig.btnPng
  110. ui.Items.Button:loadTextureNormal("res/ui/zy_tongyong/zy_face/chat/"..btnPng)
  111. ui.Items.Button:loadTexturePressed("res/ui/zy_tongyong/zy_face/chat/"..btnPng)
  112. ui.Items.Button:loadTextureDisabled("res/ui/zy_tongyong/zy_face/chat/"..btnPng)
  113. end
  114. ui.Items.Button:registerClick(function()
  115. playBtnEffect()
  116. self:sendChatMessage(ChatDefine.MessageType.Face, idx);
  117. end)
  118. return ui;
  119. end
  120. -- 创建一个快捷语节点
  121. function RoomChatView:createMessageItem(idx)
  122. local ui = self.ui.Items.Layout_Message:getCopied()
  123. ui.Items = getUIItems(ui)
  124. local messageConfig = self.messageList[idx]
  125. if messageConfig and messageConfig.txt then
  126. ui.Items.Text_Message:setText(messageConfig.txt)
  127. end
  128. ui:registerClick(function()
  129. self:sendChatMessage(ChatDefine.MessageType.Message, idx);
  130. end)
  131. return ui;
  132. end
  133. -- 创建一个聊天节点
  134. function RoomChatView:addChatItem(nUserId,message)
  135. local userInfo = app.room:getUserInfo(nUserId)
  136. local childrens = self.ui.Items.ScrollView_Chat:getChildren()
  137. if #childrens>=20 then
  138. childrens[1]:removeFromParent()
  139. end
  140. local str = nUserId==app.user.loginInfo.uid and "My" or "Other"
  141. local ui = self.ui.Items["Layout_Chat_"..str]:getCopied()
  142. ui.Items = getUIItems(ui)
  143. ui.Items["Text_Chat_"..str]:setText(message)
  144. local textSize = ui.Items["Text_Chat_"..str]:getContentSize()
  145. ui.Items["Layout_Chat_Text_"..str]:setSize(cc.size(textSize.width+35,textSize.height+35))
  146. ui.Items["Layout_Chat_"..str]:setSize(cc.size(ui.Items["Layout_Chat_"..str]:getContentSize().width,textSize.height+35+35))
  147. local height = ui.Items["Layout_Chat_"..str]:getContentSize().height
  148. ui.Items["Layout_Head_"..str]:setPositionY(height-20)
  149. ui.Items["Layout_Chat_Text_"..str]:setPositionY(height-20)
  150. if userInfo and userInfo.headimgurl then
  151. setPlayerHeadImage(nUserId,userInfo.headimgurl,ui.Items["ImageView_Head_"..str],true)
  152. end
  153. self.ui.Items.ScrollView_Chat:addChild(ui)
  154. self.ui.Items.ScrollView_Chat:jumpToBottomOnSizeChanged()
  155. return ui
  156. end
  157. --表情
  158. function RoomChatView:initFace()
  159. local uiScrollMessage = self.ui.Items.ScrollView_Face
  160. uiScrollMessage:removeAllChildren()
  161. uiScrollMessage:hideAllBar();
  162. uiScrollMessage:getInnerContainer():setAutoSize(true)
  163. for msgId, msgInfo in pairsByKeys(self.faceList) do
  164. local item = self:createFaceItem(msgId)
  165. if item then
  166. uiScrollMessage:addChild(item)
  167. end
  168. end
  169. uiScrollMessage:jumpToTopOnSizeChanged()
  170. end
  171. --短语
  172. function RoomChatView:initMessage()
  173. local uiScrollMessage = self.ui.Items.ScrollView_Message
  174. uiScrollMessage:removeAllChildren()
  175. uiScrollMessage:hideAllBar();
  176. uiScrollMessage:getInnerContainer():setAutoSize(true)
  177. for msgId, msgInfo in pairsByKeys(self.messageList) do
  178. local item = self:createMessageItem(msgId)
  179. if item then
  180. uiScrollMessage:addChild(item)
  181. end
  182. end
  183. uiScrollMessage:jumpToTopOnSizeChanged()
  184. end
  185. --聊天
  186. function RoomChatView:initChat(messages)
  187. self.ui.Items.ScrollView_Chat:hideAllBar()
  188. self.ui.Items.ScrollView_Chat:getInnerContainer():setAutoSize(true)
  189. for k,v in pairs(messages) do
  190. self:addChatItem(v.nUserId,v.content)
  191. end
  192. end
  193. function RoomChatView:sendChatMessage(chatType, chatIdx)
  194. if not app.user:canSendChatMessage(chatType) then
  195. showTooltip("操作太过频繁,请稍后再发!")
  196. return
  197. end
  198. local nUserSex = app.user.sex==0 and 1 or app.user.sex;
  199. local data =
  200. {
  201. sex = nUserSex,
  202. chatIdx = chatIdx,
  203. }
  204. -- 发送消息到服务器
  205. --1: 表情 2: 语音 3:聊天
  206. local str = json.encode(data)
  207. app.user:sendChatMessage(chatType, str)
  208. self:removeFromParent()
  209. end
  210. function RoomChatView:onClickSend()
  211. if not app.user:canSendChatMessage(ChatDefine.MessageType.Chat) then
  212. showTooltip("您发送信息的间隔时间太短,请稍后再发!")
  213. return
  214. end
  215. local content = self.ui.Items.TextField_Input:getString()
  216. if not content or content=="" then
  217. return
  218. end
  219. self.ui.Items.TextField_Input:setText("")
  220. local nUserSex = app.user.sex==0 and 1 or app.user.sex;
  221. local data =
  222. {
  223. sex = nUserSex,
  224. content = content,
  225. }
  226. -- 发送消息到服务器
  227. --1: 表情 2: 语音 3:聊天
  228. local str = json.encode(data)
  229. app.user:sendChatMessage(ChatDefine.MessageType.Chat, str)
  230. self:removeFromParent()
  231. -- self:onClickCloseFace()
  232. -- self.messageRadio:setDefault(ChatDefine.MessageType.Chat)
  233. end
  234. --聊天响应事件
  235. function RoomChatView:onChatResposeEvent(event)
  236. self:addChatItem(event.nUserId,event.content)
  237. end
  238. --获取聊天记录
  239. function RoomChatView:onGetRecordRespEvent(event)
  240. self:initChat(event.messages)
  241. end
  242. return RoomChatView;