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.

1512 lines
44 KiB

  1. local ChatDefine = require("luaScript.GameChatDefine")
  2. local ChatView = class("ChatView", cc.UIView)
  3. MESSAGE_RECT = {
  4. [300] = cc.rect(60,57,5,0),
  5. [301] = cc.rect(60,50,0,0),
  6. [302] = cc.rect(70,57,5,0),
  7. [303] = cc.rect(70,55,0,0),
  8. [304] = cc.rect(55,55,0,0),
  9. [305] = cc.rect(60,50,0,0),
  10. }
  11. --[[
  12. -- 使用方法
  13. function XXRoomView:initChatView()
  14. ...
  15. local messageList = {}
  16. local headInfos = {}
  17. self.chatView = import("luaScript.Views.Room.ChatView"):new(messageList, headInfos, showBtn)
  18. self:addChild(self.chatView)
  19. end
  20. --
  21. messageList =
  22. {
  23. chatList = {
  24. [1] = {oggStandard = "", oggLocal = "", txt = "各位前辈,我要开车了!"},
  25. [2] = {oggStandard = "", oggLocal = "", txt = "今天牌真是太好了!"},
  26. [3] = {oggStandard = "", oggLocal = "", txt = "快点啦!准备开局!"},
  27. [4] = {oggStandard = "", oggLocal = "", txt = "你们打的好,但是我要自摸了"},
  28. [5] = {oggStandard = "", oggLocal = "", txt = "牌神来了,让座让座!"},
  29. [6] = {oggStandard = "", oggLocal = "", txt = "稍等一下,我拉个朋友"},
  30. [7] = {oggStandard = "", oggLocal = "", txt = "时间很宝贵的,快点出牌吧"},
  31. [8] = {oggStandard = "", oggLocal = "", txt = "辛苦十几年,一把回到解放前!!"},
  32. [9] = {oggStandard = "", oggLocal = "", txt = "等下再来一把!"},
  33. },
  34. --采用plist
  35. faceList = {
  36. [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},
  37. [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},
  38. [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},
  39. [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},
  40. [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},
  41. },
  42. }
  43. -- 玩家头像所在的节点 voiceDir不定义将会根据屏幕的中心判断是左边还是右边
  44. headInfos =
  45. {
  46. [1] = {headPos = cc.p(0,0), voiceDir = 1},
  47. [2] = {headPos = cc.p(0,0), voiceDir = 1},
  48. [3] = {headPos = cc.p(0,0), voiceDir = 1},
  49. [4] = {headPos = cc.p(0,0), voiceDir = 1},
  50. [5] = {headPos = cc.p(0,0), voiceDir = 1},
  51. [6] = {headPos = cc.p(0,0), voiceDir = 1},
  52. [7] = {headPos = cc.p(0,0), voiceDir = 1},
  53. [8] = {headPos = cc.p(0,0), voiceDir = 1},
  54. }
  55. -- 是否直接显示表情和语音按钮
  56. showBtn : true or false
  57. --]]
  58. function ChatView:ctor(messageList, headInfos, showBtn)
  59. ChatView.super.ctor(self);
  60. self.messageList = {};
  61. self.headInfos = headInfos;
  62. self.showBtn = showBtn;
  63. self.messageList.chatList = messageList.chatList or messageList --兼容老的数据格式
  64. self.messageList.faceList = messageList.faceList or ChatDefine.FaceConfigs
  65. local ui = loadUI("res/ui/ui_fangjian/ui_fangjian_chat.ui");
  66. self.ui = ui;
  67. self:addChild(ui);
  68. self.uiHeads = {}
  69. -- 是否说普通话
  70. self.isStandardLanguage = true
  71. self.recording = false; -- 是否正在录音
  72. --是否关闭点击信息
  73. self.m_isClose = true
  74. --系统的音效状态
  75. self.soundState = app.systemSetting.info.sound
  76. -- 记录每个玩家的最后一条语音
  77. self.playerVoice = {}
  78. --[[
  79. self.playerVoice =
  80. {
  81. [1] = {url = url, filePath = filePath, recordTime = recordTime},
  82. [2] = {url = url, filePath = filePath, recordTime = recordTime},
  83. [3] = {url = url, filePath = filePath, recordTime = recordTime},
  84. [4] = {url = url, filePath = filePath, recordTime = recordTime},
  85. [5] = {url = url, filePath = filePath, recordTime = recordTime},
  86. }
  87. --]]
  88. -- 最后一条音效
  89. self.lastSound = nil
  90. -- 正在说话的椅子号(同一时刻只允许一个玩家说话)
  91. self.lastSpeakingSeatId = false
  92. -- 我的视图椅子号
  93. self.myViewId = 1;
  94. -- 道具播放器
  95. self.propAniPlayer = import("luaScript.Tools.PropAniPlayer"):new()
  96. -- 房间内存在的玩家ID
  97. self.uids = {}
  98. --是否显示道具动画
  99. self.isPropEnabled = true
  100. --是否播放语音
  101. self.isVoiceEnabled = true
  102. self.recordingTime = os.time()
  103. --ID 默认显示,金币场隐藏(不能暴露机器人ID)
  104. self.IDbVisible = true
  105. self.voicepropid = 0
  106. self.messagepropid = 0
  107. if app.php.mypropdata then
  108. for k,v in pairs(app.php.mypropdata) do
  109. if v.prop_id and v.prop_id >= 400 and v.prop_id < 500 then
  110. self.voicepropid = v.prop_id
  111. end
  112. if v.prop_id and v.prop_id >= 300 and v.prop_id < 400 then
  113. self.messagepropid = v.prop_id
  114. end
  115. end
  116. end
  117. end
  118. -- 设置是否说普通话
  119. function ChatView:setLanguage(isStandard)
  120. self.isStandardLanguage = isStandard
  121. end
  122. -- 显示表情和语音按钮
  123. -- 个别游戏在刚进入房间还没坐下时是不允许显示这两个按钮的
  124. function ChatView:showBtns()
  125. self.ui.Items.Button_Face:setVisible(true)
  126. self.ui.Items.Button_Voice:setVisible(true)
  127. end
  128. function ChatView:hideBtns()
  129. self.ui.Items.Button_Face:setVisible(false)
  130. self.ui.Items.Button_Voice:setVisible(false)
  131. end
  132. function ChatView:hideFace()
  133. self.ui.Items.Button_Face:setVisible(false)
  134. end
  135. function ChatView:hideVoice()
  136. self.ui.Items.Button_Voice:setVisible(false)
  137. end
  138. -- 添加一个玩家
  139. function ChatView:addPlayer(uid)
  140. self.uids[uid] = true
  141. end
  142. -- 删除一个玩家
  143. function ChatView:delPlayer(uid)
  144. self.uids[uid] = nil
  145. end
  146. function ChatView:setPlayerUids(uids)
  147. --self.uids = {}
  148. self.uids = uids
  149. if not self.myViewId then
  150. self.myViewId = app.room:getViewIdByUserId(app.user.loginInfo.uid)
  151. end
  152. end
  153. function ChatView:setBtnFace(pos,img)
  154. if pos then
  155. if pos.x < 1 and pos.y < 1 then
  156. self.ui.Items.Button_Face:setPositionPercent(pos)
  157. else
  158. self.ui.Items.Button_Face:setPosition(pos)
  159. end
  160. end
  161. if img then
  162. self.ui.Items.Button_Face:loadTextureNormal(img, cc.TextureResType.plistType)
  163. self.ui.Items.Button_Face:loadTexturePressed(img, cc.TextureResType.plistType)
  164. self.ui.Items.Button_Face:loadTextureDisabled(img, cc.TextureResType.plistType)
  165. end
  166. end
  167. function ChatView:setBtnVoice(pos,img)
  168. if pos then
  169. if pos.x < 1 and pos.y < 1 then
  170. self.ui.Items.Button_Voice:setPositionPercent(pos)
  171. else
  172. self.ui.Items.Button_Voice:setPosition(pos)
  173. end
  174. end
  175. if img then
  176. self.ui.Items.Button_Voice:loadTextureNormal(img, cc.TextureResType.plistType)
  177. self.ui.Items.Button_Voice:loadTexturePressed(img, cc.TextureResType.plistType)
  178. self.ui.Items.Button_Voice:loadTextureDisabled(img, cc.TextureResType.plistType)
  179. end
  180. end
  181. function ChatView:setPropEnabled(enable)
  182. self.isPropEnabled = enable
  183. end
  184. function ChatView:setVoiceEnabled(enable)
  185. self.isVoiceEnabled = enable
  186. end
  187. -- 重新播放某个玩家说过的话
  188. function ChatView:replayRecord(uid)
  189. local nSeatShowId = app.room:getViewIdByUserId(uid)
  190. if nSeatShowId then
  191. self:_replayRecord(nSeatShowId);
  192. end
  193. end
  194. function ChatView:onEnter()
  195. ChatView.super.onEnter(self)
  196. local nMyUserId = app.user.loginInfo.uid
  197. self.myViewId = app.room:getViewIdByUserId(nMyUserId)
  198. -- 初期只加载按钮会用得到的纹理
  199. -- loadSpriteFrameFile(ChatDefine.FaceBtnFile)
  200. -- 点击表情按钮打开表情界面
  201. self.ui.Items.Button_Face:registerClick(handler(self , self.onClickOpenFace))
  202. -- 点击空白区域会关闭表情界面
  203. self.ui.Items.Layout_FaceMenu:registerClick(handler(self , self.onClickCloseFace))
  204. -- 监听服务器下发的聊天事件
  205. self.ui:bindEvent(app.user, "onChatMessageResponse", handler(self, self.onChatMessageResponse))
  206. --绑定change音效
  207. self:bindEvent(app , "onChangeYinXiao" , handler(self , self.onChangeYinXiao));
  208. -- 语音按钮
  209. -- self:initVoiceTouchEvent()
  210. -- self.ui.Items.Button_Voice:registerClick(handler(self , self.stopRecord), handler(self , self.beginRecord), handler(self , self.stopRecord));
  211. self.ui.Items.Button_Voice:registerClick(handler(self , self.stopGRecord), handler(self , self.beginGRecord), handler(self , self.stopGRecord));
  212. -- 发送按钮
  213. self.ui.Items.Button_Send:registerClick(handler(self , self.onClickSend));
  214. -- self.ui.Items.Button_Voice:addTouchEventListener(handler(self, self.onTouchEvent))
  215. -- 录音成功的回调
  216. -- app:dispatchEvent({name = "recordCallback", filePath = info.filePath, recordUrl = info.recordUrl, recordTime = info.recordTime });
  217. self:bindEvent(app , "recordCallback" , handler(self , self.recordCallback));
  218. -- 录制完成回调,Gvoice
  219. self:bindEvent(app , "recordGCallback" , handler(self , self.recordGCallback))
  220. --登录成功后获取到个人的道具id
  221. self:bindEvent(app,"PHP_LGIN_SUCCESSED",handler(self , self.onLoadMyProp))
  222. -- 头像上的语音节点
  223. self.templateHead = self.ui.Items.Layout_Head
  224. self.templateHead:setVisible(false)
  225. -- 快捷语
  226. self.templateMessage = self.ui.Items.Layout_Message;
  227. self.templateMessage:setVisible(false)
  228. -- 表情
  229. self.templateFace = self.ui.Items.Layout_Face;
  230. self.templateFace:setVisible(false)
  231. --聊天
  232. self.templateChatOther = self.ui.Items.Layout_Chat_Other;
  233. self.templateChatOther:setVisible(false)
  234. self.templateChatMy = self.ui.Items.Layout_Chat_My;
  235. self.templateChatMy:setVisible(false)
  236. -- self.ImageView_Line_Face:setVisible(false)
  237. -- 创建每个椅子对应的语音头像节点
  238. local uiLayoutHeads = self.ui.Items.Layout_Heads
  239. for idx, headInfo in ipairs(self.headInfos) do
  240. local uiHead = self:createHeadItem(headInfo.voiceDir, headInfo.headPos)
  241. uiHead:setAnchorPoint(cc.p(0.5, 0.5))
  242. uiHead:setPosition(headInfo.headPos or cc.p(50*idx, 50*idx))
  243. uiLayoutHeads:addChild(uiHead)
  244. local szHead = uiHead:getContentSize()
  245. uiHead:registerClick(function() self:onClickPlayerHead(idx, szHead) end)
  246. self.uiHeads[idx] = uiHead
  247. end
  248. -- 初始化快捷语
  249. local uiScrollMessage = self.ui.Items.ScrollView_Message
  250. uiScrollMessage:removeAllChildren()
  251. uiScrollMessage:hideAllBar();
  252. uiScrollMessage:getInnerContainer():setAutoSize(true)
  253. for msgId, msgInfo in pairsByKeys(self.messageList.chatList) do
  254. local item = self:createMessageItem(msgId)
  255. if item then
  256. uiScrollMessage:addChild(item)
  257. end
  258. end
  259. uiScrollMessage:jumpToTopOnSizeChanged()
  260. -- 初始化聊天表情
  261. local uiScrollFace = self.ui.Items.ScrollView_Face
  262. uiScrollFace:removeAllChildren()
  263. uiScrollFace:hideAllBar();
  264. uiScrollFace:getInnerContainer():setAutoSize(true)
  265. for k,v in pairsByKeys(self.messageList.faceList) do --ChatDefine.FaceConfigs) do
  266. local item = self:createFaceItem(k)
  267. if item then
  268. uiScrollFace:addChild(item)
  269. end
  270. end
  271. uiScrollFace:jumpToTopOnSizeChanged()
  272. -- 初始化聊天
  273. local uiScrollChat = self.ui.Items.ScrollView_Chat
  274. uiScrollChat:removeAllChildren()
  275. uiScrollChat:hideAllBar();
  276. uiScrollChat:getInnerContainer():setAutoSize(true)
  277. -- local texts = {
  278. -- {nUserId=nMyUserId,message="按"},
  279. -- {nUserId=nMyUserId,message="按时"},
  280. -- {nUserId=nMyUserId,message="按时按时按时按时按时按时"},
  281. -- {nUserId=nMyUserId,message="按时按时按时按时按时按时按时按时按时按时按时按时按时按时按时按时按时按时"},
  282. -- }
  283. -- for k,v in pairsByKeys(texts) do --ChatDefine.FaceConfigs) do
  284. -- local item = self:createChatItem(v)
  285. -- if item then
  286. -- uiScrollChat:addChild(item)
  287. -- end
  288. -- end
  289. uiScrollChat:jumpToTopOnSizeChanged()
  290. -- 初始化checkBox的切换
  291. local radio = import("luaScript.Tools.RadioManager"):new()
  292. if radio then
  293. radio:addItem(self.ui.Items.CheckBox_Message, ChatDefine.MessageType.Message)
  294. radio:addItem(self.ui.Items.CheckBox_Face, ChatDefine.MessageType.Face)
  295. radio:addItem(self.ui.Items.CheckBox_Chat, ChatDefine.MessageType.Chat)
  296. radio:setCallback(handler(self, self.onClickCheckBox))
  297. radio:setDefault(ChatDefine.MessageType.Message)
  298. self.messageRadio = radio
  299. end
  300. if self.showBtn then
  301. self:showBtns()
  302. else
  303. self:hideBtns();
  304. end
  305. -- 默认不显示表情选择界面
  306. --self:onClickCloseFace()
  307. self.ui.Items.Layout_FaceMenu:setVisible(false)
  308. -- 不显示录音动画
  309. self:hideSpeaking()
  310. --屏蔽聊天 ljx
  311. self.ui.Items.CheckBox_Chat:setVisible(false)
  312. self.ui.Items.Button_Send:setVisible(false)
  313. self.ui.Items.ImageView_InputBG:setVisible(false)
  314. end
  315. function ChatView:onLoadMyProp()
  316. if not app.php.mypropdata then
  317. return
  318. end
  319. if app.php.mypropdata then
  320. for k,v in pairs(app.php.mypropdata) do
  321. if v.prop_id and v.prop_id >= 400 and v.prop_id < 500 then
  322. self.voicepropid = v.prop_id
  323. end
  324. if v.prop_id and v.prop_id >= 300 and v.prop_id < 400 then
  325. self.messagepropid = v.prop_id
  326. end
  327. end
  328. end
  329. end
  330. function ChatView:onClickCheckBox(idx)
  331. --playBtnEffect()
  332. self.ui.Items.ScrollView_Message:setVisible(idx == ChatDefine.MessageType.Message)
  333. self.ui.Items.ScrollView_Face:setVisible(idx == ChatDefine.MessageType.Face)
  334. self.ui.Items.ScrollView_Chat:setVisible(idx == ChatDefine.MessageType.Chat)
  335. end
  336. function ChatView:createHeadItem(voiceDir, headPos)
  337. local this = self;
  338. local uiHead = self.templateHead:getCopied()
  339. uiHead.Items = getUIItems(uiHead)
  340. uiHead.Items.ImageView_Face:setVisible(false)
  341. local szHead = uiHead:getContentSize()
  342. local uiVoiceFile, posAnchorVoice, posVoice;
  343. local uiMessageFile, posAnchorMessage, posMessage;
  344. local winSize = cc.Director:getInstance():getWinSize()
  345. local isLeft = headPos.x < winSize.width/2
  346. if not voiceDir then
  347. voiceDir = isLeft and ChatDefine.Direction.Left and ChatDefine.Direction.Right
  348. end
  349. if voiceDir == ChatDefine.Direction.Right then
  350. posAnchorVoice = cc.p(0, 0.5)
  351. posVoice = cc.p(szHead.width, szHead.height / 2)
  352. -- posAnchorMessage = cc.p(0, 0)
  353. posMessage = cc.p(szHead.width, 5)--szHead.height / 2)
  354. else
  355. posAnchorVoice = cc.p(1, 0.5)
  356. posVoice = cc.p(0, szHead.height / 2)
  357. -- posAnchorMessage = cc.p(1, 0)
  358. posMessage = cc.p(0, 5)--szHead.height / 2)
  359. end
  360. --播放语音动画
  361. uiHead.playVoice = function( timelen ,recordpid, endCallback)
  362. if uiHead.uiVoice then
  363. uiHead.uiVoice:removeFromParent()
  364. uiHead.uiVoice = nil
  365. end
  366. if voiceDir == ChatDefine.Direction.Right then
  367. if recordpid and recordpid > 0 then
  368. uiVoiceFile = "res/ui/ui_fangjian/ui_fanjian_voice_left_special.ui"
  369. else
  370. uiVoiceFile = "res/ui/ui_fangjian/ui_fangjian_voice_left.ui"
  371. end
  372. else
  373. if recordpid and recordpid > 0 then
  374. uiVoiceFile = "res/ui/ui_fangjian/ui_fanjian_voice_right_special.ui"
  375. else
  376. uiVoiceFile = "res/ui/ui_fangjian/ui_fangjian_voice_right.ui"
  377. end
  378. end
  379. local uiVoice = loadUI(uiVoiceFile);
  380. if recordpid and recordpid > 0 then
  381. uiVoice.Items.ImageView:loadTexture(string.format("res/ui/zy_tongyong/zy_voice/voice/zy_voice_%d.png",recordpid))
  382. end
  383. uiVoice.endCallback = endCallback
  384. uiVoice.Items = getUIItems(uiVoice)
  385. uiVoice.Items.ImageView:playClip("speak")
  386. uiVoice.Items.Text:setText(string.format("%d''", timelen))
  387. uiVoice:setAnchorPoint(posAnchorVoice)
  388. uiVoice:setPosition(posVoice)
  389. uiVoice:runDelay(timelen, function()
  390. if uiHead.uiVoice then
  391. uiHead.uiVoice:removeFromParent()
  392. uiHead.uiVoice = nil
  393. end
  394. if uiVoice.endCallback then
  395. uiVoice.endCallback();
  396. end
  397. end)
  398. uiHead.uiVoice = uiVoice;
  399. uiHead:addChild(uiVoice);
  400. end
  401. -- 停止语音动画
  402. uiHead.shopVoice = function()
  403. if uiHead.uiVoice then
  404. if uiHead.uiVoice.endCallback then
  405. uiHead.uiVoice.endCallback();
  406. end
  407. uiHead.uiVoice.Items.ImageView:stopClip("speak")
  408. uiHead.uiVoice:removeFromParent()
  409. uiHead.uiVoice = nil
  410. end
  411. end
  412. -- 播放一个表情动画
  413. uiHead.playFace = function(idx, sex)
  414. local faceConfig = self.messageList.faceList[idx]
  415. if not faceConfig then
  416. return
  417. end
  418. -- 如果有配语音,还要播放对应的语音
  419. local oggFile;
  420. if this.isStandardLanguage then
  421. oggFile = faceConfig.oggStandard
  422. else
  423. oggFile = faceConfig.oggLocal
  424. end
  425. if oggFile and oggFile ~= "" then
  426. if string.find(oggFile,"%%s") then --如果需要播放男女音效固定2个%s 例如:mj/res/sound/%s/mj_%s_text_7.ogg
  427. oggFile = string.format(oggFile,sex ==1 and "man" or "women",sex)
  428. end
  429. playVoice(oggFile);
  430. end
  431. if not faceConfig.aniFile then
  432. local nodeFaceImage = uiHead.Items.ImageView_Face
  433. nodeFaceImage:loadTexture(faceConfig.btnPng, cc.TextureResType.plistType)
  434. local action = cc.Sequence:create(cc.DelayTime:create(2),cc.CallFunc:create(function ()
  435. nodeFaceImage:setVisible(false)
  436. end))
  437. nodeFaceImage:stopAllActions()
  438. nodeFaceImage:runAction(action)
  439. nodeFaceImage:setVisible(true)
  440. return
  441. end
  442. -- 加载纹理到内存
  443. loadSpriteFrameFile(faceConfig.aniFile)
  444. -- 加载第一张图片
  445. local nodeFaceImage = uiHead.Items.ImageView_Face
  446. local firstFileName = string.format("face%d_%d.png",idx,1)
  447. nodeFaceImage:loadTexture(firstFileName, cc.TextureResType.plistType)
  448. -- 循环修改图片
  449. local everyFrame = 0.1
  450. local indexFace = 0
  451. local indexMax = tonumber(faceConfig.frameNum) or 0
  452. local action1 = cc.Sequence:create(cc.DelayTime:create(everyFrame),cc.CallFunc:create(function ()
  453. indexFace = indexFace + 1
  454. if 0 < indexFace and indexFace <= indexMax then
  455. local name = string.format("face%d_%d.png",idx,indexFace)
  456. if not tolua.isnull(nodeFaceImage) then
  457. nodeFaceImage:loadTexture(name, cc.TextureResType.plistType)
  458. end
  459. else
  460. nodeFaceImage:setVisible(false)
  461. end
  462. end))
  463. local action2 = cc.Repeat:create(action1, indexMax + 2)
  464. nodeFaceImage:stopAllActions()
  465. nodeFaceImage:runAction(action2)
  466. nodeFaceImage:setVisible(true)
  467. end
  468. --播放一个快捷语的动画
  469. uiHead.playMessage = function(idx, sex, messagepid)
  470. local uiMessageNode = uiHead:getChildByTag(10086)
  471. if uiMessageNode then
  472. if uiMessageNode.sound then
  473. stopVoice(uiMessageNode.sound)
  474. end
  475. uiHead:removeChildByTag(10086)
  476. end
  477. local messageConfig = self.messageList.chatList[idx]
  478. if messageConfig then
  479. if voiceDir == ChatDefine.Direction.Right then
  480. if messagepid and messagepid > 0 then
  481. uiMessageFile = "res/ui/ui_fangjian/ui_fanjian_message_left_special.ui"
  482. else
  483. uiMessageFile = "res/ui/ui_fangjian/ui_fangjian_Message_left.ui"
  484. end
  485. else
  486. if messagepid and messagepid > 0 then
  487. uiMessageFile = "res/ui/ui_fangjian/ui_fanjian_message_right_special.ui"
  488. else
  489. uiMessageFile = "res/ui/ui_fangjian/ui_fangjian_Message_right.ui"
  490. end
  491. end
  492. local uiMessage = loadUI(uiMessageFile)
  493. uiMessage.Items = getUIItems(uiMessage)
  494. if messagepid and messagepid > 0 then
  495. uiMessage.Items.ImageView:loadTexture(string.format("res/ui/zy_tongyong/zy_voice/chat/zy_chat_%d.png",messagepid))
  496. uiMessage.Items.ImageView:setCapInsets(MESSAGE_RECT[messagepid]);
  497. end
  498. if messageConfig.txt then
  499. uiMessage.Items.Text_Chat:setText(messageConfig.txt)
  500. end
  501. -- 添加到头像节点上
  502. -- uiMessage:setAnchorPoint(posAnchorMessage)
  503. --uiMessage:setPosition(posMessage)
  504. --添加到头像节点上
  505. uiMessage:setPosition(self:getPlayMessagePos(viewId) or posMessage)
  506. local textSize = uiMessage.Items.Text_Chat:getContentSize()
  507. uiMessage.Items.Layout_Chat:setSize(cc.size(textSize.width+60,textSize.height+44))
  508. if messagepid == 304 then
  509. uiMessage.Items.Text_Chat:setTextColor(cc.c4b(255,255,255,255))
  510. end
  511. if messagepid and messagepid > 0 then
  512. uiMessage.Items.Layout_Chat:setSize(cc.size(textSize.width+94,textSize.height+78))
  513. uiMessage.Items.ImageView:setSize(cc.size(textSize.width+94,textSize.height+78))
  514. end
  515. local y = uiHead:getPositionY() + uiMessage.Items.Layout_Chat:getContentSize().height/2
  516. if y>720 then
  517. local offsetY = y-720;
  518. uiMessage:setPositionY(-offsetY+5)
  519. end
  520. uiHead:addChild(uiMessage);
  521. -- 如果有配语音,还要播放对应的语音
  522. local oggFile;
  523. if this.isStandardLanguage then
  524. oggFile = messageConfig.oggStandard
  525. else
  526. oggFile = messageConfig.oggLocal
  527. end
  528. if oggFile and oggFile ~= "" then
  529. if string.find(oggFile,"%%s") then --如果需要播放男女音效固定2个%s 例如:mj/res/sound/%s/mj_%s_text_7.ogg
  530. oggFile = string.format(oggFile,sex ==1 and "man" or "women",sex)
  531. end
  532. playVoice(oggFile);
  533. end
  534. -- 播放动画,完成后自删
  535. uiMessage.Items.Layout_Chat:playClip("fade", function()
  536. uiMessage:removeFromParent()
  537. end)
  538. end
  539. end
  540. --播放一个聊天的动画
  541. uiHead.playChat = function(message, messagepid)
  542. if voiceDir == ChatDefine.Direction.Right then
  543. if messagepid and messagepid > 0 then
  544. uiMessageFile = "res/ui/ui_fangjian/ui_fanjian_message_left_special.ui"
  545. else
  546. uiMessageFile = "res/ui/ui_fangjian/ui_fangjian_Message_left.ui"
  547. end
  548. else
  549. if messagepid and messagepid > 0 then
  550. uiMessageFile = "res/ui/ui_fangjian/ui_fanjian_message_right_special.ui"
  551. else
  552. uiMessageFile = "res/ui/ui_fangjian/ui_fangjian_Message_right.ui"
  553. end
  554. end
  555. local uiMessage = loadUI(uiMessageFile)
  556. uiMessage.Items = getUIItems(uiMessage)
  557. uiMessage.Items.Text_Chat:setText(message)
  558. if messagepid and messagepid > 0 then
  559. uiMessage.Items.ImageView:loadTexture(string.format("res/ui/zy_tongyong/zy_voice/chat/zy_chat_%d.png",messagepid))
  560. uiMessage.Items.ImageView:setCapInsets(MESSAGE_RECT[messagepid]);
  561. end
  562. if messagepid == 304 then
  563. uiMessage.Items.Text_Chat:setTextColor(cc.c4b(255,255,255,255))
  564. end
  565. -- 添加到头像节点上
  566. -- uiMessage:setAnchorPoint(posAnchorMessage)
  567. --uiMessage:setPosition(posMessage)
  568. --添加到头像节点上
  569. uiMessage:setPosition(self:getPlayMessagePos(viewId) or posMessage)
  570. local textSize = uiMessage.Items.Text_Chat:getContentSize()
  571. uiMessage.Items.Layout_Chat:setSize(cc.size(textSize.width+60,textSize.height+44))
  572. if messagepid and messagepid > 0 then
  573. uiMessage.Items.Layout_Chat:setSize(cc.size(textSize.width+94,textSize.height+78))
  574. uiMessage.Items.ImageView:setSize(cc.size(textSize.width+94,textSize.height+78))
  575. end
  576. local y = uiHead:getPositionY() + uiMessage:getContentSize().height/2
  577. if y>720 then
  578. local offsetY = y-720;
  579. uiMessage:setPositionY(-offsetY)
  580. end
  581. uiHead:addChild(uiMessage);
  582. -- 播放动画,完成后自删
  583. uiMessage.Items.Layout_Chat:playClip("fade", function()
  584. uiMessage:removeFromParent()
  585. end)
  586. end
  587. return uiHead;
  588. end
  589. -- 创建一个表情按钮的节点
  590. function ChatView:createFaceItem(idx)
  591. local ui = self.templateFace:getCopied()
  592. ui.Items = getUIItems(ui)
  593. local faceConfig = self.messageList.faceList[idx]--ChatDefine.FaceConfigs[idx]
  594. if faceConfig then
  595. local btnPng = faceConfig.btnPng
  596. ui.Items.Button:loadTextureNormal("res/ui/zy_tongyong/zy_face/chat/"..btnPng)
  597. ui.Items.Button:loadTexturePressed("res/ui/zy_tongyong/zy_face/chat/"..btnPng)
  598. ui.Items.Button:loadTextureDisabled("res/ui/zy_tongyong/zy_face/chat/"..btnPng)
  599. end
  600. ui.Items.Button:registerClick(function()
  601. playBtnEffect()
  602. self:sendChatMessage(ChatDefine.MessageType.Face, idx);
  603. end)
  604. return ui;
  605. end
  606. -- 创建一个快捷语节点
  607. function ChatView:createMessageItem(idx)
  608. local ui = self.templateMessage:getCopied()
  609. ui.Items = getUIItems(ui)
  610. local messageConfig = self.messageList.chatList[idx]
  611. if messageConfig and messageConfig.txt then
  612. ui.Items.Text_Message:setText(messageConfig.txt)
  613. end
  614. ui:registerClick(function()
  615. self:sendChatMessage(ChatDefine.MessageType.Message, idx);
  616. end)
  617. return ui;
  618. end
  619. -- 创建一个聊天节点
  620. function ChatView:addChatItem(nUserId,message)
  621. local userInfo = app.room:getUserInfo(nUserId)
  622. userInfo = type(userInfo) == "string" and json.decode(userInfo) or userInfo
  623. local childrens = self.ui.Items.ScrollView_Chat:getChildren()
  624. if #childrens>=20 then
  625. childrens[1]:removeFromParent()
  626. end
  627. local str = nUserId==app.user.loginInfo.uid and "My" or "Other"
  628. local ui = self["templateChat"..str]:getCopied()
  629. ui.Items = getUIItems(ui)
  630. ui.Items["Text_Chat_"..str]:setText(message)
  631. local textSize = ui.Items["Text_Chat_"..str]:getContentSize()
  632. ui.Items["Layout_Chat_Text_"..str]:setSize(cc.size(textSize.width+35,textSize.height+35))
  633. ui.Items["Layout_Chat_"..str]:setSize(cc.size(ui.Items["Layout_Chat_"..str]:getContentSize().width,textSize.height+35+35))
  634. local height = ui.Items["Layout_Chat_"..str]:getContentSize().height
  635. ui.Items["Layout_Head_"..str]:setPositionY(height-20)
  636. ui.Items["Layout_Chat_Text_"..str]:setPositionY(height-20)
  637. if userInfo and userInfo.headimgurl then
  638. setPlayerHeadImage(nUserId,userInfo.headimgurl,ui.Items["ImageView_Head_"..str],true)
  639. end
  640. self.ui.Items.ScrollView_Chat:addChild(ui)
  641. self.ui.Items.ScrollView_Chat:jumpToBottomOnSizeChanged()
  642. return ui
  643. end
  644. function ChatView:onClickPlayerHead(idx, szHead)
  645. playBtnEffect()
  646. local nUserId = app.room:getUserIdByViewId(idx)
  647. local userInfo = app.room:getUserInfo(nUserId)
  648. if nUserId and userInfo then
  649. local headPos = self.headInfos[idx].headPos
  650. local recordTime = 0
  651. if self.playerVoice[idx] then
  652. recordTime = self.playerVoice[idx].recordTime or 0
  653. end
  654. local recordCallback = function(uid) self:replayRecord(uid) end
  655. local view = import("luaScript.Views.Room.RoomPlayerInfoView"):new(nUserId, userInfo, headPos, szHead, recordTime, recordCallback,self.IDbVisible)
  656. view:setAnchorPoint(cc.p(0.5, 0.5))
  657. app:showWaitDialog(view, 0, true)
  658. --互斥,点开头像详情后,隐藏一次聊天框
  659. self.ui.Items.Layout_FaceMenu:setVisible(false)
  660. --通知游戏Room界面关闭互斥界面
  661. app:dispatchEvent({name = "onCloseRoomMutexView"});
  662. end
  663. end
  664. --设置隐藏ID
  665. function ChatView:setIDVisible(bVisible)
  666. self.IDbVisible = bVisible;
  667. end
  668. -- 打开表情界面
  669. function ChatView:onClickOpenFace()
  670. playBtnEffect()
  671. self.ui.Items.Layout_FaceMenu:setVisible(not self.ui.Items.Layout_FaceMenu:isVisible())
  672. app:dispatchEvent({name = "onCloseRoomMutexView"});
  673. end
  674. -- 关闭表情界面
  675. function ChatView:onClickCloseFace()
  676. playBtnEffect()
  677. self.ui.Items.Layout_FaceMenu:setVisible(false)
  678. end
  679. -- 播放表情动画
  680. function ChatView:playFace(seatid, faceId,sex)
  681. if self.uiHeads[seatid] then
  682. self.uiHeads[seatid].playFace(faceId,sex)
  683. end
  684. end
  685. --播放快捷语动画
  686. function ChatView:playMessage(seatid, chatIdx, sex, messagepid)
  687. if self.uiHeads[seatid] then
  688. self.uiHeads[seatid].playMessage(chatIdx, sex, messagepid)
  689. end
  690. end
  691. function ChatView:playChat(seatid, message, sex, messagepid)
  692. if self.uiHeads[seatid] then
  693. self.uiHeads[seatid].playChat(message, messagepid)
  694. end
  695. end
  696. --播放语音动画
  697. function ChatView:playVoice(seatid, timelen, recordpid, endCallback)
  698. if self.uiHeads[seatid] then
  699. self.uiHeads[seatid].playVoice(timelen, recordpid, endCallback)
  700. end
  701. end
  702. --
  703. function ChatView:shopVoice(seatid)
  704. if self.uiHeads[seatid] then
  705. self.uiHeads[seatid].shopVoice()
  706. end
  707. end
  708. -- 显示屏幕中间滑动手指取消录音的提示
  709. function ChatView:showSpeaking()
  710. logD("ChatView:showSpeaking")
  711. self.ui.Items.Layout_luyinzhong:setVisible(true)
  712. self.ui.Items.Text_Tips:setText("")
  713. if isAndroidPlatform() then
  714. self.ui.Items.Layout_luyinzhong:playClip("speaking")
  715. elseif isIOSPlatform() then
  716. self.ui.Items.Text_Tips:setText("正在开启录音设备")
  717. self.ui.Items.Layout_3:setVisible(false);
  718. runDelay(1.1, function ()
  719. logD("ChatView:runDelay")
  720. self.ui.Items.Text_Tips:setText("")
  721. self.ui.Items.Layout_3:setVisible(true);
  722. self.ui.Items.Layout_luyinzhong:playClip("speaking")
  723. end)
  724. end
  725. end
  726. -- 隐藏屏幕中间滑动手指取消录音的提示
  727. function ChatView:hideSpeaking()
  728. self.ui.Items.Layout_luyinzhong:setVisible(false)
  729. self.ui.Items.Layout_luyinzhong:stopClip("speaking")
  730. end
  731. -- 开始录音
  732. function ChatView:beginRecord()
  733. local t = os.time() - self.recordingTime
  734. if not app.user:canSendChatMessage(2) or t < ChatDefine.TimeInterval[2] then
  735. showTooltip("您发送语音信息的间隔时间太短,请稍后再发!")
  736. return
  737. end
  738. -- 停止所有的表情声音
  739. if self.lastSound then
  740. stopVoice(self.lastSound)
  741. end
  742. --为了不影响录制的过程,还可以听到音效,关闭所有的音效
  743. app.systemSetting.info.sound = false
  744. --暂停音乐
  745. cc.AudioController:getInstance():pause();
  746. --暂停音效
  747. --cc.AudioController:getInstance():setPlaySound(false)
  748. -- 停止所有的玩家语音
  749. app.plugin:stopPlayRecord()
  750. -- 如果有玩家正在说话
  751. if self.lastSpeakingSeatId then
  752. self:shopVoice(self.lastSpeakingSeatId)
  753. end
  754. self.recording = true;
  755. self.recordingTime = os.time()
  756. --showTooltip("开始录音")
  757. self:showSpeaking()
  758. app.plugin:startRecord()
  759. end
  760. -- 开始录音
  761. function ChatView:beginGRecord()
  762. local t = os.time() - self.recordingTime
  763. if not app.user:canSendChatMessage(2) or t < ChatDefine.TimeInterval[2] then
  764. showTooltip("您发送语音信息的间隔时间太短,请稍后再发!")
  765. return
  766. end
  767. -- 停止所有的表情声音
  768. if self.lastSound then
  769. stopVoice(self.lastSound)
  770. end
  771. --为了不影响录制的过程,还可以听到音效,关闭所有的音效
  772. app.systemSetting.info.sound = false
  773. --暂停音乐
  774. cc.AudioController:getInstance():pause();
  775. --暂停音效
  776. --cc.AudioController:getInstance():setPlaySound(false)
  777. -- 停止所有的玩家语音
  778. app.plugin:stopGPlayRecord()
  779. -- 如果有玩家正在说话
  780. if self.lastSpeakingSeatId then
  781. self:shopVoice(self.lastSpeakingSeatId)
  782. end
  783. self.recording = true;
  784. self.recordingTime = os.time()
  785. --showTooltip("开始录音")
  786. self:showSpeaking()
  787. app.plugin:startGRecord()
  788. end
  789. -- 结束录音
  790. function ChatView:stopRecord()
  791. if not self.recording then
  792. return
  793. end
  794. -- showTooltip("结束录音111")
  795. self.recording = false;
  796. self:hideSpeaking();
  797. app.systemSetting.info.sound = self.soundState
  798. --继续音乐
  799. cc.AudioController:getInstance():resume();
  800. local recordpropid = tonumber(self.voicepropid)
  801. if cc.Application:getInstance():getTargetPlatform() == 0 then
  802. local jsonData =
  803. {
  804. recordUrl = "recordUrl",
  805. recordTime = 2,
  806. recordpropid = recordpropid,
  807. }
  808. local content = json.encode(jsonData)
  809. if app.user:sendChatMessage(2, content) then
  810. -- 记录我发送过的语音
  811. self.playerVoice[self.myViewId] = {}
  812. self.playerVoice[self.myViewId].filePath = ""
  813. self.playerVoice[self.myViewId].recordUrl = jsonData.recordUrl
  814. self.playerVoice[self.myViewId].recordTime = jsonData.recordTime;
  815. self.playerVoice[self.myViewId].recordpropid = jsonData.recordpropid
  816. -- 播放我的语音
  817. self:_replayRecord(self.myViewId)
  818. end
  819. return
  820. end
  821. app.plugin:stopRecord()
  822. end
  823. -- 结束录音
  824. function ChatView:stopGRecord()
  825. if not self.recording then
  826. return
  827. end
  828. -- showTooltip("结束录音111")
  829. self.recording = false;
  830. self:hideSpeaking();
  831. app.systemSetting.info.sound = self.soundState
  832. --继续音乐
  833. cc.AudioController:getInstance():resume();
  834. local recordpropid = tonumber(self.voicepropid)
  835. if cc.Application:getInstance():getTargetPlatform() == 0 then
  836. local jsonData =
  837. {
  838. -- recordUrl = "recordUrl",
  839. recordUrl = "fileId",
  840. recordTime = 2,
  841. -- recordpropid = recordpropid,
  842. }
  843. local content = json.encode(jsonData)
  844. if app.user:sendChatMessage(2, content) then
  845. -- 记录我发送过的语音
  846. self.playerVoice[self.myViewId] = {}
  847. self.playerVoice[self.myViewId].filePath = ""
  848. self.playerVoice[self.myViewId].recordUrl = jsonData.recordUrl
  849. self.playerVoice[self.myViewId].recordTime = jsonData.recordTime;
  850. -- self.playerVoice[self.myViewId].recordpropid = jsonData.recordpropid
  851. -- 播放我的语音
  852. self:_replayGRecord(self.myViewId)
  853. end
  854. return
  855. end
  856. app.plugin:stopGRecord()
  857. end
  858. -- 取消录音
  859. function ChatView:cancelRecord()
  860. if not self.recording then
  861. return
  862. end
  863. --继续音乐
  864. cc.AudioController:getInstance():resume();
  865. --恢复音效
  866. --cc.AudioController:getInstance():setPlaySound(app.systemSetting.info.sound)
  867. app.systemSetting.info.sound = self.soundState
  868. --showTooltip("取消录音")
  869. self.recording = false;
  870. self:hideSpeaking();
  871. app.plugin:cancelRecord()
  872. end
  873. -- 播放回调函数
  874. function ChatView:recordCallback(event)
  875. --恢复音乐
  876. if app.systemSetting.info.music then
  877. cc.AudioController:getInstance():resume();
  878. end
  879. if not event then return end
  880. local filePath, recordUrl, recordTime = event.filePath, event.recordUrl, event.recordTime
  881. log("log_event", filePath, recordUrl, recordTime)
  882. if not filePath or not recordUrl or not recordTime then
  883. return
  884. end
  885. recordTime = math.ceil(recordTime / 1000);
  886. local recordpropid = tonumber(self.voicepropid)
  887. --腾讯语音GVoice 开启录音设备大概有1秒延迟
  888. if isIOSPlatform() then
  889. recordTime = recordTime - 1;
  890. end
  891. local jsonData =
  892. {
  893. recordUrl = recordUrl,
  894. recordTime = recordTime,
  895. recordpropid = recordpropid,
  896. }
  897. if recordTime<=1 then
  898. showTooltip("录音时间过短!")
  899. return
  900. end
  901. local config=getSubGameConfig(tonumber(app.gameId))
  902. if config and config.belongType==2 then
  903. local nMyUserId = app.user.loginInfo.uid
  904. self.myViewId = app.room:getViewIdByUserId(nMyUserId)
  905. end
  906. local content = json.encode(jsonData);
  907. if app.user:sendChatMessage(2, content) then
  908. -- 记录我发送过的语音
  909. self.playerVoice[self.myViewId] = {}
  910. self.playerVoice[self.myViewId].filePath = filePath;
  911. self.playerVoice[self.myViewId].recordUrl = recordUrl;
  912. self.playerVoice[self.myViewId].recordTime = recordTime;
  913. self.playerVoice[self.myViewId].recordpropid = jsonData.recordpropid
  914. -- 播放我的语音
  915. self:_replayRecord(self.myViewId)
  916. end
  917. end
  918. -- 播放回调函数
  919. function ChatView:recordGCallback(event)
  920. --恢复音乐
  921. if app.systemSetting.info.music then
  922. cc.AudioController:getInstance():resume();
  923. end
  924. if not event then return end
  925. local fileId, filePath, recordTime = event.fileId, event.filePath, event.recordTime
  926. log("log_event", fileId, filePath, recordTime)
  927. if not fileId or not filePath or not recordTime then
  928. return
  929. end
  930. recordTime = math.ceil(recordTime / 1);
  931. -- local recordpropid = tonumber(self.voicepropid)
  932. local jsonData =
  933. {
  934. recordUrl = fileId,
  935. recordTime = recordTime,
  936. }
  937. if recordTime<=1 then
  938. showTooltip("录音时间过短!")
  939. return
  940. end
  941. local config=getSubGameConfig(tonumber(app.gameId))
  942. if config and config.belongType==2 then
  943. local nMyUserId = app.user.loginInfo.uid
  944. self.myViewId = app.room:getViewIdByUserId(nMyUserId)
  945. end
  946. local content = json.encode(jsonData);
  947. if app.user:sendChatMessage(2, content) then
  948. -- 记录我发送过的语音
  949. self.playerVoice[self.myViewId] = {}
  950. self.playerVoice[self.myViewId].filePath = filePath;
  951. self.playerVoice[self.myViewId].recordUrl = fileId;
  952. self.playerVoice[self.myViewId].recordTime = recordTime;
  953. -- self.playerVoice[self.myViewId].recordpropid = jsonData.recordpropid
  954. -- 播放我的语音
  955. self:_replayGRecord(self.myViewId)
  956. end
  957. end
  958. -- 重新播放某个玩家之前说的话,Gvoice
  959. function ChatView:_replayGRecord(nSeatShowId)
  960. log("ChatView:_replayRecord() nSeatShowId = ", nSeatShowId);
  961. log("ChatView:_replayRecord() self.playerVoice = ", table.tostring(self.playerVoice));
  962. local data = self.playerVoice[nSeatShowId]
  963. if data then
  964. if data.filePath then
  965. app.plugin:playGRecord(data.filePath)
  966. end
  967. local recordTime = data.recordTime
  968. local recordpid = data.recordpropid
  969. self.lastSpeakingSeatId = nSeatShowId
  970. self:playVoice(nSeatShowId, recordTime, recordpid, function()
  971. self.lastSpeakingSeatId = nil
  972. end)
  973. else
  974. showTooltip("无历史消息")
  975. end
  976. end
  977. -- 重新播放某个玩家之前说的话
  978. function ChatView:_replayRecord(nSeatShowId)
  979. log("ChatView:_replayRecord() nSeatShowId = ", nSeatShowId);
  980. log("ChatView:_replayRecord() self.playerVoice = ", table.tostring(self.playerVoice));
  981. local data = self.playerVoice[nSeatShowId]
  982. if data then
  983. if data.filePath then
  984. app.plugin:playRecord(data.filePath)
  985. end
  986. local recordTime = data.recordTime
  987. local recordpid = data.recordpropid
  988. self.lastSpeakingSeatId = nSeatShowId
  989. self:playVoice(nSeatShowId, recordTime, recordpid, function()
  990. self.lastSpeakingSeatId = nil
  991. end)
  992. else
  993. showTooltip("无历史消息")
  994. end
  995. end
  996. function ChatView:sendChatMessage(chatType, chatIdx)
  997. if not app.user:canSendChatMessage(chatType) then
  998. showTooltip("操作太过频繁,请稍后再发!")
  999. return
  1000. end
  1001. local nUserSex = app.user.sex==0 and 1 or app.user.sex;
  1002. local data =
  1003. {
  1004. sex = nUserSex,
  1005. chatIdx = chatIdx,
  1006. messagepid = self.messagepropid,
  1007. }
  1008. -- 发送消息到服务器
  1009. --1: 表情 2: 语音 3:聊天
  1010. local str = json.encode(data)
  1011. app.user:sendChatMessage(chatType, str);
  1012. self:onClickCloseFace()
  1013. end
  1014. function ChatView:onChatMessageResponse(event)
  1015. if not event or not event.response then
  1016. return
  1017. end
  1018. --[[
  1019. -- 聊天内容
  1020. ChatMessageResponse = defClass("ChatMessageResponse"
  1021. -- 用户uid
  1022. , defVar("nUserId", VT_Int, 0)
  1023. -- //类型 1: 表情 2: 语音 3:聊天
  1024. , defVar("type", VT_Short, 0)
  1025. -- //内容,客户端自定义格式
  1026. , defVar("content", VT_String, "")
  1027. )
  1028. --]]
  1029. local nUserId = event.response.nUserId
  1030. local chatType = event.response.type
  1031. local content = event.response.content
  1032. if not nUserId or not chatType or not content then
  1033. return
  1034. end
  1035. local nSeatShowId = app.room:getViewIdByUserId(nUserId);
  1036. local data = json.decode(content)
  1037. if not data then
  1038. return
  1039. end
  1040. -- //类型 1: 表情 2: 语音 3:聊天,4:道具 5:聊天
  1041. if chatType == 1 then
  1042. -- 表情
  1043. local sex = data.sex
  1044. local chatIdx = data.chatIdx
  1045. self:playFace(nSeatShowId, chatIdx,sex);
  1046. elseif chatType == 2 then
  1047. if nUserId == app.user.loginInfo.uid or not self.isVoiceEnabled then
  1048. -- 如果是我自己发送的录音,在发送之前就已经在播放了,现在不需要再播放一次 或者屏蔽播放语音
  1049. return
  1050. end
  1051. -- -- 语音
  1052. -- local jsonData = json.decode(event.response.content)
  1053. -- local recordUrl = tostring(jsonData.recordUrl)
  1054. -- local recordTime = tonumber(jsonData.recordTime)
  1055. -- local recordpid = tonumber(jsonData.recordpropid)
  1056. -- -- -- 下载玩家的语音文件并播放
  1057. -- -- -- 下载完成后返回该文件在本地存储的路径
  1058. -- -- app.plugin:downloadRecord(nUserId, recordUrl, function(recordTag, filePath)
  1059. -- -- local userId = tonumber(recordTag)
  1060. -- -- local seatShowId = app.room:getViewIdByUserId(userId)
  1061. -- -- if not seatShowId or not self.playerVoice[seatShowId] then
  1062. -- -- print("LHQChatView:onChatMessageResponse() seatShowId = ", seatShowId);
  1063. -- -- return
  1064. -- -- end
  1065. -- -- self.playerVoice[seatShowId].filePath = filePath
  1066. -- -- end);
  1067. -- -- 腾讯语音GVoice
  1068. -- app.plugin:downloadRecord(nUserId, recordUrl, function(recordTag, filePath)
  1069. -- local userId = tonumber(recordTag)
  1070. -- local seatShowId = app.room:getViewIdByUserId(userId)
  1071. -- if not seatShowId or not self.playerVoice[seatShowId] then
  1072. -- print("LHQChatView:onChatMessageResponse() seatShowId = ", seatShowId);
  1073. -- return
  1074. -- end
  1075. -- self.playerVoice[seatShowId].filePath = filePath
  1076. -- end);
  1077. -- -- 记录该玩家的语音文件,重播要用
  1078. -- self.playerVoice[nSeatShowId] = {}
  1079. -- self.playerVoice[nSeatShowId].recordUrl = recordUrl;
  1080. -- self.playerVoice[nSeatShowId].recordTime = recordTime;
  1081. -- self.playerVoice[nSeatShowId].recordpropid = recordpid;
  1082. -- -- 该玩家头像上播放说话的效果
  1083. -- self:_replayRecord(nSeatShowId)
  1084. -- Gvoice begin
  1085. -- 语音
  1086. local jsonData = json.decode(event.response.content)
  1087. local recordUrl = tostring(jsonData.recordUrl)
  1088. local recordTime = tonumber(jsonData.recordTime)
  1089. local recordpid = tonumber(jsonData.recordpropid)
  1090. -- 腾讯语音GVoice
  1091. app.plugin:downloadGRecord(nUserId, recordUrl, function( filePath)
  1092. -- local userId = tonumber(recordTag)
  1093. local userId = nUserId;
  1094. local seatShowId = app.room:getViewIdByUserId(userId)
  1095. if not seatShowId or not self.playerVoice[seatShowId] then
  1096. print("LHQChatView:onChatMessageResponse() seatShowId = ", seatShowId);
  1097. return
  1098. end
  1099. self.playerVoice[seatShowId].filePath = filePath
  1100. end);
  1101. -- 记录该玩家的语音文件,重播要用
  1102. self.playerVoice[nSeatShowId] = {}
  1103. self.playerVoice[nSeatShowId].recordUrl = recordUrl;
  1104. self.playerVoice[nSeatShowId].recordTime = recordTime;
  1105. self.playerVoice[nSeatShowId].recordpropid = recordpid;
  1106. -- 该玩家头像上播放说话的效果
  1107. self:_replayRecord(nSeatShowId)
  1108. -- Gvoice end
  1109. elseif chatType == 3 then
  1110. -- 聊天
  1111. local sex = data.sex
  1112. local chatIdx = data.chatIdx
  1113. local messagepid = data.messagepid
  1114. self:playMessage(nSeatShowId, chatIdx, sex ,messagepid);
  1115. elseif chatType == 4 then
  1116. if not self.isPropEnabled then
  1117. return
  1118. end
  1119. -- 道具
  1120. --[[
  1121. local data =
  1122. {
  1123. propIdx = propIdx,
  1124. uidSender = uidSender,
  1125. uidReceiver = uidReceiver,
  1126. }
  1127. --]]
  1128. local propId = data.propIdx
  1129. local uidSender = data.uidSender
  1130. local uidReceiver = data.uidReceiver
  1131. local nViewIdBegin = app.room:getViewIdByUserId(uidSender)
  1132. local nViewIdEnd = app.room:getViewIdByUserId(uidReceiver)
  1133. if not nViewIdBegin or not nViewIdEnd or not propId then
  1134. return
  1135. end
  1136. local posBegin = self.headInfos[nViewIdBegin].headPos
  1137. -- 对自己 == 群发
  1138. if uidSender == uidReceiver then
  1139. local isHave = false
  1140. -- 首先要搞清楚,房间里面哪些座位是有人的
  1141. local viewIds = {}
  1142. for uid,_ in pairs(self.uids) do
  1143. local viewId = app.room:getViewIdByUserId(uid)
  1144. table.insert(viewIds, viewId)
  1145. end
  1146. for _, viewId in pairs(viewIds) do
  1147. if viewId ~= nViewIdBegin then
  1148. local headInfo = self.headInfos[viewId]
  1149. if headInfo then
  1150. isHave = true
  1151. local posEnd = self.headInfos[viewId].headPos
  1152. self.propAniPlayer:playAnimationWithPos(posBegin, posEnd, propId)
  1153. end
  1154. end
  1155. end
  1156. if not isHave then
  1157. showTooltip("当前没有其他玩家!")
  1158. end
  1159. else
  1160. if self.propAniPlayer then
  1161. local posEnd = self.headInfos[nViewIdEnd].headPos
  1162. self.propAniPlayer:playAnimationWithPos(posBegin, posEnd, propId)
  1163. end
  1164. end
  1165. elseif chatType == 5 then
  1166. local sex = data.sex
  1167. local content = data.content
  1168. local messagepid = data.messagepid
  1169. self:addChatItem(nUserId,content)
  1170. self:playChat(nSeatShowId, content, sex, messagepid)
  1171. else
  1172. showTooltip("未知的聊天消息 "..tostring(chatType))
  1173. end
  1174. end
  1175. function ChatView:initVoiceTouchEvent()
  1176. local initPos=cc.p(0,0)
  1177. local isCancel = false
  1178. local function onTouchEvent(sender, eventType, touch)
  1179. logD("ChatView:eventType:"..eventType)
  1180. if eventType == cc.TouchEventType.began then
  1181. logD("ChatView:began")
  1182. isCancel=false
  1183. initPos=touch:getLocation()
  1184. self:beginRecord()
  1185. self:playClip("anxia")
  1186. -- 点击结束的回调
  1187. elseif eventType == cc.TouchEventType.ended then
  1188. logD("ChatView:ended")
  1189. -- local currentView = app.currentView
  1190. -- local buttonName = self:getName()
  1191. -- local currentViewName = "";
  1192. -- if(currentView)then
  1193. -- currentViewName = currentView.__cname
  1194. -- end
  1195. -- if(callbackFunc)then
  1196. -- callbackFunc(self, eventType, touch);
  1197. -- end
  1198. dump(isCancel)
  1199. if not isCancel then
  1200. self:stopRecord()
  1201. end
  1202. self:playClip("huanyuan")
  1203. -- 点击取消的回调
  1204. elseif eventType == cc.TouchEventType.moved then
  1205. -- logD("ChatView:moved")
  1206. -- local curPos=touch:getLocation()
  1207. -- -- local delta=touch:getDelta()
  1208. -- if math.abs(curPos.x-initPos.x)>20 or math.abs(curPos.y-initPos.y)>20 then
  1209. -- isCancel=true
  1210. -- self:cancelRecord()
  1211. -- end
  1212. elseif eventType == cc.TouchEventType.canceled then
  1213. logD("ChatView:canceled")
  1214. -- if(clickCancelFunc)then
  1215. -- clickCancelFunc(self);
  1216. -- end
  1217. isCancel=true
  1218. self:stopRecord()
  1219. self:playClip("huanyuan")
  1220. end
  1221. return true;
  1222. end
  1223. self.ui.Items.Button_Voice:addTouchEventListener(onTouchEvent)
  1224. end
  1225. function ChatView:onChangeYinXiao(data)
  1226. self.soundState = app.systemSetting.info.sound
  1227. end
  1228. function ChatView:onClickSend()
  1229. if not app.user:canSendChatMessage(ChatDefine.MessageType.Chat) then
  1230. showTooltip("您发送信息的间隔时间太短,请稍后再发!")
  1231. return
  1232. end
  1233. local content = self.ui.Items.TextField_Input:getString()
  1234. if not content or content=="" then
  1235. return
  1236. end
  1237. self.ui.Items.TextField_Input:setText("")
  1238. local nUserSex = app.user.sex==0 and 1 or app.user.sex;
  1239. local data =
  1240. {
  1241. sex = nUserSex,
  1242. content = content,
  1243. messagepid = self.messagepropid,
  1244. }
  1245. -- 发送消息到服务器
  1246. --1: 表情 2: 语音 3:聊天
  1247. local str = json.encode(data)
  1248. app.user:sendChatMessage(ChatDefine.MessageType.Chat, str)
  1249. self:onClickCloseFace()
  1250. -- self.messageRadio:setDefault(ChatDefine.MessageType.Chat)
  1251. end
  1252. --- 显示/隐藏常用聊天语按钮
  1253. function ChatView:setBtnFaceVisible(isVisible)
  1254. self.ui.Items.Button_Face:setVisible(isVisible)
  1255. end
  1256. --- 显示/隐藏语音按钮
  1257. function ChatView:setBtnVoiceVisible(isVisible)
  1258. self.ui.Items.Button_Voice:setVisible(isVisible)
  1259. end
  1260. function ChatView:setPlayMessagePos(pos)
  1261. self.playMessagePos = pos
  1262. end
  1263. function ChatView:getPlayMessagePos(idx)
  1264. if self.playMessagePos then
  1265. return self.playMessagePos[idx]
  1266. end
  1267. return nil
  1268. end
  1269. return ChatView;