using System; using System.Collections.Generic; using UnityEngine; using AX.MessageSystem; using UnityEngine.UI; using SuperScrollView; using System.IO; public class PrivateChatPanel : UIView { //用户信息scrollList public LoopListView2 UserInfoList; ////查询输入框 //public InputField SearchInput; //文本发送按钮 public Button TextEnterButton; //语音发送按钮 public GameObject AudioEnterButton; //私聊输入框 public InputField ChatInputField; //聊天无限循环 public LoopListView2 LoopListView; //任务图标循环List public LoopListView2 TaskLoopListView; // public Button LeftButton; public Button RightButton; private ClickEventListener listener; //录音机 private AudioRecorder recorder; //文件名称 string fileName; //文件路径 string filePath; //移动索引 int moveIndex = 0; //2018-06-26 //私聊用户列表 public List UsersInfo = new List(); //当前激活的频道 private ChatChannel currentChannel = null; //关闭按钮 public CommonButton ButtonClose; // 录音时间 private TimeSpan time; private DateTime startTime; public UserData CurrentPrivateChatUserData;//当前私聊聊天窗口聊天对象的的用户信息 public override UIViewType ViewType { get { return UIViewType.Normal; } } // Use this for initialization void Awake() { recorder = AudioEnterButton.GetComponent(); //用户信息窗口 UserInfoList.InitListView(UsersInfo.Count, UpdateUserItem); //初始化聊天信息窗口 LoopListView.InitListView(0, OnGetItemByIndex); //指令发送界面 TaskLoopListView.InitListView(ChatManager.Instance.TaskData.Count, OnGetTaskItemByIndex); //监听文本发送信息按钮点击 TextEnterButton.onClick.AddListener(TextEnterButtonOnClick); //监听语音消息信息按钮点击 listener = ClickEventListener.Get(AudioEnterButton); listener.SetPointerDownHandler(OnAudioEnterButtonDown); listener.SetPointerUpHandler(OnAudioEnterButonUp); // LeftButton.onClick.AddListener(LeftButtonClick); RightButton.onClick.AddListener(RightButtonClick); //关闭按钮事件 ButtonClose.OnClicked += ButtonClose_OnClicked; } public override void OnDestroy() { base.OnDestroy(); //关闭按钮事件 ButtonClose.OnClicked -= ButtonClose_OnClicked; } private void ButtonClose_OnClicked(CommonButton obj) { Hide(); } private void RightButtonClick() { moveIndex = Mathf.Clamp(++moveIndex, 0, 15); TaskLoopListView.MovePanelToItemIndex(moveIndex, 0); } private void LeftButtonClick() { moveIndex = Mathf.Clamp(--moveIndex, 0, 15); TaskLoopListView.MovePanelToItemIndex(moveIndex, 0); } /// /// 更新指令 /// /// /// /// private LoopListViewItem2 OnGetTaskItemByIndex(LoopListView2 listView, int index) { LoopListViewItem2 item = listView.NewListViewItem("TaskItem"); TaskModel taskModel = ChatManager.Instance.GetTaskById(index); TaskItemView itemView = item.GetComponent(); if (item.IsInitHandlerCalled == false) { item.IsInitHandlerCalled = true; itemView.Init(); } itemView.SetData(taskModel, index); return item; } /// /// 保存音频 /// /// private void OnRecorderStopped(ArraySegment segment) { fileName = string.Format("{0}.opus", Guid.NewGuid().ToString("N")); string userPath = ChatManager.Instance.UserPath; if (!Directory.Exists(userPath)) { Directory.CreateDirectory(userPath); } filePath = Path.Combine(userPath, fileName); using (var filestream = new FileStream(filePath, FileMode.CreateNew)) { filestream.Write(segment.Array, segment.Offset, segment.Count); } //if (currentChannel != null) //{ ////提交语音信息 //ChatMsg msg = new ChatMsg(); //msg.SenderId = CurrentUserInfo.mySelf.Id; //msg.MsgType = ChatMsgType.Audio; //msg.ReplayFrameCount = InputManager.frameCount; //msg.Channel = currentChannel; //msg.FileName = fileName; //msg.FilePath = filePath; //msg.BattleId = 0; // 结束记录语音时间 time = DateTime.Now - startTime; int timeSpan = Mathf.RoundToInt((float)(time.TotalSeconds)); timeSpan = Mathf.Clamp(timeSpan, 0, 60); ChatAudioMessage textMsg = new ChatAudioMessage(); textMsg.Channel = currentChannel; textMsg.FileName = fileName; textMsg.SenderId = CurrentUserInfo.mySelf.Id; textMsg.ReplayFrameCount = InputManager.frameCount; textMsg.BattleId = 0; textMsg.ChatGroup = ChatType.PrivateChat; textMsg.TimeSpanSeconds = timeSpan; ChatManager.Instance.SendPrivateChatAudioMsg(textMsg, filePath); // 输入框获取焦点 ChatInputField.ActivateInputField(); //} } /// /// 停止录制 /// /// private void OnAudioEnterButonUp(GameObject obj) { if (currentChannel != null) { recorder.StopRecorder(); ////提交语音信息 //ChatMsg msg = new ChatMsg(); //msg.SenderId = CurrentUserInfo.mySelf.Id; //msg.MsgType = ChatMsgType.Audio; //msg.ReplayFrameCount = InputManager.frameCount; //msg.Channel = currentChannel; //msg.FileName = fileName; //msg.FilePath = filePath; //msg.BattleId = 0; //ChatManager.Instance.SendPrivateChatAudioMsg(msg); } } /// /// 开始录制 /// /// private void OnAudioEnterButtonDown(GameObject obj) { if (currentChannel != null) { if (recorder.Ready) { startTime = DateTime.Now; // 开始记录语音事件 recorder.StartRecorder(); } else { LoadPromptWin.Instance.LoadTextPromptWindow("没有可用设备", 1f); } } } private LoopListViewItem2 UpdateUserItem(LoopListView2 listView, int index) { UserInfoItemViewModel itemData = UsersInfo[index]; itemData.Index = index; LoopListViewItem2 item = listView.NewListViewItem("UserInfoItem"); UserInfoItemView itemScript = item.GetComponent(); if (item.IsInitHandlerCalled == false) { item.IsInitHandlerCalled = true; itemScript.Init(this); } itemScript.SetItemData(itemData); return item; } /// /// 文本框按钮 /// private void TextEnterButtonOnClick() { // 当前频道为空,返回 if (currentChannel == null) { return; } // 获取输入信息 string inputContent = ChatInputField.text; // 文本输入 if (!string.IsNullOrEmpty(inputContent) && ChatManager.Instance.SelectTaskIndex == -1) { Debug.Log("发送文本消息"); //服务器发送消息 ChatManager.Instance.SendPrivateChatStrMsg(inputContent, currentChannel); } // 指令输入 if (ChatManager.Instance.SelectTaskIndex != -1) { Debug.Log("发送指令消息:" + ChatManager.Instance.GetSelectedTaskName()); ChatManager.Instance.SendPrivateChatTaskMsg(inputContent, currentChannel, GetSelectUserData()); } // 清空输入内容 ChatInputField.text = ""; // 输入框获取焦点 ChatInputField.ActivateInputField(); } /// /// 刷新LoopList /// public void RefreshChatLoopList() { if (currentChannel != null) { // 更新前数据 int nextItemIndex = LoopListView.ItemTotalCount; //增加新条目刷新UI LoopListView.SetListItemCount(ChatManager.Instance.Msgs[currentChannel.ID].Count, false); if (nextItemIndex < ChatManager.Instance.Msgs[currentChannel.ID].Count) { //移动进度条到前一次数据底部 LoopListView.MovePanelToItemIndex(nextItemIndex, 0); } } } public void RefreshUserInfoLoopList() { if (UsersInfo != null) { UserInfoList.SetListItemCount(UsersInfo.Count, false); //移动进度条到底部,偏移0 UserInfoList.MovePanelToItemIndex(UsersInfo.Count, 0); } } /// /// 更新Item /// /// /// /// private LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index) { // 获取数据 ChatMessage itemData = ChatManager.Instance.Msgs[currentChannel.ID][index]; if (itemData == null) { return null; } LoopListViewItem2 item = null; if (itemData.SenderId != CurrentUserInfo.mySelf.Id) { // 产品要求左边蓝色显示其他人,右边绿色显示自己人 // 其他人发送的消息UI if (itemData is ChatTextMessage) { item = listView.NewListViewItem("ChatTextItem_Private_Self"); ChatTextItem_Private itemScript = item.GetComponent(); if (item.IsInitHandlerCalled == false) { item.IsInitHandlerCalled = true; itemScript.Init(); } itemScript.SetData(itemData as ChatTextMessage, index); } else if (itemData is ChatAudioMessage) { item = listView.NewListViewItem("ChatAudioItem_Private_Self"); ChatAudioItem_Private itemScript = item.GetComponent(); if (item.IsInitHandlerCalled == false) { item.IsInitHandlerCalled = true; itemScript.Init(); } itemScript.SetData(itemData as ChatAudioMessage, index); } else if (itemData is ChatTaskMessage) { item = listView.NewListViewItem("ChatTaskItem_Private_Self"); ChatTaskItem_Private itemScript = item.GetComponent(); if (item.IsInitHandlerCalled == false) { item.IsInitHandlerCalled = true; itemScript.Init(); } itemScript.SetData(itemData as ChatTaskMessage, index); } } else { // 自己发送的消息UI if (itemData is ChatTextMessage) { item = listView.NewListViewItem("ChatTextItem_Private_Other"); ChatTextItem_Private itemScript = item.GetComponent(); if (item.IsInitHandlerCalled == false) { item.IsInitHandlerCalled = true; itemScript.Init(); } itemScript.SetData(itemData as ChatTextMessage, index); } else if (itemData is ChatAudioMessage) { item = listView.NewListViewItem("ChatAudioItem_Private_Other"); ChatAudioItem_Private itemScript = item.GetComponent(); if (item.IsInitHandlerCalled == false) { item.IsInitHandlerCalled = true; itemScript.Init(); } itemScript.SetData(itemData as ChatAudioMessage, index); } else if (itemData is ChatTaskMessage) { item = listView.NewListViewItem("ChatTaskItem_Private_Other"); ChatTaskItem_Private itemScript = item.GetComponent(); if (item.IsInitHandlerCalled == false) { item.IsInitHandlerCalled = true; itemScript.Init(); } itemScript.SetData(itemData as ChatTaskMessage, index); } } return item; } private void OnEnable() { MessageDispatcher.AddListener(MessageName.RefreshChatContentLoopList.ToString(), RefreshChatContentLoopList); MessageDispatcher.AddListener(MessageName.RefreshUserInfoLoopList.ToString(), RefreshUserInfoLoopList); recorder.Stopped += OnRecorderStopped; } private void OnDisable() { MessageDispatcher.RemoveListener(MessageName.RefreshChatContentLoopList.ToString(), RefreshChatContentLoopList); MessageDispatcher.RemoveListener(MessageName.RefreshUserInfoLoopList.ToString(), RefreshUserInfoLoopList); recorder.Stopped -= OnRecorderStopped; } private void RefreshChatContentLoopList(IMessage obj) { RefreshChatLoopList(); } private void RefreshUserInfoLoopList(IMessage obj) { RefreshUserInfoLoopList(); } //设置当前显示的频道 public void SetCurrentChannel(long channelId) { if (channelId == -1) { currentChannel = null; } else { currentChannel = new ChatChannel() { ID = channelId, RoomID = CurrentUserInfo.room.Id }; } } public override void Refresh() { base.Refresh(); RefreshUserInfoLoopList(); RefreshChatLoopList(); } public UserData GetSelectUserData() { UserData data = new UserData(); UserInfoItemView[] userInfoItems = GetComponentsInChildren(); for (int i = 0; i < userInfoItems.Length; i++) { if (userInfoItems[i].GetComponent().isOn) { data = userInfoItems[i].GetItemUserData(); break; } } return data; } }