local ChatDefine = require("luaScript.GameChatDefine") local RoomChatView = class("RoomChatView", cc.UIView) --[[ -- 使用方法 function XXRoomView:initChatView() ... local messageList = {} local headInfos = {} self.chatView = import("luaScript.Views.Room.RoomChatView"):new(messageList,faceList) self:addChild(self.chatView) end -- messageList = { chatList = { [1] = {oggStandard = "", oggLocal = "", txt = "各位前辈,我要开车了!"}, [2] = {oggStandard = "", oggLocal = "", txt = "今天牌真是太好了!"}, [3] = {oggStandard = "", oggLocal = "", txt = "快点啦!准备开局!"}, [4] = {oggStandard = "", oggLocal = "", txt = "你们打的好,但是我要自摸了"}, [5] = {oggStandard = "", oggLocal = "", txt = "牌神来了,让座让座!"}, [6] = {oggStandard = "", oggLocal = "", txt = "稍等一下,我拉个朋友"}, [7] = {oggStandard = "", oggLocal = "", txt = "时间很宝贵的,快点出牌吧"}, [8] = {oggStandard = "", oggLocal = "", txt = "辛苦十几年,一把回到解放前!!"}, [9] = {oggStandard = "", oggLocal = "", txt = "等下再来一把!"}, }, --采用plist faceList = { [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}, [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}, [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}, [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}, [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}, }, } -- 玩家头像所在的节点 voiceDir不定义将会根据屏幕的中心判断是左边还是右边 headInfos = { [1] = {headPos = cc.p(0,0), voiceDir = 1}, [2] = {headPos = cc.p(0,0), voiceDir = 1}, [3] = {headPos = cc.p(0,0), voiceDir = 1}, [4] = {headPos = cc.p(0,0), voiceDir = 1}, [5] = {headPos = cc.p(0,0), voiceDir = 1}, [6] = {headPos = cc.p(0,0), voiceDir = 1}, [7] = {headPos = cc.p(0,0), voiceDir = 1}, [8] = {headPos = cc.p(0,0), voiceDir = 1}, } --]] local DEFAULT_INDEX = ChatDefine.MessageType.Message function RoomChatView:ctor(messageList,faceList) RoomChatView.super.ctor(self) self:loadUI() self.messageList = messageList or { [1] = {oggStandard = "", oggLocal = "", txt = "各位前辈,我要开车了!"}, [2] = {oggStandard = "", oggLocal = "", txt = "今天牌真是太好了!"}, [3] = {oggStandard = "", oggLocal = "", txt = "快点啦!准备开局!"}, [4] = {oggStandard = "", oggLocal = "", txt = "你们打的好,但是我要自摸了"}, [5] = {oggStandard = "", oggLocal = "", txt = "牌神来了,让座让座!"}, [6] = {oggStandard = "", oggLocal = "", txt = "稍等一下,我拉个朋友"}, [7] = {oggStandard = "", oggLocal = "", txt = "时间很宝贵的,快点出牌吧"}, [8] = {oggStandard = "", oggLocal = "", txt = "辛苦十几年,一把回到解放前!!"}, [9] = {oggStandard = "", oggLocal = "", txt = "等下再来一把!"}, } self.faceList = faceList or ChatDefine.FaceConfigs end function RoomChatView:loadUI() local ui = loadUI("res/ui/ui_fangjian/ui_fangjian_chat_new.ui") autoAdapt(ui) self.ui = ui self:addChild(ui) end function RoomChatView:onEnter() self.ui.Items.Layout_Face:setVisible(false) self.ui.Items.Layout_Message:setVisible(false) self.ui.Items.Layout_Chat_Other:setVisible(false) self.ui.Items.Layout_Chat_My:setVisible(false) self:initFace() self:initMessage() -- 初始化checkBox的切换 local radio = import("luaScript.Tools.RadioManager"):new() radio:addItem(self.ui.Items.CheckBox_Message, ChatDefine.MessageType.Message) radio:addItem(self.ui.Items.CheckBox_Face, ChatDefine.MessageType.Face) radio:addItem(self.ui.Items.CheckBox_Chat, ChatDefine.MessageType.Chat) radio:setCallback(handler(self, self.onClickCheckBox)) radio:setDefault(DEFAULT_INDEX) self.messageRadio = radio -- 发送按钮 self.ui.Items.Button_Send:registerClick(handler(self , self.onClickSend)) self:bindEvent(app.user, GAME_EVENT.CHAT_PRSPONSE, handler(self, self.onChatResposeEvent)) self:bindEvent(app.user, GAME_EVENT.CHAT_GET_RECORED_RESP, handler(self, self.onGetRecordRespEvent)) app.user:dispatchEvent({name = GAME_EVENT.CHAT_GET_RECORED}) --屏蔽聊天 ljx self.ui.Items.CheckBox_Chat:setVisible(false) self.ui.Items.Button_Send:setVisible(false) self.ui.Items.ImageView_InputBG:setVisible(false) end function RoomChatView:onClickCheckBox(idx) playBtnEffect() self.ui.Items.ScrollView_Message:setVisible(idx == ChatDefine.MessageType.Message) self.ui.Items.ScrollView_Face:setVisible(idx == ChatDefine.MessageType.Face) self.ui.Items.ScrollView_Chat:setVisible(idx == ChatDefine.MessageType.Chat) DEFAULT_INDEX = idx end -- 创建一个表情按钮的节点 function RoomChatView:createFaceItem(idx) local ui = self.ui.Items.Layout_Face:getCopied() ui.Items = getUIItems(ui) local faceConfig = self.faceList[idx]--ChatDefine.FaceConfigs[idx] if faceConfig then local btnPng = faceConfig.btnPng ui.Items.Button:loadTextureNormal("res/ui/zy_tongyong/zy_face/chat/"..btnPng) ui.Items.Button:loadTexturePressed("res/ui/zy_tongyong/zy_face/chat/"..btnPng) ui.Items.Button:loadTextureDisabled("res/ui/zy_tongyong/zy_face/chat/"..btnPng) end ui.Items.Button:registerClick(function() playBtnEffect() self:sendChatMessage(ChatDefine.MessageType.Face, idx); end) return ui; end -- 创建一个快捷语节点 function RoomChatView:createMessageItem(idx) local ui = self.ui.Items.Layout_Message:getCopied() ui.Items = getUIItems(ui) local messageConfig = self.messageList[idx] if messageConfig and messageConfig.txt then ui.Items.Text_Message:setText(messageConfig.txt) end ui:registerClick(function() self:sendChatMessage(ChatDefine.MessageType.Message, idx); end) return ui; end -- 创建一个聊天节点 function RoomChatView:addChatItem(nUserId,message) local userInfo = app.room:getUserInfo(nUserId) local childrens = self.ui.Items.ScrollView_Chat:getChildren() if #childrens>=20 then childrens[1]:removeFromParent() end local str = nUserId==app.user.loginInfo.uid and "My" or "Other" local ui = self.ui.Items["Layout_Chat_"..str]:getCopied() ui.Items = getUIItems(ui) ui.Items["Text_Chat_"..str]:setText(message) local textSize = ui.Items["Text_Chat_"..str]:getContentSize() ui.Items["Layout_Chat_Text_"..str]:setSize(cc.size(textSize.width+35,textSize.height+35)) ui.Items["Layout_Chat_"..str]:setSize(cc.size(ui.Items["Layout_Chat_"..str]:getContentSize().width,textSize.height+35+35)) local height = ui.Items["Layout_Chat_"..str]:getContentSize().height ui.Items["Layout_Head_"..str]:setPositionY(height-20) ui.Items["Layout_Chat_Text_"..str]:setPositionY(height-20) if userInfo and userInfo.headimgurl then setPlayerHeadImage(nUserId,userInfo.headimgurl,ui.Items["ImageView_Head_"..str],true) end self.ui.Items.ScrollView_Chat:addChild(ui) self.ui.Items.ScrollView_Chat:jumpToBottomOnSizeChanged() return ui end --表情 function RoomChatView:initFace() local uiScrollMessage = self.ui.Items.ScrollView_Face uiScrollMessage:removeAllChildren() uiScrollMessage:hideAllBar(); uiScrollMessage:getInnerContainer():setAutoSize(true) for msgId, msgInfo in pairsByKeys(self.faceList) do local item = self:createFaceItem(msgId) if item then uiScrollMessage:addChild(item) end end uiScrollMessage:jumpToTopOnSizeChanged() end --短语 function RoomChatView:initMessage() local uiScrollMessage = self.ui.Items.ScrollView_Message uiScrollMessage:removeAllChildren() uiScrollMessage:hideAllBar(); uiScrollMessage:getInnerContainer():setAutoSize(true) for msgId, msgInfo in pairsByKeys(self.messageList) do local item = self:createMessageItem(msgId) if item then uiScrollMessage:addChild(item) end end uiScrollMessage:jumpToTopOnSizeChanged() end --聊天 function RoomChatView:initChat(messages) self.ui.Items.ScrollView_Chat:hideAllBar() self.ui.Items.ScrollView_Chat:getInnerContainer():setAutoSize(true) for k,v in pairs(messages) do self:addChatItem(v.nUserId,v.content) end end function RoomChatView:sendChatMessage(chatType, chatIdx) if not app.user:canSendChatMessage(chatType) then showTooltip("操作太过频繁,请稍后再发!") return end local nUserSex = app.user.sex==0 and 1 or app.user.sex; local data = { sex = nUserSex, chatIdx = chatIdx, } -- 发送消息到服务器 --1: 表情 2: 语音 3:聊天 local str = json.encode(data) app.user:sendChatMessage(chatType, str) self:removeFromParent() end function RoomChatView:onClickSend() if not app.user:canSendChatMessage(ChatDefine.MessageType.Chat) then showTooltip("您发送信息的间隔时间太短,请稍后再发!") return end local content = self.ui.Items.TextField_Input:getString() if not content or content=="" then return end self.ui.Items.TextField_Input:setText("") local nUserSex = app.user.sex==0 and 1 or app.user.sex; local data = { sex = nUserSex, content = content, } -- 发送消息到服务器 --1: 表情 2: 语音 3:聊天 local str = json.encode(data) app.user:sendChatMessage(ChatDefine.MessageType.Chat, str) self:removeFromParent() -- self:onClickCloseFace() -- self.messageRadio:setDefault(ChatDefine.MessageType.Chat) end --聊天响应事件 function RoomChatView:onChatResposeEvent(event) self:addChatItem(event.nUserId,event.content) end --获取聊天记录 function RoomChatView:onGetRecordRespEvent(event) self:initChat(event.messages) end return RoomChatView;