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.
440 lines
15 KiB
440 lines
15 KiB
4 years ago
|
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<UserInfoItemViewModel> UsersInfo = new List<UserInfoItemViewModel>();
|
||
|
//当前激活的频道
|
||
|
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<AudioRecorder>();
|
||
|
|
||
|
//用户信息窗口
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 更新指令
|
||
|
/// </summary>
|
||
|
/// <param name="arg1"></param>
|
||
|
/// <param name="arg2"></param>
|
||
|
/// <returns></returns>
|
||
|
private LoopListViewItem2 OnGetTaskItemByIndex(LoopListView2 listView, int index)
|
||
|
{
|
||
|
LoopListViewItem2 item = listView.NewListViewItem("TaskItem");
|
||
|
TaskModel taskModel = ChatManager.Instance.GetTaskById(index);
|
||
|
TaskItemView itemView = item.GetComponent<TaskItemView>();
|
||
|
if (item.IsInitHandlerCalled == false)
|
||
|
{
|
||
|
item.IsInitHandlerCalled = true;
|
||
|
itemView.Init();
|
||
|
}
|
||
|
itemView.SetData(taskModel, index);
|
||
|
return item;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 保存音频
|
||
|
/// </summary>
|
||
|
/// <param name="segment"></param>
|
||
|
private void OnRecorderStopped(ArraySegment<byte> 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();
|
||
|
//}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 停止录制
|
||
|
/// </summary>
|
||
|
/// <param name="obj"></param>
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 开始录制
|
||
|
/// </summary>
|
||
|
/// <param name="obj"></param>
|
||
|
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<UserInfoItemView>();
|
||
|
if (item.IsInitHandlerCalled == false)
|
||
|
{
|
||
|
item.IsInitHandlerCalled = true;
|
||
|
itemScript.Init(this);
|
||
|
}
|
||
|
itemScript.SetItemData(itemData);
|
||
|
return item;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 文本框按钮
|
||
|
/// </summary>
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 刷新LoopList
|
||
|
/// </summary>
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 更新Item
|
||
|
/// </summary>
|
||
|
/// <param name="listView"></param>
|
||
|
/// <param name="index"></param>
|
||
|
/// <returns></returns>
|
||
|
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<ChatTextItem_Private>();
|
||
|
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<ChatAudioItem_Private>();
|
||
|
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<ChatTaskItem_Private>();
|
||
|
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<ChatTextItem_Private>();
|
||
|
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<ChatAudioItem_Private>();
|
||
|
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<ChatTaskItem_Private>();
|
||
|
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<UserInfoItemView>();
|
||
|
for (int i = 0; i < userInfoItems.Length; i++)
|
||
|
{
|
||
|
if (userInfoItems[i].GetComponent<Toggle>().isOn)
|
||
|
{
|
||
|
data = userInfoItems[i].GetItemUserData();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return data;
|
||
|
}
|
||
|
}
|